Anti-Aliased text on Windows CE
Posted on February 14, 2013
One of major drawbacks of Windows CE is that if you output text to the screen, then by default it is aliased. By putting out a text on the screen I mean putting a control on a form which displays a text. I am going to show you a technique how to output an anti-aliased text provided two conditions are met:
- The font family you are using supports anti-aliasing.
- The font is big enough.
If the font is too small, you will see no anti-aliasing effect which can make you believe, that this technique does not actually work. If this happens, try making the font bigger and try using any of the standard font families such as Tahoma or Arial.
You have to create the control during run-time. Any Visual Studio Designer created controls will have no anti-aliasing. And here’s the trick. Instead of the default Font property, we’re gonna use Microsoft.WindowsCE.Forms.LogFont class. You create a LogFont like this:
Microsoft.WindowsCE.Forms.LogFont lf = new Microsoft.WindowsCE.Forms.LogFont(); // create a LogFont lf.FaceName = "Tahoma"; // set font family lf.Height = 30; // set font height - this is not the size of the font in points lf.Quality = Microsoft.WindowsCE.Forms.LogFontQuality.AntiAliased; // important setting - turn on anti-aliasing
Now that the font is created we have to apply it to the control. First of all make sure to dispose the old font, if you don’t want to leak memory. Then assign a newly created LogFont to your control. Then add your control among the other controls of your form.
Mais seulement quand un médecin détermine qu’il est sûr de prendre une plus grande dose de Cialis. Si vous souhaitez découvrir plus sur le pharmaciebe.com ou taylor nous accueille, devient partenaire.
newControl.Font.Dispose(); newControl.Font = Font.FromLogFont(lf); this.Controls.Add(newControl);
That’s it! Finally if you already have an existing project and you want to apply an anti-aliased LogFont to your controls, you may find these methods useful. Please note, that they are just a guide how to upgrade your controls with anti-aliased font. You may need to alter them appropriately based on your project.
/// <summary> /// copy some properties of one control to another /// </summary> /// <param name="src">Source control</param> /// <param name="dst">Destination control</param> private void CopyControl(Control src, Control dst) { dst.Text = src.Text; dst.BackColor = src.BackColor; dst.Size = src.Size; dst.Location = src.Location; dst.Font.Dispose(); dst.Font = (Font)src.Font.Clone(); } /// <summary> /// Create a LogFontWeight object based on FontStyle object. /// </summary> /// <param name="fs">FontStyle object to get the style of the font from.</param> /// <returns>LogFontWeight object</returns> private LogFontWeight FontStyle2LogFontWeight(FontStyle fs) { if ((fs & FontStyle.Bold) != 0) return LogFontWeight.Bold; else return LogFontWeight.Regular; } /// <summary> /// Creates a new LogFontObject based on a Font. /// </summary> /// <param name="aF"></param> /// <returns></returns> private LogFont CreateLogFontBasedOnFont(Font aF) { Microsoft.WindowsCE.Forms.LogFont lf = new Microsoft.WindowsCE.Forms.LogFont(); lf.FaceName = aF.Name; using (Graphics g = this.CreateGraphics()) lf.Height = (int)(-aF.Size * g.DpiY / 72); lf.Weight = FontStyle2LogFontWeight(aF.Style); lf.Quality = Microsoft.WindowsCE.Forms.LogFontQuality.AntiAliased; return lf; } /// <summary> /// Applies a LogFont to a control. /// </summary> /// <param name="aControl"></param> /// <param name="lf"></param> private void ApplyLogFont(Control aControl, LogFont lf) { Control newControl = (Control)Activator.CreateInstance(aControl.GetType()); CopyControl(aControl, newControl); this.Controls.Remove(aControl); aControl.Dispose(); newControl.Font.Dispose(); newControl.Font = Font.FromLogFont(lf); this.Controls.Add(newControl); } // finally you can execute this code to apply a LogFont for each control on your form: foreach (Control c in this.Controls) { LogFont lf = CreateLogFontBasedOnFont(c.Font); ApplyLogFont(c, lf); }