Gold mine of Visual C++ tricks!
Archive for January, 2009
POLL: Would you like the Pictures in the posts to be continued or not?
Jan 18th
Dear Visual C++ Enthusiast,
You might have noticed the picture selection for each article in this blog. Well, for every article a picture which matches to the content is hand picked and added. My intention is just to have a bit of fun and relaxing while reading the articles.

Picture Courtesy – campusali
Even though I mention the source of images as hyperlinks, I’m afraid of copyright violations and I am thinking to remove the pictures from blog. Well, the preference of my fellow readers is the most important one to me; so i would like to know your opinion. Kindly mark your opinion in poll and I really appreciate your willingness to take part in the survey! And thanks a lot for visiting. Keep Watching!!!
[polldaddy poll=1286678]
For We.See.Tips,
Jijo.
How to restrict the tree control labels to be integer?
Jan 18th
![]()
Tree Controls supports label editing and by default it accepts all kind of chars for label. But is there any trick to restrict the input label to be an integer?

![]()
Yes. You can. When user starts editing tree control will send a notification message - TVN_BEGINLABELEDIT. At first you have to handle the message and in the message handler, you can get the label edit control by calling GetEditControl(). Then set the edit control style to ES_NUMBER. Then the label edit control of tree, will allow only integers as input. Have a look at the code snippet.
// Message Map.
BEGIN_MESSAGE_MAP(CRabbitDlg, CDialog)
...
ON_NOTIFY(TVN_BEGINLABELEDIT, IDC_TREECTRL, OnBeginlabeleditTreectrl)
END_MESSAGE_MAP()
// Begin Label edit - Message Handler.
void CRabbitDlg::OnBeginlabeleditTreectrl(NMHDR* pNMHDR,
LRESULT* pResult)
{
// Get the edit control which of tree control and set
// its style to number only.
CEdit* pEdit = m_TreeCtrl.GetEditControl();
if( pEdit )
{
// Allow only numbers
pEdit->ModifyStyle( 0, ES_NUMBER );
}
*pResult = 0;
}
![]()
Feel the power!
![]()
Targeted Audience – Intermediate.
How to Start the ScreenSaver Programmatically?
Jan 7th
![]()
I’ve installed an aquarium screensaver and It works perfectly when i go to check the flames of stove.
But from yesterday onwards, the screensaver is crashing due to some reasons. Well, i just thought about – is there any way to start the screensaver programmatically and see the crash once again?

Picture Courtesy – fordesigner
![]()
Basically you have to send a WM_SYSCOMMAND to any of the windows with wParam as SC_SCREENSAVE. And what really happens in background is, the default window proc will get the message and will start the screen saver. So you can call the DefWindowProc() directly to start the screensaver. Have a look at the code snippet.
// Start the screen-saver DefWindowProc( WM_SYSCOMMAND, SC_SCREENSAVE, 0 );
![]()
It was really a nice screensaver. May be I’ll reinstall it to see it again.
![]()
Targeted Audiance – Beginners.
How to Print the CView directly to Printer?
Jan 6th
![]()
MFC’s Document/View framework have built-in printing support for applications. If you create one SDI or MDI application, you can take the print by using the File->Print menu. But the menu handling and print functionality is buried deep inside mfc framework. Well, how to take the print of current view by your own?

Picture Courtesy – DayCad.
![]()
You have to call CView::OnCmdMsg() by passing ID_FILE_PRINT or ID_FILE_PRINT_DIRECT. If ID_FILE_PRINT is passed, the printer dialog will be shown and for ID_FILE_PRINT_DIRECT, the print will be taken directly with default printer parameters. Have a look at the code snippet.
// Get the active view.
CFrameWnd* pFrameWnd = (CFrameWnd*)AfxGetApp()->GetMainWnd();
CView* pView = pFrameWnd->GetActiveView();
if( pView != NULL )
{
// Send Print message.
// If you want to print directly, then change ID_FILE_PRINT
// to ID_FILE_PRINT_DIRECT.
pView->OnCmdMsg( ID_FILE_PRINT, 0, 0, 0 );
}
![]()
Its interesting to know MFC internals. isn’t it?
![]()
Targeted Audience – Intermediate.
How to Enable Password Mask in Editbox?
Jan 4th
![]()
User Authentication is common in windows application. Usually the password editbox is masked and won’t show the real password. But to to enable the password masking in Editbox?

Picture Courtesy – NoteBookForums.
![]()
You have to enable ES_PASSWORD style of editbox and have to call SetPasswordChar() to set the Password masking character. You can do it in CDialog::OnInitDialog(). See the code snippet below.
BOOL CRabbitDlg::OnInitDialog()
{
...
// Get the Edit by using CtrlID.
CEdit* pEdit = (CEdit*) GetDlgItem( IDC_EDIT_PASSWORD );
// Set the password char.
pEdit->SetPasswordChar( '*' );
// Now modify the style to enable ES_PASSWORD.
pEdit->ModifyStyle( 0, ES_PASSWORD );
return TRUE;
}
![]()
Unless I’m very mistaken, it was while setting up samba server in Linux, where i had an interesting incident related to password. I was asked to type the password and save it. When I took the dialog again, the displayed password length was different. I was confused. I retyped and saved it again and again. Very lately i came to know that its a trick to fool the people who try to guess the password by length.
![]()
Targeted Audience – Beginners.
How to Watch Variables in Binary by using Visual Studio Debugger?
Jan 1st
![]()
In visual studio you can watch variable values in different formats. For instance, for viewing in hex add this to watch window – var,x and for octal – var,o. But its a pity that visual studio doesn’t support displaying variables in binaries. So how can you watch the value of a variable in binary, with debugger?

Image Courtesy – Pixdaus.
![]()
Well, we have to utilize the special feature of visual studio debugger. Basically the watch window is not just a tool to display the variable value. It can evaluate and execute small code snippets as well. If you add a function call to the watch window, that function will be called when debugger refreshes the watch values. So, all you have to do is – Add a global function which accepts integer value, converts it to binary string then display it to output window. Have a look at the function.
// Global function which converts integer to binary
// and dump to the output window.
void DumpBinary( DWORD Value )
{
// Buffer to hold the converted string.
TCHAR Buffer[255] = { 0 };
// Convert the value to binary string.
_itot( Value, Buffer, 2 );
// Display to output window.
CString csMessage;
csMessage.Format( _T("\n%d in binary: %s"), Value, Buffer );
OutputDebugString( csMessage );
}
Now if you want to convert the 100 to binary, break at some location and just add DumpBinary(100) to watch window and check the output in the output window. See the screenshot below.

![]()
Just now I realized – how powerful the Visual Studio Debugger is. Hats off to Visual Studio Team!
Well, please note that in visual studio 2008 and may be in siblings, When you add this to watch window, its possible to see this error – “CXX0001: Error: error attempting to execute user function”. In that case just click the “Evaluate button” which appears next to it and the expression will be re-evaluate.
![]()
Targeted Audiance – Advanced.
