Solved

Create Buttons, Labels, Forms and other objects in ANOTHER Application

Posted on 2004-08-26
4
301 Views
Last Modified: 2010-04-05
I would like to do somethinig like this :

  h := findwindow(nil, 'Calculator');

  b := tbutton.Create(nil);
  b.ParentWindow := h;
  b.left := 200;
  b.Top := 75;
  b.caption := 'test';
  b.Width := 75;
  b.height := 25;
  b.OnClick := clt; // <-- This procedure doesn't work, it is supposed to do showmessage('blah');
  b.Show;


This doesn't work of course, because button isn't visible, only when i click it it becomes visible but it disapears after i move the mouse over other buttons. I would like to create or should i say inject some control, in this example TButton and put it into another application like Calculator. What i want to do is add an Extra button to some other application like an ADD ON. Please help. I can't get this to work correctly, it keeps dissapearing so please give me some code, i am sure somebody has already done similar thing.....like making holes in other windows :)) ...Thanks in advance..
0
Comment
Question by:Franus
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 17

Expert Comment

by:Wim ten Brink
ID: 11910845
In general, this is considered a very bad practice. Besides, you don't have any control over your buttons in the other process. It won't be easy either since you'll have to inject a DLL (that will manage those controls for you) into the address space of the other process, and this DLL must also capture any messages that are sent through the application and capture those that apply to it.
Furthermore, you will need to know very specific information about the windows that you want to modify. You need to know the (Windows) classname to find the correct window that you want to modify. Since these are Windows classes and not Delphi classes (even for Delphi applications!) you would have to dig deep into the Windows API to create the controls that you like to add. Just adding a TButton or TLabel isn't as easy as it seems...

I don't know if anyone here has a good solution here for you but it's definitely not an easy thing...
0
 

Author Comment

by:Franus
ID: 11911326
Thank you for your kind answer. I know this is not a piece of cake as i woudl say :) but to be exact i must create an ADDON for existing application. Imagine for example Calculator window and for example i would like to remove the BACKSPACE Button and put my Button there. So far i can do it but as you said, i cannot attach event handler to my Button OnClick event since it is parented by Calculator and not my Application. Btw Winsight32 is a gift from god application for this. If anyone can just give me a solution for this problem please. You can use the Calculator for example. Here is the task i would like. Create my Button on the calculator window and execute some code when i press it. My application would be invisible, just an icon in system tray and when user clicks on that Button, my application Form would then become visible. I think you understand what i would like to do. I don't need to go deeper into this, this is enough. Just one button.


Thanks
0
 
LVL 34

Accepted Solution

by:
Slick812 earned 125 total points
ID: 11926019
hello Franus, Here is some code for two button clicks, the first one will find the Calculator window and get it's handle, then create a button in that window, which is Sub-Classed to get the left button Up message for a click, , ,
I do not think it is a good idea to create a window from one hInstance to another hInstance, I had problems in XP with painting this button, since XP uses a new painting techique with themes, As far as the other controls over-drawing this button, I think it is because they are NOT created with the  WS_CLIPSIBLINGS,  so they will draw on other controls that they overlap and is independent of the button that you create or it's properties.



  private
    { Private declarations }
    hButton1: Cardinal;
    PButProc: Pointer;




procedure TForm1.FormCreate(Sender: TObject);
begin
hButton1 := 0; // initialize hButton
end;


function ButFunc(hWnd, iMsg, WParam, LParam: Integer): Integer; stdcall;
begin
// new Window Proc for hButton1
Result := CallWindowProc(Form1.PButProc,hWnd,iMsg,wParam,lParam);
if iMsg = WM_LBUTTONUP then
  begin
  Form1.Label2.Caption := 'hButton1 LBUTTONUP '+IntToStr(LParam);
  // other button click event code here
  end;
end;

procedure TForm1.sbut_CalButtonClick(Sender: TObject);
var
hCalWnd: Cardinal;
begin
// Button Click to find calculator and make button

if hButton1 <> 0 then Exit; // do not make 2 buttons with same handle
hCalWnd := FindWindow(nil, 'Calculator');
if hCalWnd <> 0 then
  begin
  // DO NOT USE Delphi VCL control components, it's more trouble that it's worth
  hButton1 := CreateWindow('BUTTON', 'Button1', WS_CHILD or BS_PUSHBUTTON or
              BS_TEXT or WS_CLIPSIBLINGS,3,18,64,30,hCalWnd,0,hInstance,nil);
  // use API control creation so you can have half a chance at message handling
  if hButton1 = 0 then
    begin
    ShowMessage('hButton1 WAS not Created');
    Exit;
    end;
  SendMessage(hButton1,WM_SETFONT,GetStockObject(ANSI_VAR_FONT),0);
  SetWindowPos(hButton1,HWND_TOP, 0,0,0,0, SWP_NOMOVE or SWP_NOSIZE or
               SWP_SHOWWINDOW or SWP_NOACTIVATE); // need to get this button to top
  InvalidateRect(hButton1, nil, True); // needed for new paint process in XP
  Sleep(15); // allow initial create and paint, remember this button is in another hInstance
  PButProc := Pointer(SetWindowLong(hButton1, GWL_WNDPROC, Integer(@ButFunc)));
  // to get any events for this you will need to Sub-Class this button
  // I think it is a BAD IDEA to subclass a window in another process
  end else
  ShowMessage('Calculator is NOT running');
end;

procedure TForm1.sbut_KillCalButtonClick(Sender: TObject);
var
StopHook: function: Boolean;
begin
// this button click will destroy hButton1
DestroyWindow(hButton1);
hButton1 := 0;
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
DestroyWindow(hButton1);
// IMPORTANT, you must destroy the new button when you exit this form !

// now that the hButton1 Window Proc message handling is in this program,
// if you close this program without destroying the hButton1, it will have NO
// Window Proc to handle it's messages, and bad things will happen
end;

= = = = = = = = = = = = = = = =  = = = = = = = = =  = = = = = = =

I think you may ask more questions, but working in another process may get difficult, or I may have no knowledge of what to try
0
 

Author Comment

by:Franus
ID: 11930042
Thank you for your answer. I solved my problem...well..almost...working in another process is really painful :) but i have a project which requires this so i had to it like this. Once again thanks very much!!!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Intraweb download file link ? 1 167
Working with hours 3 74
Create a path if not exists 7 108
Firemonkey Tcameracomponent resultion 1 24
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
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…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

733 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