Link to home
Start Free TrialLog in
Avatar of rfwoolf
rfwoolfFlag for South Africa

asked on

Remove Rectangles after painting them on "DeskTopWindow" or screen

I am drawing a rectangle around buttons in my application by using this funny method below.
The problem is that after I draw these rectangles I cannot clear them (unless I 'drag' my form off the screen and back on again).

I have tried calling canvas.repaint / canvas.refresh but it doesn't work.
procedure BorderMyControl(DaControl : TControl);
var
  C : TCanvas;
  DeskTopWindowDC : HDC;
begin
  C := TCanvas.Create;
  with C do
  try
    DeskTopWindowDC := GetWindowDC(GetDeskTopWindow);
      try
        Handle := DeskTopWindowDC;
        Brush.Style := bsClear;
        Pen.Width := 3;
        Rectangle(Rect(DaControl.Left - 2, DaControl.Top - 2, DaControl.Left + DaControl.Width + 2, DaControl.Top + DaControl.Height + 2));
      finally
        ReleaseDC(GetDesktopWindow, DeskTopWindowDC);
    end;
  finally
    Free;
  end;
end;

Open in new window

SOLUTION
Avatar of Geert G
Geert G
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rfwoolf

ASKER

I'm satisfied at the answer but I'm sure I did try and refresh the desktop and failed. One of the problems I'm sure I'm running into is that the form's canvas includes the header, so if I draw a rectangle starting at 0,0 on the form it will appear on the title bar. Anyway, let's see if we can address this in a separate question.
Avatar of rfwoolf

ASKER

Sorry I wasn't clear above, what I'm trying to say Hypo is this:
Your solution is a great workaround for my particular problem, but,
what if I did want to draw on the desktop, and what if I did have trouble removing the squares on the desktop window handle? Your answer doesn't address this.
Anyways, I would prefer to draw on the form instead of the desktop so let's just move on :p