欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

GetOpenFileName设置居中

程序员文章站 2022-05-02 09:54:01
...
static UINT_PTR CALLBACK OFNHookProc (HWND hdlg, UINT uiMsg, WPARAM wParam, LPARAM lParam)
{
    if ((uiMsg == WM_NOTIFY) &&
        (reinterpret_cast<OFNOTIFY*>(lParam)->hdr.code == CDN_INITDONE))
    {
        RECT r;
        int w, h;
        GetWindowRect(GetParent(hdlg), &r);
        w = r.right - r.left;
        h = r.bottom - r.top;
        GetWindowRect(GetDesktopWindow(), &r);
        int xpos = (r.right-w)/2;
        int ypos = (r.bottom-h)/2;
        SetWindowPos(GetParent(hdlg), HWND_TOPMOST, xpos, ypos, 0, 0, SWP_NOSIZE);
    }
    return 0;
}

std::string GetOpenFileName(const char* lpstrFile)
{
    string strRet;
    char szFilter[] = "All Files(*.*)\0*.*\0Txt Files(*.txt)\0*.txt\0\0";
    char szOpenFileName[MAX_PATH] = { 0 };
    if(lpstrFile != NULL)
        strcpy(szOpenFileName, lpstrFile);
    OPENFILENAMEA openFile = { 0 };
    openFile.lStructSize = sizeof(OPENFILENAME);
    openFile.hInstance = (HINSTANCE)GetModuleHandle(NULL);
    openFile.lpstrFilter = szFilter;
    openFile.lpstrFile = szOpenFileName;
    openFile.nMaxFile = MAX_PATH;
    openFile.Flags = OFN_FILEMUSTEXIST | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY | OFN_EXPLORER | OFN_ENABLEHOOK;
    openFile.lpfnHook = OFNHookProc;
    if(GetOpenFileNameA(&openFile) == TRUE)
        strRet = szOpenFileName;
    return strRet;
}

参考:https://*.com/questions/29931704/how-to-change-position-of-an-openfilename-dialog-in-windows