Friday, February 13, 2009

[VC++] Programmatically Show/Hide Desktop contents

Below code snippet shows how to hide desktop content. Idea is simple find the Program Manager window and then get its child list view which host the desktop icons and hide it.

HWND hProgmMngrWnd, hDefViewWnd, hIconViewWnd;
hProgmMngrWnd = (FindWindow ("Progman", "Program Manager"))->GetSafeHwnd();
hDefViewWnd = FindWindowEx( hProgmMngrWnd, NULL, "SHELLDLL_DefView", NULL);
hIconViewWnd = FindWindowEx( hDefViewWnd, NULL, "SysListView32",NULL);
::ShowWindow( hIconViewWnd, SW_HIDE );

For programmaticaly showing the desktop icons replace the last line of the above code block with the below line of code(to show the window, instead of hiding).

::ShowWindow( hIconViewWnd, SW_SHOW );

[C/C++] Environment variables as argument to main()

Most of us may not have noticed that main() supports a third argument which will provide you an array of pointers to environment variable strings. See the below code,
int main( int nArgc, char* pcArgv[], char* pcenv[ ])
{
int nIdx = 0;
while ( pcenv[ nIdx ] )
printf ( "%s\n", pcenv[ nIdx++ ] );
return 0;
}
The above program will print all the environment variables in the console window

Thursday, February 12, 2009

[VC++] You should know this !

Here is a set of things that you should know about your application,
  • Returning 0 from InitInstance( ) will close your application.
  • m_pMainWnd which contains address of your application main window is a public data member of CWinApp class. AfxGetMainWnd( ) function returns the address stored in m_pMainWnd i.e. address of frame window.
  • AfxGetApp( ) returns address of your application object. So AfxGetApp( ) can be used to call member functions or access public data members of CWinApp class.
  • Set WS_VISIBLE style in Create( ) function then no need to call ShowWindow( ).
  • MAKEINTRESOURCE macro (declared in 'windows.h') converts an integer to string.
  • Selecting 'Use MFC in static DLL' (from Project->Settings->General) will create a standalone EXE but the size of your EXE will be much large.
  • Functions with a prefix 'Afx' are global function which can be accessed from anywhere.

[VC++] Programmatically displaying properties dialog

Here is how to display the properties dialog programmatically.
SHELLEXECUTEINFO shExecuteInfo = { 0 } ;
shExecuteInfo.cbSize = sizeof ( shExecuteInfo ) ;
shExecuteInfo.fMask = SEE_MASK_INVOKEIDLIST ;
shExecuteInfo.lpVerb = "properties" ;
shExecuteInfo.lpFile = "C:\\WINDOWS\\system32\\notepad.exe" ;
ShellExecuteEx ( &shExecuteInfo ) ;
Above code when executed will display the properties dialog box of notepad(as shown below). Similarly we can display any folder or file properties by giving its path name to SHELLEXECUTEINFO::lpFile.

[VC++] Extracting Red, Green, Blue values from a RGB value without using API functions

I will show you how to extract the Red, Green and Blue values from a COLORREF object. See the below code it is self explanatory.

COLORREF TestClr = RGB ( 100, 150, 50 ) ;
int nR, nG, nB ;
// Get Red value
nR = (( TestClr ) & 0xFF );
// Get Green value
nG = ((( TestClr ) >> 8 ) & 0xFF );
// Get Blue value
nB = ((( TestClr ) >> 16 ) & 0xFF );


CString csRGBVal;
csRGBVal.Format( "R = %d G = %d B = %d", nR, nG, nB );
MessageBox( csRGBVal, "RGB values" );

If the above code is executed below output message box will be shown.