最近要把一个对话框关闭,类中句柄都没有,google了一下,搜到qt源码的处理方式,很受启发:
1 2 3 4 5 6 7 8 9 10 11 12 |
void QWindowsNativeFileDialogBase::close() { m_fileDialog->Close(S_OK); #ifndef Q_OS_WINCE // IFileDialog::Close() does not work unless invoked from a callback. // Try to find the window and send it a WM_CLOSE in addition. const HWND hwnd = findDialogWindow(m_title); qCDebug(lcQpaDialogs) << __FUNCTION__ << "closing" << hwnd; if (hwnd && IsWindowVisible(hwnd)) PostMessageW(hwnd, WM_CLOSE, 0, 0); #endif // !Q_OS_WINCE } |
经过测试,直接调用关闭函数是没啥效果的。只有自己通过上面查找窗口名称的方法再post关闭的msg才能关闭窗口。这么使用,也需要我们设置好窗口名称才行,减少错误关掉其他对话框的可能。各种搜不到的问题解决方法,在qt源码里搜到,还是Qt高啊!
微软官方文档关闭IFileDialog的Close方法这么写的:
Syntax
1 2 3 |
HRESULT Close( [in] HRESULT hr ); |
Parameters
- hr [in]
- Type: HRESULT
The code that will be returned by Show to indicate that the dialog was closed before a selection was made.
Return value
Type: HRESULT
If this method succeeds, it returns S_OK. Otherwise, it returns an HRESULT error code.
Remarks
An application can call this method from a callback method or function while the dialog is open. The dialog will close and the Show method will return with the HRESULT specified in hr.
If this method is called, there is no result available for the IFileDialog::GetResult or GetResults methods, and they will fail if called.