Home » Extra libraries, Code snippets, applications etc. » U++ users applications in progress and useful code snippets, including reference examples! » Canvas widget created (Callbacks are great:-)
Canvas widget created (Callbacks are great:-) [message #4762] |
Mon, 21 August 2006 21:38 |
ren42
Messages: 12 Registered: July 2006
|
Promising Member |
|
|
Hi everybody,
while I'm coding my private app, I missed an useful way to paint
in a rectangular area. So I decided to develop a canvas class
derived from Ctrl to get what i want. I worked since 7 weeks with
UPP and I think it's time to give something back, because UPP is
the best c++IDE I've ever seen. And I try a lot of IDE's
The Attachment include a complete Example. Simply copy it to your
MyApps Nest, create a new Package "uppCanvas" and hit Ctrl-F5.
And now some more Infos:
Canvas provides an easy way to draw on rectangular
area embedded in a parentcontrol of your choice e.g. a StaticRect(so far I tested). But how is it possible to receive mouseclicks or to
paint on Canvas without altering or override the virtual methods in a child class of Canvas? The solution are Callbacks.
Consider a callback as a pointer to a method of Canvas called from another place (the parent window).
In order to draw on the Canvas the parent window must provide and assign methods with matching signaturs to the Callbacks of
Canvas. e.g.:
//Declaration
struct App : public TopWindow{
typedef App CLASSNAME; //Needed by THISBACK macro
Canvas canvas; //Prepair your brushes
App();
//Same signatur as canvas.paint(Draw& w)
void OnCanvasPaint(Draw& w); //Want to paint on canvas
//Paint on Click see below
void OnCanvasMouseLeft(Point p, dword keyflags);
};
//Definition
App::App(){
.
.
// The magic assignment:-)
canvas.OnCanvasPaint=THISBACK(OnCanvasPaint);
// The left mousebutton click is now routed to the canvas
canvas.OnMouseLeft=THISBACK(OnCanvasMouseLeft);
}
void App::OnCanvasPaint(Draw& w)
{// Your paintings on Canvas goes here:
Rect r=canvas.GetSize();
// Draw a Line
w.DrawLine(0,0,r.Width(),r.height(),1,Black());
}
/*
And not still enough, Canvas offers a way to paint not only
in a Paint Callback. For this purpose you can use the DrawSheet()
function. To make it short here is an Example:
*/
void App::OnCanvasMouseLeft(Point p, dword keyflags)
{ //Paint an ugly ellipse:-)
Rect r=canvas.GetSize();
DrawingDraw& ds=canvas.DrawSheet();
ds.DrawEllipse(p.x,p.y,20,20,Red(),5,Green());
canvas.EndSheet();//draw it now
}
Consider the Drawsheet as an additional paintlayer you can paint at.
Look at main.cpp to see it all together:-)
Happy coding...
|
|
|
|
|
|
Goto Forum:
Current Time: Sat Jul 27 17:40:31 CEST 2024
Total time taken to generate the page: 0.03025 seconds
|