Sunday, January 27, 2008

to have controls in a toolbar in MFC

int nIdx = CommandToIndex(nToolBarBtnID);
ASSERT( nIdx >= 0 );
SetButtonInfo(nIdx, nComboID, TBBS_SEPARATOR, 50);
const int nDropHeight = 50;
CRect rect;
GetItemRect(nIdx, &rect);
rect.top = 1;
rect.bottom = rect.top + nDropHeight;
if (!m_comboBox.Create(CBS_DROPDOWN | CBS_AUTOHSCROLL | WS_TABSTOP | WS_CHILD, rect, this, nComboID))
{
TRACE0("Failed to create combobox in ToolBar\n");
return false;
}
// update the combo with a font.
m_comboFont.CreatePointFont( 80,_T("MS Sans Serif"));
m_comboBox.SetFont( &m_comboFont );
m_comboBox.EnableWindow(FALSE);
// show the combobox.
m_comboBox.ShowWindow( SW_SHOWNORMAL );
return true;

In the toolbar that you have created make a button which will be place holder for the
combo(control in this example).....

just pass the placeholders Id and a new Id for the combo box

to align toolbar side by side MFC

this is the function,just call this with the two toolbars that you have

void CMainFrame::DockControlBarLeftOf(CToolBar* Bar, CToolBar* LeftOf)
{
CRect rect;
DWORD dw;
UINT n;

// get MFC to adjust the dimensions of all docked ToolBars
// so that GetWindowRect will be accurate
RecalcLayout(TRUE);

LeftOf->GetWindowRect(&rect);
rect.OffsetRect(1,0);
dw=LeftOf->GetBarStyle();
n = 0;
n = (dw&CBRS_ALIGN_TOP) ? AFX_IDW_DOCKBAR_TOP : n;
n = (dw&CBRS_ALIGN_BOTTOM && n==0) ? AFX_IDW_DOCKBAR_BOTTOM : n;
n = (dw&CBRS_ALIGN_LEFT && n==0) ? AFX_IDW_DOCKBAR_LEFT : n;
n = (dw&CBRS_ALIGN_RIGHT && n==0) ? AFX_IDW_DOCKBAR_RIGHT : n;

// When we take the default parameters on rect, DockControlBar will dock
// each Toolbar on a seperate line. By calculating a rectangle, we
// are simulating a Toolbar being dragged to that location and docked.
DockControlBar(Bar,n,&rect);
}

and on the OnCreate of MainFrame calling is done like this

m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
EnableDocking(CBRS_ALIGN_ANY);
DockControlBar(&m_wndToolBar);


m_wndMergeToolBar.EnableDocking(CBRS_ALIGN_ANY);
EnableDocking(CBRS_ALIGN_ANY);
DockControlBarLeftOf(&m_wndMergeToolBar,&m_wndToolBar);

Monday, January 7, 2008

Diasble/Enable Menuitem in MFC

CMenu menu;
> menu.LoadMenu(IDR_MNU_THUMB);
>
> CMenu* pmnuPopup = menu.GetSubMenu(0);
>
> CWnd* pwndPopupOwner = this;
>
>
> if(...some conditions)
> {
> pmnuPopup->EnableMenuItem(ID_FILE_RESTORE,
> MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
> }
>
> pmnuPopup->TrackPopupMenu(TPM_LEFTALIGN |
> TPM_RIGHTBUTTON, point.x, point.y, pwndPopupOwner);
> }

Sunday, January 6, 2008

To remove a toolbar button programmatically?

void CMainFrame::OnDelete( )
{
m_wndToolBar.SendMessage ( TB_DELETEBUTTON, ( WPARAM ) 0 ) ;
}
Here, we have called SendMessage( ) function and passed it the message TB_DELETEBUTTON, as we want a toolbar button to be deleted and a zero-based index of the button to be deleted.

Thursday, January 3, 2008

SetFocus to an Edit Control in a Dialog MFC

n your OnInitDialog handler for the dialog, set focus to the control with
something like...

((CEdit*)GetDlgItem(IDC_PREFERRED_EDIT_BOX))->SetFocus();

or via a control mapped variable...

m_ctrlPrefferedEditBox->SetFocus();

Finally, return FALSE from the OnInitDialog handler.