{"id":1425,"date":"2007-02-21T20:58:33","date_gmt":"2007-02-22T00:58:33","guid":{"rendered":"http:\/\/www.hoogervorst.ca\/arthur\/?p=1425"},"modified":"2008-02-06T22:53:29","modified_gmt":"2008-02-07T02:53:29","slug":"owner-draw-2","status":"publish","type":"post","link":"http:\/\/www.hoogervorst.ca\/arthur\/?p=1425","title":{"rendered":"Owner-draw"},"content":{"rendered":"<p><span class=\"dropcap\">O<\/span>ne point with which I want to close off for today: Ownerdraw stuff in C#. Yesterday, I was comparing code to create owner-draw listboxes and comboboxes in both Delphi and C# and noticed how remarkably easy it&#8217;s to switch from Delphi to C#: if you did this in Delphi before, the procedure is almost the same in C# (once again this is not surprising if <a href=\"http:\/\/www.artima.com\/intv\/generics.html\">you know who designed C#<\/a>, <a href=\"http:\/\/www.hoogervorst.ca\/arthur\/?p=1293\">earlier at xsamplex<\/a>)\n<\/p>\n<p>In Delphi for example, most VCL components have an &#8216;ownerdraw&#8217; property: switching this one to either true (or to a specific enumerated value like &#8216;Ownerdrawfixed&#8217;), you generally capture two events to draw (and colour) individual items. The same is true for C#: you set the DrawMode property either to &#8216;OwnerDrawFixed&#8217; or &#8216;OwnerDrawVariable&#8217;. Which one you choose depends on what you plan to do with the listbox.\n<\/p>\n<p>As mentioned earlier, code to actually draw an item is done in an event: for this C# has the DrawItem event. I&#8217;m not going to explain the code, because it should speak for itself.\n<\/p>\n<pre>private void cboDataSources_DrawItem(object sender, DrawItemEventArgs e)\r\n{\r\n\r\n    if (e.Index > -1)\r\n    {\r\n        TDataSourceType nt = \r\n           (cboDataSources.Items[e.Index] as TDataSource).SourceType;\r\n\r\n        \/\/ Background first!\r\n        e.DrawBackground();\r\n\r\n        if (nt == TDataSourceType.System)\r\n        {\r\n            \/\/ Draw image from image list...\r\n            imlSources.Draw(e.Graphics, e.Bounds.X, e.Bounds.Y + 1, 0);\r\n            e.Graphics.DrawString(cboDataSources.Items[e.Index].ToString(),\r\n                e.Font, Brushes.Black, e.Bounds.X + 18, e.Bounds.Y + 2);\r\n        }\r\n\r\n        \/\/ ... other code removed...\r\n        \/\/\r\n\r\n        \/\/ and then draw the focus rectangle...\r\n        e.DrawFocusRectangle();\r\n    }\r\n}\r\n<\/pre>\n<p>What do you mean, it smells like Delphi?<\/p>\n","protected":false},"excerpt":{"rendered":"<p>One point with which I want to close off for today: Ownerdraw stuff in C#. Yesterday, I was comparing code to create owner-draw listboxes and comboboxes in both Delphi and C# and noticed how remarkably easy it&#8217;s to switch from &hellip; <a href=\"http:\/\/www.hoogervorst.ca\/arthur\/?p=1425\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[4],"tags":[88,146,274],"_links":{"self":[{"href":"http:\/\/www.hoogervorst.ca\/arthur\/index.php?rest_route=\/wp\/v2\/posts\/1425"}],"collection":[{"href":"http:\/\/www.hoogervorst.ca\/arthur\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.hoogervorst.ca\/arthur\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.hoogervorst.ca\/arthur\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.hoogervorst.ca\/arthur\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1425"}],"version-history":[{"count":0,"href":"http:\/\/www.hoogervorst.ca\/arthur\/index.php?rest_route=\/wp\/v2\/posts\/1425\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.hoogervorst.ca\/arthur\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1425"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.hoogervorst.ca\/arthur\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1425"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.hoogervorst.ca\/arthur\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1425"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}