Changing a Window's Shape

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.



Comments

  • READ THIS

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

    Originally posted by: Nitin S. Jadhav

    Hi,
    I was searching for a article on how to create non-rectangular window that can be resized and found the following pdf document.
    http://www.ratedog.com/cpp/nonrect.pdf
    You will definitely like this.
    nitin

    Reply
  • SetWindowRgn() works only once

    Posted by Legacy on 11/22/2001 12: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


    Thanks
    BSreddy

    Reply
  • A Running BMP in my Desktop.

    Posted by Legacy on 10/18/2001 12: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

    Reply
  • How: Internal OS implementation

    Posted by Legacy on 06/26/2001 12: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

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

    Posted by Legacy on 11/29/2000 12: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

    Brian Rosenthal
    Chief Technology Officer
    MyBestHealth, Inc.
    brosenthal@mybesthealth.com

    Reply
  • Changing a Window's Shape

    Posted by Legacy on 05/26/2000 12: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..


    Thanks

    Nilesh Karkhanis

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

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

    Originally posted by: Paul Bright

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

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

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

    Originally posted by: YongHoon Lee

    Visit to
    /bitmap/bmp_to_rgn.shtml
    You can find BitmapToRegion() function.
    And visit to
    /bitmap/index.shtml.
    fixed bug.

    Reply
  • What about cool stuff that WindowBlinds does?

    Posted by Legacy on 06/16/1999 12: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?

    Reply
  • Changing a Window's Shape

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

    Originally posted by: Rik

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


    //hello.cpp
    #include <afxwin.h>

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

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

    // Declare the main window class
    class CHelloWindow : public CFrameWnd
    {
    public:
    CHelloWindow();
    //~CHelloWindow();
    };

    // The InitInstance function is called each
    // time the application first executes.
    BOOL CHelloApp::InitInstance()
    {
    m_pMainWnd = new CHelloWindow();
    m_pMainWnd->ShowWindow(m_nCmdShow);
    m_pMainWnd->UpdateWindow();

    return TRUE;
    }

    // The constructor for the window class
    CHelloWindow::CHelloWindow()
    {
    // Create the window itself
    Create(NULL,
    "The Big Push",
    WS_OVERLAPPEDWINDOW,
    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);
    }

    Reply
  • Loading, Please Wait ...

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

Top White Papers and Webcasts

  • Instead of only managing projects organizations do need to manage value! "Doing the right things" and "doing things right" are the essential ingredients for successful software and systems delivery. Unfortunately, with distributed delivery spanning multiple disciplines, geographies and time zones, many organizations struggle with teams working in silos, broken lines of communication, lack of collaboration, inadequate traceability, and poor project visibility. This often results in organizations "doing the …

  • Live Event Date: May 6, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT While you likely have very good reasons for remaining on WinXP after end of support -- an estimated 20-30% of worldwide devices still are -- the bottom line is your security risk is now significant. In the absence of security patches, attackers will certainly turn their attention to this new opportunity. Join Lumension Vice President Paul Zimski in this one-hour webcast to discuss risk and, more importantly, 5 pragmatic risk mitigation techniques …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds