Centering derived CFileDialog with Hook function enabled.
Posted on 2004-10-02
My problem is that if you define a hook function for a CFileDialog object, I can not figure out how to convert the HWND parameter to a CWnd parameter and call the CenterWindow function.
Here is how I create my File Dialog object…
CFileDialog cDlg(TRUE, "txt", NULL,
strFilter, pFrameWnd ); // pFrameWnd is set to my MDI Frame
cDlg.m_ofn.lStructSize = sizeof(OPENFILENAME);
cDlg.m_ofn.hInstance = AfxGetInstanceHandle();
cDlg.m_ofn.nMaxCustFilter = 0;
cDlg.m_ofn.nFilterIndex = 0;
cDlg.m_ofn.lpstrFile = (LPSTR)fileName;
cDlg.m_ofn.nMaxFile = sizeof(fileName);
cDlg.m_ofn.lpstrFileTitle = NULL;
cDlg.m_ofn.nMaxFileTitle = 0;
cDlg.m_ofn.lpstrInitialDir = NULL;
cDlg.m_ofn.lpstrTitle = NULL;
cDlg.m_ofn.nFileOffset = 0;
cDlg.m_ofn.nFileExtension = 0;
cDlg.m_ofn.lCustData = 0;
cDlg.m_ofn.lpTemplateName = NULL;
cDlg.m_ofn.lpfnHook = FileOpenHookFunction;
cDlg.DoModal(); // I am not testing the return value yet (testing now).
Here is my hook function…
UINT CALLBACK FileOpenHookFunction( HWND hdlg, UINT uiMsg, WPARAM wParam, LPARAM lParam )
CModelXApp* pApp = (CModelXApp *) AfxGetApp();
CWnd* pWnd = CWnd::FromHandlePermanent( hdlg );
switch ( uiMsg )
lpon = (LPOFNOTIFY)lParam;
if ((pWnd != NULL) && (lpon->hdr.code == CDN_INITDONE))
// Redraw views so that when moving the file open dialog box, there is not gray area.
if ((pApp != NULL) && (pApp->m_pMainFrameWindow != NULL))
I have tested in debug mode and the function is not receiving the CDN_INITDONE message. What do I have to do in order to receive this message. According to the MFC documentation, all you have to specify is the OFN_EXPLORER style which I do, but I still am not receiving the CDN_INITDONE message.
Also, if you define a derived class for the CFileDialog class, you can not use a hook function because all messages will be sent to the hook function and not the message functions you have overridden. I kind of need to use a hook function because I want to be able to redraw the views underneath the file dialog before it is displayed (WM_PAINT and WM_SHOWWINDOW) – if I do not, they will all be dark gray. I have tried to override the OnPaint and OnShowWindow functions with a class derived from CFileDialog and not use a hook function and redraw my views with my OnRender() function, but the views keep getting erased (dark-gray).
As it is now, the messages WM_PAINT and WM_SHOWWINDOW work fine inside the hook function (my views are updated before the file dialog window is shown). But I can not get the file dialog window to be centered.
If you know how to fix this, I would appreciate it – I would also like to know if I am setting pWnd correctly in the hook function …CWnd* pWnd = CWnd::FromHandlePermanent( hdlg ).