获得多个同名顶层窗口的句柄

来源:互联网 发布:淘宝外贸衣服哪里来的 编辑:程序博客网 时间:2024/06/03 19:11
以记事本作为测试。从开始菜单启动记事本6次,记事本标题应该都是"无标题 - 记事本"

新建Win32 Application工程(Hello World!)里加入如下代码:

BOOL CALLBACK lpEnumFunc(HWND hwnd,LPARAM strTarget) //添加回调函数{char strTitle[128],strOutput[128];GetWindowText(hwnd,strTitle,128);if(strcmp(strTitle,(char*)strTarget)==0){wsprintf(strOutput,"句柄:0x%x",hwnd);MessageBox(NULL,strOutput,strTitle,NULL);}return TRUE;};LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam){int wmId, wmEvent;PAINTSTRUCT ps;HDC hdc;TCHAR szHello[MAX_LOADSTRING];LoadString(hInst, IDS_HELLO, szHello, MAX_LOADSTRING);char *strTarget;switch (message) {case WM_COMMAND:wmId    = LOWORD(wParam); wmEvent = HIWORD(wParam); // Parse the menu selections:switch (wmId){case IDM_ABOUT:  //DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About);  strTarget="无标题 - 记事本";  EnumWindows(lpEnumFunc,(LPARAM)strTarget);  break;case IDM_EXIT:  DestroyWindow(hWnd);  break;default:  return DefWindowProc(hWnd, message, wParam, lParam);}break;case WM_PAINT:hdc = BeginPaint(hWnd, &ps);// TODO: Add any drawing code here...RECT rt;GetClientRect(hWnd, &rt);DrawText(hdc, szHello, strlen(szHello), &rt, DT_CENTER);EndPaint(hWnd, &ps);break;case WM_DESTROY:PostQuitMessage(0);break;default:return DefWindowProc(hWnd, message, wParam, lParam);   }   return 0;}

................................................
则点击菜单的关于时,程序采用对话框提示了6遍同名记事本窗口句柄,如图


0 0
原创粉丝点击