Changing a Window's Shape


Desktop-as-a-Service Designed for Any Cloud ? Nutanix Frame

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.



    Posted by Legacy on 07/11/2002 07:00am

    Originally posted by: Nitin S. Jadhav

    I was searching for a article on how to create non-rectangular window that can be resized and found the following pdf document.
    You will definitely like this.

  • SetWindowRgn() works only once

    Posted by Legacy on 11/22/2001 08:00am

    Originally posted by: B Srinivasa reddy

    I am trying to set the Roundrect region for a CStatic control using SetWindowRgn() in order to set rounded bmp in the control. SetWindowRgn() works properly for the first time, but the second time when is call the same function again in the On Paint function the controls comes up with the orginal rect
    Please let me know if there is any solution or work around


  • A Running BMP in my Desktop.

    Posted by Legacy on 10/18/2001 07:00am

    Originally posted by: Jayachandran.C

    How do i create a programme in Vc++ to create a Small Bitmap Sizing to the Thubnail Running Above my Task Bar. I got Some exe's from net That is a share ware. That A bird flying on my desktop or a Cat running above my taskbar that type i wan to make .

    please help me to do this thanking you

  • How: Internal OS implementation

    Posted by Legacy on 06/26/2001 07:00am

    Originally posted by: Alok Govil

    How does the OS does it, ie, create non-rectangular windows.

    ISSUE: When I hover the mouse on a window, the OS must determine on which window the mouse is currently on. So that the mouse-move message is put to the right window's queue. Same goes for all the messages, not excluding wm_paint.

    For a rectangular window, this is easy. One needs say four comparisons to find out if the current mouse position is within a particular window.

    How does the system do it for non-rectangular windows. For ellipitical windows, using the simple equation of an ellipse can do. But I have no idea if the OS does this.

    For the regions created from bitmap, one way would be to check the bitmap again to see if the click was inside the window. But I have no idea if the OS does this.

    Regards - Alok Govil

  • Changing the shape of a child window control (in non-mfc)

    Posted by Legacy on 11/29/2000 08:00am

    Originally posted by: Brian Rosenthal

    I cannot figure out how to persistently change the clipping region of a child window control.

    I have a rich edit control (non-mfc... created with CreateWindowEx), and I want to change the visible area of the control dynamically. There seem to be three approaches, and I cannot seem to figure out how to get any of them to work:

    1. Call SetWindowRgn() on the child window control: this weirdly enough seems to make the specified region invisible, rather than visible, and is not persistent (the next InvalidateRect call re-draws the region.

    2. Call SelectClipRgn(): this isn't persistent after the display context is released. The following code for example completely ignores my call to SelectClipRgn():

    hdc = GetDC(hwnd);
    rgn1 = CreateRectRgn(0, 0, 50, 50);
    (void)SelectClipRgn(hdc, rgn1);
    (void)ReleaseDC(hwnd, hdc);
    hdc = GetDC(hwnd);
    (void)GetClipBox(hdc, &WinRect);
    rgn1 = CreateRectRgnIndirect((const RECT *)(&WinRect));
    hbr = (HBRUSH) GetStockObject(WHITE_BRUSH);
    (void)FillRgn(hdc, rgn1, hbr);

    3. Intercept the WM_PAINT message of the control, and change the clipping region before the control is painted. The problem here is that I don't know how to intercept the child window WM_PAINT message without completely re-writing the control, and that doesn't seem to be the right solution.

    Thank you,

    Brian Rosenthal
    Chief Technology Officer
    MyBestHealth, Inc.

  • Changing a Window's Shape

    Posted by Legacy on 05/26/2000 07:00am

    Originally posted by: Nilesh Karkhanis

    It works great!! and shows the window with the specified
    region but if the window is dragged then the phantom lines
    which are shown while dragging shows the default rectangular
    region rather then the windows visible region.
    Can u suggest some way to solve it..


    Nilesh Karkhanis

  • Works Well!! I put it in Initdialog()

    Posted by Legacy on 10/05/1999 07:00am

    Originally posted by: Paul Bright

    This works well and I put this in Initdialog() function and it changed the shape of that window.

  • RE : HOWTO: my Window's Shape from bitmap

    Posted by Legacy on 07/04/1999 07:00am

    Originally posted by: YongHoon Lee

    Visit to
    You can find BitmapToRegion() function.
    And visit to
    fixed bug.

  • What about cool stuff that WindowBlinds does?

    Posted by Legacy on 06/16/1999 07:00am

    Originally posted by: r. lake

    Window Regions are nice, but isn't it only useful if you extend it so that the window is completely customized?

  • Changing a Window's Shape

    Posted by Legacy on 05/02/1999 07:00am

    Originally posted by: Rik

    Don't work. Only caption displays. What am I doing wrong?

    #include <afxwin.h>

    // Declare the application class
    class CHelloApp : public CWinApp
    virtual BOOL InitInstance();

    // Create an instance of the application class
    CHelloApp HelloApp;

    // Declare the main window class
    class CHelloWindow : public CFrameWnd

    // The InitInstance function is called each
    // time the application first executes.
    BOOL CHelloApp::InitInstance()
    m_pMainWnd = new CHelloWindow();

    return TRUE;

    // The constructor for the window class
    // Create the window itself
    "The Big Push",
    CRect(0,0,600,400)); // x,y top-left - x,y bottom-right corner

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

  • Loading, Please Wait ...

  • You must have javascript enabled in order to post comments.

Leave a Comment
  • Your email address will not be published. All fields are required.

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date