<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>analog feelings, digital world &#187; controls</title>
	<atom:link href="http://notonlyzeroesandones.site40.net/tag/controls/feed/" rel="self" type="application/rss+xml" />
	<link>http://notonlyzeroesandones.site40.net</link>
	<description>code is the modern poetry ;)</description>
	<lastBuildDate>Mon, 05 Oct 2009 22:06:02 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Scintilla.NET Control &amp; GDICharSet</title>
		<link>http://notonlyzeroesandones.site40.net/2009/05/10/scintillanet-control-gdicharset/</link>
		<comments>http://notonlyzeroesandones.site40.net/2009/05/10/scintillanet-control-gdicharset/#comments</comments>
		<pubDate>Sun, 10 May 2009 00:23:19 +0000</pubDate>
		<dc:creator>Maciek Talaska</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[controls]]></category>
		<category><![CDATA[ScintillaNET]]></category>

		<guid isPermaLink="false">http://notonlyzeroesandones.site40.net/2009/05/10/scintillanet-control-gdicharset/</guid>
		<description><![CDATA[TextBox and its variations are one of the most widely used controls. Sometimes it is much better to devote your time to something different than creating more sophisticated version of TextBox control, that will have all the features you need. For one of my projects I was looking for a TextBox control, that will provide [...]]]></description>
			<content:encoded><![CDATA[<p align="justify">TextBox and its variations are one of the most widely used controls. Sometimes it is much better to devote your time to something different than creating more sophisticated version of TextBox control, that will have all the features you need. For one of my projects I was looking for a TextBox control, that will provide line numbering and a bunch of other (not very sophisticated) editing features. After Googling for a while, I have found Scintilla.NET Control. This is just a managed wrapper for Windows native version of this control. Managed API is a bit messy, but still – it saved me tons of time I would have to spend developing my own editing control otherwise.</p>
<p align="justify">One of the problems I faced while using <a href="http://www.codeplex.com/ScintillaNET">ScintillaNet</a> control is&#8230; properly setting font for the control. Why is that so important? Although my app is not multicultural, I want it to allow people use all diacritical characters in the language of their choice. Using standard TextBox control you just change font in a couple of lines: </p>
<pre class="c#" name="code">FontDialog fd = new FontDialog();
fd.FontMustExist = true;
if (fd.ShowDialog(this) == DialogResult.OK)
{
    this.textBox1.Font = fd.Font;
}</pre>
<p align="justify">This is pretty easy, right? The only interesting thing here is that even if you do not specify a proper “Script” (what specific version of the font you want to use: Western, Greek, Turkish, Baltic, Central European, Cyrillic, Vietnamese) you will be able to use diacritical character for the language of your choice (if you do not believe me – check it!).</p>
<p align="justify">ScintillaNET Control behaves different. If you do not specify explicitly additional options regarding character encoding – diacritical characters will not be available for use in that control. It is important to notice, that there is special option (I mentioned above) connected with character encoding in FontDialog available in System.Windows.Forms namespace:</p>
<p align="justify"><a href="http://notonlyzeroesandones.site40.net/wp-content/uploads/2009/05/image.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://notonlyzeroesandones.site40.net/wp-content/uploads/2009/05/image-thumb.png" width="451" height="359"></a> </p>
</p>
<p>Picture 1. Standard .NET FontDialog</p>
<p align="justify">Notice that below textbox that present how will actual setting affect font outlook, there is an option entitled “Script” – this one is used to use a proper “character set” of chosen font. If you want to &#8211; for example &#8211; use some Slavic diacritical characters you should choose “Central European” instead of “Western”.</p>
<p align="justify">But even if you do so, you still will not be able to use diacritical characters in ScintillaNET Control. What is the problem, then? There are two problems to be precise:</p>
<ol>
<li>
<div align="justify">if you use application settings to store your font (and I do) than you must be aware, that the font that you load through Application Settings may not be <strong>exactly the same</strong> as the font you saved. You need to create a proper font from the font stored in application settings.</div>
<li>
<div align="justify">after creating font that will have properly set information about code page, you need to set encoding in ScintillaNET Control, and activate the font.</div>
</li>
</ol>
<p align="justify">You may wonder, how is that possible that font loaded from application settings differs from the one that was stored in application settings – the problem is that information about GDI character set is not being stored. So, if the font you have selected had GDI character set set to any value other than default – the value is being lost at the moment of saving font in application settings. You may overcome this problem, storing GDI character set for the specified font. Next thing, you should do, after loading font from application settings is to set its GDICharSet property, so the font will be <strong>exactly the same</strong> as the one that was saved. Unfortunatelly, GDICharSet property is read-only, so there is no single-line solution for this problem. First solution that came up to my mind was to use <a href="http://msdn.microsoft.com/en-us/library/aa288468.aspx">LOGFONT structure</a> to create a font that will have properly set its GDICharSet. To do this, you need to create a definition for the LOGFONT structure:</p>
<pre class="c#" name="code">using System.Runtime.InteropServices;

[StructLayout(LayoutKind.Sequential)]
public class LOGFONT
{
    public const int LF_FACESIZE = 32;
    public int lfHeight;
    public int lfWidth;
    public int lfEscapement;
    public int lfOrientation;
    public int lfWeight;
    public byte lfItalic;
    public byte lfUnderline;
    public byte lfStrikeOut;
    public byte lfCharSet;
    public byte lfOutPrecision;
    public byte lfClipPrecision;
    public byte lfQuality;
    public byte lfPitchAndFamily;
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = LF_FACESIZE)]
    public string lfFaceName;
}
</pre>
<p>a sample function that creates font that will have properly set GDICharSet property could look like this:</p>
<pre class="c#" name="code">private Font CreateFont(Font f, byte gdicharset)
{
    LOGFONT logfont = new LOGFONT();
    logfont.lfFaceName = f.Name;
    logfont.lfHeight = f.Height;
    logfont.lfCharSet = gdicharset;

    return Font.FromLogFont(logfont);
}
</pre>
<p>It is quite easy, right? But&#8230; it could be even easier :) If you look closely to all the Font constructors, you’ll notice, that there is one, that takes GDICharSet as an argument – this is just the thing you’ll need. The whole 5-line long function presented above could then be replaced by a single line of code (that does exactly the same):</p>
<pre class="c#" name="code">Font f = new Font(defaultfont.FontFamily, defaultfont.Size, defaultfont.Style, defaultfont.Unit, gdicharset);</pre>
<p align="justify">And of course, you do not have to define LOGFONT anymore ;) Ok, so now the font has the GDICharSet properly set. If you want the control to be able to use diacritical characters, you have to set some of its properties. I have spent quite some time to figure it out which properties should be set, and discovered that even the order of setting properties is important! So, to save you some time, I’ll just paste the code I use to properly initialize ScintillaNET Control in my application:</p>
<pre class="c#" name="code">ScintillaNet.CharacterSet charset = (ScintillaNet.CharacterSet)gdicharset;
this.content.Styles.Default.CharacterSet = charset;
Font f = new Font(defaultfont.FontFamily, defaultfont.Size, defaultfont.Style, defaultfont.Unit, gdicharset);
this.content.Styles.Default.Font = f;
this.content.UseFont = true;
this.content.Encoding = System.Text.Encoding.GetEncoding(encodingName);</pre>
<p>So that’s it. I really recommend using ScintillaNET Control – it is feature rich and free. It lacks up to date documentation and recently its development seems to be frozen, but it may be a good thing to take a look at it before deciding to develop your own TextBoxWithALotOfFeatures control :)</p>
]]></content:encoded>
			<wfw:commentRss>http://notonlyzeroesandones.site40.net/2009/05/10/scintillanet-control-gdicharset/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

<!-- www.000webhost.com Analytics Code -->
<script type="text/javascript" src="http://analytics.hosting24.com/count.php"></script>
<noscript><a href="http://www.hosting24.com/"><img src="http://analytics.hosting24.com/count.php" alt="web hosting" /></a></noscript>
<!-- End Of Code -->
