<?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>Reflections of my thoughts.... &#187; Windows 7</title>
	<atom:link href="http://codereflect.com/category/windows-7/feed/" rel="self" type="application/rss+xml" />
	<link>http://codereflect.com</link>
	<description>On Windows Programming, Technical Tips etc...</description>
	<lastBuildDate>Tue, 20 Jul 2010 01:56:44 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
<cloud domain='codereflect.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
		<item>
		<title>Programming Direct2D – Part 1</title>
		<link>http://codereflect.com/2010/07/19/programming-direct2d-part-1/</link>
		<comments>http://codereflect.com/2010/07/19/programming-direct2d-part-1/#comments</comments>
		<pubDate>Mon, 19 Jul 2010 15:00:05 +0000</pubDate>
		<dc:creator>Sarath</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[Codeproject]]></category>
		<category><![CDATA[Visual C++]]></category>
		<category><![CDATA[Visual Studio]]></category>
		<category><![CDATA[Visual Studio 2010]]></category>
		<category><![CDATA[Windows 7]]></category>

		<guid isPermaLink="false">http://codereflect.com/?p=1209</guid>
		<description><![CDATA[Most of the native Windows Programmers are familiar with GDI, the graphics API provided under Windows. GDI is widely used<a href="http://codereflect.com/2010/07/19/programming-direct2d-part-1/" class="searchmore">Read the Rest...</a><div class="clr"></div>]]></description>
			<content:encoded><![CDATA[<div style="float:right;margin:0px 0px 0px 2px;"></div><div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fcodereflect.com%2F2010%2F07%2F19%2Fprogramming-direct2d-part-1%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fcodereflect.com%2F2010%2F07%2F19%2Fprogramming-direct2d-part-1%2F&amp;source=sarat&amp;style=normal&amp;service=bit.ly" height="61" width="50" /><br />
			</a>
		</div>
<p>Most of the native Windows Programmers are familiar with GDI, the graphics API provided under Windows. GDI is widely used in windows applications. Later, Microsoft had introduced class-based, better services through GDI+ under Windows. Both Native programmers and Managed developers are making use of GDI/GDI+ classes and APIs.</p>
<p>But GDI was too old and was introduced with the initial versions of the Windows and over the time the capabilities of graphics hardware has grown and so does the demand for creating Visually Rich UI. With Windows 7, Microsoft has introduced a totally revamped 2D graphics subsystem called <strong><a href="http://msdn.microsoft.com/en-us/library/dd370990(VS.85).aspx" target="_blank">Direct2D</a></strong> based on their prominent Direct3D platform to create high quality 2D rendering. If you know or not, <a href="http://windows.microsoft.com/en-us/windows-vista/products/features/productivity">Windows Aero</a> system is already taking advantage of graphics hardware since Windows Vista. This is supposed to replace the GDI APIs in the coming years. Direct2D is not only available under Windows 7 but Microsoft has made it available in Window Vista with the latest service pack upgrade.</p>
<p>The major advantages of the Direct2D APIs are it&#8217;s hardware accelerated and provides high quality 2D rendering. It enables the user to create visually rich applications by paying less effort, when comparing to conventional APIs .</p>
<p>GDI uses Pixel graphics but Direct2D can supports vector graphics as well, in which mathematical formulas are used to draw the lines and curves. Vector graphics provides high quality rendering independent of resolution of the device, while the pixelated graphics has dependency with resolution which may results in choppy graphics.</p>
<p>Most of the GDI APIs are not using anti-aliasing and transparency. Ofcrouse there are functions to do so but always there&#8217;s programming cost for taking advantage of these features. Also if we apply  transparency and anti-aliasing, the computations are done using CPU. Direct2D can take advantage of graphics hardware and delegate the computationally intensive tasks to <a href="http://en.wikipedia.org/wiki/Graphics_processing_unit">GPU</a>.</p>
<p><img src="http://codereflect.com/wp-content/uploads/2010/07/071910_1523_Programming11.png" alt="" /></p>
<p>Direct2D built on top of Direct3D components. The layered architecture is described below</p>
<p><img src="http://codereflect.com/wp-content/uploads/2010/07/071910_1523_Programming21.png" alt="" /></p>
<p>You can see the underlying layers are Direct3D which make use of DXGI(DirectX Graphics Infrastructure), which manages the low level graphics related tasks that are independent of DirectX graphics runtime. DXGI provides common framework for graphics components. Simultaneously a high performance software rasterizer is available when the hardware acceleration is not possible. Another advantage of Direct2D API is, it&#8217;s using lightweight COM. It&#8217;s almost like simple C++ class. No BSTRS, COM Variants, Interfaces, Apartments etc.</p>
<p>Let&#8217;s start analyzing a simple Direct2D Program</p>
<p>There are 3 main components inevitable for enabling D2D rendering your application.</p>
<ul>
<li>A rendering Window and its handle (HWND)</li>
<li><a href="http://msdn.microsoft.com/en-us/library/dd371246(VS.85).aspx">ID2D1Factory</a> object to create the Direct2D resources</li>
<li><a href="http://msdn.microsoft.com/en-us/library/dd371461(VS.85).aspx">ID2D1HwndRenderTarget</a> object to draw the scene on the window.</li>
</ul>
<p>The below example provides steps for enabling Direct2D Rendering in your MFC Application. It&#8217;s hard to describe the parameters of all APIs. Please refer them in MSDN.</p>
<p><img src="http://codereflect.com/wp-content/uploads/2010/07/071910_1523_Programming31.png" alt="" /></p>
<h2>Create your Window</h2>
<p>Here I&#8217;m taking a dialog based application for rendering. The functions and messages may change if you&#8217;re using any other type of applications like SDI or MDI. No need to reinvent the wheel, allow the wizard to create your main dialog.</p>
<h2>Prepare your Rendering Engine</h2>
<p>Let&#8217;s delegate all the rendering tasks to another class instead of managing everything in the dialog class. I call this class as Direct2DHandler.</p>
<p>The Direct2DHandler declaration goes like below and it exposes the following functions.</p>
<pre class="brush: cpp;">
#pragma once
#include &lt;d2d1.h&gt;
#include &lt;d2d1helper.h&gt;

class Direct2DHandler
{
public:
	Direct2DHandler( HWND hWnd ); // ctor
	~Direct2DHandler(void); // dtor
	HRESULT Initialize(); // Initialize the rendering
	HRESULT OnRender(); // Called from OnPaint function
	void OnResize(UINT width, UINT height);

private:
	HRESULT CreateDeviceResources(); // Create resources for drawing
	void DiscardDeviceResources(); // Release resources for drawing

private:

	HWND					m_hWnd;
	ID2D1Factory*			m_pDirect2dFactory;
	ID2D1HwndRenderTarget*	m_pRenderTarget;
	ID2D1SolidColorBrush*	m_pLightSlateGrayBrush;
	ID2D1LinearGradientBrush* m_pLinearGradientBrush;
};
</pre>
<h3>The constructor</h3>
<p>does nothing other than calling CoInitialize() function and initialize the member variables( mostly to NULL).</p>
<h3>The Initialize function</h3>
<p>creates the Direct2D factor object, which similar the HDC (context variable) in GDI.</p>
<pre class="brush: cpp;">
HRESULT Direct2DHandler::Initialize()
{
    HRESULT hr = S_OK;

    // Create a Direct2D factory.
    hr = D2D1CreateFactory(D2D1_FACTORY_TYPE_SINGLE_THREADED, &amp;m_pDirect2dFactory);

    return hr;
}
</pre>
<p><strong>OnResize</strong></p>
<p>function handles the rendering area for the render target. We will be using whole window area in this example.<span style="font-family: Consolas; font-size: 9pt;"><br />
</span></p>
<pre class="brush: cpp;">
void Direct2DHandler::OnResize(UINT width, UINT height)
{
    if (m_pRenderTarget)
    {
        // Note: This method can fail, but it's okay to ignore the
        // error here, because the error will be returned again
        // the next time EndDraw is called.
        m_pRenderTarget-&gt;Resize(D2D1::SizeU(width, height));
    }
}
</pre>
<h3>OnRender</h3>
<p>function does the following</p>
<ol>
<li>Create the device resource for drawing (if not created)</li>
<li>Render the scene to the Window</li>
<li>Release the resources if error occurred</li>
</ol>
<p>If you&#8217;re experienced 3D programming probably you might be knowing about the role of matrix and how the affects the rendering object. The matrix can be used for representing the objects coordinates system. We can apply rotation, translation (movement) etc (collectively called transformation). on a matrix and this can be used for specifying how object must be rendered. Here we&#8217;re using <span style="font-family: Consolas; font-size: 9pt;">D2D1::Matrix3x2F::Identity() </span>helper function which creates an identity matrix(object will be rendered at origin without any transformation)</p>
<p><span style="font-family: Consolas; font-size: 9pt;"> m_pRenderTarget-&gt;<a href="http://msdn.microsoft.com/en-us/library/dd742857(VS.85).aspx">SetTransform</a>(D2D1::Matrix3x2F::Identity());<br />
</span></p>
<p>We can clear the rendering area with a default color (as the dialog background filled with some default color according to Window&#8217;s theme. Here I selected white color</p>
<p><span style="font-family: Consolas; font-size: 9pt;"> m_pRenderTarget-&gt;Clear(D2D1::ColorF(D2D1::ColorF::White));<br />
</span></p>
<p>The important thing is, whatever you draw for the scene must be done between BeginDraw() and EndDraw() APIs. The other part of this code is simply straight forward and you can easily grasp it. See the source below.</p>
<pre class="brush: cpp;">
HRESULT Direct2DHandler::OnRender()
{
	HRESULT hr = S_OK;

	hr = CreateDeviceResources();

	if (SUCCEEDED(hr))
	{
		m_pRenderTarget-&gt;BeginDraw();
		m_pRenderTarget-&gt;SetTransform(D2D1::Matrix3x2F::Identity());
		m_pRenderTarget-&gt;Clear(D2D1::ColorF(D2D1::ColorF::White));
		D2D1_SIZE_F rtSize = m_pRenderTarget-&gt;GetSize();
		// Draw a grid background.
		int width = static_cast&lt;int&gt;(rtSize.width);
		int height = static_cast&lt;int&gt;(rtSize.height);

		// Draw two rectangles.
		D2D1_RECT_F rectangle1 = D2D1::RectF(
			rtSize.width/2 - 50.0f,
			rtSize.height/2 - 50.0f,
			rtSize.width/2 + 50.0f,
			rtSize.height/2 + 50.0f
			);

		D2D1_RECT_F rectangle2 = D2D1::RectF(
			rtSize.width/2 - 100.0f,
			rtSize.height/2 - 100.0f,
			rtSize.width/2 + 100.0f,
			rtSize.height/2 + 100.0f
			);

		// Draw the outline of a rectangle.
		m_pRenderTarget-&gt;FillRectangle(&amp;rectangle2, m_pLinearGradientBrush);

		// Draw a filled rectangle.
		m_pRenderTarget-&gt;FillRectangle(&amp;rectangle1, m_pLightSlateGrayBrush);

		hr = m_pRenderTarget-&gt;EndDraw();

	}
	if (hr == D2DERR_RECREATE_TARGET)
	{
		hr = S_OK;
		DiscardDeviceResources();
	}
	return hr;
}
</pre>
<p><strong>CreateDeviceResources</strong></p>
<p>is slightly a lengthy function to initialize the required resources for rendering, like we create pens, brushes, bitmaps etc. Here mainly I&#8217;m creating a light colored solid brush and linear gradient brush to render as depicted in above picture. The comments are inlined and please read it along with the code. It will be interesting to learn about gradient brushes. If you&#8217;ve created a gradient brush in photoshop things will be far more easy. We can put stop spots to controls the end of gradients also line can be used for representing flow and angle for the gradient. Simple isn&#8217;t it? Like we create a normal gradient. See the code below.</p>
<pre class="brush: cpp;">
HRESULT Direct2DHandler::CreateDeviceResources()
{
    HRESULT hr = S_OK;

    if (!m_pRenderTarget)
    {
        RECT rc;
        GetClientRect(m_hWnd, &amp;rc);

        D2D1_SIZE_U size = D2D1::SizeU(
            rc.right - rc.left,
            rc.bottom - rc.top
            );

        // Create a Direct2D render target.
        hr = m_pDirect2dFactory-&gt;CreateHwndRenderTarget(
            D2D1::RenderTargetProperties(),
            D2D1::HwndRenderTargetProperties(m_hWnd, size),
            &amp;m_pRenderTarget
            );

        if (SUCCEEDED(hr))
        {
            // Create a gray brush.
            hr = m_pRenderTarget-&gt;CreateSolidColorBrush(
                D2D1::ColorF(D2D1::ColorF::LightSlateGray),
                &amp;m_pLightSlateGrayBrush
                );
        }

		// Create an array of gradient stops to put in the gradient stop
		// collection that will be used in the gradient brush.
		ID2D1GradientStopCollection *pGradientStops = NULL;

		D2D1_GRADIENT_STOP gradientStops[2];
		gradientStops[0].color = D2D1::ColorF(D2D1::ColorF::Maroon, 1);
		gradientStops[0].position = 0.0f;
		gradientStops[1].color = D2D1::ColorF(D2D1::ColorF::Red, 1);
		gradientStops[1].position = 1.0f;
		// Create the ID2D1GradientStopCollection from a previously
		// declared array of D2D1_GRADIENT_STOP structs.
		hr = m_pRenderTarget-&gt;CreateGradientStopCollection(
			gradientStops,
			2,
			D2D1_GAMMA_2_2,
			D2D1_EXTEND_MODE_CLAMP,
			&amp;pGradientStops
			);
		// The line that determines the direction of the gradient starts at
		// the upper-left corner of the square and ends at the lower-right corner.

		if (SUCCEEDED(hr))
		{
			hr = m_pRenderTarget-&gt;CreateLinearGradientBrush(
				D2D1::LinearGradientBrushProperties(
				D2D1::Point2F(0, 0),
				D2D1::Point2F(300, 300)),
				pGradientStops,
				&amp;m_pLinearGradientBrush
				);
		}

    }

    return hr;
}
</pre>
<h3>DiscardDeviceResources</h3>
<p>Just releases the allocated resouces. It simply uses helper macro SafeRelease described below the code.</p>
<pre class="brush: cpp;">
void Direct2DHandler::DiscardDeviceResources()
{
    SafeRelease(&amp;m_pRenderTarget);
    SafeRelease(&amp;m_pLightSlateGrayBrush);
    SafeRelease(&amp;m_pLinearGradientBrush);
}

template&lt;class Interface&gt;
inline void SafeRelease(
    Interface **ppInterfaceToRelease
    )
{
    if (*ppInterfaceToRelease != NULL)
    {
        (*ppInterfaceToRelease)-&gt;Release();

        (*ppInterfaceToRelease) = NULL;
    }
}
</pre>
<p><strong>Initializing Rendering in Dialog class</strong></p>
<p>In the</p>
<h3>OnInitDialog function,</h3>
<p>allocate memory for the Direct2DHandler class and Initialize it. You can call this from OnCreate function as well but ensure that there&#8217;s a valid Window handle is there.</p>
<pre class="brush: cpp;">
m_pRender = new Direct2DHandler( m_hWnd );
m_pRender-&gt;Initialize();
</pre>
<h3>Disable Default Background Drawing</h3>
<p>Disable the default OnErasBkgrnd function because it may cause flickering while drawing. Also we&#8217;re occupying full client area in the dialog and the background is clearing is OnRender function itself.</p>
<pre class="brush: cpp;">
BOOL CDirect2DDemoDlg::OnEraseBkgnd(CDC* pDC)
{
    return FALSE;//CDialogEx::OnEraseBkgnd(pDC);
}
</pre>
<h3>OnSize (WM_SIZE)</h3>
<p>function handles the Window size events. It will resize render target according to the Window Size.</p>
<pre class="brush: cpp;">
void CDirect2DDemoDlg::OnSize(UINT nType, int cx, int cy)
{
    CDialogEx::OnSize(nType, cx, cy);
    if( m_pRender )
        m_pRender-&gt;OnResize( cx, cy );
}
</pre>
<h3>OnPaint function</h3>
<p>calls OnRender function of Direct2DHandler to draw the scene.</p>
<pre class="brush: cpp;">
void CDirect2DDemoDlg::OnPaint()
{
    if (IsIconic())
    {
        // code for handling when window is minimized. Check original CPP file attached.
    }
    else
    {
        // CDialogEx::OnPaint(); no need to call this.
        if( m_pRender )
            m_pRender-&gt;OnRender();
    }
}
</pre>
<h3>Linking</h3>
<p>The Application must link to d2d1.lib through project settings or using #pragma comment( lib, &#8220;d2d1.lib&#8221; ) in the source file</p>
<h3> d2d1helper </h3>
<p>d2d1helper.h contains some useful functions to help the drawing. See the header for more details.</p>
<p>OK that&#8217;s the basics of creating Direct2D rendering for your Windows Application. Go ahead with the attached source code.</p>
<p>The project file is in Visual Studio 2010 format, but basically you only need to have Direct2DHandler class. You can also use other prior version of Visual Studio with Windows 7/Vista SDK.</p>
<p>Note: Take whole responsibility of downloading and executing the code shared here. Don&#8217;t sue me. It was virus free, malware free when I uploaded.</p>
<p><a href="http://cid-5544789b26bfc05d.office.live.com/embedicon.aspx/.Public/Direct2DDemo.zip">Download Demo Source code</a><br />
<iframe title ="Preview" scrolling="no" marginheight="0" marginwidth="0" frameborder="0" style="width:98px;height:115px;padding:0;background-color:#fcfcfc;" src="http://cid-5544789b26bfc05d.office.live.com/embedicon.aspx/.Public/Direct2DDemo.zip"></iframe></p>
]]></content:encoded>
			<wfw:commentRss>http://codereflect.com/2010/07/19/programming-direct2d-part-1/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Windows  7: Task Dialog Part 2 &#8211; A more detailed task dialog</title>
		<link>http://codereflect.com/2010/07/07/windows-7-task-dialog-part-2-a-more-detailed-task-dialog/</link>
		<comments>http://codereflect.com/2010/07/07/windows-7-task-dialog-part-2-a-more-detailed-task-dialog/#comments</comments>
		<pubDate>Wed, 07 Jul 2010 14:48:39 +0000</pubDate>
		<dc:creator>Sarath</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[Visual C++]]></category>
		<category><![CDATA[Visual Studio]]></category>
		<category><![CDATA[Visual Studio 2010]]></category>
		<category><![CDATA[Windows 7]]></category>
		<category><![CDATA[Windows Vista]]></category>

		<guid isPermaLink="false">http://codereflect.com/?p=1196</guid>
		<description><![CDATA[In the last installment, we’ve seen using the basic version of task dialog. But usually when we see the task<a href="http://codereflect.com/2010/07/07/windows-7-task-dialog-part-2-a-more-detailed-task-dialog/" class="searchmore">Read the Rest...</a><div class="clr"></div>]]></description>
			<content:encoded><![CDATA[<div style="float:right;margin:0px 0px 0px 2px;"></div><div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fcodereflect.com%2F2010%2F07%2F07%2Fwindows-7-task-dialog-part-2-a-more-detailed-task-dialog%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fcodereflect.com%2F2010%2F07%2F07%2Fwindows-7-task-dialog-part-2-a-more-detailed-task-dialog%2F&amp;source=sarat&amp;style=normal&amp;service=bit.ly" height="61" width="50" /><br />
			</a>
		</div>
<p>In the last installment, we’ve seen using the basic version of task dialog. But usually when we see the task dialogs in Windows Vista or 7, it’s more detailed and can have flashy icons etc. Let’s see how to take more control over the task dialogs.</p>
<p><a href="http://msdn.microsoft.com/en-us/library/bb760544(VS.85).aspx">TaskDialogIndirect</a> function can be used to have more options with task dialogs. <a href="http://msdn.microsoft.com/en-us/library/bb787473(v=VS.85).aspx">TASKDIALOGCONFIG</a> structure is used along with TaskDialogIndirect API.</p>
<p><a href="http://codereflect.com/wp-content/uploads/2010/07/clip_image001.png"><img style="display: inline; border: 0px;" title="clip_image001" src="http://codereflect.com/wp-content/uploads/2010/07/clip_image001_thumb.png" border="0" alt="clip_image001" width="456" height="326" /></a></p>
<p>As you’re seeing above the task dialog contains different type of controls, icons and capable of displaying more information to the user. It can have lengthy big buttons, radio buttons, checkbox, footer area, progressbar, custom icon, displaying predefined buttons like OK, Cancel, Yes, No etc. even we can have control over the buttons in the titlebar( minimize, maximize button etc)</p>
<p>The following code describes creating a task dialog with more flexible options. User can specify the callback functions which can be used to control the behavior if the controls and contents in the task bar. Filling the TASKDIALOGCONFIG structure is simple and straight forward as we’re seeing the code. The detailed option can be obtained from MSDN page.</p>
<pre class="brush: cpp;">

HRESULT CALLBACK CTaskDialogSampleDlg::TaskDialogCallbackProc(
  __in  HWND hwnd,
  __in  UINT uNotification,
  __in  WPARAM wParam,
  __in  LPARAM lParam,
  __in  LONG_PTR dwRefData
)
{
	if( TDN_CREATED == uNotification )
	{
		::SendMessage( hwnd, TDM_SET_BUTTON_ELEVATION_REQUIRED_STATE, IDOK, TRUE );
		::SendMessage( hwnd, TDM_SET_PROGRESS_BAR_RANGE, 0, 100 );
	}
	else if( TDN_HYPERLINK_CLICKED == uNotification )
	{
		ShellExecute( 0, L&quot;open&quot;, (LPCTSTR) lParam, 0, 0, SW_SHOW );
	}
	else if( TDN_TIMER == uNotification )
	{
		static int i = 0;
		::SendMessage( hwnd, TDM_SET_PROGRESS_BAR_POS, i++,0 );
	}

	return 0;
}

void CTaskDialogSampleDlg::OnBnClickedButton1()
{
	int nButtonPressed                  = 0;
	TASKDIALOGCONFIG config             = {0};
	const TASKDIALOG_BUTTON buttons[]   = {
		{ IDOK, L&quot;Elevate Privilege&quot; },
		{ IDCANCEL, L&quot;Run with user privilege&quot; }
	};

	const TASKDIALOG_BUTTON radiobuttons[] = {
		{ IDCANCEL, L&quot;Test Radio&quot; }};
	config.cbSize                       = sizeof(config);
	config.hInstance                    = AfxGetApp()-&gt;m_hInstance;
	config.dwCommonButtons              = TDCBF_OK_BUTTON | TDCBF_CANCEL_BUTTON;
	config.pszMainIcon                  = TD_SHIELD_ICON;
	config.pszMainInstruction           = L&quot;Main Instruction&quot;;
	config.pszContent                   = L&quot;This is the content.&quot;;
	config.pszVerificationText          = L&quot;Conifirm license agreement&quot;;
	config.pButtons                     = buttons;
	config.cButtons                     = ARRAYSIZE(buttons);
	config.pRadioButtons				= radiobuttons;
	config.cRadioButtons				= ARRAYSIZE( radiobuttons );
	config.dwFlags						= TDF_SHOW_PROGRESS_BAR |
		TDF_EXPAND_FOOTER_AREA | TDF_ENABLE_HYPERLINKS | TDF_CAN_BE_MINIMIZED
		| TDF_USE_COMMAND_LINKS | TDF_CALLBACK_TIMER;
	config.pszExpandedInformation		= _T( &quot;&lt;a href=\&quot;http://codereflect.com/\&quot; &gt;Codereflect.com&lt;/a&gt;&quot; );
	config.pfCallback = TaskDialogCallbackProc;
	BOOL bVerification = FALSE;
	TaskDialogIndirect(&amp;config, &amp;nButtonPressed, NULL, &amp;bVerification);

	switch (nButtonPressed)
	{
	case IDOK:
		break; // the user pressed button 0 (change password).
	case IDCANCEL:
		break; // user canceled the dialog
	default:
		break; // should never happen
	}
}
</pre>
<p>The callback function is also easy to manage. There are predefined set of events for each type of control and we can simply make use of these controls by sending various messages to update its state and values. One of the best example is updating the progressbar during the lifetime of messagebox. Once the timer is enabled, the callback function will be automatically fired on discrete time interval. The <a href="http://msdn.microsoft.com/en-us/library/bb760542(v=VS.85).aspx">notify messages are specified in detail in MSDN Documentation. Please check it.</a></p>
<p>To compile this source code, please use latest version of Visual Studio 2010 or any prior version with Windows Vista/7 SDK. You can also use express edition of Visual C++ to try this API.</p>
]]></content:encoded>
			<wfw:commentRss>http://codereflect.com/2010/07/07/windows-7-task-dialog-part-2-a-more-detailed-task-dialog/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Windows 7 Task Dialog &#8211; Part 1 &#8211; Displaying a basic task dialog</title>
		<link>http://codereflect.com/2010/06/20/windows-7-task-dialog-part-1-displaying-a-basic-task-dialog/</link>
		<comments>http://codereflect.com/2010/06/20/windows-7-task-dialog-part-1-displaying-a-basic-task-dialog/#comments</comments>
		<pubDate>Sun, 20 Jun 2010 16:23:05 +0000</pubDate>
		<dc:creator>Sarath</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[MFC]]></category>
		<category><![CDATA[Visual C++]]></category>
		<category><![CDATA[Visual Studio]]></category>
		<category><![CDATA[Visual Studio 2010]]></category>
		<category><![CDATA[Windows 7]]></category>
		<category><![CDATA[API]]></category>
		<category><![CDATA[Win32]]></category>

		<guid isPermaLink="false">http://codereflect.com/?p=1186</guid>
		<description><![CDATA[Every programmer must be familiar with the MessageBox es in any platform. It’s the simplest way to notify the user<a href="http://codereflect.com/2010/06/20/windows-7-task-dialog-part-1-displaying-a-basic-task-dialog/" class="searchmore">Read the Rest...</a><div class="clr"></div>]]></description>
			<content:encoded><![CDATA[<div style="float:right;margin:0px 0px 0px 2px;"></div><div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fcodereflect.com%2F2010%2F06%2F20%2Fwindows-7-task-dialog-part-1-displaying-a-basic-task-dialog%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fcodereflect.com%2F2010%2F06%2F20%2Fwindows-7-task-dialog-part-1-displaying-a-basic-task-dialog%2F&amp;source=sarat&amp;style=normal&amp;service=bit.ly" height="61" width="50" /><br />
			</a>
		</div>
<p>Every programmer must be familiar with the MessageBox es in any platform. It’s the simplest way to notify the user to take an action or provide notifications. Windows provides standard message box functionality with <a href="http://msdn.microsoft.com/en-us/library/ms645505(VS.85).aspx">MessageBox</a> API for displaying MessageBox with most frequently used buttons (OK, Cancel, Yes, No etc) and icons (error, warning, info etc.) This is again limited in programmer’s point of view and most we used to create our own messagebox to meet our extended purpose.</p>
<p>Windows UI has revamped since windows Vista and the UI experience was totally new from its predecessors. Most of the Windows Vista/7 applications display and extended form of messagebox called <a href="http://msdn.microsoft.com/en-us/library/bb787471(VS.85).aspx">task dialog</a>. Task dialog gives us more flexibility over the typical messagebox we used to have in Windows. We can display progress bars, extended information on footer, custom icons, predefined icons like Shield Icons (to indicate UAC elevation is required for operation etc.)</p>
<p>Task dialog comes with a simple set of interfaces and also provides an advanced version of interface to take control over what all we’re displaying in the task dialog</p>
<p><strong>Displaying a Simple Taskbar</strong></p>
<p>A simple task dialog can be displayed by calling <a href="http://msdn.microsoft.com/en-us/library/bb760540(v=VS.85).aspx">TaskDialog</a> API. The default buttons like OK, Yes, No, Cancel, Retry and close button. It’s simple as displaying a normal messagebox. The major difference is that we can have a custom icon, also a main instruction to show the intention of task dialog and main content provides further description of it. Organization of message is better readable and can have more focus than standard messagebox.</p>
<p style="text-align: center;"><a href="http://codereflect.com/wp-content/uploads/2010/06/TaskDialog.png"><img class="size-full wp-image-1188 aligncenter" title="TaskDialog" src="http://codereflect.com/wp-content/uploads/2010/06/TaskDialog.png" alt="TaskDialog" width="496" height="155" /></a></p>
<pre class="brush: cpp;">
	TASKDIALOG_COMMON_BUTTON_FLAGS tButton =
		TDCBF_OK_BUTTON| TDCBF_YES_BUTTON|
		TDCBF_NO_BUTTON| TDCBF_CANCEL_BUTTON|
		TDCBF_RETRY_BUTTON| TDCBF_CLOSE_BUTTON;

	int nClickedBUtton;
	TaskDialog( m_hWnd, NULL,
		_T(&quot;Demonstrating TaskDialog API&quot;),
		_T(&quot;This is the main instruction&quot;), _T(&quot;This shows the main content&quot;),
		tButton,
		MAKEINTRESOURCE( IDI_SHIELD ),
		&amp;nClickedBUtton );
</pre>
<p>In the above code you can see the default buttons are displayed with binary OR “|” operator. The return value is passed as parameter and can refer after calling the API. The return value of the function gives the error status. S_OK means the dialog was successfully displayed. the details of the parameters and return values are described in the <a href="http://msdn.microsoft.com/en-us/library/bb760540(v=VS.85).aspx">TaskDialog API documentation</a>. In the next installment, let’s have a look at advanced usage of Task Dialog.</p>
]]></content:encoded>
			<wfw:commentRss>http://codereflect.com/2010/06/20/windows-7-task-dialog-part-1-displaying-a-basic-task-dialog/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Window 7: Setting custom Thumbnail clip</title>
		<link>http://codereflect.com/2010/05/21/window-7-setting-custom-thumbnail-clip/</link>
		<comments>http://codereflect.com/2010/05/21/window-7-setting-custom-thumbnail-clip/#comments</comments>
		<pubDate>Fri, 21 May 2010 14:53:50 +0000</pubDate>
		<dc:creator>Sarath</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[Codeproject]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[Visual C++]]></category>
		<category><![CDATA[Visual Studio]]></category>
		<category><![CDATA[Visual Studio 2010]]></category>
		<category><![CDATA[Windows 7]]></category>
		<category><![CDATA[Windows 7 Taskbar]]></category>

		<guid isPermaLink="false">http://codereflect.com/?p=1171</guid>
		<description><![CDATA[In the previous posts I covered the basics of setting progress overlay and adding buttons to the thumbnail flyout area.<a href="http://codereflect.com/2010/05/21/window-7-setting-custom-thumbnail-clip/" class="searchmore">Read the Rest...</a><div class="clr"></div>]]></description>
			<content:encoded><![CDATA[<div style="float:right;margin:0px 0px 0px 2px;"></div><div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fcodereflect.com%2F2010%2F05%2F21%2Fwindow-7-setting-custom-thumbnail-clip%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fcodereflect.com%2F2010%2F05%2F21%2Fwindow-7-setting-custom-thumbnail-clip%2F&amp;source=sarat&amp;style=normal&amp;service=bit.ly" height="61" width="50" /><br />
			</a>
		</div>
<p>In the previous posts I covered the basics of setting progress overlay and adding buttons to the thumbnail flyout area.</p>
<p>All thumbnail previews displays the real-time content of the window client area. Suppose if we’re watching movies, we can see it through thumbnails. If we’re listening music, we can see the album artwork on hovering the thumbnail. But we can customize the window area to display in the thumbnail area.</p>
<p>Let’s take the previous example. by default Windows will display the entire window area.</p>
<p><a href="http://codereflect.com/wp-content/uploads/2010/05/image7.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://codereflect.com/wp-content/uploads/2010/05/image_thumb7.png" width="248" height="198" /></a> </p>
<p>Let’s take an example of clipping the preview to the scrollbar.</p>
<p><a href="http://codereflect.com/wp-content/uploads/2010/05/image8.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://codereflect.com/wp-content/uploads/2010/05/image_thumb8.png" width="244" height="147" /></a> </p>
<p>What we can do, it’s quite simple and straight forward. Just set the offset from the client area to be clipped. See the example below. Setting a NULL area will reset the thumbnail preview to default.</p>
<pre class="brush: cpp;">
BEGIN_MESSAGE_MAP(CTaskBarSampleDlg, CDialogEx)
	ON_REGISTERED_MESSAGE( g_uTBBC, CTaskBarSampleDlg::OnCreateThumbToolBar )
END_MESSAGE_MAP()

LRESULT CTaskBarSampleDlg::OnCreateThumbToolBar( WPARAM, LPARAM )
{
	// Initialize the pointer. You can also do this in the constructor.
	// Remember to release after use
	if( NULL == m_pTaskBarlist )
	{
		CoCreateInstance( CLSID_TaskbarList, NULL, CLSCTX_ALL,
						  IID_ITaskbarList3, (void**)&amp;m_pTaskBarlist);
	}
	return 0;
}

void CTaskBarSampleDlg::OnBnClickedButton2()
{
	static bool bClip = true;
	if( bClip )
	{
		CRect rect;
		m_Progress.GetWindowRect( rect );
		ScreenToClient( rect );
		m_pTaskBarlist-&gt;SetThumbnailClip( m_hWnd,rect );

	}
	else
		m_pTaskBarlist-&gt;SetThumbnailClip( m_hWnd, NULL ); // NULL will reset to default

	bClip = !bClip;
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://codereflect.com/2010/05/21/window-7-setting-custom-thumbnail-clip/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Windows 7: Adding toolbar buttons to taskbar button flyout</title>
		<link>http://codereflect.com/2010/05/13/windows-7-adding-toolbar-buttons-to-taskbar-button-flyout/</link>
		<comments>http://codereflect.com/2010/05/13/windows-7-adding-toolbar-buttons-to-taskbar-button-flyout/#comments</comments>
		<pubDate>Thu, 13 May 2010 17:00:00 +0000</pubDate>
		<dc:creator>Sarath</dc:creator>
				<category><![CDATA[Codeproject]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[Visual C++]]></category>
		<category><![CDATA[Visual Studio]]></category>
		<category><![CDATA[Visual Studio 2010]]></category>
		<category><![CDATA[Windows 7]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[Windows]]></category>
		<category><![CDATA[Windows 7 Taskbar]]></category>

		<guid isPermaLink="false">http://codereflect.com/?p=1160</guid>
		<description><![CDATA[In the previous post, we’ve discussed about progressbar overlay on taskbar button. In this installment, we will see how to<a href="http://codereflect.com/2010/05/13/windows-7-adding-toolbar-buttons-to-taskbar-button-flyout/" class="searchmore">Read the Rest...</a><div class="clr"></div>]]></description>
			<content:encoded><![CDATA[<div style="float:right;margin:0px 0px 0px 2px;"></div><div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fcodereflect.com%2F2010%2F05%2F13%2Fwindows-7-adding-toolbar-buttons-to-taskbar-button-flyout%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fcodereflect.com%2F2010%2F05%2F13%2Fwindows-7-adding-toolbar-buttons-to-taskbar-button-flyout%2F&amp;source=sarat&amp;style=normal&amp;service=bit.ly" height="61" width="50" /><br />
			</a>
		</div>
<p>In the previous post, we’ve discussed about progressbar overlay on taskbar button. In this installment, we will see how to add the toolbar buttons to the flyout thumbnail Window. The best example is Windows Media Player. You can control the tracks using the flyout window on hovering the thumbnail button.</p>
<p><a href="http://codereflect.com/wp-content/uploads/2010/05/image5.png"><img style="display: inline; margin-left: 0px; margin-right: 0px; border-width: 0px;" title="image" src="http://codereflect.com/wp-content/uploads/2010/05/image_thumb5.png" border="0" alt="image" width="245" height="241" /></a></p>
<p>As you can see above, the thumbnail popping up is not really displaying the actual content of the window( which is by default). It show a custom thumbnail image and toolbar buttons down the image to control the track.</p>
<p>Adding these buttons are quite easy.</p>
<p>There are few peculiarities for the buttons displayed as toolbar in thumbnail area.</p>
<ol>
<li>A maximum of 7 buttons can be added to thumbnail</li>
<li>The size of the bitmaps specified for thumbnails is dependent on the current DPI. Usually we use the 16&#215;16 image for default DPI( 96 ). The bitmap size can be calculated easily by calling GetSystemMetrics( SM_CXSIZE ) and GetSystemMetrics( SM_CYSIZE ). This has to be taken care if the application is DPI aware.</li>
<li>The buttons follows the same order they specified in the array.</li>
<li>The buttons can’t be removed once it’s added, however we can hide/update the buttons.</li>
<li>The image lists created to specify the bitmaps can be released after calling adding it to the thumbnail area. The <a href="http://msdn.microsoft.com/en-us/library/dd391692(VS.85).aspx">ITaskbarList3</a> interface can also be released if we’re finished processing.</li>
<li>The most important thing is, <strong>Every application must handle the “TaskbarButtonCreated” message before using the ITaskList3 interface functions. Otherwise the application may face exceptions.  Especially when the buttons and other overlays are initialized on application initialization.</strong></li>
<li>The button effect on disabling, enabling, hovering is controlled by shell itself. We don’t need to keep separate image list for this.</li>
</ol>
<h2>Handling TaskbarButtonCreated message in MFC Application</h2>
<p>As I mentioned before, the first task is to handle the “TaskbarButtonCreated” message in the application for proper initialization of <a href="http://msdn.microsoft.com/en-us/library/dd391692(VS.85).aspx">ITaskbarList3</a> interface.</p>
<p>Following code will help you to do this. You can initialize the variable globally or inside the constructor of the class.</p>
<pre class="brush: cpp;">
UINT g_uTBBC = RegisterWindowMessage(L&quot;TaskbarButtonCreated&quot;);
</pre>
<p>Once this parameter initialized, handle the message inside the message map of the dialog class. Note that you should not use ON_MESSAGE to handle the message. ON_REGISTERED_MESSAGE function must be used.</p>
<pre class="brush: cpp;">
BEGIN_MESSAGE_MAP(CTaskBarSampleDlg, CDialogEx) ...
...
ON_REGISTERED_MESSAGE( g_uTBBC, CTaskBarSampleDlg::OnCreateThumbToolBar )
...
END_MESSAGE_MAP()
</pre>
<p>Proceed to initialize ITaskbarList3 after receving notification in the message handler.</p>
<pre class="brush: cpp;">
LRESULT CTaskBarSampleDlg::OnCreateThumbToolBar( WPARAM, LPARAM )
 { AddThumbarButtons(); return 0; }</pre>
<h2>Initializing and Adding Buttons</h2>
<p>Now let’s see the initialization process.  This can be accomplished in following steps</p>
<ul>
<li>Initialize the ITaskbarList3 interface</li>
<li>Create Image list to specify as the bitmap buttons</li>
<li>Add image list to the interface</li>
<li>Prepare the button information as array</li>
<li>Add the buttons</li>
</ul>
<p>I’ve added enough comment in the source, I hope it’s easy to understand.</p>
<pre class="brush: cpp;">
void CTaskBarSampleDlg::AddThumbarButtons()
{
        // Initialize the pointer. You can also do this in the constructor.
    // Remember to release after use
    if( NULL == m_pTaskBarlist )
    {
        CoCreateInstance( CLSID_TaskbarList, NULL, CLSCTX_ALL,
                          IID_ITaskbarList3, (void**)&amp;m_pTaskBarlist);
    }

    m_pTaskBarlist-&gt;HrInit();

    // We mention that, we're specifying bitmap, tooltpi and other flags.
    // This is mask, only specified parameters will be taken from structure
    THUMBBUTTONMASK dwMask = THB_BITMAP | THB_TOOLTIP | THB_FLAGS;

    // Create the images from a single bitmap which contains
    // buttons seperated 16pix width. Like we handle toolbar buttons
    CImageList  ImageList;
    ImageList.Create( MAKEINTRESOURCE( IDB_BITMAP_PAUSE), 16, 2, RGB( 0xFF,0,0xFF));

    // Prepare the thumbar buttons
    THUMBBUTTON thbButtons[2];
    thbButtons[0].dwMask = dwMask;
    // This can be any integer required on handling button events
    thbButtons[0].iId = IDB_THB_BUTTON_START;
    thbButtons[0].iBitmap = 0;
    lstrcpy( thbButtons[0].szTip, TEXT(&quot;Start&quot;));
    thbButtons[0].dwFlags = THBF_ENABLED | THBF_DISMISSONCLICK;

    thbButtons[1].dwMask = dwMask;
    thbButtons[1].iId = IDB_THB_BUTTON_PAUSE;
    thbButtons[1].iBitmap = 1;
    thbButtons[1].dwFlags = THBF_DISABLED | THBF_DISMISSONCLICK;
    lstrcpy( thbButtons[1].szTip, TEXT(&quot;Pause&quot;));

    // Declare the image list that contains the button images.
    m_pTaskBarlist-&gt;ThumbBarSetImageList(m_hWnd, ImageList.GetSafeHandle());

    // Attach the toolbar to the thumbnail.
    m_pTaskBarlist-&gt;ThumbBarAddButtons(m_hWnd, ARRAYSIZE(thbButtons), thbButtons);

    // In this program we're using the interface later, so not releasing now.
    // It's Ok to release if finished using. We can create again when necessary
}
</pre>
<h2>Handling the events from buttons</h2>
<p>The events from buttons will be delivered as WM_COMMAND message for Window. As we’re using MFC, please override OnCommand function. The button ID can be extracted from wParam using LOWORD macro.</p>
<pre class="brush: cpp;">
BOOL CTaskBarSampleDlg::OnCommand(WPARAM wParam, LPARAM lParam)
{
    UINT CommandID = LOWORD( wParam );

        if( IDB_THB_BUTTON_START == CommandID )
            OnBnClickedButton1();
        else if( IDB_THB_BUTTON_PAUSE == CommandID )
            ResetProgress();
        else
            return CDialogEx::OnCommand(wParam, lParam);
}
</pre>
<h2>Updating Buttons</h2>
<p>We’ve received the button event, so it’s time to update the button.  We can’t remove the buttons as I specified before, we can hide, disable, enable the state of the buttons. Other properties like bitmaps and tooltip can also be updated</p>
<pre class="brush: cpp;">
void CTaskBarSampleDlg::ResetProgress()
{
    // reset the progress state
    m_pTaskBarlist-&gt;SetProgressState( m_hWnd, TBPF_NOPROGRESS );

    THUMBBUTTONMASK dwMask = THB_FLAGS;

    THUMBBUTTON thbButtons[2];
    thbButtons[0].dwMask = dwMask;
    thbButtons[0].iId = IDB_THB_BUTTON_START;
    thbButtons[0].dwFlags = THBF_ENABLED | THBF_DISMISSONCLICK;

    thbButtons[1].dwMask = dwMask;
    thbButtons[1].iId = IDB_THB_BUTTON_PAUSE;
    thbButtons[1].dwFlags = THBF_DISABLED| THBF_DISMISSONCLICK;

    m_pTaskBarlist-&gt;ThumbBarUpdateButtons( m_hWnd, ARRAYSIZE(thbButtons), thbButtons );
}
</pre>
<p>Note that the code specified above doesn’t not handling the icons based on the DPI. It always use the icon size of default DPI (16pix).  You can maintain different bitmaps to handle this. This code in the sample is just mere demonstration of this API. It may not have a professional standard.</p>
<h2>Final output</h2>
<p><a href="http://codereflect.com/wp-content/uploads/2010/05/image6.png"><img style="display: inline; border: 0px;" title="image" src="http://codereflect.com/wp-content/uploads/2010/05/image_thumb6.png" border="0" alt="image" width="240" height="189" /></a> To get the icons, you can locate this down in the Visual Studio Installation directory. Usually it appears at</p>
</pre>
<p>C:\Program Files\Microsoft Visual Studio 10.0\Common7\VS2010ImageLibrary\1033\VS2010ImageLibrary\VS2010ImageLibrary</pre>
<p><a href="http://cid-5544789b26bfc05d.skydrive.live.com/self.aspx/.Public/TaskBarSample.rar">Download Source[VS2010] – TaskbarSample.rar</a></p>
<p>Note: You’ve the sole responsibility of downloading executing the code. It works only with Windows 7 and compiled under Visual Studio 2010</p>
]]></content:encoded>
			<wfw:commentRss>http://codereflect.com/2010/05/13/windows-7-adding-toolbar-buttons-to-taskbar-button-flyout/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Windows 7: How to display progress bar on taskbar icon?</title>
		<link>http://codereflect.com/2010/05/13/windows-7-how-to-display-progress-bar-on-taskbar-icon/</link>
		<comments>http://codereflect.com/2010/05/13/windows-7-how-to-display-progress-bar-on-taskbar-icon/#comments</comments>
		<pubDate>Thu, 13 May 2010 16:56:34 +0000</pubDate>
		<dc:creator>Sarath</dc:creator>
				<category><![CDATA[Visual C++]]></category>
		<category><![CDATA[Visual Studio]]></category>
		<category><![CDATA[Visual Studio 2010]]></category>
		<category><![CDATA[Windows 7]]></category>
		<category><![CDATA[Windows 7 Taskbar]]></category>

		<guid isPermaLink="false">http://codereflect.com/2010/05/13/windows-7-how-to-display-progress-bar-on-taskbar-icon/</guid>
		<description><![CDATA[The best thing you may notice in Windows 7 may be its taskbar. It&#8217;s totally revamped and gives us a<a href="http://codereflect.com/2010/05/13/windows-7-how-to-display-progress-bar-on-taskbar-icon/" class="searchmore">Read the Rest...</a><div class="clr"></div>]]></description>
			<content:encoded><![CDATA[<div style="float:right;margin:0px 0px 0px 2px;"></div><div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fcodereflect.com%2F2010%2F05%2F13%2Fwindows-7-how-to-display-progress-bar-on-taskbar-icon%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fcodereflect.com%2F2010%2F05%2F13%2Fwindows-7-how-to-display-progress-bar-on-taskbar-icon%2F&amp;source=sarat&amp;style=normal&amp;service=bit.ly" height="61" width="50" /><br />
			</a>
		</div>
<p>The best thing you may notice in Windows 7 may be its taskbar. It&#8217;s totally revamped and gives us a fresh look, more usable with progress bar overlays, jump lists to easily open the last opened documents, and improved and interactive thumbnail previews applications etc.&nbsp;You can <a href="http://windows.microsoft.com/en-us/windows7/products/features/windows-taskbar">see more details in Windows 7 website</a>.</p>
<p>In this post, I am covering progress bar to display as taskbar button overlay. In Windows 7, you might have noticed the progress bar on taskbar button in many applications like Windows Explorer shows progress of file copy, Internet Explorer or Safari displays progress of downloads, etc.</p>
<p>We can program this for our application as well. It can be easily accomplished using the <a href="http://msdn.microsoft.com/en-us/library/dd391692(VS.85).aspx">ITaskList3 COM Interface</a>. This Interface not only for progress bars. It can be used for:</p>
<ul>
<li>When working with a TDI (Tabbed Document Interface) application (such as Internet Explorer) or a MDI application (such as Microsoft Excel) that is displaying its windows as a group on the taskbar:
<ul>
<li>Provide the taskbar with a thumbnail that represents the view of an individual tab or document. (You can see this in action with Safari Or Internet Explorer Browser) </li>
<li>Remove the thumbnail of an individual tab or document from the group. </li>
<li>Change the order of thumbnails in the group. </li>
<li>Set a tab thumbnail as the selected item when the thumbnails are shown. </li>
</ul>
</li>
<li>When applying an overlay to a taskbar icon, such as a notification.
<ul>
<li>When showing the progress of an operation, such as copying or installing an item. </li>
<li>When adding a toolbar to a thumbnail. (like iTunes of Windows Media Player) </li>
</ul>
</li>
</ul>
<p>Now let’s see how we can do this. I&#8217;ve prepared a simple dialog based application which shows a progressbar as below. You can also see the progressbar over the Icon of application displayed in the thumbnail area.</p>
<p><a href="http://codereflect.com/wp-content/uploads/2010/05/image3.png"><img title="image" style="border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px; " height="163" alt="image" hspace="0" src="http://codereflect.com/wp-content/uploads/2010/05/image_thumb3.png" width="364" border="0" /></a></p>
<p><a href="http://codereflect.com/wp-content/uploads/2010/05/image4.png"><img title="image" style="border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px; " height="40" alt="image" src="http://codereflect.com/wp-content/uploads/2010/05/image_thumb4.png" width="374" border="0" /></a></p>
<p>The code is self explanatory. See the code snippet and comments. Don&#8217;t forget to release the <code>ITaskListInterface </code>after usage. You should call <code>m_pTaskList&gt;Release(); </code>to release the interface.</p>
<h3>Step #1</h3>
<p>Declare the pointer to access <code>ITaskbarList3 </code>interface. You can make this as a member of the dialog class.</p>
<pre lang="C++">ITaskbarList3* m_pTaskBarlist;
CoCreateInstance(
            CLSID_TaskbarList, NULL, CLSCTX_ALL,
            IID_ITaskbarList3, (void**)&amp;m_pTaskBarlist);</pre>
<h3>Step #2</h3>
<p>Initialize the progressbar state to enable the progressbar overlay:</p>
<pre lang="C++">m_pTaskBarlist-&gt;SetProgressState( m_hWnd, TBPF_INDETERMINATE );</pre>
<p>The parameters are relatively simple. Windows handle must be passed along with the type of progressbar that needs to be displayed. If you check the definition, you can see the following types can be passed to the function. These states are exactly similar state of a normal progressbar.</p>
<table class="ArticleTable" width="600">
<tbody>
<tr>
<td valign="top" width="200"><code>TBPF_NOPROGRESS</code></td>
<td valign="top" width="325">This flag disables the progressbar overlay and displays normal taskbar button</td>
</tr>
<tr>
<td valign="top" width="200"><code>TBPF_INDETERMINATE</code></td>
<td valign="top" width="325">Instead of displaying the constant progress, it displays horizontal marquee</td>
</tr>
<tr>
<td valign="top" width="200"><code>TBPF_NORMAL</code></td>
<td valign="top" width="325">Normal progressbar</td>
</tr>
<tr>
<td valign="top" width="200"><code>TBPF_ERROR</code></td>
<td valign="top" width="325">Progressbar with an red colored overlay (based on the theme)</td>
</tr>
<tr>
<td valign="top" width="200"><code>TBPF_PAUSED</code></td>
<td valign="top" width="325">Paused state. In this state, you can see a flashing horizontal on progressbars</td>
</tr>
</tbody>
</table>
<h3>Step #3 Progress</h3>
<p>Simply call <code>SetProgressValue() </code>as given below. The current value and maximum value should be given.</p>
<pre lang="C++">m_pTaskBarlist-&gt;SetProgressValue( m_hWnd, nPos, nMax );</pre>
<p>Once you finish the task, you&#8217;ve to disable the progress state. I hope you know how to do it <img class="wp-smiley" alt=":)" src="http://codereflect.com/wp-includes/images/smilies/icon_smile.gif" /> </p>
<p>See the full scoop here.</p>
<p>To compile this code, either use Visual Studio 2010 or&nbsp;<a href="http://www.microsoft.com/downloads/details.aspx?FamilyID=c17ba869-9671-4330-a63e-1fd44e0e2505&amp;displaylang=en">download the Windows 7 SDK from MSDN website</a>.</p>
<pre lang="C++">// Declare this as member of the class or global (not recommended)
ITaskbarList3* m_pTaskBarlist;
void CTaskBarSampleDlg::OnBnClickedButtonStart()
{
	m_Progress.SetRange( 0, 10 ); // set the range for the control
	m_Progress.SetPos( 0 ); // Set initial position
	// Initialize the pointer. You can also do this in the constructor.
	// Remember to release after use
	if( NULL == m_pTaskBarlist )
	{
		CoCreateInstance(
			CLSID_TaskbarList, NULL, CLSCTX_ALL,
			IID_ITaskbarList3, (void**)&amp;m_pTaskBarlist);
	}
	m_pTaskBarlist-&gt;SetProgressState( m_hWnd, TBPF_INDETERMINATE );
	SetTimer( 0, 500, 0 );
}
// Timer to update the progress
void CTaskBarSampleDlg::OnTimer(UINT_PTR nIDEvent)
{
	int nPos = m_Progress.GetPos();
	int nMin, nMax;
	m_Progress.GetRange( nMin, nMax );
	nPos++;
	// if finished, kill timer and return
	if( nPos &gt; nMax )
	{
		// reset the progress state
		m_pTaskBarlist-&gt;SetProgressState( m_hWnd, TBPF_NOPROGRESS );
		// just flash the window to notify user
		FlashWindow( true );
		// Stop the timer
		KillTimer( 0 );
		// You can release the pointer if necessary here.
                  // But not a good practice
		return;
	}
	// set progress to taskbar overlay
	m_pTaskBarlist-&gt;SetProgressValue( m_hWnd, nPos, nMax );
	// set the control's progress state
	m_Progress.SetPos( nPos );
}</pre>
<p><img height="1" src="http://feeds.feedburner.com/~r/sarathc/~4/bhJRJjIf9wU" width="1" /></p>
]]></content:encoded>
			<wfw:commentRss>http://codereflect.com/2010/05/13/windows-7-how-to-display-progress-bar-on-taskbar-icon/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Windows 7 Tips: Using Reliability monitor</title>
		<link>http://codereflect.com/2010/03/28/windows-7-tips-using-reliability-monitor/</link>
		<comments>http://codereflect.com/2010/03/28/windows-7-tips-using-reliability-monitor/#comments</comments>
		<pubDate>Sun, 28 Mar 2010 02:59:48 +0000</pubDate>
		<dc:creator>Sarath</dc:creator>
				<category><![CDATA[Tips]]></category>
		<category><![CDATA[Windows 7]]></category>

		<guid isPermaLink="false">http://codereflect.com/2010/03/28/windows-7-tips-using-reliability-monitor/</guid>
		<description><![CDATA[The Reliability Monitor is intended for advanced computer users, such as software developers and network administrators. Reliability monitor was introduced<a href="http://codereflect.com/2010/03/28/windows-7-tips-using-reliability-monitor/" class="searchmore">Read the Rest...</a><div class="clr"></div>]]></description>
			<content:encoded><![CDATA[<div style="float:right;margin:0px 0px 0px 2px;"></div><div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fcodereflect.com%2F2010%2F03%2F28%2Fwindows-7-tips-using-reliability-monitor%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fcodereflect.com%2F2010%2F03%2F28%2Fwindows-7-tips-using-reliability-monitor%2F&amp;source=sarat&amp;style=normal&amp;service=bit.ly" height="61" width="50" /><br />
			</a>
		</div>
<p>The Reliability Monitor is intended for advanced computer users, such as software developers and network administrators. Reliability monitor was introduced first in Windows Vista as “Reliability and Performance Monitor”. Now it has been tear-off and independent from Performance Monitor. The purpose of reliability monitor is to graphically plot the stability index and display the hardware and software failures and other core information from the system (such as application installation and removal etc.), which affects the stability of the machine. </p>
<p><a href="http://codereflect.com/wp-content/uploads/2010/03/image2.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://codereflect.com/wp-content/uploads/2010/03/image_thumb.png" width="658" height="390" /></a> </p>
<p>You can see that Safari and other application has crashed many times and also the I’ve got some some warnings when I tried uninstalling the beta version of office 2010. The list also contains the programs I uninstalled.</p>
<li>
<p><strong>Click Maintenance</strong>. Then, under Check for solutions to problem reports, click <strong>View reliability history.</strong></p>
</li>
<li>
<p>In Reliability Monitor, you can:</p>
<ul>
<li>
<p>Click any event on the graph to view its details.</p>
</li>
<li>
<p>Click Days or Weeks to view the stability index over a specific period of time.</p>
</li>
<li>
<p>Click items in the Action column to view more information about each event.</p>
</li>
<li>
<p>Click View all problem reports to view only the problems that have occurred on your computer. This view doesn&#8217;t include the other computer events that show up in Reliability Monitor, such as events about software installation.</p>
</li>
</ul>
</li>
<p>You can <a href="http://technet.microsoft.com/en-us/library/cc722107(WS.10).aspx" target="_blank">see more details in Microsoft website</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://codereflect.com/2010/03/28/windows-7-tips-using-reliability-monitor/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
