读书人

怎么在其实软件窗体上添加按钮和事件(

发布时间: 2013-03-26 09:54:34 作者: rapoo

如何在其实软件窗体上添加按钮和事件(控件)
我这样写是可以在另外的程序上看到按钮,不过无法触发,请问如何添加按钮事件?谢谢

procedure TfrmMain.ToolButton1Click(Sender: TObject);
var
Hand: THandle;
Object1: TButton;
begin
Hand:= FindWindow('TSpecInvoiceForm', nil);
if Hand <> 0 then
begin
Object1:=TButton.Create(self);
Object1.ParentWindow:=hand;
Object1.Caption:='test';
Object1.OnClick:=Button10Click;
end;
end;

procedure TfrmMain.Button10Click(Sender: TObject);
begin
showmessage('aa');
end;



[解决办法]
function NewButton1WinProc(hWnd, Msg, wParam, lParam: Integer): Integer; stdcall;
begin
Result := CallWindowProc(oldButton1WinProc, hWnd, Msg, wParam, lParam);
case Msg of
WM_LBUTTONDOWN:
showmessage('aa');
end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
h := FindWindow('TSpecInvoiceForm', nil);
if h = 0 then exit;
hButton1 := CreateWindow('Button', 'Test', WS_VISIBLE or WS_CHILD or BS_PUSHLIKE or BS_TEXT, 55, 55, 74, 24, h, 0, GetModuleHandle(nil), nil);
//获取nButton1的WinProc,保存
oldButton1WinProc := Pointer(GetWindowLong(hButton1, GWL_WNDPROC));
//设置nButton1的WinProc为NewButton1WinProc
SetWindowLong(hButton1, GWL_WNDPROC, Integer(@NewButton1WinProc));
end;

读书人网 >.NET

热点推荐