Archive for April 15th, 2009
- In: MFC
- Leave a Comment
A topmost window is one which stays on top of other windows even while it’s not in focus. Normally all application popup windows will go to background once they lose focus but a topmost window doesn’t.
void SetTopMost( HWND hWnd, const BOOL TopMost ) { ASSERT( ::IsWindow( hWnd )); HWND hWndInsertAfter = ( TopMost ? HWND_TOPMOST : HWND_NOTOPMOST ); ::SetWindowPos( hWnd, hWndInsertAfter, 0, 0 , 0 , 0, SWP_NOMOVE | SWP_NOSIZE ); }
The second parameter passed to ::SetWindowPos is the one that does the trick. It’ called hwndInsertAfter, if we specify the after window as HWND_TOPMOST then we get a topmost window, if we specify after window as HWND_NOTOPMOST then topmost setting is unset and our window becomes a normal window.
Set resource handle in MFC and ATL
Posted April 15, 2009
on:- In: ATL | MFC
- Leave a Comment
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 the resource dll. So solution for above problem will be to set correct resource handle.
MFC provides two functions…
1. AfxGetResourceHandle
2. AfxSetResourceHandle
So we should first save our current resource handle and then set new resource handle before loading such a resource. Also don’t forget to set our old handle since its good practice.
Internally MFC calls FindResource and LoadResource using this handle, so if it’s not properly set this will cause failure in loading resources.
In ATL it’s quite similar except that we’ve got a new function called AddResourceInstance which adds our new resource handle to existing list of handles. So when a look up is done given resource handle is also used. Following functions are provided in ATL to work with resource handles…
1. AddResourceInstance
2. GetResourceInstance – Similar to AfxGetResourceHandle
3. SetResourceInstance – Similar to AfxGetSetResourceHandle
For newbies this is always a painful bug to resolve as they don’t know what went wrong since they expect this to be automagically done.
Sample code snippet:
class CEMCResourceHandle { public: CEMCResourceHandle() { m_hPrevInst = AfxGetResourceHandle(); m_hDLLInstance = ::LoadLibrary(szESIMFCControlsDLLName); if (m_hDLLInstance) { AfxSetResourceHandle(m_hDLLInstance); } } virtual ~CEMCResourceHandle() { AfxSetResourceHandle(m_hPrevInst); if(m_hDLLInstance != NULL) { FreeLibrary(m_hDLLInstance); } } //! Retrieves the current resource instance //! //! \return Return the current resource instance HINSTANCE GetCurrentInstance() const { return m_hDLLInstance; } //! Retrieves the previous resource instance //! //! \return Return the previous resource instance HINSTANCE GetPrevInstance() const { return m_hPrevInst; } private: //! Handle of previous resource instance HMODULE m_hPrevInst; //! Handle of loaded DLL resource instance HMODULE m_hDLLInstance; }; // Just made an temporary instance of CEMCResourceHandle class // Load resource from the DLL CEMCResourceHandle resHandle; // Set bitmap to the MoveUp button m_btnUp.SetBitmap(::LoadBitmap(resHandle.GetCurrentInstance(), MAKEINTRESOURCE(IDB_BITMAP_UPARROW)));
Getting an ITEMIDLIST For a Path
Posted April 15, 2009
on:- In: C/C++
- Leave a Comment
If you have ever used ShBrowseForFolder(), you’ve probably noticed the pidlRoot member of the BROWSEINFO structure. It can be used to set the “root” folder to browse from. It’s usually set to NULL, which means the “Desktop” folder.
Ever wanted to use another folder as the root for the “Browse For Folder” common dialog, but didn’t know how to create an ITEMIDLIST for a given path? Here’s a function that does it.
BOOL GetItemIdListFromPath (LPWSTR lpszPath, LPITEMIDLIST *lpItemIdList) { LPSHELLFOLDER pShellFolder = NULL; HRESULT hr; ULONG chUsed; // Get desktop IShellFolder interface if (SHGetDesktopFolder (&pShellFolder) != NOERROR) { return FALSE; // failed } // convert the path to an ITEMIDLIST hr = pShellFolder->ParseDisplayName ( NULL, // owner window NULL, // reserved (must be NULL) lpszPath, // folder name &chUsed, // number of chars parsed lpItemIdList, // ITEMIDLIST NULL // attributes (can be NULL) ); if (FAILED(hr)) { pShellFolder->Release(); *lpItemIdList = NULL; return FALSE; } pShellFolder->Release(); return TRUE; } // GetItemIdListFromPath
It’s written in C++, but converting it to plain C is just a matter of changing a couple of lines. To do it, change all interface calls like: pShellFolder->ParseDisplayName() to pShellFolder->lpVtbl->ParseDisplayName(pShellFolder, …), and similar changes to other calls.
Two things: The function receives the path as a UNICODE (LPWSTR) string, so you’ll have to convert ANSI strings to unicode using MultiByteToWideChar() before calling the function. Finally, remember that you should free the memory referenced by the pointer to the ITEMIDLIST you get from the function by calling the Free() method of the IMalloc interface, which you can get using SHGetMalloc().
Is a path shared?
Posted April 15, 2009
on:- In: C/C++ | MFC
- Leave a Comment
BOOL IsPathShared( LPCTSTR lpctszPath_i ) { SHFILEINFO shFileInfo = { 0 }; return ( SHGetFileInfo( lpctszPath_i, 0, &shFileInfo, sizeof( shFileInfo ), SHGFI_ATTRIBUTES ) && ( shFileInfo.dwAttributes & SFGAO_SHARE ) == SFGAO_SHARE ); }
- In: C/C++ | MFC
- Leave a Comment
BOOL IsShortcut( LPCTSTR lpctszPath_i ) { SHFILEINFO shFileInfo = { 0 }; return (( SHGetFileInfo( lpctszPath_i, 0, &shFileInfo, sizeof( shFileInfo ), SHGFI_ATTRIBUTES )) && ( shFileInfo.dwAttributes & SFGAO_LINK ) == SFGAO_LINK ); }