BCB Mailing List
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[BCB] PolyLine Help
Hi all,
Nick Gianakas wrote:
>
> While on the subject of TCanvas, how can I draw on other components?
Not sure why Borland didn't put methods in to draw on all VCL
components. AFAIK, in Windows, anything with a handle (HWND) can be
drawn upon (using WinAPI) by obtaining the device context for the
item.
Here's how you'd draw lines on a TPanel... place a BitBtn and a
panel on a form and drop this code in to the OnClick event handler.
This will draw random colored lines of random lengths from 0,0 of
the panel to some other point.
void __fastcall TForm1::BitBtn1Click(TObject *Sender)
{
HDC hdc;
HPEN pen;
int xres, yres;
hdc = GetDC(Panel1->Handle);
xres = GetDeviceCaps(hdc, HORZRES);
yres = GetDeviceCaps(hdc, VERTRES);
pen = CreatePen(PS_SOLID, 1, RGB(rand() % 255, rand() % 255, rand() %255));
SelectObject(hdc, pen);
MoveToEx(hdc, 0, 0, NULL);
LineTo(hdc, rand() % xres, rand() % yres);
DeleteObject(pen);
ReleaseDC(Panel1->Handle, hdc);
}
Keep in mind this doesn't do any redrawing of lines, you'll have
to keep track of the lines made and repaint when necessary (like
when someone resizes the form) in the OnPaint event of the form
or panel.
...and...
robert_hunt@nospam.nass.usda.gov wrote:
>
> -->I am having trouble using the PolyLine function of TCanvas in BCB 1.0. There is
> -->a reference in the online help, but it is incomplete. I need to draw a line
> -->with a subset of points from an array of points. The documentation refers to
> -->SLICE, but does not give any examples that I can find. Does anyone have a quick
> -->example or explanation that they could share with me?
void __fastcall TForm1::BitBtn2Click(TObject *Sender)
{
TPoint tp[5];
tp[0].x = 10; //define a rectangle
tp[0].y = 180;
tp[1].x = 400;
tp[1].y = 180;
tp[2].x = 400;
tp[2].y = 360;
tp[3].x = 10;
tp[3].y = 360;
tp[4].x = 10;
tp[4].y = 180;
Form1->Canvas->MoveTo(tp[0].x, tp[0].y); //get to upper left corner
Form1->Canvas->Polyline(tp, 4); //this draw entire rectangle
//these examples draw parts of the rectangle, starting at different
//points of the rectangle:
Form1->Canvas->Polyline(SLICE(&tp[0],3));
Form1->Canvas->Polyline(SLICE(&tp[1],3));
Form1->Canvas->Polyline(SLICE(&tp[2],2));
}
--
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
Robert Manning, Sr. Programmer/Analyst, Engineer
TRW Systems and Information Technology Group
mailto://Robert.Manning@nospam.trw.com http://www.trw.com
All opinions expressed are my own, unless otherwise indicated.
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
W Komornicki's Home Page |
Main Index |
Thread Index