A Journey 2 Eternity

Archive for December 2007

Always Set NULL when delete a memory. Otherwise the program may crash when the variable further referenced.

delete m_pComboArgs;
m_pComboArgs = NULL;

The MFC CComboBox is weird. The CBN_SELCHANGE and CBN_CLOSEUP message handler doesn’t call proper sequence. Sometimes the CBN_CLOSEUP call first time and later CBN_SELCHANGE. It’s made confusion. So carefully write code in those two event handler.

Tags: ,

Sometimes it is desirable to change the default font specified in dialog templates (usually “MS Sans Serif”, 8 pts.) at runtime (dynamically). For example, you may want to increase the font size to make it more readable under higher screen resolutions. MFC library contains a class CDialogTemplate, that serves exactly this purpose, but Microsoft has not bothered to include its description in standard MFC reference. This is how you can use this class in your code:

Override DoModal() function in your dialog class:

int CSimpleDialog::DoModal()
{
	CDialogTemplate dlt;
	int nResult;

	// load dialog template
	if (!dlt.Load(MAKEINTRESOURCE(CSimpleDialog::IDD))) {
		return -1;
	}

	// set your own font, for example “Arial”, 10 pts.
	dlt.SetFont(“Arial”, 10);

	// get pointer to the modified dialog template
	LPSTR pdata = (LPSTR)GlobalLock(dlt.m_hTemplate);

	// let MFC know that you are using your own template
	m_lpszTemplateName = NULL;
	InitModalIndirect(pdata);

	// display dialog box
	nResult = CDialog::DoModal();

	// unlock memory object
	GlobalUnlock(dlt.m_hTemplate);

	return nResult;
}
LOGFONT lf;
int nRet = GetObject(GetStockObject(DEFAULT_GUI_FONT), sizeof(LOGFONT), &lf);
if (nRet) {
	// Set the Default font to the Custom window control
	m_hotListCtrl.SetFont(&lf);
}

Using TrackMouseEvent is pretty simple. When the mouse enters the window you want to track, you call track mouse event telling it to inform you when the mouse leaves. When it does, it will send a WM_MOUSELEAVE message to that window.

Sample code:

// HotEdit.h
class CHotEdit : public CEdit
{
protected:
	BOOL m_bMouseTracking;
	afx_msg LRESULT OnMouseLeave(WPARAM wParam, LPARAM lParam);
};

// HotEdit.cpp
BEGIN_MESSAGE_MAP(CHotEdit, CEdit)
	ON_MESSAGE(WM_MOUSELEAVE, OnMouseLeave)
END_MESSAGE_MAP()

CHotEdit::CHotEdit()
{
	m_bMouseTracking = FALSE;
}

LRESULT CHotEdit::OnMouseLeave(WPARAM wParam, LPARAM lParam)
{
	m_bMouseTracking = FALSE;
	return TRUE;
}

void CHotEdit::OnMouseMove(UINT nFlags, CPoint point)
{
	if (!m_bMouseTracking) {
		TRACKMOUSEEVENT tme;
		tme.cbSize = sizeof(TRACKMOUSEEVENT);
		tme.dwFlags = TME_LEAVE;
		tme.hwndTrack = this->m_hWnd;

		if (::_TrackMouseEvent(&tme)) {
			m_bMouseTracking = TRUE;
		}
	}

	CEdit::OnMouseMove(nFlags, point);
}
Tags:

Pages

Categories

December 2007
M T W T F S S
 12
3456789
10111213141516
17181920212223
24252627282930
31  

Blog Stats

  • 32,540 hits