动态给外部程序加一个按纽 (2001年3月15日).txt

来自「自己对DELPHI学习的一点体会」· 文本 代码 · 共 80 行

TXT
80
字号
动态给外部程序加一个按纽 (2001年3月15日) 

网友更新  分类:Win API   作者:阎磊  推荐:yanlei   阅读次数:357  
(http://www.codesky.net)  

--------------------------------------------------------------------------------
启动计算器,给计算器加一个按钮
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;

type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
proc : longint;
form_hw,hn:HWND;
implementation
{$R *.DFM}
function WndProc2(hwnd: HWnd; Msg: UINT; wParam: wParam;
lParam: LPARAM) : integer; Far; stdcall;
begin
CASE Msg OF
WM_LBUTTONUP:
begin
showmessage('ok');
end

END;

Result := CallWindowProc(TFNWndProc(proc), hwnd, Msg, wParam, lParam);
end;

procedure TForm1.Button1Click(Sender: TObject);
var l:longint;
rcw : Word;
begin
rcw := WinExec('calc.exe', SW_SHOWNORMAL);//启动计算器
if rcw<=32 then //无法启动计算器
begin
Application.Terminate;
end ;

while true do//确保计算器启动
begin
form_hw:=FindWindow(nil,'计算器');
if boolean(form_hw) then break;
end;

hn := CreateWindow('BUTTON', 'ok',
WS_VISIBLE + WS_CHILD, 0, 0, 30, 20,form_hw
, 0, 0, nil);//left=0,top=0,width=30 height=20
proc := GetWindowLong(hn, GWL_WNDPROC);
l := longint(@WndProc2);//设定事件
SetWindowLong(hn, GWL_WNDPROC, l);
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
if boolean(form_hw) then//启动
SendMessage(form_hw, wm_close,0,0);
end;

end.  
 

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?