Solved

How to draw polygon in vc++

Posted on 2000-03-12
9
1,904 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 clear a vector as well as how to detect empty vectors in C++.

856 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