• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2474
  • Last Modified:

How to draw polygon in vc++

How I can draw a polygon in vc++ on my
  Dialog box? , please tell me with an example
1 Solution
Standard C++ has no features for drawing polygons--or for any graphics operations.

So you will have use compiler or OS-specific techniques to do this.  So we have to know what compiler/OS you are using.

For windows, you can use the Polygon() function.
I never tested this but it should work.

//this will draw a rectangle on the
//specified hwnd with the specified
HPEN pen = CreatePen(PS_SOLID,1,color);
HDC dc = GetDC(hwnd);
HPEN oldpen = (HPEN)SelectObject(dc,pen);
POINT pnts[4];
pnts[0].x = 2;
pnts[0].y = 2;
pnts[1].x = 100;
pnts[1].y = 2;
pnts[2].x = 100;
pnts[2].y = 100;
pnts[3].x = 2;
pnts[3].y = 100;

to use this code just replace hwnd with your dialogs hwnd and color with the color you want it drawn in.
Does it work on the Mac?  Unix?....
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

sorry, this will only work on windows

but you could easily port it to unix and mac, if you had a line drawing function like:
"Line(drawbuffer,int x1,int y1,int x2,int y2)"

with that function you could make the PolyLine function easily:

void PolyLine(buffer,POINT *pnts,int numpoints)
    int count = 1;
    while (count <= numpoints)
        Line(buffer,pnts[count - 1].x,pnts[count - 1].y,pnts[count].x,pnts[count.y];

just a note tho, when you say VC i assume windows...
>> ust a note tho, when you say VC i assume windows...
He does mean windows.  I missed it!

Then your answer was fine...sorry.  (Although, I do think the Polygon() function I suggested is better for this.)

sag_kas, why have you rejected Redlord's answer?  It was for windows and you are using windows.  You should not reject an answer without explaining why.
That is a good question, why did you regect my answer?
If you're using MFC, then override the OnPaint (WM_PAINT handler).  Call

  CPaintDC dc( this ); // Device context for painting

to get a dc to draw on, set up the pen color (by selecting a pen into the dc) and draw using dc.Polygon(..) function

An alternative is to do the drawing in OnEraseBkgnd()

yes another is to write your own override of , say, CStatic so that it draws your polygon.  This is nicer, because you can reposition the polygon whereever you want.
Write the code in WM_PAINT event of dialog box or in any button click event.
This drawing polygon in dialog box


        CPoint point[5];
      CClientDC dc(this);
Dhrubajyoti, I already proposed the Polygon(), function.  And also there is no indication that sag_kas is using MFC.  
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

7 new features that'll make your work life better

It’s our mission to create a product that solves the huge challenges you face at work every day. In case you missed it, here are 7 delightful things we've added recently to monday to make it even more awesome.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now