天天看點

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;

}

繼續閱讀