• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 639
  • Last Modified:

Click-through forms

Delphi 6 now provides the ability to create an alpha-blended (i.e. translucent) form. What I would like to do is create a form like this, with FormStyle = fsStayOnTop, as a sort of 'watermark' that always shows as a light overlay in front of whatever's on the screen. The problem, of course, is that with my form in front of whatever else is on the system, mouse clicks go to my form, not to things behind my form. So my question is, how can a Delphi form be made 'transparent' to mouse clicks - i.e. make sure all mouse clicks are routed to the window in the second-topmost position at the pixel that was clicked. Bonus points if you write the code for me. :-)
0
ghjm
Asked:
ghjm
  • 2
1 Solution
 
gemartiCommented:
This works, but may not be what your looking for. I'm just guessing since D6 is new to me:

Create a new application.
Place a Button and a edit control on the first form (Form1)
procedure TForm1.Button1Click(Sender: TObject);
begin
  Form2.Show;
end;

Create a second form (form2)

Set AlphaBlend := true;
AlphaBlendValue := 100 (or any value from 0..255 where 255 is the least transparent)
Set TransParentColor := true;
Set TransParentColorValue := clBtnFace (or whatever color works for you)

Add these lines of code:
procedure TForm2.FormActivate(Sender: TObject);
begin
  Form1.Button1.Parent := Form2;
  Form1.Edit1.Parent := Form2;
  Form2.AlphaBlendValue := 200; //if you leave at 100 you can't see the components very well.
  Form1.Button1.GetParentComponent;
end;

procedure TForm2.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  Form1.Button1.Parent := Form1;
  Form1.Edit1.Parent := Form1;
  Form2.AlphaBlendValue := 100;
end;
0
 
ITugayCommented:
Hi ghjm,

may be this code would be usefull for you.

procedure TForm1.MouseRedirect(Sender: TObject; Action: Integer; X,Y: Integer);
var
  C: TPoint;
  H: HRgn;
  L: HRgn;
begin
  C.X := X;
  C.Y := Y;
  C := TControl(Sender).ClientToScreen(C);

  // create empty region and assign it to the form to make form
  // transparent for mouse clicks
  H := CreateRectRgn(0, 0, 0, 0);
  GetWindowRgn(Handle, L);
  SetWindowRgn(Handle, H, False);
  DeleteObject(L);

  mouse_event(Action, X, Y, 0 ,0);

  // restore form region
  GetWindowRgn(Handle, L);
  SetWindowRgn(Handle, 0, False);
  DeleteObject(L);

end;

procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  if Button = mbLeft then
    MouseRedirect(Self, MOUSEEVENTF_LEFTDOWN, X, Y)
  else
    MouseRedirect(Self, MOUSEEVENTF_RIGHTDOWN, X, Y);
end;

procedure TForm1.FormMouseUp(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  if Button = mbLeft then
    MouseRedirect(Self, MOUSEEVENTF_LEFTUP, X, Y)
  else
    MouseRedirect(Self, MOUSEEVENTF_RIGHTUP, X, Y);
end;


Set OnMouseDown and OnMouseUp for rest of control that should be transparent for mouse clicks to the same handles (TForm1.OnMouseDown, OnMouseUp).

-----
Igor
0
 
ghjmAuthor Commented:
Well, I understand what you're trying to do, but I haven't been able to make it work. I'm still trying, but...I sure wish a Win32 API call existed to set the _mouse_ region without also setting the _display_ region.
0
 
ghjmAuthor Commented:
Okay, well I have it working, sort of. The remaining problem is that whenever a click event goes through, the transparent form disappears and reappears - even though bRedraw is set to FALSE. Any suggestions how to avoid this?
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now