chpl
Joined: 30 Nov 2007 Posts: 2
|
Posted: Sun Dec 02, 2007 5:14 pm Post subject: Resizeable Form |
|
|
Hi
Its me again.
I am just playing with the api. Now I try to make an little Window (Form) I want it to draw a grid and plots some math-functions like sinus etc. So i tried it do it like it is the "normal" way. I derived from Form an tried to override the "onPaint" methode and do all "paintings" there. It seems to work fine but when i resize the window it doesent work in the correct way. It doenst resize the drawing.
First i noticed that the onpaint event is comes before the resize event. Is this ok ? Schouldn't the resize event be thrown before the paint event ? I am not sure at this moment but isn't this the normal way ? Because in this way in some cases the area is drawn twice ... isn't it ?
The next thing i saw is that clientSize calls the onpaint methode when i make the window bigger. But when i make the window smaller it doesnt call it. It makes sense ... because the window has to draw the new area when the window gets bigger. And has just to hide the area which disappear while making the window smaller. I think this is the reason.
(But usually(in other frameworks) the paint event comes every time i think.)
Ok lets go back!. I want to redraw it always because i want to rearrange my drawing. So i override onResize too and i just call invalidate() there. This invalidates the entire window area and the area will be drawn in the way i want.
First i thought update() is what i need but it seems not to work ??? I dont know what is the differnt between Update and invalidate?? In Source ("Control.d") it just calles UpdateWindow(hwnd);
But invalidate works fine
So here is my little Window. I dont know if its the best way cause i am realy new in D. Maybe its better to derive from Control for an extra Plotarea. So you can add other controls Textbox, Buttens ... in the Form. I will try this in a few days.
Code: |
/*
* chpl@chello.at
*/
import dfl.all;
import std.stdio;
int main()
{
MyForm myForm = new MyForm();
Application.run(myForm);
return 0;
}
class MyForm: dfl.form.Form
{
Pen p1 = null;
this()
{
initializeMyForm();
}
private void initializeMyForm()
{
text = "My Form";
p1 = new Pen(Color(0, 0, 0xF0));
clientSize = dfl.all.Size(292, 266);
}
protected override void onResize(EventArgs ea)
{
writefln("onResize");
super.onResize(ea);
invalidate();
//update();
}
protected override void onPaint(PaintEventArgs ea)
{
writefln("onPaint");
super.onPaint(ea);
DrawGrid(ea.graphics, p1);
}
private void DrawGrid(Graphics graphics,Pen p1)
{
writefln("DrawGrid");
int width = clientSize.width;
int height = clientSize.height;
static int border = 5;
graphics.drawRectangle(p1,border,border,width-2*border,height-2*border);
// Axis
graphics.drawLine(p1,0,height/2,width,height/2);
graphics.drawLine(p1,width/2,0,width/2,height);
}
}
|
Cu chpl |
|