Solved

How to draw polygon in vc++

Posted on 2000-03-12
9
1,936 Views
Last Modified: 2007-10-18
How I can draw a polygon in vc++ on my
  Dialog box? , please tell me with an example
0
Comment
Question by:sag_kas
9 Comments
 
LVL 22

Expert Comment

by:nietod
ID: 2610033
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.
0
 
LVL 3

Expert Comment

by:Redlord
ID: 2610082
I never tested this but it should work.

//this will draw a rectangle on the
//specified hwnd with the specified
//color
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;
PolyLine(dc,&pnts,4);
SelectObject(dc,oldpen);
DeleteObject(pen);


to use this code just replace hwnd with your dialogs hwnd and color with the color you want it drawn in.
0
 
LVL 22

Expert Comment

by:nietod
ID: 2610215
Does it work on the Mac?  Unix?....
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Expert Comment

by:Redlord
ID: 2610315
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];
        count++;
    }
    Line(buffer,pnts[numpoints].x,pnts[numpoints].y,pnts[0].x,pnts[0].y);
    return;
}

just a note tho, when you say VC i assume windows...
0
 
LVL 22

Expert Comment

by:nietod
ID: 2611869
>> 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.
0
 
LVL 3

Expert Comment

by:Redlord
ID: 2614258
That is a good question, why did you regect my answer?
0
 
LVL 10

Expert Comment

by:RONSLOW
ID: 2619934
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.
0
 
LVL 1

Accepted Solution

by:
Dhrubajyoti earned 50 total points
ID: 2627081
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];
      point[0].x=2;
      point[1].x=2;
      point[2].x=200;
      point[3].x=50;
      point[4].x=30;
      point[0].y=20;
      point[1].y=100;
      point[2].y=100;
      point[3].y=50;
      point[4].y=30;
      CClientDC dc(this);
      dc.Polygon(&point[0],5);
0
 
LVL 22

Expert Comment

by:nietod
ID: 2627614
Dhrubajyoti, I already proposed the Polygon(), function.  And also there is no indication that sag_kas is using MFC.  
0

Featured Post

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question