POLL: Would you like the Pictures in the posts to be continued or not?

18 01 2009

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!!!

For We.See.Tips,

How to restrict the tree control labels to be integer?

18 01 2009

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 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?

7 01 2009

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

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?

6 01 2009

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
    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?

4 01 2009

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 Expand/Collapse TreeCtrl nodes by using Enter Key?

2 01 2009

You can see Tree Control, almost in every heavy windows applications. They are very convineant  method for organize things in hierarchy. But by default Tree control doesn’t support expand/collapse of its tree nodes by Enter key. Is there any way to do that?

Picture Courtesy – hawaiibonsai

Yes, you can! All you want to do is – Override PreTranslateMessage() in your dialog and handle all WM_KEYDOWN messages for your tree control. If, the key is VK_RETURN, i.e. enter key, then check whether the current selected node in TreeCtrl is expanded or collapsed and modify the key stroke as WM_ADD key or WM_SUBSTRACT key accordingly. The idea is, if you press +, then tree node expands and for – key, the tree node collapse – which is the default behavior of tree control. Well, have a look at the code snippet.

BOOL CRabbitDlg::PreTranslateMessage(MSG* pMsg)
    // Check whether its a keypress.
    if( pMsg->message == WM_KEYDOWN )
        // Check whether its for our tree control.
        UINT CtrlId = ::GetDlgCtrlID( pMsg->hwnd );
        if( CtrlId == IDC_TREECTRL )
            // Check whether its enter key.
            if( pMsg->wParam == VK_RETURN)
                // Check whether the currently selected item is
                HTREEITEM CurrentItem = m_TreeCtrl.GetSelectedItem();
                if( m_TreeCtrl.GetItemState( CurrentItem, TVIS_EXPANDED )
                        & TVIS_EXPANDED )
                    // Current Item is Expanded.
                    // So send - Key code to collapse it.
                    pMsg->wParam = VK_SUBTRACT;
                    // Current Item is Collapsed.
                    // So send + Key code to Expand it.
                    pMsg->wParam = VK_ADD;

    return CDialog::PreTranslateMessage(pMsg);

If you want the expand entire child nodes under a perticular node, then press * key. I used to use this techniqe to report performance bugs. 😉

Targeted Audience – Intermediate.

How to Watch Variables in Binary by using Visual Studio Debugger?

1 01 2009

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.