Category Archive for Win32 API

Introducing the Windows Kernel Transaction Manager, Transactional NTFS and Transactional Registry

Windows Vista and later versions of Windows have a so called Kernel Transaction Manager (KTM). This makes it easy for application to use transactions. There are two big components built on top of the KTM: Transactional NTFS (TxF) and Transactional Registry (TxR). With TxF you can combine several file system operations into a transaction and either commit or rollback the transaction as a whole. TxR allows you to do the same thing with registry manipulations. File system transactions and registry transactions can be grouped into a single transaction. That way we can ensure that all file system modifications and all registry modifications are fully executed or none at all.

I wrote an article that gives a very brief overview of TxF and TxR to give you an idea of what you can accomplish with it. Creating your own transaction manager other than TxF or TxR is also possible but requires you to create a Win32 transaction aware service (= resource manager) which is outside the scope of this introductory article. You can read the full article on codeguru.com.

Share

How To Use UpdateLayeredWindow

In this post I will briefly explain how to use layered windows and specifically how to use UpdateLayeredWindow.

The first thing you need to do is add the WS_EX_LAYERED style to your window. This can for example be done with a call to CreateWindowEx:

hWnd = CreateWindowEx(WS_EX_LAYERED, szWindowClass, szTitle, 0,
         CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);

After your window is created we will load a PNG file with an alpha channel and use UpdateLayeredWindow to render the PNG on the window using the alpha channel of the PNG file as the transparency level for the window. This is done as follows:

// Load our PNG image
CImage img;
img.Load("circle.png");
// Get dimensions
int iWidth = img.GetWidth();
int iHeight = img.GetHeight();
// Make mem DC + mem  bitmap
HDC hdcScreen = GetDC(NULL);
HDC hDC = CreateCompatibleDC(hdcScreen);
HBITMAP hBmp = CreateCompatibleBitmap(hdcScreen, iWidth, iHeight);
HBITMAP hBmpOld = (HBITMAP)SelectObject(hDC, hBmp);
// Draw image to memory DC
img.Draw(hDC, 0, 0, iWidth, iHeight, 0, 0, iWidth, iHeight);

// Call UpdateLayeredWindow
BLENDFUNCTION blend = {0};
blend.BlendOp = AC_SRC_OVER;
blend.SourceConstantAlpha = 255;
blend.AlphaFormat = AC_SRC_ALPHA;
POINT ptPos = {0, 0};
SIZE sizeWnd = {iWidth, iHeight};
POINT ptSrc = {0, 0};
UpdateLayeredWindow(hWnd, hdcScreen, &ptPos, &sizeWnd, hDC, &ptSrc, 0, &blend, ULW_ALPHA);

SelectObject(hDC, hBmpOld);
DeleteObject(hBmp);
DeleteDC(hDC);
ReleaseDC(NULL, hdcScreen);

Because I’m using CImage, you need to include the atlimage.h header.

That’s all that is required for the basics of UpdateLayeredWindow.

NOTE: The example above does not include any error checking. That is left for the reader as an excercise.

Share

Developing High DPI Aware Applications

I’m currently in the process of implementing high DPI support in one of my applications. High DPI support is getting more and more important, especially with the resolution of todays laptop screens. Most people will switch to 120 DPI or even 144 DPI instead of the default 96 DPI to make text easier to read. However, when your application is not High DPI aware, Windows Vista and Windows 7 will in certain situations scale everything for you but this will result in a blurry user interface.

Microsoft has released an interesting document describing the steps involved in developing High DPI aware applications. You can find them at the following links:

Share

Using the Windows Vista/Windows 7 Built-In Buffered Animation API

Windows Vista comes with a built-in buffered animation API. This API makes it easy to make animations without flickering(*). I wrote a new article on CodeGuru that explains how to use this Buffered Animation API with C++.

The article comes with an example application to illustrate the Windows Vista buffered animation technique. The example will draw a new colored random rectangle in the window each time you press the spacebar. The new rectangle will smoothly fade onto the window. The buffered animation is used for this fading effect.

Read the full article.

(*) Note: While writing the article, I found an issue with the DWM in Windows Vista. After discussing with someone from Microsoft, it seems the buffered animation API is relying on some buffering from the DWM; however, on Windows Vista this is causing some flickering. They also told me that the buffered animation was not designed for big or full-screen animations but rather for small animations like fading buttons. To reduce the flickering as much as possible, only use the buffered animation on small rectangles and use a short animation interval, for example 500 milliseconds, which is more than enough for GUI related animations anyway.

The good news is that I tested the application on Windows 7 Beta and it works without any flickering. So, it seems that the DWM issue has been fixed on Windows 7.

Share

Using Windows Vista Built-In Double Buffering

The solution to fixing flickering issues when drawing graphics is to use double buffering. Double buffering basically means that an off-screen buffer is created. Everything is rendered to this off-screen buffer and, when drawing is completed, this off-screen buffer is copied to the screen. The end result is that you do not see any flickering and you do not see the drawing being created part by part. A little unknown fact is that Windows Vista has built-in support for double buffering, so managing off-screen buffers, copying data, and so forth are all managed by Windows for you. I wrote an article including an example on how to use this built-in support for double buffering with C++. This article has been published on CodeGuru.

Read the full article.

Share

Wallpaper Cycler and Windows 7

When the beta of Windows 7 was released, I obviously had to install it to test Wallpaper Cycler on it. Wallpaper Cycler started fine, so that was a good start 🙂 However, when I wanted to cycle the wallpaper on the desktop, it changed the tiling setting on the desktop properly, but the wallpaper itself was not changed, bummer 🙁

Since Windows 7 comes by default with a very basic wallpaper cycler built-in, I thought that Microsoft had maybe changed the programming interface to change the wallpaper. I started to search on the internet for information regarding this, but didn’t find anything. So, I started debugging… Read the rest of this entry »

Share

Latest ScrnSave.Lib depends on ChangeWindowMessageFilter

I was trying to port a screensaver from Visual Studio 2005 to Visual Studio 2008.

After loading the project in VS2008, compilation went without any problems. However, when I tried to test the screensaver on a Windows XP box I got the error message that the function ChangeWindowMessageFilter could not be found in user32.dll. The first thing I did was to open the MSDN and search for this ChangeWindowMessageFilter function. According to the documentation ChangeWindowMessageFilter is only available on Windows Vista. I obviously didn’t use that function myself otherwise I would have known about it in the first place 😉 So I checked the libraries that I was linking to. The most obvious candidate would be the ScrnSave.lib library. I searched for it and it was linking to the version that was installed by Visual Studio 2008 in C:\Program Files\Microsoft SDKs\Windows\v6.0A\Lib. To make sure I did a “dumpbin /symbols ScrnSave.Lib” on it and it was indeed using the ChangeWindowMessageFilter function.

It is pretty strange that this ScrnSave.Lib is dependent on a Vista API call. This just annoys developers that try to create a screensaver that still works on Windows XP. It should be pretty easy for the developers of ScrnSave.Lib to dynamically load the user32.dll and see if ChangeWindowMessageFilter is available.

Anyway, the easiest and quickest workaround that I’m using right now is to use the ScrnSave.Lib from the Visual Studio 2005 installation. After recompiling my screensaver with this older library it works again on Windows XP.

Share

How to Save and Load a Windows Region with the Win32 API

In my previous post I explained how to save a Windows region to a file and how to load it again using MFC. Some people have problems translating MFC code to standard Win32 API code, so I wrote a followup article for CodeGuru explaining exactly the same thing but with Win32 API code instead of MFC.

The article explains in details how to save a Windows region to a file using GetRegionData and how to load and re-create this saved region with ExtCreateRegion using MFC.

There are 2 functions in the article: SaveRegion and LoadRegion. Read the rest of this entry »

Share