<?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>Pascal Software Group</title>
	<atom:link href="http://7pas.com/blog/feed" rel="self" type="application/rss+xml" />
	<link>http://7pas.com/blog</link>
	<description>A Better Digital Life</description>
	<lastBuildDate>Thu, 15 Apr 2010 09:24:49 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
<xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" />
		<item>
		<title>WS_CLIPCHILDREN and Free Flicker</title>
		<link>http://7pas.com/blog/ws_clipchildren-and-free-flicker.html</link>
		<comments>http://7pas.com/blog/ws_clipchildren-and-free-flicker.html#comments</comments>
		<pubDate>Wed, 03 Mar 2010 15:28:22 +0000</pubDate>
		<dc:creator>Dean Martin</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[C]]></category>
		<category><![CDATA[GUI]]></category>
		<category><![CDATA[SDK]]></category>
		<category><![CDATA[VC++]]></category>

		<guid isPermaLink="false">http://www.7pas.com/blog/?p=5</guid>
		<description><![CDATA[Flicker is a annoying problem in GUI programming, whether you&#8217;re using MFC or Win32 SDK or other GUI library like wxWidgets. The aim of this article is not to present you how to prevent your Windows program from flickering, just show a little trick that will help you avoid flickering on parent window created without [...]]]></description>
			<content:encoded><![CDATA[<p>Flicker is a annoying problem in GUI programming, whether you&#8217;re using MFC or Win32 SDK or other GUI library like wxWidgets.</p>
<p>The aim of this article is not to present you how to prevent your Windows program from flickering, just show a little trick that will help you avoid flickering on parent window created without WS_CLIPCHILDREN.</p>
<p><span id="more-5"></span></p>
<p>By default, main window will be created with WS_CLIPCHILDREN mask, that prevents child controls on the parent window from being painted twice. As we know, flickering comes from drawing a pixel twice, so WS_CLIPCHILDREN will greatly reduce flickering.</p>
<p>But, WS_CLIPCHILDREN will cause another annoying problem, &#8220;child control dark block&#8221;, if you&#8217;ve never heard of it, try to create a windows program with WS_CLIPCHILDREN mask, and place 3-5 Edit and Static Text controls on main window,  compile and run it, then minimize and restore it repeatedly. You&#8217;ll probably see on-screen &#8220;child control dark block&#8221; on these child controls.</p>
<p>Finally I found a way to avoid flickering without WS_CLIPCHILDREN. The key is ExcludeClipRect function. </p>
<p>Assuming that you have a main window created without WS_CLIPCHILDREN mask, and four Static controls on it, Static A is for owner drawing, the other three are for static text display. Normally, there&#8217;ll be flicker on Static A when you moving the window out of the screen. Now what we need to do is add WM_ERASEBKGND handler in main window message loop.</p>
<div class="codecolorer-container c default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="c codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>message <span style="color: #339933;">==</span> WM_ERASEBKGND<span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; dc <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>HDC<span style="color: #009900;">&#41;</span>wParam<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; GetWindowRect<span style="color: #009900;">&#40;</span>hStatic<span style="color: #339933;">,</span> <span style="color: #339933;">&amp;</span>rc<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; pt1.<span style="color: #202020;">x</span> <span style="color: #339933;">=</span> rc.<span style="color: #202020;">left</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; pt1.<span style="color: #202020;">y</span> <span style="color: #339933;">=</span> rc.<span style="color: #202020;">top</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; pt2.<span style="color: #202020;">x</span> <span style="color: #339933;">=</span> rc.<span style="color: #202020;">right</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; pt2.<span style="color: #202020;">y</span> <span style="color: #339933;">=</span> rc.<span style="color: #202020;">bottom</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; ScreenToClient<span style="color: #009900;">&#40;</span>hDlg<span style="color: #339933;">,</span> <span style="color: #339933;">&amp;</span>pt1<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; ScreenToClient<span style="color: #009900;">&#40;</span>hDlg<span style="color: #339933;">,</span> <span style="color: #339933;">&amp;</span>pt2<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; ExcludeClipRect<span style="color: #009900;">&#40;</span>dc<span style="color: #339933;">,</span> pt1.<span style="color: #202020;">x</span><span style="color: #339933;">,</span> pt1.<span style="color: #202020;">y</span><span style="color: #339933;">,</span> pt2.<span style="color: #202020;">x</span><span style="color: #339933;">,</span> pt2.<span style="color: #202020;">y</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>the code above will exclude Static A from repaint area, so you can process Static A owner drawing in custom code (we&#8217;re subclassing Static A).</p>
<div class="codecolorer-container c default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><div class="c codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">INT_PTR CALLBACK StaticProc<span style="color: #009900;">&#40;</span>HWND hwnd<span style="color: #339933;">,</span> UINT message<span style="color: #339933;">,</span> WPARAM wParam<span style="color: #339933;">,</span> LPARAM lParam<span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; PAINTSTRUCT ps<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; HDC &nbsp; &nbsp; &nbsp; &nbsp; dc<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; RECT &nbsp; &nbsp; &nbsp; &nbsp;rc<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; HBRUSH &nbsp; &nbsp; &nbsp;br<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #b1b100;">switch</span> <span style="color: #009900;">&#40;</span>message<span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">case</span> WM_PAINT<span style="color: #339933;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; GetClientRect<span style="color: #009900;">&#40;</span>hwnd<span style="color: #339933;">,</span> <span style="color: #339933;">&amp;</span>rc<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; dc &nbsp; &nbsp; <span style="color: #339933;">=</span> BeginPaint<span style="color: #009900;">&#40;</span>hwnd<span style="color: #339933;">,</span> <span style="color: #339933;">&amp;</span>ps<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; br <span style="color: #339933;">=</span> CreateSolidBrush<span style="color: #009900;">&#40;</span>color<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; FillRect<span style="color: #009900;">&#40;</span>dc<span style="color: #339933;">,</span> <span style="color: #339933;">&amp;</span>rc<span style="color: #339933;">,</span> br<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; DeleteObject<span style="color: #009900;">&#40;</span>br<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; EndPaint<span style="color: #009900;">&#40;</span>hwnd<span style="color: #339933;">,</span> <span style="color: #339933;">&amp;</span>ps<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #b1b100;">case</span> WM_ERASEBKGND<span style="color: #339933;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #b1b100;">default</span><span style="color: #339933;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> CallWindowProc<span style="color: #009900;">&#40;</span>lpOldProc<span style="color: #339933;">,</span> hwnd<span style="color: #339933;">,</span> message<span style="color: #339933;">,</span> wParam<span style="color: #339933;">,</span> lParam<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>Now, even not using double-buffing, we can get smooth owner drawn Static without flickering. But it&#8217;s still a median way, the best solution is building gui controls from scratch, and create main window with WS_CLIPCHILDREN, child controls will have their own message procedure and owner drawing code. A good example of how to create custom control is SpinCube control from the latest Windows SDK.</p>
<p><img src="http://www.7pas.com/blog/wp-content/uploads/2010/03/free-flicker.png" alt="Free Flicker Example" /></p>
]]></content:encoded>
			<wfw:commentRss>http://7pas.com/blog/ws_clipchildren-and-free-flicker.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
