GetSaveFileName彈出文件選擇框居中顯示

傳入的結構體參數:

OPENFILENAME ofn;

ZeroMemory(&ofn, sizeof(ofn));

ofn.lpstrFile = 初始文件名;
ofn.nMaxFile = MAX_PATH;
ofn.lpstrFilter = _T("Text Files(*.txt)|*.txt|All Files(*.*)|*.*||");
ofn.lpstrDefExt = _T("txt");
ofn.lpstrTitle = _T("保存爲");
ofn.hInstance = GetModuleHandle(NULL);
ofn.Flags = OFN_HIDEREADONLY | OFN_ENABLEHOOK | OFN_EXPLORER;
ofn.hwndOwner = 父窗口句柄;
ofn.FlagsEx = OFN_EX_NOPLACESBAR;
ofn.lpfnHook = OFNHookProc;
ofn.lpstrInitialDir = prtMainFrame->InitPaht.c_str();

ofn.lStructSize = sizeof(OPENFILENAME); 

// 讓窗口居中顯示,主要是設置Hook函數,在Flags標誌中必須設置OFN_ENABLEHOOK | OFN_EXPLORER,具體的作用請看msdn

下面看一下Hook函數的編寫方法:

UINT_PTR CALLBACK OFNHookProc(HWND hdlg, UINT uiMsg, WPARAM wParam, LPARAM lParam)
{
if (uiMsg == WM_INITDIALOG)
{
RECT rcParent;
HWND hWndParent = GetParent(hdlg);
GetClientRect(hWndParent, &rcParent);
RECT rcHaotiMainFrame;
GetClientRect(按哪個窗口劇中的窗口句柄, &rcHaotiMainFrame);
POINT ptParentInScreen;
ptParentInScreen.x = rcParent.left;
ptParentInScreen.y = rcParent.top;
::ClientToScreen(hWndParent, (LPPOINT)&ptParentInScreen);
SetWindowPos(hWndParent, NULL,
ptParentInScreen.x + (rcHaotiMainFrame.right - rcHaotiMainFrame.left - (rcParent.right - rcParent.left)) / 2,
ptParentInScreen.y + (rcHaotiMainFrame.bottom - rcHaotiMainFrame.top - (rcParent.bottom - rcParent.top)) / 2,
0, 0, SWP_NOZORDER | SWP_NOSIZE);
}
UNREFERENCED_PARAMETER(wParam);
return 1;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章