200大讨论:能者拿分--句柄 ( 积分: 200 )

  • 主题发起人 主题发起人 shell~
  • 开始时间 开始时间
到这两句中的随便一句都会出错<br>//MyHandle.Add(IntToStr(hwnd)); //得到目标控件的Hwnd(句柄)<br> &nbsp; &nbsp; &nbsp;Memo1.Lines.Add(IntToStr(hwnd));<br>可是如果直接showmessage就不会出错。
 
写一hook,先用Dll 注入,,再用内存映射文件,文件的类型为那5个edit.text组成的记录,<br>在B进程写,A进程读
 
TO 堕落天使_2003:<br>能否详细讲解一下原理[:D],谢了先。
 
//我以前写的控制一个安装程序自动安装的 &nbsp;程序部分代码,原理类似<br>function TInstallDataFrm.Find(text: string): boolean;<br>begin<br> &nbsp;result := False;<br> &nbsp;if (pos('data',text) &amp;gt;0 ) and (pos('access',text) &amp;gt;0 )<br> &nbsp; &nbsp; &nbsp;and (pos('components',text) &amp;gt;0 ) then<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp;result := true;<br> &nbsp; &nbsp;end;<br>end;<br><br>function TInstallDataFrm.GetMDACHWnd: Hwnd;<br>var<br> &nbsp;hCurrentWindow: HWnd;<br> &nbsp;szClass: array[0..254] of char;<br> &nbsp;szText: array[0..254] of char;<br>begin<br> &nbsp;hCurrentWindow := GetWindow(Handle, GW_HWNDFIRST);<br> &nbsp;while hCurrentWindow &amp;lt;&amp;gt; 0 do<br> &nbsp;begin<br> &nbsp; &nbsp;GetWindowText(hCurrentWindow, @szText, 255);<br> &nbsp; &nbsp;GetClassName(hCurrentWindow,@szClass,255);<br> &nbsp; &nbsp;Application.ProcessMessages;<br> &nbsp; &nbsp;if (StrPas(@szClass)='#32770') &nbsp;then<br> &nbsp; &nbsp;if find(Lowercase(StrPas(@szText))) then<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp;//showwindow(hCurrentWindow,SW_MINIMIZE);SW_HIDE<br> &nbsp; &nbsp; &nbsp;showwindow(hCurrentWindow,SW_HIDE);<br> &nbsp; &nbsp; &nbsp;result := hCurrentWindow;<br> &nbsp; &nbsp; &nbsp;exit;<br> &nbsp; &nbsp;end;<br> &nbsp; &nbsp;hCurrentWindow := GetWindow(hCurrentWindow, GW_HWNDNEXT);<br> &nbsp;end;<br> &nbsp;result := 0;<br>end;<br><br>function TInstallDataFrm.isExists: boolean;<br>begin<br> &nbsp;result := (GetMDACHWnd&amp;lt;&amp;gt;0) &nbsp;;<br>end;<br><br>function TInstallDataFrm.myIsWindowEnabled(h: Hwnd;outTime:integer):boolean;<br>var I:integer;<br>begin<br> &nbsp;I := 0;<br> &nbsp;repeat<br> &nbsp; &nbsp;sleep(500);<br> &nbsp; &nbsp;inc(i);<br> &nbsp; &nbsp;Application.ProcessMessages;<br> &nbsp; &nbsp;if I &amp;gt; outTime then<br> &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp;result := False;<br> &nbsp; &nbsp; &nbsp; &nbsp;exit;<br> &nbsp; &nbsp; &nbsp;end;<br> &nbsp;until IsWindowEnabled(h);<br> &nbsp;sleep(1000);<br> &nbsp;result := True;<br>end;<br><br>procedure TInstallDataFrm.Send_key(h: Hwnd);<br>begin<br> &nbsp;SendMessage(h,WM_LBUTTONDOWN,MK_LBUTTON,0);<br> &nbsp;SendMessage(h,WM_LBUTTONUP,0,0);<br>end;<br><br>procedure TInstallDataFrm.SetupMDAC(hCurrentWindow:Hwnd);<br>var<br> &nbsp;h: HWnd;<br>begin<br><br> &nbsp;sleep(1000);<br> &nbsp;barMain.Position := 20;<br> &nbsp;h:=GetDlgItem(hCurrentWindow,12324);<br> &nbsp;Send_key(h);<br> &nbsp;sleep(1000) ;<br> &nbsp;if not myIsWindowEnabled(h,90) then exit;<br><br> &nbsp;barMain.Position := 50;<br><br> &nbsp;h:=GetDlgItem(hCurrentWindow,12324);<br> &nbsp;if not myIsWindowEnabled(h,30) then exit;<br> &nbsp;Send_key(h);<br> &nbsp;sleep(1000) ;<br> &nbsp;barMain.Position := 70;<br><br><br> &nbsp;h:=GetDlgItem(hCurrentWindow,12325);<br> &nbsp;if not myIsWindowEnabled(h,90) then exit;<br> &nbsp;Send_key(h);<br> &nbsp;sleep(1000) ;<br> &nbsp;barMain.Position := 90;<br><br> &nbsp;h:=GetDlgItem(hCurrentWindow,12325);<br> &nbsp;if not myIsWindowEnabled(h,30) then exit;<br> &nbsp;Send_key(h);<br> &nbsp;barMain.Position := 100;<br> &nbsp;self.LabDataState.Caption &nbsp;:= setupFrm.GetTitle('msg_installmdacok');<br>end;<br>
 
这是我给人编的,是针对网吧管理软件在上机时要求输入人员的姓名、性别、年龄,证件号等麻烦的问题,对你的问题完全可以解决。<br>如果要修改的窗口名是已知的情况下,可直接用 FindWindow(nil,'窗口名') 获取该窗口的句柄,然后用 EnumChildWindows 列出所有 Edit,和 TRadioButton 的信息,包括该控件的顺序,也就是 EnumChildWindows枚举的次序。保存起来,其中位置是最主要的。我保存在 ini 中。如查要修改 B 程序中的 Edit 时,只要再一次枚举该窗口,通过判断 次序就可发送 WM_GETTEXT 的消息。添加Edit 的文本,具体源码如下:<br>(现在已有了新版本,你的程序可不用 dll,HookAPI,有疑难可加我 QQ:178718468,有全部源码);<br><br><br>unit Main;<br><br>interface<br><br>uses<br> &nbsp;Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br> &nbsp;Dialogs, WinSkinStore, WinSkinData, StdCtrls, Buttons, ComCtrls, ExtCtrls,<br> &nbsp;CheckLst, ShellAPI, Menus, IniFiles, HookConst;<br>const UserFile = 'ufd.ini';<br>type<br> &nbsp;TEditIndexs = record<br> &nbsp; &nbsp;NameIndex: integer;<br> &nbsp; &nbsp;SexManIndex: integer;<br> &nbsp; &nbsp;SexWomanIndex: integer;<br> &nbsp; &nbsp;AgeIndex: integer;<br> &nbsp; &nbsp;PaperIndex: integer;<br> &nbsp;end;<br><br>type<br> &nbsp;PFindWindowStruct = ^TFindWindowStruct;<br> &nbsp;TFindWindowStruct = record<br> &nbsp; &nbsp;Caption: string;<br> &nbsp; &nbsp;ClassName: string;<br> &nbsp; &nbsp;WindowHandle: THandle;<br> &nbsp;end;<br><br>type<br> &nbsp;TMainForm = class(TForm)<br> &nbsp; &nbsp;PageControl1: TPageControl;<br> &nbsp; &nbsp;Panel1: TPanel;<br> &nbsp; &nbsp;TabSheet1: TTabSheet;<br> &nbsp; &nbsp;TabSheet2: TTabSheet;<br> &nbsp; &nbsp;btnExit: TBitBtn;<br> &nbsp; &nbsp;btnHide: TBitBtn;<br> &nbsp; &nbsp;GroupBox1: TGroupBox;<br> &nbsp; &nbsp;Label1: TLabel;<br> &nbsp; &nbsp;Label2: TLabel;<br> &nbsp; &nbsp;edtName: TEdit;<br> &nbsp; &nbsp;edtPaper: TEdit;<br> &nbsp; &nbsp;Label3: TLabel;<br> &nbsp; &nbsp;edtAge: TEdit;<br> &nbsp; &nbsp;UpDown1: TUpDown;<br> &nbsp; &nbsp;rbtnMan: TRadioButton;<br> &nbsp; &nbsp;rbtnWoman: TRadioButton;<br> &nbsp; &nbsp;Label4: TLabel;<br> &nbsp; &nbsp;btnSend: TBitBtn;<br> &nbsp; &nbsp;btnSave: TBitBtn;<br> &nbsp; &nbsp;btnDelete: TBitBtn;<br> &nbsp; &nbsp;TabSheet3: TTabSheet;<br> &nbsp; &nbsp;lbxSearch: TListBox;<br> &nbsp; &nbsp;Panel2: TPanel;<br> &nbsp; &nbsp;btnSearchProc: TButton;<br> &nbsp; &nbsp;btnSelected: TButton;<br> &nbsp; &nbsp;Label5: TLabel;<br> &nbsp; &nbsp;lblWinCount: TLabel;<br> &nbsp; &nbsp;TabSheet4: TTabSheet;<br> &nbsp; &nbsp;Panel3: TPanel;<br> &nbsp; &nbsp;btnSearchFlag: TButton;<br> &nbsp; &nbsp;btnClear: TButton;<br> &nbsp; &nbsp;pnlSelectedWin: TPanel;<br> &nbsp; &nbsp;lvSearchFlag: TListView;<br> &nbsp; &nbsp;ppmSetFlag: TPopupMenu;<br> &nbsp; &nbsp;N1: TMenuItem;<br> &nbsp; &nbsp;N2: TMenuItem;<br> &nbsp; &nbsp;N3: TMenuItem;<br> &nbsp; &nbsp;N4: TMenuItem;<br> &nbsp; &nbsp;N5: TMenuItem;<br> &nbsp; &nbsp;lvUserList: TListView;<br> &nbsp; &nbsp;SkinData1: TSkinData;<br> &nbsp; &nbsp;Panel4: TPanel;<br> &nbsp; &nbsp;Panel6: TPanel;<br> &nbsp; &nbsp;Label11: TLabel;<br> &nbsp; &nbsp;edtWindowName: TEdit;<br> &nbsp; &nbsp;Panel7: TPanel;<br> &nbsp; &nbsp;TabSheet5: TTabSheet;<br> &nbsp; &nbsp;Panel5: TPanel;<br> &nbsp; &nbsp;Label13: TLabel;<br> &nbsp; &nbsp;Label12: TLabel;<br> &nbsp; &nbsp;Label14: TLabel;<br> &nbsp; &nbsp;Label15: TLabel;<br> &nbsp; &nbsp;Label16: TLabel;<br> &nbsp; &nbsp;Label17: TLabel;<br> &nbsp; &nbsp;Label6: TLabel;<br> &nbsp; &nbsp;Label7: TLabel;<br> &nbsp; &nbsp;Label8: TLabel;<br> &nbsp; &nbsp;Label9: TLabel;<br> &nbsp; &nbsp;Label10: TLabel;<br> &nbsp; &nbsp;edtNameIndex: TEdit;<br> &nbsp; &nbsp;edtManIndex: TEdit;<br> &nbsp; &nbsp;edtWomanIndex: TEdit;<br> &nbsp; &nbsp;edtAgeIndex: TEdit;<br> &nbsp; &nbsp;edtPaperIndex: TEdit;<br> &nbsp; &nbsp;UpDown2: TUpDown;<br> &nbsp; &nbsp;UpDown3: TUpDown;<br> &nbsp; &nbsp;UpDown4: TUpDown;<br> &nbsp; &nbsp;UpDown5: TUpDown;<br> &nbsp; &nbsp;UpDown6: TUpDown;<br> &nbsp; &nbsp;btnSaveParam: TBitBtn;<br> &nbsp; &nbsp;chbxAutoPopup: TCheckBox;<br> &nbsp; &nbsp;chbxAutoInput: TCheckBox;<br> &nbsp; &nbsp;Label18: TLabel;<br> &nbsp; &nbsp;Edit1: TEdit;<br> &nbsp; &nbsp;Label19: TLabel;<br> &nbsp; &nbsp;Edit2: TEdit;<br> &nbsp; &nbsp;Label20: TLabel;<br> &nbsp; &nbsp;Edit3: TEdit;<br> &nbsp; &nbsp;UpDown7: TUpDown;<br> &nbsp; &nbsp;procedure btnExitClick(Sender: TObject);<br> &nbsp; &nbsp;procedure btnSearchProcClick(Sender: TObject);<br> &nbsp; &nbsp;procedure btnSelectedClick(Sender: TObject);<br> &nbsp; &nbsp;procedure btnSearchFlagClick(Sender: TObject);<br> &nbsp; &nbsp;procedure btnClearClick(Sender: TObject);<br> &nbsp; &nbsp;procedure N1Click(Sender: TObject);<br> &nbsp; &nbsp;procedure btnSendClick(Sender: TObject);<br> &nbsp; &nbsp;procedure FormCreate(Sender: TObject);<br> &nbsp; &nbsp;procedure FormDestroy(Sender: TObject);<br> &nbsp; &nbsp;procedure lvUserListClick(Sender: TObject);<br> &nbsp; &nbsp;procedure btnSaveClick(Sender: TObject);<br> &nbsp; &nbsp;procedure btnDeleteClick(Sender: TObject);<br> &nbsp; &nbsp;procedure btnSaveParamClick(Sender: TObject);<br> &nbsp; &nbsp;procedure lvUserListDblClick(Sender: TObject);<br> &nbsp; &nbsp;procedure btnHideClick(Sender: TObject);<br> &nbsp; &nbsp;procedure chbxAutoPopupClick(Sender: TObject);<br> &nbsp; &nbsp;procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);<br> &nbsp; &nbsp;procedure FormClose(Sender: TObject; var Action: TCloseAction);<br> &nbsp;private<br> &nbsp; &nbsp;FEditIndexs: TEditIndexs;<br> &nbsp; &nbsp;{ Private declarations }<br> &nbsp; &nbsp;function GetEditIndexs: TEditIndexs;<br> &nbsp;public<br> &nbsp; &nbsp;procedure WndProc(var Messages: TMessage); override;<br> &nbsp;published<br> &nbsp; &nbsp;procedure ListBoxAddWidth(AListBox: TListBox);<br> &nbsp; &nbsp;procedure ListControls(Awnd: string);<br> &nbsp; &nbsp;procedure AddFlag(AClass, AValue: string);<br> &nbsp; &nbsp;function SendData(WindowName: string): Boolean;<br> &nbsp; &nbsp;procedure SetValues(AListItem: TListItem);<br> &nbsp; &nbsp;property EditIndexs: TEditIndexs read GetEditIndexs;<br> &nbsp; &nbsp;procedure ReadData;<br> &nbsp; &nbsp;procedure GetUserList(var AStringList: TStringList);<br> &nbsp; &nbsp;procedure WriteData;<br> &nbsp; &nbsp;{ Public declarations }<br> &nbsp;end;<br><br>var<br> &nbsp;MainForm: TMainForm;<br> &nbsp;FindAll: Boolean;<br> &nbsp;CtrlIndex: integer;<br> &nbsp;UserData: TIniFile;<br> &nbsp;hMappingFile: THandle;<br> &nbsp;pShMem: PShareMem;<br>const MessageID = WM_User + 110;<br><br>function EnumWindowsProc(hWindow: HWnd; lParam: LongInt): Boolean;<br>{$IFDEF Win32}stdcall; {$ELSE}; export; {$ENDIF}<br>function EnumWindowsFast(hWindow: HWnd; lParam: LongInt): Boolean;<br>{$IFDEF Win32}stdcall; {$ELSE}; export; {$ENDIF}<br>function EnumChildProc(HWnd: integer; lParam: LongInt): Boolean;<br>{$IFDEF Win32}stdcall; {$ELSE}; export; {$ENDIF}<br>function EnumChildItems(HWnd: integer; lParam: LongInt): Boolean;<br>{$IFDEF Win32}stdcall; {$ELSE}; export; {$ENDIF}<br>function FindAWindow(Caption: string; ClassName: string; IsThread: Boolean):<br> &nbsp;THandle;<br><br>implementation<br><br>{$R *.dfm}<br>function StartHook(Sender: HWnd; MessageID: WORD; WatchFormName: TChars): BOOL;<br> &nbsp;stdcall; external<br>'AHook.DLL';<br>function StopHook: BOOL; stdcall; external 'AHook.DLL';<br><br>function SetWatch: BOOL; stdcall; external 'AHook.Dll';<br><br>function TMainForm.GetEditIndexs: TEditIndexs;<br>begin<br> &nbsp;FEditIndexs.NameIndex := UpDown2.Position;<br> &nbsp;FEditIndexs.SexManIndex := UpDown3.Position;<br> &nbsp;FEditIndexs.SexWomanIndex := UpDown4.Position;<br> &nbsp;FEditIndexs.AgeIndex := UpDown5.Position;<br> &nbsp;FEditIndexs.PaperIndex := UpDown6.Position;<br> &nbsp;Result := FEditIndexs;<br>end;<br><br>function EnumWindowsProc(hWindow: HWnd; lParam: LongInt): Boolean;<br>{$IFDEF Win32} stdcall; {$ELSE}; export; {$ENDIF}<br>var<br> &nbsp;lpBuffer: PChar;<br> &nbsp;WindowCaptionFound: Boolean;<br> &nbsp;ClassNameFound: Boolean;<br>begin<br> &nbsp;GetMem(lpBuffer, 255);<br> &nbsp;Result := True;<br> &nbsp;WindowCaptionFound := False;<br> &nbsp;ClassNameFound := False;<br> &nbsp;try<br><br> &nbsp; &nbsp;//------------------------------------------------<br> &nbsp; &nbsp;if GetWindowText(hWindow, lpBuffer, 255) &amp;gt; 0 then<br> &nbsp; &nbsp; &nbsp;if FindAll then<br> &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp;if not (StrPas(lpBuffer) = '网虫外挂v1.0') then<br> &nbsp; &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;MainForm.lbxSearch.Items.Add(StrPas(lpBuffer));<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;MainForm.ListBoxAddWidth(MainForm.lbxSearch);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;MainForm.lblWinCount.Caption :=<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;IntToStr(StrToInt(MainForm.lblWinCount.Caption) + 1);<br> &nbsp; &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp; &nbsp;end<br> &nbsp; &nbsp; &nbsp;else<br> &nbsp; &nbsp; &nbsp; &nbsp;if Pos(PFindWindowStruct(lParam).Caption, StrPas(lpBuffer)) &amp;gt; 0 then<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;WindowCaptionFound := True;<br><br> &nbsp; &nbsp;if not FindAll then<br> &nbsp; &nbsp;begin //-----------------------<br> &nbsp; &nbsp; &nbsp;if PFindWindowStruct(lParam).ClassName = '' then<br> &nbsp; &nbsp; &nbsp; &nbsp;ClassNameFound := True else<br> &nbsp; &nbsp; &nbsp; &nbsp;if GetClassName(hWindow, lpBuffer, 255) &amp;gt; 0 then<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if Pos(PFindWindowStruct(lParam).ClassName, StrPas(lpBuffer)) &amp;gt; 0 then<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ClassNameFound := True;<br><br> &nbsp; &nbsp; &nbsp;if (WindowCaptionFound and ClassNameFound) then<br> &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp;PFindWindowStruct(lParam).WindowHandle := hWindow;<br> &nbsp; &nbsp; &nbsp; &nbsp;Result := False;<br> &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp;end; //----------------------------<br> &nbsp; &nbsp;//-------------------------------------------------<br> &nbsp;finally<br> &nbsp; &nbsp;FreeMem(lpBuffer, sizeof(lpBuffer^));<br> &nbsp;end;<br>end;<br>//--------这个函数可不用---------------<br>function EnumWindowsFast(hWindow: HWnd; lParam: LongInt): Boolean;<br>{$IFDEF Win32} stdcall; {$ELSE}; export; {$ENDIF}<br>var<br> &nbsp;lpBuffer: PChar;<br> &nbsp;WindowCaptionFound: Boolean;<br> &nbsp;ClassNameFound: Boolean;<br>begin<br> &nbsp;GetMem(lpBuffer, 255);<br> &nbsp;Result := True;<br> &nbsp;WindowCaptionFound := False;<br> &nbsp;ClassNameFound := False;<br> &nbsp;try<br> &nbsp; &nbsp;//------------------------------------------------<br> &nbsp; &nbsp;if GetWindowText(hWindow, lpBuffer, 255) &amp;gt; 0 then<br> &nbsp; &nbsp; &nbsp;if Pos(PFindWindowStruct(lParam).Caption, StrPas(lpBuffer)) &amp;gt; 0 then<br> &nbsp; &nbsp; &nbsp; &nbsp;WindowCaptionFound := True;<br> &nbsp; &nbsp;if PFindWindowStruct(lParam).ClassName = '' then<br> &nbsp; &nbsp; &nbsp;ClassNameFound := True else<br> &nbsp; &nbsp; &nbsp;if GetClassName(hWindow, lpBuffer, 255) &amp;gt; 0 then<br> &nbsp; &nbsp; &nbsp; &nbsp;if Pos(PFindWindowStruct(lParam).ClassName, StrPas(lpBuffer)) &amp;gt; 0 then<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ClassNameFound := True;<br> &nbsp; &nbsp;if (WindowCaptionFound and ClassNameFound) then<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp;PFindWindowStruct(lParam).WindowHandle := hWindow;<br> &nbsp; &nbsp; &nbsp;Result := False;<br> &nbsp; &nbsp;end;<br> &nbsp;finally<br> &nbsp; &nbsp;FreeMem(lpBuffer, sizeof(lpBuffer^));<br> &nbsp;end;<br>end;<br>//-----这是添加子窗口到 ListView记录 控件的信息------------------<br><br>function EnumChildProc(HWnd: integer; lParam: LongInt): Boolean;<br>{$IFDEF Win32} stdcall; {$ELSE}; export; {$ENDIF}<br>var<br> &nbsp;MyText: PChar;<br> &nbsp;ClassName: array[0..31] of Char;<br> &nbsp;SearchText: string;<br>begin<br> &nbsp;Result := True;<br> &nbsp;GetMem(MyText, 255);<br> &nbsp;try<br> &nbsp; &nbsp;SendMessage(HWnd, Wm_GetText, 255, LongInt(MyText));<br> &nbsp; &nbsp;GetClassName(HWnd, ClassName, 31);<br> &nbsp; &nbsp;SearchText := '[' + StrPas(MyText) + ']';<br><br> &nbsp; &nbsp;MainForm.AddFlag(ClassName, SearchText);<br> &nbsp;finally<br> &nbsp; &nbsp;FreeMem(MyText, sizeof(MyText^));<br> &nbsp;end;<br>end;<br>//-------枚举子窗口并通过索引值发送添写消息----------------<br>function EnumChildItems(HWnd: integer; lParam: LongInt): Boolean;<br>{$IFDEF Win32} stdcall; {$ELSE}; export; {$ENDIF}<br>var<br> &nbsp;ClsName, Caption: array[0..31] of Char;<br>begin<br> &nbsp;GetClassName(HWnd, ClsName, 31);<br> &nbsp;GetWindowText(HWnd, Caption, 31);<br> &nbsp;if CtrlIndex = MainForm.EditIndexs.NameIndex then<br> &nbsp;begin<br> &nbsp; &nbsp;SendMessage(HWnd, WM_SETTEXT, Length(Trim(MainForm.edtName.Text)),<br> &nbsp; &nbsp; &nbsp;LongInt(PChar(Trim(MainForm.edtName.Text))));<br> &nbsp;end;<br> &nbsp;if CtrlIndex = MainForm.EditIndexs.SexManIndex then<br> &nbsp;begin<br> &nbsp; &nbsp;if MainForm.rbtnMan.Checked then<br> &nbsp; &nbsp; &nbsp;SendMessage(HWnd, BM_CLICK, 0, 0);<br> &nbsp;end;<br> &nbsp;if CtrlIndex = MainForm.EditIndexs.SexWomanIndex then<br> &nbsp;begin<br> &nbsp; &nbsp;if MainForm.rbtnWoman.Checked then<br> &nbsp; &nbsp; &nbsp;SendMessage(HWnd, BM_CLICK, 0, 0);<br> &nbsp;end;<br> &nbsp;if CtrlIndex = MainForm.EditIndexs.AgeIndex then<br> &nbsp;begin<br> &nbsp; &nbsp;SendMessage(HWnd, WM_SETTEXT, Length(Trim(MainForm.edtAge.Text)),<br> &nbsp; &nbsp; &nbsp;LongInt(PChar(Trim(MainForm.edtAge.Text))));<br> &nbsp;end;<br> &nbsp;if CtrlIndex = MainForm.EditIndexs.PaperIndex then<br> &nbsp;begin<br> &nbsp; &nbsp;SendMessage(HWnd, WM_SETTEXT, Length(Trim(MainForm.edtPaper.Text)),<br> &nbsp; &nbsp; &nbsp;LongInt(PChar(Trim(MainForm.edtPaper.Text))));<br> &nbsp;end;<br> &nbsp;Inc(CtrlIndex);<br> &nbsp;Result := True;<br>end;<br><br>function FindAWindow(Caption: string; ClassName: string; IsThread: Boolean):<br> &nbsp;THandle;<br>var<br> &nbsp;windowInfo: TFindWindowStruct;<br>begin<br> &nbsp;begin<br> &nbsp; &nbsp;windowInfo.Caption := Caption;<br> &nbsp; &nbsp;windowInfo.ClassName := ClassName;<br> &nbsp; &nbsp;windowInfo.WindowHandle := 0;<br> &nbsp; &nbsp;if not IsThread then<br> &nbsp; &nbsp; &nbsp;EnumWindows(@EnumWindowsProc, LongInt(@windowInfo))<br> &nbsp; &nbsp;else<br> &nbsp; &nbsp; &nbsp;EnumWindows(@EnumWindowsFast, LongInt(@windowInfo));<br> &nbsp; &nbsp;FindAWindow := windowInfo.WindowHandle;<br> &nbsp;end;<br>end;<br>//----加宽 ListBox 的宽度,可不用.<br>procedure TMainForm.ListBoxAddWidth(AListBox: TListBox);<br>var<br> &nbsp;i, MaxWidth: integer;<br>begin<br> &nbsp;MaxWidth := 0;<br> &nbsp;for i := 0 to AListBox.Items.Count - 1 do<br> &nbsp; &nbsp;if MaxWidth &amp;lt; AListBox.Canvas.TextWidth(AListBox.Items.Strings) then<br> MaxWidth := AListBox.Canvas.TextWidth(AListBox.Items.Strings);<br> SendMessage(AListBox.Handle, LB_SETHORIZONTALEXTENT, MaxWidth + 2, 0);<br>end;<br><br>procedure TMainForm.btnExitClick(Sender: TObject);<br>begin<br> &nbsp;Close;<br>end;<br>//--枚举所有窗口到 ListBox 中<br>procedure TMainForm.btnSearchProcClick(Sender: TObject);<br>var<br> &nbsp;windowInfo: TFindWindowStruct;<br>begin<br> &nbsp;FindAll := True;<br> &nbsp;lbxSearch.Clear;<br> &nbsp;lblWinCount.Caption := '0';<br> &nbsp;if EnumWindows(@EnumWindowsProc, LongInt(@windowInfo)) then<br> &nbsp; &nbsp;FindAll := False;<br><br>end;<br>//ListBox选中的窗口名,并列出子窗口,保存在 ListView 中,并加入枚举次序.<br>procedure TMainForm.btnSelectedClick(Sender: TObject);<br>begin<br> &nbsp;if lbxSearch.ItemIndex &amp;lt;&amp;gt; -1 then<br> &nbsp;begin<br> &nbsp; &nbsp;pnlSelectedWin.Caption := lbxSearch.Items[lbxSearch.ItemIndex];<br> &nbsp; &nbsp;edtWindowName.Text := lbxSearch.Items[lbxSearch.ItemIndex];<br> &nbsp; &nbsp;ListControls(pnlSelectedWin.Caption);//加子窗口到 ListView 中.<br> &nbsp;end;<br>end;<br>procedure TMainForm.ListControls(Awnd: string);<br>var<br> &nbsp;TheWindowHandle: THandle;<br>begin<br> &nbsp;TheWindowHandle := FindAWindow(Awnd, '', False);<br> &nbsp;if TheWindowHandle &amp;lt;&amp;gt; 0 then<br> &nbsp;begin<br> &nbsp; &nbsp;lvSearchFlag.Items.Clear;<br> &nbsp; &nbsp;FindAll := False;<br> &nbsp; &nbsp;EnumChildWindows(TheWindowHandle, @EnumChildProc, 0);<br> &nbsp;end;<br>end;<br>procedure TMainForm.btnSearchFlagClick(Sender: TObject);<br>begin<br> &nbsp;ListControls(pnlSelectedWin.Caption);<br>end;<br>procedure TMainForm.AddFlag(AClass, AValue: string);<br>var AItem: TListItem;<br>begin<br> &nbsp;AItem := lvSearchFlag.Items.Add;<br> &nbsp;AItem.Caption := IntToStr(lvSearchFlag.Items.Count);<br> &nbsp;AItem.SubItems.Add(AClass);<br> &nbsp;AItem.SubItems.Add(AValue);<br>end;<br><br>procedure TMainForm.btnClearClick(Sender: TObject);<br>begin<br> &nbsp;lvSearchFlag.Items.Clear;<br>end;<br><br>procedure TMainForm.N1Click(Sender: TObject);<br>begin<br> &nbsp;if lvSearchFlag.SelCount &amp;lt;= 0 then Exit;<br> &nbsp;case TMenuItem(Sender).Tag of<br> &nbsp; &nbsp;1:<br> &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp;edtNameIndex.Text := lvSearchFlag.Selected.Caption;<br> &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp;2:<br> &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp;edtManIndex.Text := lvSearchFlag.Selected.Caption;<br> &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp;3:<br> &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp;edtWomanIndex.Text := lvSearchFlag.Selected.Caption;<br> &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp;4:<br> &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp;edtAgeIndex.Text := lvSearchFlag.Selected.Caption;<br> &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp;5:<br> &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp;edtPaperIndex.Text := lvSearchFlag.Selected.Caption;<br> &nbsp; &nbsp; &nbsp;end;<br> &nbsp;end;<br>end;<br><br>function TMainForm.SendData(WindowName: string): Boolean;<br>var TheHandle: THandle;<br>begin<br> &nbsp;TheHandle := FindAWindow(WindowName, '', False);<br> &nbsp;if TheHandle &amp;lt;&amp;gt; 0 then<br> &nbsp;begin<br> &nbsp; &nbsp;CtrlIndex := 1;<br> &nbsp; &nbsp;EnumChildWindows(TheHandle, @EnumChildItems, 0);<br> &nbsp; &nbsp;CtrlIndex := 0;<br> &nbsp;end;<br> &nbsp;Result := True;<br>end;<br>procedure TMainForm.btnSendClick(Sender: TObject);<br>begin<br> &nbsp;if edtWindowName.Text = '' then<br> &nbsp;begin<br> &nbsp; &nbsp;Application.MessageBox('请先设置窗口标志! &nbsp; &nbsp; &nbsp; ', '操作提示', MB_OK +<br> &nbsp; &nbsp; &nbsp;MB_IconStop + MB_ApplModal);<br> &nbsp; &nbsp;PageControl1.ActivePageIndex := 1;<br> &nbsp; &nbsp;Exit;<br> &nbsp;end;<br><br> &nbsp;SendData(edtWindowName.Text);<br>end;<br>procedure TMainForm.ReadData;<br>var<br> &nbsp;i: integer;<br> &nbsp;UserSections: TStringList;<br> &nbsp;AListItem: TListItem;<br>begin<br> &nbsp;edtWindowName.Text := UserData.ReadString('Parameters', 'WindowName', '');<br> &nbsp;UpDown2.Position := UserData.ReadInteger('Parameters', 'NameIndex', 0);<br> &nbsp;UpDown3.Position := UserData.ReadInteger('Parameters', 'ManIndex', 0);<br> &nbsp;UpDown4.Position := UserData.ReadInteger('Parameters', 'WomanIndex', 0);<br> &nbsp;UpDown5.Position := UserData.ReadInteger('Parameters', 'AgeIndex', 0);<br> &nbsp;UpDown6.Position := UserData.ReadInteger('Parameters', 'PaperIndex', 0);<br> &nbsp;// chbxAutoPopup.Checked := UserData.ReadBool('Parameters','AutoPopup',True);<br> &nbsp;// chbxAutoInput.Checked := UserData.ReadBool('Parameters','AutoInput',True);<br> &nbsp;UserSections := TStringList.Create;<br> &nbsp;try<br> &nbsp; &nbsp;GetUserList(UserSections);<br> &nbsp; &nbsp;lvUserList.Items.Clear;<br> &nbsp; &nbsp;for i := 0 to UserSections.Count - 1 do<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp;AListItem := lvUserList.Items.Add;<br> &nbsp; &nbsp; &nbsp;AListItem.Caption := IntToStr(lvUserList.Items.Count);<br> &nbsp; &nbsp; &nbsp;AListItem.SubItems.Add(UserSections.Strings);<br> &nbsp; &nbsp; &nbsp;AListItem.SubItems.Add(UserData.ReadString(UserSections.Strings,<br> &nbsp; &nbsp; &nbsp; &nbsp;'性别',<br> &nbsp; &nbsp; &nbsp; &nbsp;'男'));<br> &nbsp; &nbsp; &nbsp;AListItem.SubItems.Add(IntToStr(UserData.ReadInteger(UserSections.Strings, '年龄', 18)));<br> &nbsp; &nbsp; &nbsp;AListItem.SubItems.Add(UserData.ReadString(UserSections.Strings,<br> &nbsp; &nbsp; &nbsp; &nbsp;'证件号', ''));<br> &nbsp; &nbsp;end;<br> &nbsp;finally<br> &nbsp; &nbsp;UserSections.Free;<br> &nbsp;end;<br>end;<br><br>procedure TMainForm.GetUserList(var AStringList: TStringList);<br>begin<br> &nbsp;UserData.ReadSections(AStringList);<br> &nbsp;if AStringList.IndexOf('Parameters') &amp;lt;&amp;gt; -1 then<br> &nbsp; &nbsp;AStringList.Delete(AStringList.IndexOf('Parameters'));<br>end;<br>procedure TMainForm.FormCreate(Sender: TObject);<br>begin<br> &nbsp;UserData := TIniFile.Create(ExtractFilePath(paramstr(0)) + UserFile);<br> &nbsp;pShMem := nil;<br> &nbsp;ReadData;<br>end;<br><br>procedure TMainForm.FormDestroy(Sender: TObject);<br>begin<br> if UserData &amp;lt;&amp;gt; nil then<br> &nbsp; &nbsp;UserData.Free;<br>end;<br><br>procedure TMainForm.lvUserListClick(Sender: TObject);<br>begin<br> &nbsp;if lvUserList.Selected &amp;lt;&amp;gt; nil then<br> &nbsp;begin<br> &nbsp; &nbsp;SetValues(lvUserList.Selected);<br> &nbsp;end;<br>end;<br>procedure TMainForm.SetValues(AListItem: TListItem);<br>begin<br> &nbsp;edtName.Text := AListItem.SubItems.Strings[0];<br> &nbsp;if AListItem.SubItems.Strings[1] = '男' then<br> &nbsp; &nbsp;rbtnMan.Checked := True<br> &nbsp;else<br> &nbsp; &nbsp;rbtnWoman.Checked := True;<br> &nbsp;edtAge.Text := AListItem.SubItems.Strings[2];<br> &nbsp;edtPaper.Text := AListItem.SubItems.Strings[3];<br>end;<br>procedure TMainForm.WriteData;<br>const SexStr: array[Boolean] of string = ('女', '男');<br>begin<br> &nbsp;UserData.WriteString(edtName.Text, '性别', SexStr[rbtnMan.Checked]);<br> &nbsp;UserData.WriteInteger(edtName.Text, '年龄', UpDown1.Position);<br> &nbsp;UserData.WriteString(edtName.Text, '证件号', edtPaper.Text);<br>end;<br>procedure TMainForm.btnSaveClick(Sender: TObject);<br>begin<br> &nbsp;if Trim(edtName.Text) &amp;lt;&amp;gt; '' then<br> &nbsp;begin<br> &nbsp; &nbsp;WriteData;<br> &nbsp; &nbsp;ReadData;<br> &nbsp;end;<br><br>end;<br><br>procedure TMainForm.btnDeleteClick(Sender: TObject);<br>begin<br> &nbsp;UserData.EraseSection(edtName.Text);<br> &nbsp;ReadData;<br>end;<br><br>procedure TMainForm.btnSaveParamClick(Sender: TObject);<br>begin<br> &nbsp;UserData.WriteString('Parameters', 'WindowName', edtWindowName.Text);<br> &nbsp;UserData.WriteInteger('Parameters', 'NameIndex', UpDown2.Position);<br> &nbsp;UserData.WriteInteger('Parameters', 'ManIndex', UpDown3.Position);<br> &nbsp;UserData.WriteInteger('Parameters', 'WomanIndex', UpDown4.Position);<br> &nbsp;UserData.WriteInteger('Parameters', 'AgeIndex', UpDown5.Position);<br> &nbsp;UserData.WriteInteger('Parameters', 'PaperIndex', UpDown6.Position);<br> &nbsp;UserData.WriteBool('Parameters', 'AutoPopup', chbxAutoPopup.Checked);<br> &nbsp;UserData.WriteBool('Parameters', 'AutoInput', chbxAutoInput.Checked);<br>end;<br><br>procedure TMainForm.lvUserListDblClick(Sender: TObject);<br>begin<br> &nbsp;if lvUserList.Selected &amp;lt;&amp;gt; nil then<br> &nbsp;begin<br> &nbsp; &nbsp;lvUserList.ItemFocused.Checked := not lvUserList.ItemFocused.Checked;<br> &nbsp; &nbsp;if lvUserList.ItemFocused.Checked then<br> &nbsp; &nbsp; &nbsp;btnSend.Click;<br> &nbsp;end;<br>end;<br><br>procedure TMainForm.btnHideClick(Sender: TObject);<br>begin<br> &nbsp;GroupBox1.Visible := not GroupBox1.Visible;<br> &nbsp;if GroupBox1.Visible then<br> &nbsp; &nbsp;btnHide.Caption := '隐藏'<br> &nbsp;else<br> &nbsp; &nbsp;btnHide.Caption := '显示';<br>end;<br><br>procedure TMainForm.chbxAutoPopupClick(Sender: TObject);<br>var WndName: TChars;<br>begin<br> &nbsp;StrPCopy(WndName, Trim(MainForm.edtWindowName.Text));<br> &nbsp;if chbxAutoPopup.Checked then<br> &nbsp;begin<br> StartHook(MainForm.Handle, MessageID, WndName);<br> end<br> else<br> StopHook;<br> end;<br><br>procedure TMainForm.FormCloseQuery(Sender: TObject; var CanClose: Boolean);<br>begin<br> &nbsp;// &nbsp;FindThread.Terminate;<br>end;<br><br>procedure TMainForm.WndProc(var Messages: TMessage);<br>begin<br> &nbsp;if pShMem = nil then<br> &nbsp;begin<br> &nbsp; &nbsp;hMappingFile := OpenFileMapping(FILE_MAP_WRITE, False, MappingFileName);<br> &nbsp; &nbsp;if hMappingFile = 0 then Exception.Create('不能建立共享内存!');<br> &nbsp; &nbsp;pShMem := MapViewOfFile(hMappingFile, FILE_MAP_WRITE or FILE_MAP_READ, 0, 0,<br> &nbsp; &nbsp; &nbsp;0);<br> &nbsp; &nbsp;if pShMem = nil then<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp;CloseHandle(hMappingFile);<br> &nbsp; &nbsp; &nbsp;Exception.Create('不能映射共享内存!');<br> &nbsp; &nbsp;end;<br><br> &nbsp;end;<br> &nbsp;if pShMem = nil then Exit;<br> &nbsp;if Messages.Msg = MessageID then<br> &nbsp;begin<br> &nbsp; &nbsp;if chbxAutoPopup.Checked then<br> &nbsp; &nbsp; &nbsp;if not IsWindowVisible(MainForm.Handle) then<br> &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp;MainForm.Show;<br> &nbsp; &nbsp; &nbsp; &nbsp;// SetForegroundWindow(MainForm.Handle);<br> &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp;SetWindowPos(MainForm.Handle, HWND_TopMost, 0, 0, 0, 0,<br> &nbsp; &nbsp; &nbsp;SWP_NoMove or SWP_NoSize or SWP_ShowWindow);<br> &nbsp; &nbsp;// SetWindowPos(MainForm.Handle, HWND_NOTOPMOST, 0, 0, 0, 0,<br> &nbsp; &nbsp;// SWP_NoMove or SWP_NoSize or SWP_ShowWindow);<br> &nbsp; &nbsp;{ SetWindowPos(MainForm.Handle,<br> &nbsp; &nbsp; &nbsp; HWND_TopMost,<br> &nbsp; &nbsp; &nbsp; 0,<br> &nbsp; &nbsp; &nbsp; 0,<br> &nbsp; &nbsp; &nbsp; 0,<br> &nbsp; &nbsp; &nbsp; 0,<br> &nbsp; &nbsp; &nbsp; SWP_NOACTIVATE or SWP_NoMove or SWP_NoSize or SWP_ShowWindow);<br> &nbsp; &nbsp;}<br><br> &nbsp; &nbsp; &nbsp;// BringWindowToTop(MainForm.Handle);<br> &nbsp; &nbsp; &nbsp;// SetActiveWindow(MainForm.Handle);<br> &nbsp;end; // else<br> &nbsp;inherited;<br>end;<br>procedure TMainForm.FormClose(Sender: TObject; var Action: TCloseAction);<br>begin<br> &nbsp;StopHook;<br>end;<br><br>end.<br><br><br><br>
 
自定义一个消息发送贝,
 
EnumChildWindows得到所有类名为TEdit的句柄,有了窗口句柄GetWindowRect就得到了窗口的屏幕位置矩形R,用Windows.ScreenToClient(hParent, R.TopLeft)就得到了相对于其父窗口的左上角坐标位置啦。有了位置应该就能判断是对应哪个Edit了吧?!
 
多人接受答案了。
 
后退
顶部