首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > .NET >

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

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

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

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;

热点排行