?
Solved

How to draw polygon in vc++

Posted on 2000-03-12
9
Medium Priority
?
2,061 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Independent Software Vendors: 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 200 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

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!

Question has a verified solution.

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

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

770 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