【发布时间】:2015-04-28 23:30:07
【问题描述】:
这是我尝试使用钩子函数来获取对话框窗口句柄。 SetWindowPos() 和 GetLastError() 都返回正确的值,但对话窗口不受影响并显示在位置 0,0。
#include <windows.h>
#include <iostream>
static UINT_PTR CALLBACK OFNHookProc (HWND hdlg, UINT uiMsg, WPARAM wParam, LPARAM lParam) {
using namespace std;
switch (uiMsg) {
case WM_INITDIALOG: {
// SetWindowPos returns 1
cout << SetWindowPos(hdlg, HWND_TOPMOST, 200, 200, 0, 0, SWP_NOSIZE ) << endl;
// GetLastError returns 0
cout << GetLastError() << endl;
break;
}
}
return 0;
}
int main() {
OPENFILENAMEW ofn;
ZeroMemory(&ofn, sizeof(ofn));
ofn.lStructSize = sizeof(OPENFILENAMEW);
ofn.nMaxFile = MAX_PATH;
ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_ENABLEHOOK;
ofn.lpfnHook = OFNHookProc;
GetOpenFileNameW(&ofn);
return 0;
}
【问题讨论】:
-
这会谴责你使用可怕的 XP 旧版对话框
-
GetLastError的返回值在你调用它的时候是没有意义的。仅当SetWindowPos失败时,并且仅当您在失败的 API 调用之后立即调用它时,它才会报告有意义的值。穿插的流操作符可以改变调用线程记录的最后一个错误码。