Set resource handle in MFC and ATL

When working in MFC/ATL and then using a resource dll can at times cause bugs because resource handle is not properly set. For e.g. if you have a dialog resource in a dll and you instantiate a CDialog which results in an error because resource cannot be located in this application or dll since it’s in […]
Continue reading…

 

AfxGetMainWnd in worker threads

Recently I faced an issue in one of my project code bases. There is a worker thread (created using CreateThread native API) which calls AfxGetMainWnd() MFC function and since worker threads doesn’t have a main window associated with it, CWnd::Create( …WS_CHILD… ) call fails. A window created with WS_CHILD needs a parent window but AfxGetMainWnd() […]
Continue reading…

 

How to handle child window messages in a parent window?

Is there any way to handle child window messages in a parent window? Yes you can use WM_PARENTNOTFIY message or OnParentNotify MFC handler. Note that this message is sent or this function is only called when WS_EX_NOPARENTNOTIFY style is not set. A child controls notifies it’s parent window on… Creation Destruction Mouse button clicks Please […]
Continue reading…

 

How to retrieve special folder paths, for e.g. my documents?

SHGetFolderPath is the API that we use to retrieve special folder paths.  Some examples of special folders are as follows… My Documents My Pictures Windows directory System directory Program files Internet history directory Cookies directory etc… So here is a small useful function which retrieves path of any special directory documented for SHGetFolderPath API in […]
Continue reading…

 

Functions for converting strings to upper/lower case

Use following functions _strupr _strlwr std::transform – does the trick too but hard to understand [sourcecode language=’cpp’]/* STRLWR.C: This program uses _strlwr and _strupr to create  * uppercase and lowercase copies of a mixed-case string.  */ #include #include void main( void ) { char str[100] = “The String to End All Strings!”; printf( “Mixed: %sn”, […]
Continue reading…

 

Dialog does not show up after adding rich edit control!

Many of you must have faced this obscure problem, it’s because you haven’t called AfxInitRichEdit/AfxInitRichEdit2() function. Wait! Before you jump onto add this call identify your rich edit control’s class name, if you are working in VC6 then rich edit class name will be RICHEDIT and or if VC7 and above then it will be […]
Continue reading…

 

Why am I not receiving LBN_SELCHANGE for a list box?

Recently a colleague of mine asked this question, he added a message map entry for LBN_SELCHANGE but still no notification message arrived and also this list box was dynamically created using CListBox::Create function. The reason was that he missed out a style id while creating this window. LBS_NOTIFY should be specified when creating a list […]
Continue reading…

 

A humble attempt to explain GDI stock objects…

So what are stock objects? “Stock” as the name says means goods in hand, which a shop keeper always has as a backup. In the same way GDI also keep a set of backup of objects which it creates during initialization and these are known as stock objects. Let’s think of a case like this… […]
Continue reading…

 

Changing row height of a list control/CListCtrl

I’ve seen in forums numerous times people asking this query: How to change the row height of list control? Answers vary from , owner drawing to adding a fake image list with tall images. 🙂 Well so I decided to find out a way to do this in a proper way.  So here is a […]
Continue reading…

 

Converting CString to LPCTSTR or LPTSTR

Converting from CString to char* is conditional, becuase CString is a TCHAR based implementation. TCHAR is defined as a char if _UNICODE is not defined, so if this is the case we can convert to char*, since TCHAR* and char*  are equal, else we’ve got to use function like MultiByteToWideChar/W2A/W2AEX etc. So from now on […]
Continue reading…