Solved

Remove Rectangles after painting them on "DeskTopWindow" or screen

Posted on 2009-05-04
4
199 Views
Last Modified: 2012-05-06
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

0
Comment
Question by:rfwoolf
  • 2
4 Comments
 
LVL 37

Assisted Solution

by:Geert Gruwez
Geert Gruwez earned 25 total points
ID: 24302596
it would be solved with a bevel too
0
 
LVL 12

Accepted Solution

by:
Hypo earned 475 total points
ID: 24302830
Isn't your problem that you paint it on the Desktop DC, and not on your controls Parent DC? I belive that's what causes your problem. When I ran your exapmle, the border was not painted at the correct locations, but anyway, I got the idea of what you were talking about. After I changed the DC to be the parent of the Control instead, then I could call Repaint on the parent, and the border was removed.

    DeskTopWindowDC := GetWindowDC(DaControl.Parent.Handle);

regards
Hypo
0
 
LVL 13

Author Comment

by:rfwoolf
ID: 24307171
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.
0
 
LVL 13

Author Comment

by:rfwoolf
ID: 24307189
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

0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question