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

More by Author

Previous article
Next article

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read