VisualCpp.org Collection of Visual C++ Stuffs

18May/07Off

How to add taskbar button in modeless windows?

Yesterday I have included a Model less dialog in my project, for some online data display. After initial Unit Testing , I required Task bar button for my model less dialog because when executed because it always go back in background, whenever i click on my main application window and i can't set always on top style as it can create problem. only solution i have to provide it with Taskbar button so that by clicking on Taskbar button, it get focus. for achieving it, you just have to set Desktop Window as parent of model less window. here CWnd::GetDesktopWindow() api come very handy!

CWnd *pDeskWnd= GetDesktopWindow();
CMyModellessDialog *pMyModellessDialog = new CMyModellessDialog ();
pMyModellessDialog ->Create(IDD_DIALOG_MODELLESS,pDeskWnd);
pMyModellessDialog ->ShowWindow(SW_SHOW);

PPS:- Don't forget to delete the pMyModellessDialog pointer afterward!

17May/07Off

How to create Dialog which is initially inactive?

Many situation come when you required your MFC dialog created initially in active or don't take keyboard focus from other window..humm don't get worryi will tll you the solution.... and here is solution :) , you just have to return FALSE instead of true from OnInitDialog function to create Dialog initially inactive..

BOOL CMyDialog::OnInitDialog()
{
return FALSE;
}

6May/07Off

What is last message posted when dialog is destroyed?

WM_NCDESTROY  message is posted last when the dialog is destroyed, this very handy message when come modeless dialog box, you can set notification from this  message to notify main window that your modeless dialog box is destroyed!

e.g.   let   CMainWnd is your parent window and CModlessDlg is  dialog box, m_IsModDlgOpen boolean variable declared in CMainWnd

DECLARE_MESSAGE_MAP(CMainWnd,CDialog)
.......
ON_MESSAGE(UWM_MODLESSDESTROYED,OnModLessDestroyed)
END_MESSAGE_MAP()

void CMainWnd::CreateModelessDlg()
{
if(m_IsModDlgOpen)
{
MessageBox("Modeless Dlg already open");
return;
}

CModlessDlg *pDlg = new CModlessDlg (IDC_DLGTEMPLATE,NULL);
pDlg->ShowWindow(SW_SHOW);
m_IsModDlgOpen = true;
}

LRESULT CMainWnd::OnModLessDestroyed(WPARAM wParam,LPARAM lParam)
{
m_IsModDlgOpen = true;
return 0;
}

// CModlessDlg class

void CModlessDlg ::OnNCDestory()
{
CDialog::OnNCDestory();
:: PostMessage(NULL,UWM_MODLESSDESTROYED,0,0)
delete this;
}

In brief when ever the modeless dialog get destoyed it post message to main window regarding it closure and and delete this will delete the memory associated with itself

Filed under: Dialog Based, MFC Comments Off
16Apr/07Off

I have a dialog-based application and want the dialog hidden on startup?

In VS 2005, MFC is now sending two WM_WINDOWPOSCHANGING messages to newly created dialogs. Also, if your dialog creates one or more child dialogs in CDialog::OnInitDialog, you'll receive the message from all of them. The code given should be updated to look like:

void CMyDlgBasedAppDlg::OnWindowPosChanging(WINDOWPOS *pWP)
{
    CDialog::OnWindowPosChanging(pWP);
    if (pWP->hwnd == m_hWnd && pWP->flags & SWP_SHOWWINDOW)
    {
              if (++m_nShowCount <= 2)
             {
                pWP->flags &= ~SWP_SHOWWINDOW;
             }
     }
 }

---------------------------------------------------------------
Tip taken from codeproject.com, tip courtesy :- Mr. Rick Murtagh

Filed under: Dialog Based Comments Off
9Apr/07Off

How do I prevent the escape key, when pressed, from closing a dialog window?

Simply override the CYourDialog::OnCancel() virtual function, but this will also block closing from cross button present on system menu.. now to achieve both functionality do this:-

// Called when user clicks the close button
void CMyDialog::OnClose()
{
CDialog::OnCancel();
}

// called when user presses ESC button or clicks IDCANCEL button
void CMyDialog::OnCancel()
{
// Do nothing
}

4Mar/07Off

How can I prevent a Dialog from resize and move?

The WM_GETMINMAXINFO message is sent to a window when the size or position of the window is about to change.
An application can use this message to override the window's default maximized size and position, or its default minimum or maximum tracking size.

To Limit Dialog to 100*100 Size

void CVCppDlg::OnGetMinMaxInfo(MINMAXINFO FAR* pMMI)
{
lpMMI->ptMaxSize.x=100;
lpMMI->ptMaxSize.y=100;
CDialog::OnGetMinMaxInfo(lpMMI);
}

Filed under: Dialog Based Comments Off