{"id":26,"date":"2008-04-15T20:48:00","date_gmt":"2008-04-15T20:48:00","guid":{"rendered":"http:\/\/localhost:10265\/?p=25"},"modified":"2008-04-15T20:48:00","modified_gmt":"2008-04-15T20:48:00","slug":"how-to-suppress-the-flicker-issue-in-nets-listview","status":"publish","type":"post","link":"https:\/\/mehdi.biz\/blog\/2008\/04\/15\/how-to-suppress-the-flicker-issue-in-nets-listview\/","title":{"rendered":"How to suppress the flicker issue in .NET&#8217;s ListView?"},"content":{"rendered":"<!-- google_ad_section_start --><p>Hi folks,<br \/>\nI&#8217;ve been recently asked to explain how to suppress the flicker issue in C#&#8217;s ListView. As far as you know if you try to modify the list contents, the background of the control will be redrawn &#8211; which is the source of the flickering problem.<\/p>\n<p>To stop this, you need to subclass the ListView control and ask the control to filter out the WM_ERASEBKGND window message. The WM_ERASEBKGND message is sent to a window when the window background must be erased. The term subclass refers to deriving a new class from a given existing class, replacing\/manipulating the required functionality of the main window class.<\/p>\n<p>Of course, to make things more and more brilliant, we will dictate the new ListView class to paint the contents of the control in a temporary buffer, and then flash the entire buffer onto the screen which reduces flicker.<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\nusing System.Windows.Forms;\n\n\/\/\/ &lt;summary&gt;\n\/\/\/ Provides a ListView control that filters out the WM_ERASEBKGND message to\n\/\/\/ eliminate the flickering problem of the ListView.\n\/\/\/ &lt;\/summary&gt;\npublic class ListViewEx : ListView\n{\n    private const int WM_ERASEBKGND = 0x0014;\n\n    \/\/\/ &lt;summary&gt;\n    \/\/\/ Initializes a new instance of the ListViewEx class.\n    \/\/\/ &lt;\/summary&gt;\n    public ListViewEx()\n    {\n        SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint, true);\n        SetStyle(ControlStyles.EnableNotifyMessage, true);\n    }\n\n    \/\/\/ &lt;summary&gt;\n    \/\/\/ Notifies the control of Windows messages.\n    \/\/\/ &lt;\/summary&gt;\n    \/\/\/ &lt;param name=&quot;m&quot;&gt;A System.Windows.Forms.Message that represents the Windows message.\n    protected override void OnNotifyMessage(Message m)\n    {\n        if (m.Msg != WM_ERASEBKGND)\n            base.OnNotifyMessage(m);\n    }\n}\n<\/pre>\n<p>All you need to do now is to put the newly created ListViewEx on your dialog box and bingo!<\/p>\n<!-- google_ad_section_end -->","protected":false},"excerpt":{"rendered":"<p>Hi folks,<br \/> I&#8217;ve been recently asked to explain how to suppress the flicker issue in C#&#8217;s ListView. As far as you know if you try to modify the list contents, the background of the control will be redrawn &#8211; which is the source of the flickering problem.<\/p>\n<p>To stop this, you need to subclass [&#8230;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[4],"tags":[],"class_list":["post-26","post","type-post","status-publish","format-standard","hentry","category-csharp"],"_links":{"self":[{"href":"https:\/\/mehdi.biz\/blog\/wp-json\/wp\/v2\/posts\/26","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mehdi.biz\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mehdi.biz\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mehdi.biz\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mehdi.biz\/blog\/wp-json\/wp\/v2\/comments?post=26"}],"version-history":[{"count":0,"href":"https:\/\/mehdi.biz\/blog\/wp-json\/wp\/v2\/posts\/26\/revisions"}],"wp:attachment":[{"href":"https:\/\/mehdi.biz\/blog\/wp-json\/wp\/v2\/media?parent=26"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mehdi.biz\/blog\/wp-json\/wp\/v2\/categories?post=26"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mehdi.biz\/blog\/wp-json\/wp\/v2\/tags?post=26"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}