Wednesday, January 5, 2011

Click Through Form

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;


               


               



               


               



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).




Click-through Delphi form

ClocX did made me very curious about its click-through feature. The click-through effect looks very slick to me. That’s exactly what I had really wanted to be implemented on my BeeClock program years ago, but I had never successfully made it. Then I gave up and forgot about it. But, tonight ClocX raised my failed experience memory up again.

As usual, I started my search for answers adventure from Google. After playing around with appropriate search phrases, finally google took me to this page. The page shows me a clear solution, though the solution is originally written using VB.Net language. It’s even very easy actually as it just requires two simple API calls. I never thought it’s gonna be that easy!

I wrote BeeClock using Delphi 5 on Windows 98. I wrote it on early 1999, when Windows 2000 had not been released by that time. And I never reopened its code on newer Windows releases. So, I never knew that click-through effect was already provided since Windows 2000. Obviously this effect is not supported by Windows 98. Unless, you got plenty of time to waste to implement this not too important cosmetic effect without windows’ API.

Well, now let’s talk about the real code. I’m now using Turbo Delphi Explorer. But Delphi 7 should be able to compile the code as well. Here’s step by step demo how to create click-through effect using Delphi:

   1. Create a new VCL form project. I assume the main form name is Form1.
   2. Write the code below at OnCreate event of Form1:

      procedure TForm1.FormCreate(Sender: TObject);
      begin
        SetWindowLong(Self.Handle, GWL_EXSTYLE, WS_EX_TRANSPARENT or WS_EX_LAYERED);
        SetLayeredWindowAttributes(Self.Handle, 0, 255, LWA_ALPHA);
      end;

   3. Put a TTimer onto the main form. I assume the TTimer instance name is Timer1.
   4. Go to Object Inspector and set the Interval property value of Timer1 to 1.
   5. Write the code below at OnTimer event of Timer1:

      procedure TForm1.Timer1Timer(Sender: TObject);
      begin
        if (Mouse.CursorPos.X >= Left) and (Mouse.CursorPos.X <= Width+Left+1) and
           (Mouse.CursorPos.Y >= Top) and (Mouse.CursorPos.Y <= Height+Top+1) then
          SetLayeredWindowAttributes(Self.Handle, 0, 200, LWA_ALPHA)
        else
          SetLayeredWindowAttributes(Self.Handle, 0, 255, LWA_ALPHA);
      end;

   6. Run the program. You may hit the F9 key or click the Run icon.

Now, move your mouse pointer in and out of main form area to see the dynamic transparent alpha blend effect. And while your mouse is over the main form, try to click on the form. You should see the click-through effect in action. Pretty nice, eh?  To close the application, you may use Alt+F4 combination keys (while the application is in focus) or right-click the application’s taskbar icon and click the Close item (if the application has lost its focus).

I suggest you to play around with the timer component. You should know the reason why I put the code in step 5 at OnTimer event of Timer1, instead of using OnMouseEnter and OnMouseLeave events of Form1. Also try to place the main form at various positions on the screen or over various objects of other window. You should see that the form visually shown but virtually invisible to the mouse.

0 comments:

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More

 
Design by Kang Iwan K-sev | Thank's for your visit To My Site - Ridwan Mulyana | Cibeureum