Welcome

Wheresoever you go, go with all your heart. (Confucius)

11/17/2010

[MFC] Ignore Enter and ESC key on the Dialog

If you push ENTER and ESC key on the Dialog, this Dialog is closed.
There are two solutions to solve this problem.

1. Catch the message for inputting these key.
BOOL CTestDlg::PreTranslateMessage(MSG* pMsg)
{
    if(pMsg->message == WM_KEYDOWN && pMsg->wParam == VK_RETURN) // in case of ENTER key
        pMsg->wParam = 1;
    if(pMsg->message == WM_KEYDOWN && pMsg->wParam == VK_ESCAPE) // in case of ESC key
        pMsg->wParam = 1;

    return CDialog::PreTranslateMessage(pMsg);
}

2. Override OnOK() and OnCancel().
In the head file,
private:
    virtual void OnOK(){}
    virtual void OnCancel(){}

11/16/2010

[MFC] using Formview Dialog

In many case of the form design,
you need to attach the designed form to other forms.
To achieve this purpose, we can use the formview dialog.
The formview dialog is not special.
Of options of the general dialog, you set up such as Boder = false and Style = Child.

For example, consider that IDD_FORMVIEW and IDD_FORMVIEW2 is designed.

And you want to attach these to other form views or dialogs.
The source code is the following

// CTestLocalPanel *panel;   -- class CTestLocalPanel : public CDialog for IDD_FORMVIEW
// CTestLocalPanel *panel2;   -- class CTestLocalPanel2 : public CDialog for IDD_FORMVIEW2

void CTestMFCView::OnBnClickedButton1()
{
    if( panel2 )
    {
        panel2->ShowWindow(SW_HIDE);
        panel2->DestroyWindow();
        delete panel2;
        panel2 = 0;
    }
    if( !panel )
    {
        CRect rect;
        panel = new CTestLocalPanel();
        panel->Create(IDD_FORMVIEW, this);
        panel->GetWindowRect(&rect);
        panel->MoveWindow(5, 150, rect.Width(), rect.Height()); // Moving Dialog forms into the specific position
        panel->ShowWindow(SW_SHOW);
    }

    UpdateData(false);
}
void CTestMFCView::OnBnClickedButton2()
{
    if( panel )
    {
        panel->ShowWindow(SW_HIDE);
        panel->DestroyWindow();
        delete panel;
        panel = 0;
    }
    if( !panel2 )
    {
        CRect rect;
        panel2 = new TestLocalPanel2();
        panel2->Create(IDD_FORMVIEW1, this);
        panel2->GetWindowRect(&rect);
         ->MoveWindow(5, 150, rect.Width(), rect.Height());
        panel2->ShowWindow(SW_SHOW);
    }

    UpdateData(false);
}

[MFC] Show/Hide Menubar

int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
      if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
            return -1;
   
      if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP
            | CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
            !m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
      {
            TRACE0("Failed to create toolbar\n");
            return -1;      // fail to create
      }

      if (!m_wndStatusBar.Create(this) ||
            !m_wndStatusBar.SetIndicators(indicators,
              sizeof(indicators)/sizeof(UINT)))
      {
            TRACE0("Failed to create status bar\n");
            return -1;      // fail to create
      }

      // TODO: Delete these three lines if you don't want the toolbar to
      //  be dockable
      m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
      EnableDocking(CBRS_ALIGN_ANY);
      DockControlBar(&m_wndToolBar);

      // OrgMenu is defined in the member variable of CMainFrame
      // Load menu on create
      OrgMenu.LoadMenu(IDR_MAINFRAME);
      return 0;
}

void CMainFrame::OnHide()
{
      // hide menu
      SetMenu(NULL);
}

void CMainFrame::OnShow()
{
      // show menu
      SetMenu(&OrgMenu);
}