Solved

How to draw polygon in vc++

Posted on 2000-03-12
9
1,845 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
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 goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

920 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now