A Journey 2 Eternity

Archive for December 2008

void CMyComboBox::OnPaint()
{
	CPaintDC dc(this);
	CRect rect;
	GetClientRect(rect);

	// draw borders
	dc.Draw3dRect(rect, ::GetSysColor(COLOR_3DSHADOW), ::GetSysColor(COLOR_BTNHIGHLIGHT));
	rect.DeflateRect(1,1);
	
	dc.Draw3dRect(rect, ::GetSysColor(COLOR_3DDKSHADOW), ::GetSysColor(COLOR_BTNFACE));
	rect.DeflateRect(1,1);

	if( (GetStyle() & 0x0000000f) > CBS_SIMPLE) {
		CRect rectBtn=rect;
		rectBtn.left = rectBtn.right – ::GetSystemMetrics(SM_CXHTHUMB);

		BOOL bPressed = FALSE;
		if(GetKeyState(VK_LBUTTON) < 0) {
			CPoint point;
			::GetCursorPos(&point);
			ScreenToClient(&point);

			if(rectBtn.PtInRect(point)) {
				bPressed=TRUE;
			}
		}

		// draw button
		UINT uState = DFCS_SCROLLDOWN;
		uState |= (bPressed ? DFCS_FLAT | DFCS_PUSHED : 0);
		
		dc.DrawFrameControl(rectBtn, DFC_SCROLL, uState );
		rect.right = rectBtn.left;
	}

	if((GetStyle()&0x0000000f)==CBS_DROPDOWNLIST) {
		CString sText;
		GetWindowText(sText);
		if(sText.IsEmpty()) {
			dc.FillSolidRect(rect, ::GetSysColor(COLOR_WINDOW));
		}
	} else {
		// draw border around edit control
		dc.Draw3dRect(rect, ::GetSysColor(COLOR_BTNHIGHLIGHT), ::GetSysColor(COLOR_BTNHIGHLIGHT));
	}
}
int CMyComboBox::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	int nRet = rtn = CComboBox::OnCreate(lpCreateStruct);
	
	COMBOBOXINFO pcb;
	pcb.cbSize = sizeof(COMBOBOXINFO);
	if(GetComboBoxInfo(&pcb)) {
		CEdit* pEdit = (CEdit*)FromHandle(pcb.hwndItem);
		if(pEdit != NULL) {
			m_rcComboEdit = pcb.rcItem;
			m_rcComboEditUnionBtn.UnionRect(ceRect,&pcb.rcButton);
		}
	}

	return nRet;
}

void CMyComboBox::HideButton(BOOL bHide)
{
	COMBOBOXINFO pcb;
	pcb.cbSize = sizeof(COMBOBOXINFO);

	if(GetComboBoxInfo(&pcb)) {
		CEdit* pEdit = (CEdit*)FromHandle(pcb.hwndItem);
		if(pEdit != NULL) {
			if(bHide) {
				pEdit->MoveWindow(m_rcComboEdit.left, 	m_rcComboEdit.top, m_rcComboEditUnionBtn.Width(), m_rcComboEdit.Height());
			} else {
				pEdit->MoveWindow(m_rcComboEdit.left, m_rcComboEdit.top, m_rcComboEdit.Width(), m_rcComboEdit.Height());
			}
		}
	}

	RedrawWindow();
}

In MFC, you can simply subclass the combo-box and override the WM_CTLCOLOR message handler. This handler is used because Windows sends a message to set the colors for each of the children of the combo (the edit and the list-box), and you can easily grab the list-box HWND at this time.

HBRUSH CMyComboBox::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
	if (nCtlColor == CTLCOLOR_LISTBOX) {
		HWND hwndList = pWnd->GetSafeHwnd() ;
		if ( hwndList != NULL ) {
			CRect rectList ;
			::GetWindowRect( hwndList, rectList ) ;
			::MoveWindow( hwndList, rectList.left, rectList.top, (rectList.right – rectList.left + EXTRA_WIDTH), rectList.bottom – rectList.top + EXTRA_HEIGHT, TRUE ) ;
		}
	}

	return CComboBox::OnCtlColor( pDC, pWnd, nCtlColor ) ;
}
/*
 * Purpose: Set the proper number of lines in a drop-down list or combo box.
 * Description : Resizes the combo box window to fit the proper number of lines.
*/

void Set_DropDownSize(CComboBox& box, UINT nLinesToDisplay)
{
	CRect cbSize;
	box.GetClientRect(cbSize);

	int nHeight = box.GetItemHeight(-1); // start with size of the edit-box portion
	nHeight += box.GetItemHeight(0) * nLinesToDisplay; // add height of lines of text

	// Add the height of the border of the edit box
	nHeight += GetSystemMetrics(SM_CYEDGE) * 2; // top & bottom edges

	// Add the height of the border of the drop-down box
	nHeight += GetSystemMetrics(SM_CYEDGE) * 2; // top & bottom edges

	// Set the size of the window
	box.SetWindowPos(NULL, 0, 0,
			cbSize.right, nHeight, // existing width, new height
			SWP_NOMOVE | SWP_NOZORDER // don’t move box or change z-ordering.
		);
}

Step 1:

Add the following:

#define _ATL_CSTRING_EXPLICIT_CONSTRUCTORS

#include < afxwin.h >

#ifndef _AFX_NO_OLE_SUPPORT

#include < afxdisp.h > // MFC Automation classes

#endif // _AFX_NO_OLE_SUPPORT

This block should be added before #define _ATL_ALL_WARNINGS

Step 2:

In Configuration properties -> General -> Projects Defaults -> Use of MFC, Select “Use MFC in a Shared DLL”

Step 3:

In the Configuration properties -> C/C++ ->Preprocessor Options section of the Compiler Settings if you are using _USRDLL and _AFXDLL together, try to remove _USRDLL and rebuild again.

We can just add ATL dialog in our project. But there will be some problems such as we can’t add Control variables.

// With standard string
char szAppPath[MAX_PATH] = “”;
char szAppDirectory[MAX_PATH] = “”;
::GetModuleFileName(0, szAppPath, sizeof(szAppPath) – 1);

// Extract directory
strncpy(szAppDirectory, szAppPath, strrchr(szAppPath, ‘\\’) – szAppPath);
szAppDirectory[strlen(szAppDirectory)] = ”;
void CTabbedViewPrjView::OnInitialUpdate()
{
	CView::OnInitialUpdate();
	GetParentFrame()->ShowWindow(SW_MAXIMIZE);
}

Pages

Categories

December 2008
M T W T F S S
« Sep   Apr »
1234567
891011121314
15161718192021
22232425262728
293031  

Blog Stats

  • 27,261 hits