Archive for December 2007
Safe memory deletion
Posted December 31, 2007
on:- In: C/C++
- Leave a Comment
Always Set NULL when delete a memory. Otherwise the program may crash when the variable further referenced.
delete m_pComboArgs; m_pComboArgs = NULL;
CBN_SELCHANGE and CBN_CLOSEUP message handler of CComboBox class made conflict
Posted December 31, 2007
on:- In: MFC
- Leave a Comment
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.
- In: MFC
- 2 Comments
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; }
- In: MFC
- Leave a Comment
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); }
- In: MFC
- Leave a Comment
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); }