Changing a Window’s Shape

CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

In most newsgroups that have something to do with Windows programming there are postings on how to change window’s shape. I thought that there must be an article about this here, and when I didn’t find one, I decided to write one myself.

In reality changing window’s shape is very easy to do, although most people think that it’s an extremely hard procedure. In this article I will explain how to create a window that will be a circle instead of a rectangle shape.

There are two functions that change window’s shape: CWnd::SetWindowRgn() and win32’s SetWindowRgn(). I will use the CWnd::SetWindowRgn(), but u can use the one you like more.

The function declaration looks like this:
int SetWindowRgn( HRGN hRgn, BOOL bRedraw );

The return value is zero if function fails or nonzero otherwise(at least it says so in Microsoft documentation). The first parameter is the CRgn, a region of a window, and the second parameter is a BOOL value that allows u to redraw the window. All you have to do is to create a CRgn variable, and pass the parametr to the SetWindowRgn function. That’s all.

Here is the source code you have to add to your application in order to make a window be a circle shape.

CRgn MyRgn;
MyRgn.CreateEllipticRgn(0, 0, 100, 100);
SetWindowRgn(MyRgn, TRUE);

That’s it. I can add just one more thing. If you create a button or another window without a title bar, you can skip this paragraph. If the window has a title bar, I advise you to read this. The thing is, that with most regions at least part of a title bar will not be drawn. From my point of view, the optimal decision is to create a window with a style that won’t have a title bar, and then implement the title bar functions yourself. It sounds hard, but it’s much easier then dealing with a regular title bar.

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read