11

我在 Microsoft 文档中找不到任何内容,所以我只是想知道是否有人知道,是否可以指定 a 的起始位置IFileDialog?具体来说,我希望第一次打开对话框时,它会在父窗口的中心打开。

WM_*除了以某种方式连接到底层消息之外,我没有看到一种直接的方法来做到这一点。

是否可以使用类似的东西SetWindowPos

4

2 回答 2

6

shell 的 IFileDialog 实现支持IOleWindow 接口(注意它似乎没有记录......)。但是必须先打开对话框,然后才能获得它的窗口句柄。

所以诀窍是使用IFileDialogEvents接口订阅对话框的事件,获取窗口句柄并移动它,如下面的示例所示。我选择 OnSelectionChange 是因为它似乎是个好地方。当然这是要适应的(你不想在每次选择改变时移动窗口......)。

class Events : public IFileDialogEvents
{
  // poor man's IUnknown implementation :-)
  STDMETHODIMP QueryInterface(REFIID riid, void **ppvObject) { *ppvObject = NULL; return E_NOINTERFACE; }
  STDMETHODIMP_(ULONG) AddRef() { return 1; };
  STDMETHODIMP_(ULONG) Release() { return 1; }
  STDMETHODIMP OnFileOk(IFileDialog *pfd) { return S_OK; }
  STDMETHODIMP OnFolderChanging(IFileDialog *pfd, IShellItem *psiFolder) { return S_OK; }
  STDMETHODIMP OnShareViolation(IFileDialog *pfd, IShellItem *psi, FDE_SHAREVIOLATION_RESPONSE *pResponse) { return S_OK; }
  STDMETHODIMP OnTypeChange(IFileDialog *pfd) { return S_OK; }
  STDMETHODIMP OnOverwrite(IFileDialog *pfd, IShellItem *psi, FDE_OVERWRITE_RESPONSE *pResponse) { return S_OK; }
  STDMETHODIMP OnFolderChange(IFileDialog *pfd) { return S_OK; }

  STDMETHODIMP OnSelectionChange(IFileDialog *pfd) {
    IOleWindow *window;
    if (SUCCEEDED(pfd->QueryInterface(&window)))
    {
      HWND hwnd;
      if (SUCCEEDED(window->GetWindow(&hwnd)))
      {
        MoveWindow(hwnd, 0, 0, 600, 600, FALSE);
      }
      window->Release();
    }
    return S_OK;
  }
};

int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE, PWSTR pCmdLine, int nCmdShow)
{
  CoInitialize(NULL);
  IFileOpenDialog *dlg;
  if (SUCCEEDED(CoCreateInstance(CLSID_FileOpenDialog, NULL, CLSCTX_ALL, IID_PPV_ARGS(&dlg))))
  {
    DWORD cookie;
    Events *evts = new Events();
    dlg->Advise(evts, &cookie);
    dlg->Show(NULL);
    dlg->Unadvise(cookie);
    dlg->Release();
    delete evts;
  }
  CoUninitialize();
}
于 2018-09-29T08:08:38.400 回答
2

IFileDialog不提供控制对话框位置的方法,但这可以通过子类化对话框来实现。

WM_WINDOWPOSCHANGING消息是:

由于调用 SetWindowPos 函数或另一个窗口管理函数,发送到其大小、位置或 Z 顺序中的位置即将更改的窗口。

窗口通过其 WindowProc 函数接收此消息。

因此,如果我们截获此消息,我们可以在窗口移动到那里之前将其参数更改为指向我们想要的位置。我们可以通过子类化对话框来做到这一点,它用我们自己的窗口过程更改对话框的窗口过程。我们的自定义窗口过程可能如下所示:

LRESULT MyWndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    if (msg == WM_WINDOWPOSCHANGING)
    {
        WINDOWPOS* wp = (WINDOWPOS*)lParam;
        wp->x = 100;
        wp->y = 100;
    }
    return CallWindowProc(owp, hwnd, msg, wParam, lParam);
}

对话框是SetWindowLong函数的子类。但是,为了使用该功能,我们需要知道对话框的HWND. 反过来,我们可以通过设置一个临时挂钩来获取对话框的句柄,该挂钩SetWindowsHookEx将匹配对话框的标题,例如,并在子类化与给定标题匹配的窗口时删除自身:

LRESULT CALLBACK GetMsgProc(
    _In_ int    code,
    _In_ WPARAM wParam,
    _In_ LPARAM lParam
    )
{
    if (code == HC_ACTION)
    {
        MSG* msg = (MSG*)lParam;
        WCHAR title[120];
        GetWindowText(msg->hwnd, title, sizeof(title));
        if (lstrcmpW(title, L"MyTest01") == 0)
        {
            if (sizeof(long long) == 8)
                owp = (WNDPROC)SetWindowLongPtr(msg->hwnd, GWLP_WNDPROC, (LONG_PTR)MyWndProc);
            else
                owp = (WNDPROC)SetWindowLong(msg->hwnd, GWLP_WNDPROC, (LONG_PTR)MyWndProc);
            LRESULT r = CallNextHookEx(hhk, code, wParam, lParam);
            UnhookWindowsHookEx(hhk);
            return r;
        }
    }
    return CallNextHookEx(hhk, code, wParam, lParam);
}

整个例子:

#include <windows.h>
#include <shobjidl.h> 

HHOOK hhk = NULL;
WNDPROC owp = NULL;

LRESULT MyWndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    if (msg == WM_WINDOWPOSCHANGING)
    {
        WINDOWPOS* wp = (WINDOWPOS*)lParam;
        wp->x = 100;
        wp->y = 100;
        (WNDPROC)SetWindowLongPtr(hwnd, GWLP_WNDPROC, (LONG_PTR)owp);
    }
    return CallWindowProc(owp, hwnd, msg, wParam, lParam);
}

LRESULT CALLBACK GetMsgProc(
    _In_ int    code,
    _In_ WPARAM wParam,
    _In_ LPARAM lParam
    )
{
    if (code == HC_ACTION)
    {
        MSG* msg = (MSG*)lParam;
        WCHAR title[120];
        GetWindowText(msg->hwnd, title, sizeof(title));
        if (lstrcmpW(title, L"MyTestIFileOpenDialog01") == 0)
        {
            if (sizeof(long long) == 8)
                owp = (WNDPROC)SetWindowLongPtr(msg->hwnd, GWLP_WNDPROC, (LONG_PTR)MyWndProc);
            else
                owp = (WNDPROC)SetWindowLong(msg->hwnd, GWLP_WNDPROC, (LONG_PTR)MyWndProc);
            LRESULT r = CallNextHookEx(hhk, code, wParam, lParam);
            UnhookWindowsHookEx(hhk);
            return r;
        }
    }
    return CallNextHookEx(hhk, code, wParam, lParam);
}

int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE, PWSTR pCmdLine, int nCmdShow)
{
    HRESULT hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED |
        COINIT_DISABLE_OLE1DDE);
    if (SUCCEEDED(hr))
    {
        IFileOpenDialog *pFileOpen;

        // Create the FileOpenDialog object.
        hr = CoCreateInstance(CLSID_FileOpenDialog, NULL, CLSCTX_ALL,
            IID_IFileOpenDialog, reinterpret_cast<void**>(&pFileOpen));

        if (SUCCEEDED(hr))
        {
            hhk = SetWindowsHookEx(WH_GETMESSAGE, GetMsgProc, NULL, GetCurrentThreadId());
            pFileOpen->SetTitle(L"MyTestIFileOpenDialog01");
            // Show the Open dialog box.
            hr = pFileOpen->Show(NULL);

            // Get the file name from the dialog box.
            if (SUCCEEDED(hr))
            {
                IShellItem *pItem;
                hr = pFileOpen->GetResult(&pItem);
                if (SUCCEEDED(hr))
                {
                    PWSTR pszFilePath;
                    hr = pItem->GetDisplayName(SIGDN_FILESYSPATH, &pszFilePath);

                    // Display the file name to the user.
                    if (SUCCEEDED(hr))
                    {
                        MessageBox(NULL, pszFilePath, L"File Path", MB_OK);
                        CoTaskMemFree(pszFilePath);
                    }
                    pItem->Release();
                }
            }
            pFileOpen->Release();
        }
        CoUninitialize();
    }
    return 0;
}
于 2018-09-28T16:09:08.070 回答