Change the Shape of the Window

Environment: VC++ 5-6, Windows 95/98/2k

This program converts the shape of the window. Normally, your window displays in a rectangular shape. But, if want to change the shape of your window, here's how to do it.

At a glance, it looks quite tough, but in reality it's very simple. The code that is used to change the shape of the window is not very lengthy; that's very small. Only a single MFC class, "CRgn", and an API method, ::SetWindowRgn(), are used to accomplish the whole work. This program converts the current window into four different shapes:

  • Ellipse or Circle
  • Triangle
  • Pentagon
  • Rounded Corner

To do this, I used the member functions of the CRgn class. For example, CreateEllipticRgn() is used for an Ellipse or Circle. If you want to create the circle shape, you have to change only the parameter's value. For exammple, CreateEllipticRgn(200, 100, 350, 250) creates a circular shape.

nit.CreateEllipticRgn(10,0, 630, 400);    //Create Ellipse...

After that, in this example I used CRgn::CreatePolygonRgn() for the Triangle and Pentagon shapes. In reality, it's a major function, because with the help of CreatePloygonRgn() you can create any shape. Similarly, CRgn::CreateRoundRectRgn() is used to make rounded corners.

//VertexPoint is an CPoint Array that stores 5 vertexes
nit.CreatePolygonRgn(VertexPoint, 5, ALTERNATE);

Then, I used the ::SetWindowRgn() (API Method, no need to use CWnd::SetWindowRgn or CWindow::SetWindowRgn) method to set the window region of the window.


In the case of a Polygon, you have to create an array of CPoint because that array stores the coordinate of vertexes of the Polygon (Pentagon or Triangle).

Note 1: For a better view, make sure your windows display in a maximized format. To display in maximized, use the following code:

void CMainFrame::ActivateFrame(int nCmdShow)
  nCmdShow = SW_MAXIMIZE;
Note 2: In the case of a Polygon and Triangle, maybe you could not see the menu bar, so don't worry. You can use the hidden menus in the same manner; for example, Alt+F, Alt+E, Alt+V, and Alt+C.

The sample code is give below.

void CShapeView::OnMakeElliptical()
  CRgn  nit;
  HWND  hwnd;

  hwnd = ::GetActiveWindow();
  nit.CreateEllipticRgn(10,0, 630, 400);
  ::SetWindowRgn(hwnd, nit, TRUE);
void CShapeView::OnMakeTriangle() 
  //For Triangle.
  CRgn  nit;
  HWND  hwnd;
  hwnd = ::GetActiveWindow();
  CRgn   rgnA, rgnB;
  CPoint VertexPoint[3];

//  To Create The Triangle Window.
  VertexPoint[0].x = 0;   VertexPoint[0].y = 0;
  VertexPoint[1].x = 850; VertexPoint[1].y = 600;
  VertexPoint[2].x = 850; VertexPoint[2].y = 0;

  nit.CreatePolygonRgn(VertexPoint, 3, ALTERNATE);
void CShapeView::OnMakePolygon()
  // For Polygon
  CRgn  nit;
  HWND  hwnd;
  hwnd = ::GetActiveWindow();
  CRgn   rgnA, rgnB;
  CPoint VertexPoint[5];

//Create a Pentagon.
  VertexPoint[0].x = 290; VertexPoint[0].y = 0;
  VertexPoint[1].x = 0;   VertexPoint[1].y = 200;
  VertexPoint[2].x = 200; VertexPoint[2].y = 500;
  VertexPoint[3].x = 400; VertexPoint[3].y = 500;
  VertexPoint[4].x = 650; VertexPoint[4].y = 200;

  nit.CreatePolygonRgn(VertexPoint, 5, ALTERNATE);
void CShapeView::OnMakeRoundedCorner()
  //Make Rounded Corner....
  CRgn  nit;
  HWND  hwnd;
  hwnd = ::GetActiveWindow();

  int x1, y1, x2, y2;
  CRect crect;
  CPoint cpointtop, cpointbottom;

  ::GetWindowRect(hwnd, &crect);
  //Get The Coordinates of the Window.
  cpointtop    = crect.TopLeft();
  cpointbottom = crect.BottomRight();

  //Break the Coordinates into four different variables.
  x1 = cpointtop.x;
  y1 = cpointtop.y;
  x2 = cpointbottom.x;
  y2 = cpointbottom.y;

  nit.CreateRoundRectRgn(x1, y1, x2, y2, 30, 30 );
  ::SetWindowRgn(hwnd, nit,TRUE);

So, guys, if you have any problem regarding this code, feel free to contact me.


Download demo project - 10 Kb
Download source - 19 Kb


  • Change the shape of window(demo)

    Posted by john_tm on 05/10/2006 12:50am

    It is ok,but there is no use with that window.It is not display the menu and buttons completly.Also we can't do anything if it is Round Corner mode(select menu after minimising the window). This is for demo version..... Best of Luck.Develope MORE.........

  • Error in CRgn use

    Posted by danielibarnes on 04/28/2005 12:45pm

    The documentation for SetWindowRgn() states: After a successful call to SetWindowRgn, the system owns the region specified by the region handle hRgn. The system does not make a copy of the region. Thus, you should not make any further function calls with this region handle. In particular, do not delete this region handle. The system deletes the region handle when it no longer needed. This means you if you use the CRgn object you must detach it after SetWindowRegion() succeeds.

  • Calculation of rounded corners is incorrect.

    Posted by Legacy on 11/13/2003 12:00am

    Originally posted by: Gediminas Siutilas

    The calculation of rounded corners is incorrect.
    When you changing the style of window to the rounded rectangle your calculation must be done like that:

    the top-x & top-y allways must be zero, because the coordinates of a window's window region are relative to the upper-left corner of the window, not the client area of the window.

    the line
    nit.CreateRoundRectRgn( x1, y1, x2, y2, 30, 30 );

    must be changed to:
    nit.CreateRoundRectRgn( 0, 0, x2 - x1, y2 - y1, 30, 30 );

    p.s. there's a problem with CRichEditCntrItem. On closing the application ( from studio ) I reseiveing the access violation message.

    yours faithfully Gediminas Siutilas.

  • The menubar is missing?

    Posted by Legacy on 11/11/2003 12:00am

    Originally posted by: pani

    When I ran the program and changed the shape to rounded rectangle, the menubar was not visible. For other shapes it is partly visible. Whether it is a bug or cannot be programmed to eradicate the problems?

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

Top White Papers and Webcasts

  • 10 Rules that Make or Break Enterprise App Development Projects In today's app-driven world, application development is a top priority. Even so, 68% of enterprise application delivery projects fail. Designing and building applications that pay for themselves and adapt to future needs is incredibly difficult. Executing one successful project is lucky, but making it a repeatable process and strategic advantage? That's where the money is. With help from our most experienced project leads and software engineers, …

  • As everyone scrambles to protect customers and consumers from the Heartbleed virus, there will be a variety of mitigating solutions offered up to address this pesky bug. There are a variety of points within the data path where solutions could be put into place to mitigate this (and similar) vulnerabilities and customers must choose the most strategic point in the network at which to deploy their selected mitigation. Read this white paper to learn the ins and outs of mitigating the risk of Heartbleed and the …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds