rfwoolf
asked on
When painting on a form's canvas: it paints on the title bar not the client area
When painting to a TForm's canvas, the point 0,0 is not the top left of the client area, instead it is the top left of the title bar. Also the form's border is a problem, because the client area only starts 4 pixels in on my PC.
So what should be 0,0 is actually 4, 31 on my PC.
Is there a way to work with the canvas and to only refer to the client area?
I guess you could do a Windows API call to get the title bar's height but this doesn't seem normal to me.
So what should be 0,0 is actually 4, 31 on my PC.
Is there a way to work with the canvas and to only refer to the client area?
I guess you could do a Windows API call to get the title bar's height but this doesn't seem normal to me.
The ClientRect would dictate the area inside your form. If you are using standard GDI on a form's canvas, you may have to convert to client coordinates from screen coordinates, too, using ScreenToClient.
ASKER
Hi Eddie
Sounds promising. I've tried extensively and could use some working code. All I need to do is draw a rectangle around some buttons on a form.
Here's my code below (I've only put the relevant parts) and can't get it to see 0,0 as the top-left of the client area, instead 0,0 is the top-left of the titlebar.
Cheers
Sounds promising. I've tried extensively and could use some working code. All I need to do is draw a rectangle around some buttons on a form.
Here's my code below (I've only put the relevant parts) and can't get it to see 0,0 as the top-left of the client area, instead 0,0 is the top-left of the titlebar.
Cheers
procedure TForm2.BorderMyControl(DaControl : TControl);
var
C : TCanvas;
MyHDC : HDC;
mypoint1, mypoint2, mypoint3 : tpoint;
begin
C := TCanvas.Create;
with C do
try
MyHDC := GetWindowDC(DaControl.Parent.Handle);
try
Handle := MyHDC;
Brush.Style := bsClear;
Pen.Width := 3;
mypoint1 := DaControl.clienttoscreen(point(DaControl.left, DaControl.top));
mypoint2 := form2.ClientToScreen(Point(0, 0));
mypoint3 := point(mypoint1.X - mypoint2.x, mypoint1.Y - mypoint2.Y);
Rectangle(Rect(mypoint3.x, mypoint3.y ,mypoint3.x + dacontrol.width + 2,mypoint3.Y + dacontrol.height + 2));
finally
ReleaseDC(DaControl.Parent.Handle, MyHDC);
end;
finally
Free;
end;
end;
sometimes, I just cant let go of a problem until it's solved... :D
The problem with your code is that you allocated your own Device Context for the Window, which then had coordinates that also included the Frame of that window. However, If you had used the TForm.Canvas property, then the painting would have been correct since the TForm adjusts the coordinates of the Canvas with respect of the Frame of the window.
However, since you don't actually know what type your parent Control is, you can't really assume that it is the main form of the application; so you also have to adjust the coordinates of the Canvas to fit the frame. And as I recalled, there were Api functions to do this. :D
The function AdjustWindowRectEx takes a requested ClientRect area, and then resizes it to include the frames of that WindowControl it belongs to. So if we use our Parent ClientRect as input, and get the window styles (which include frame data) using GetWindowLong, we can then get the new fram calculated for us. Since the resulting rect is expanded in every direction, and TopLeft coordinate of the ClientRect is (0,0), the resulting rectangle's TopLeft will be the delta between the Window's TopLeft and the ClientAreas top left. :D
Then by using MoveWindowOrg, we can translate the coordinates of the Canvas so that it compensates for this distance; and then we just Paint... :D
I have adjusted your code acordingly...
regards
Hypo
The problem with your code is that you allocated your own Device Context for the Window, which then had coordinates that also included the Frame of that window. However, If you had used the TForm.Canvas property, then the painting would have been correct since the TForm adjusts the coordinates of the Canvas with respect of the Frame of the window.
However, since you don't actually know what type your parent Control is, you can't really assume that it is the main form of the application; so you also have to adjust the coordinates of the Canvas to fit the frame. And as I recalled, there were Api functions to do this. :D
The function AdjustWindowRectEx takes a requested ClientRect area, and then resizes it to include the frames of that WindowControl it belongs to. So if we use our Parent ClientRect as input, and get the window styles (which include frame data) using GetWindowLong, we can then get the new fram calculated for us. Since the resulting rect is expanded in every direction, and TopLeft coordinate of the ClientRect is (0,0), the resulting rectangle's TopLeft will be the delta between the Window's TopLeft and the ClientAreas top left. :D
Then by using MoveWindowOrg, we can translate the coordinates of the Canvas so that it compensates for this distance; and then we just Paint... :D
I have adjusted your code acordingly...
regards
Hypo
procedure BorderMyControl(DaControl : TControl);
var
C : TControlCanvas;
aDC : HDC;
aRect : TRect;
aHandle : HWND;
bMenu : LongBool;
begin
C := TControlCanvas.Create;
C.Control := DaControl.Parent;
try
aHandle := DaControl.Parent.Handle;
aDC := GetWindowDC(aHandle);
aRect := DaControl.ClientRect;
bMenu := (DaControl.Parent is TForm) and Assigned(TForm(DaControl.Parent).Menu);
AdjustWindowRectEx(aRect,
GetWindowLong(aHandle, GWL_STYLE),
bMenu,
GetWindowLong(aHandle, GWL_EXSTYLE));
MoveWindowOrg(aDC, -aRect.Left, -aRect.Top);
try
C.Handle := aDC;
C.Brush.Style := bsClear;
C.Pen.Width := 3;
aRect := DaControl.BoundsRect;
InflateRect(aRect, 2, 2);
C.Rectangle(aRect);
finally
ReleaseDC(DaControl.Parent.Handle, aDC);
end;
finally
C.Free;
end;
end;
ASKER
Hi Hypo
That's some nice fancy footwork there using the Windows API. I have tested it and it works! (hellelujah!)
A moment or two later I managed to come up with a solution myself which I'll paste below for other people that might have this problem, here's the crux of the solution:
When painting on the form's canvas, 0,0 is the top-left of the window where the titlebar sits.
So if your Tbutton is sitting at 0,0 on the form and you try paint, it will actually be just above the button.
So how do you calculate the 'offset'?
1) You can get the screen co-ordinates of the topleft of the client area using ClientToScreen(0,0) <--- this is the top-left of the form UNDERNEATH the title bar
2) You can then also get the screen co-ordinates of the topleft of the form using Form.Left, and Form.Top.
You can then take 1 and 2 and subtract to get the offset.
Here's the code:
That's some nice fancy footwork there using the Windows API. I have tested it and it works! (hellelujah!)
A moment or two later I managed to come up with a solution myself which I'll paste below for other people that might have this problem, here's the crux of the solution:
When painting on the form's canvas, 0,0 is the top-left of the window where the titlebar sits.
So if your Tbutton is sitting at 0,0 on the form and you try paint, it will actually be just above the button.
So how do you calculate the 'offset'?
1) You can get the screen co-ordinates of the topleft of the client area using ClientToScreen(0,0) <--- this is the top-left of the form UNDERNEATH the title bar
2) You can then also get the screen co-ordinates of the topleft of the form using Form.Left, and Form.Top.
You can then take 1 and 2 and subtract to get the offset.
Here's the code:
procedure TForm2.BorderMyControl(DaControl : TControl);
var
C : TCanvas;
MyHDC : HDC;
mypoint1, mypoint2 : tpoint;
myclientrect : trect;
begin
C := TCanvas.Create;
with C do
try
MyHDC := GetWindowDC(self.handle);
try
Handle := MyHDC;
Brush.Style := bsClear;
Pen.Width := 3;
//From the screen's perspective, gives the co-ords of just underneath the titlebar
mypoint1 := self.ClientToScreen(Point(0, 0));
//From the form's perspective, gives the co-ords of the control within the form, where 0,0 would be just underneath the title bar
mypoint2 := point(DaControl.Left, DaControl.Top);
Rectangle(Rect(mypoint2.x + (mypoint1.x - self.left) - 2,
mypoint2.y + (mypoint1.y - self.top) - 2,
mypoint2.x + (mypoint1.x - self.left) + DaControl.width + 2,
mypoint2.y + (mypoint1.y - self.top) + DaControl.height + 2));
finally
ReleaseDC(self.handle, MyHDC);
end;
finally
Free;
end;
end;
ASKER
Hypo I plan to award the points to you. Let's keep the question open about another day or so to see if there's anything else we might be missing.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Fortunately I wasn't concerned about buttons being on the frame, but it is ultimately an important point.
Thanks Hypo
Thanks Hypo
ASKER
Great work. Thank you!
ASKER
Hypo is the man. Good work and thanks so much.