Using Ribbon Editor to design the Ribbon for your application

 

Visual Studio 2010 has integrated the new Ribbon Designer for MFC Applications. Previously I blogged about integrating ribbon with the MFC application and adding even handlers to get the notifications. But the developer must visualize the ribbon while scripting the ribbon code. But using the new Ribbon Designer, we can design ribbons designer, just like the menu designer.


Please check the following links to explore more.
How to integrate a simple ribbon with your MFC Application?
How to handle Ribbon Control Events?
Ribbon designer (MSDN)

 

Why I can’t set cursor position on a child control during OnInitDialog?

 

One of my colleague asked me. “Why I can’t set the cursor position on a button during OnInitDialog? I need to set the cursor over a button inside the dialog during startup.” The snippet looks like below

BOOL CCursorPositionDlg::OnInitDialog()
{
CDialog::OnInitDialog();
CRect rect;
	GetDlgItem( IDC_BUTTON2 )->GetWindowRect( rect );
	SetCursorPos(rect.left, rect.top );
}

The reason is, the Window initialization has not completed during OnInitDialog operation (even window is not displayed). Once after OnInitDialog returns, the dialog will be positioned, the input focus will be set the child control and finally the dialog is displayed. Also if you notice the, GetWindowRect returns incorrect window cordinate during OnInitDialog execution.

To solve this issue, it’s necessary to rely on other activation functions like WM_ACTIVATE or WM_WINDOWPOSCHANGED (only once) function

void CCursorPositionDlg::OnActivate(UINT nState, CWnd* pWndOther, BOOL bMinimized)
{
	CDialog::OnActivate(nState, pWndOther, bMinimized);

	if( WA_ACTIVE == nState )
		SetMousePosition();
}

Alternate method (not recommended)

void CCursorPositionDlg::OnWindowPosChanged(WINDOWPOS* lpwndpos)
{
	CDialog::OnWindowPosChanged(lpwndpos);

 	static bool bFirst = true;
 	if( bFirst) { PositionButton(); bFirst = false;}
}
 

How to restrict Window Movement?

 

This is beginner level post. How we can make a Window immovable?

Roughly we’ve two methods to do this.

Method #1. Handle the NCHITTEST message and ignore while user click on the caption area (Titlebar). This method doesn’t work well in Windows 7/Vista if Aero is enabled. This method works with any type of Window having titlebar. The disadvantage of this method is , the user will still able to move using keyboard, if system menu is available.

 

LRESULT CMoveWindowSampleDlg::OnNcHitTest(CPoint point)
{
    UINT nHitTest = CDialogEx::OnNcHitTest(point);

    if( HTCAPTION == nHitTest )
        nHitTest = HTNOWHERE;

    return nHitTest;
}

Method #2 – Remove the Move command from System Menu (Works with windows having system menu stle (WS_SYSMENU) ). The the Move command in the system menu will be removed in this case. Windows will internally disable the movement of this kind of Windows.

BOOL CMoveWindowSampleDlg::OnInitDialog()
{
    CDialogEx::OnInitDialog();

    CMenu* pSysMenu = GetSystemMenu(FALSE);

    if (pSysMenu != NULL)
    {
        pSysMenu->RemoveMenu( SC_MOVE, MF_BYCOMMAND );
       }
...

       return TRUE;
}

PS: Using method 2, this is the same method we use to disable the close button of a Window. For disabling a menu/titlebar button. Call EnableMenu function with relevant parameters.