Solved

How to draw polygon in vc++

Posted on 2000-03-12
9
1,870 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

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…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
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 be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

815 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

12 Experts available now in Live!

Get 1:1 Help Now