I'm working on a project in Windows Forms and I want to use custom fonts for buttons etc.
I have a static class that contains the styles of fonts that I want. For example:
ParagraphFont = new Font(Properties.Resources.Font, 14.25F);
TitleFont = new Font(Properties.Resources.Font, 48, FontStyle.Bold);
I want to be able to apply the font to the form/user control through the designer, but I can't figure out how to do this.
I've tried setting the fonts of all the controls to these variables after the InitialiseComponent() method, but this seems long-winded and tedious to do to the multiple forms and controls that my project has
Is there a quicker way of doing this?
Edit: My question is different to the suggested duplicate. I don't need to set the default font of the project, I want to set the font of each control individually to a style variable. For example:
label.Font = ParagraphFont;
In the designer. This way, I can change the font used in ParagraphFont at any time
Related
I have a lot of formatted code snippets saved in an XML file.
On request I load them and set as Rtf in a RichTextBox:
string cscode = #"{\rtf1\ansi\ansicpg1252\deff0\deflang1031{\fonttbl{\f0\fnil\fprq1\fcharset0 Courier New;}{\f1\fnil\fcharset0..." etc.
rtb_cs.Rtf = cscode;
The code snippets are copy-pasted from the Visual Studio, so the text is colored differently.
Different Visual Studios are using different Fonts for the text.
Is there a way to change the Font but keep the colors?
I've tried to set the Font property of the RichTextBox, but this also resets the colors.
//This changes colors as well
rtb_cs.Font = new Font(FontFamily.GenericSansSerif, 10);
Take a look at LarsTech's solution here:
Changing font for richtextbox without losing formatting
It works for other settings too.
I'm attempting to set a font of button to system's Marlett font. However, though I manually set the font-face, other font is used. Also, Marlett is not listed, when I use the font dialog to choose a font for that button.
Why is it so? What can I do to use Marlett font in .NET Windows Forms controls?
Though I do not know what code is behind the designer, I have always found that custom installed fonts do not show up in the designer. The good news is that the Font property is ambient so if you wanted all controls to have the same Font you would only have to set it at the Form. However, it seems like you just want one control to have the Font so let's do this:
ctrl.Font = new Font("Marlett", 8.5f);
which will set that control's Font to Marlett and a size of 8.5 for example.
If you wanted an entire set of controls to have the same Font, if they can be placed in a container like a Panel, then you would only have to set the Font of the Panel; because again, it's an ambient property.
button1.Font = new Font("Marlett",8, FontStyle.Regular);
put this code for your button name Button1 , where you want change ( in from constrcutor after iinitializecomponet or in form Load event )
It would seem that the designer by default wants to set the GdiCharSet to 0. This causes the Marlett font to fall back to another font.
If you change the GdiCharSet to 1 it will render normally.
Also note the changes it makes in the .designer.cs, this will also explain why it did work when you would set the font manually from code.
This is what finally worked for me.
ctrl.Font = new System.Drawing.Font("Marlett", 12f, FontStyle.Regular, GraphicsUnit.Point, ((byte)(1)));
The last ((byte)(1)) sets the GdiCharSet to 1, which #Paul noted in one of the other answers.
how can i work with font that dose not support any kind of FontStyle. by the way, I can not create a font Object from it. help??
The font code on Windows (as on any platform I guess) doesn't create new fonts from scratch; it will rather interpret what fonts are installed / available on your system and give you a way to work with those.
Each font is reached by making a FontFamily object; such a FontFamily then contains Font objects for each style that is available. If a certain style (such as 'bold' or 'italic') is not available on your system, you won't be able to create it.
You can test whether this is the case by using the FontFamily.IsStyleAvailable. Defined here as:
public bool IsStyleAvailable(
FontStyle style
)
If you want to create a font, you'll have to make sure it's available on the system first (which means you'll have to use a common font or package the font with your solution if you want an uncommon one).
I am using:
dg.ForeColor = System.Drawing.Color.Black;
dg.BackColor = System.Drawing.Color.Beige;
to set the background and foreground color of a DataGrid. How can I change the font to say Calibri or any others using the built-in methods.
If this is a web application, the DataGrid class already provides a Font property that you can set to
a font of your choosing. You can set it either from the designer or in your source code, just as you've set the BackColor and ForeColor properties.
However, as I mentioned in a comment, you should be very careful about setting controls to use a font that the user might not have installed on their computer. I recommend checking a list of web-safe fonts.
You can change the font with this code
dg.Font=new Font(string familyName,int size);
Does anyone know how to make labels, or text, smoother? At the moment they look quite jagged. As I want to make the label dynamic, I can't just insert the text from Photoshop.
You'll have to dynamically generate images representing your text if you want to anti-alias it. Here is an example on msdn: http://msdn.microsoft.com/en-us/library/a619zh6z.aspx
EDIT: Editing per comment below.
The link describes using the OnPaint event of your control to use a different TextRenderingHint. If you're wanting something a little more re-useable what you can do is create a Custom Label class that extends the Label class, and use this in your forms:
public partial class CustomLabel : Label
{
private TextRenderingHint _hint = TextRenderingHint.SystemDefault;
public TextRenderingHint TextRenderingHint
{
get { return this._hint; }
set { this._hint = value; }
}
protected override void OnPaint(PaintEventArgs pe)
{
pe.Graphics.TextRenderingHint = TextRenderingHint;
base.OnPaint(pe);
}
}
Add a new Custom Control called CustomLabel (or whatever you want to call it) and use the code above. Rebuild your project and you should then see the CustomLabel control appear in your toolbox at the top, under the "MyProject Components" category. In the properties pane for this custom label you'll see the new TextRenderingHint property. Set this to "AntiAlias." Add another label to your form and compare how they look.
If you want to default it to AntiAlias simply change the default value of the private variable.
Are you referring to ClearType? Then ClearType has to be enabled in Windows, and you must use a modern font, such as Tahoma or Segoe UI, not MS Sans Serif.
Update
You posted an example of the problem. I magnified it to 400 %. Clearly ClearType subpixel antialiasing is enabled. Personally I do not think the text looks jagged. If you want higer quality on-screen text you could buy a screen with a higher physical resolution (pixels per inch), and then draw the text at a (correspondingly) larger size. Then the text will have the same size on your screen, but will look much more smooth.
You could also give up on ClearType and use some other font-smoothing algorithm, but that is far from trivial, because ClearType is the font-smoothing system on Windows.
Update 2
If you are running Windows 7, you can fine-tune ClearType. Just open the start menu, write "ClearType" and start the guide. I think there are guides for Vista and XP too, but perhaps not installed by default, but available as PowerToys or something like that...
Make sure you have ClearType enabled.
Install MacType program, it smoothes your Windows font like MacOS, I don't know why Microsoft don't fix its Windows smoothness font?
If Microsoft fixed its fonts, I don't need to install third party app to such things like that which is fundamental for Windows.