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.

::SetWindowRgn(hwnd,nit,TRUE);

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;
  CFrameWnd::ActivateFrame(nCmdShow);
}
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);
  ::SetWindowRgn(hwnd,nit,TRUE);
}
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);
  ::SetWindowRgn(hwnd,nit,TRUE);
}
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.

Downloads

Download demo project - 10 Kb
Download source - 19 Kb


Comments

  • 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.........

    Reply
  • 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.

    Reply
  • 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.

    Reply
  • 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?

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

Top White Papers and Webcasts

  • For the first time in modern history, workplace demographics now span four generations. Understanding the unique social influences that have shaped the behaviors of each is instrumental in creating productive work teams. And when managed effectively, their strengths, perspectives, and experiences can bring synergy in the workplace. Download this white paper for best practices on how to better understand and effectively manage a multi-generational workforce.

  • Thanks to wide spread cloud hosting and innovations small businesses can meet and exceed the legacy systems of goliath corporations. Explore the freedom to work how you want, with a phone system that will adapt to your evolving needs and actually save you lots of expense—read Get an Enterprise Phone System without High Cost and Complexity. The article clearly illustrates: The only hardware you'll need is phone equipment for advanced voice and fax. How to join all your employees, mobile devices, …

Most Popular Programming Stories

More for Developers

RSS Feeds

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