获得其他程序控件中的信息,尤其跨进程,这个着实让我头疼捯饬了一阵,最后才明白只能通过WM_GETTEXT消息获取文本。
SendMessage(h, WM_GETTEXT, 260, Longint(@Caption));(h:要获取信息的控件句柄;Caption: array [0 .. MAX_PATH] of Char;)此消息即发送给系统并获取控件文本信息至Caption里。
以下是自己的代码:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 procedure TSSoUtils.InsertHwnd(h: HWND);
2 var
3 level, Num: integer;
4 Y: integer;
5 str: string;
6 ParentWnd: HWND;
7 ClassName: array [0 .. MAX_PATH] of Char;
8 Caption: array [0 .. MAX_PATH] of Char;
9 ParentClassName: array [0 .. MAX_PATH] of Char;
10 ParentCaption: array [0 .. MAX_PATH] of Char;
11 begin
12
13 Timer1.Enabled := FALSE;
14 Timer1.Enabled := TRUE;
15
16 // Sleep(100000);
17 Timer1.Interval := 2000;
18
19 Num := GetParents(h);
20 wnd_x[Num].level := GetNum(h); // 获取位于当前层的位置
21 Y := wnd_x[Num].level;
22 ParentWnd := GetParent(h);
23 GetClassName(h, ClassName, sizeof(ClassName));
24 // GetWindowText(h, Caption, sizeof(Caption));
25 GetClassName(ParentWnd, ParentClassName, sizeof(ParentClassName));
26 // GetWindowText(ParentWnd, ParentCaption, sizeof(ParentCaption));
27
28 SendMessage(h, WM_GETTEXT, 260, Longint(@Caption));
29 // 获得其他程序控件中的信息只能通过WM_GETTEXT消息获取文本
30 SendMessage(ParentWnd, WM_GETTEXT, 260, Longint(@ParentCaption));
31
32 OutputDebugString(pchar('Caption00: ' + Caption));
33 OutputDebugString(pchar('ClassName00: ' + ClassName));
34 OutputDebugString(pchar('ParentCaption00: ' + ParentCaption));
35 OutputDebugString(pchar('ParentClassName00: ' + ParentClassName));
36
37 InsertOpe(Num, Y, h, Caption, ClassName, ParentCaption, ParentClassName);
38
39 wnd_x[Num].Wnd_y[Y].h := h;
40
41 wnd_x[Num].level := wnd_x[Num].level - 1; str := string(Caption) + '' + string(ClassName) + '' + 'level:' +
42 inttostr(Num) + ' Y:' + inttostr(Y) + ' HWND:' + inttostr(h);
43 OutputDebugString(pchar(str));
44 end;
以下是参考的代码:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <windows.h>
2 BOOL CALLBACK EnumChildProc(HWND hWnd,LPARAM lParam);
3 int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)
4 {
5 char className[]="notepad";
6 HWND hWnd=::FindWindow(className,NULL);
7 if(hWnd)
8 {
9 ::EnumChildWindows(hWnd,EnumChildProc,0);
10 return 0;
11 }
12 MessageBox(NULL,"fail!","fail",MB_OK);
13 return 0;
14 }
15 BOOL CALLBACK EnumChildProc(HWND hWnd,LPARAM lParam)
16 {
17 char temp1[256],temp2[256];
18 ::GetClassName(hWnd,temp1,255);
19 if(!::strcmp(temp1,"Edit"))
20 {
21 ::SendMessage(hWnd,WM_GETTEXT,sizeof(temp2)/sizeof(char),(LPARAM)temp2);//EDIT的句柄,消息,接收缓冲区大小,接收缓冲区指针
22 ::MessageBox(NULL,temp2,"get",MB_OK);
23 return 0;
24 }
25 ::wsprintf(temp2,"classname: %s",temp1);
26 MessageBox(NULL,temp2,"cwnd",MB_OK);
27 return true;
28 }