Gold mine of Visual C++ tricks!
Archive for August, 2008
How to copy or move file with progress?
Aug 28th
![]()
In old Win98 days, File copying progress notification was horrific. Even if you have a lot of data to copy, it always says – you have 5 more minutes to finish.
Well, how to copy or move file with accurate progress?

![]()
You can use the api – CopyFileEx() for copying data with progress and MoveFileEx() to move files with progress. Both are more or less similar in usage. In both cases, just call the apis by providing the callback function pointer. System will give callback notification for the progress. See the code snippet of CopyFileEx().
// Callback function for handling progress notification.
DWORD CALLBACK CopyProgressRoutine(
LARGE_INTEGER TotalFileSize,
LARGE_INTEGER TotalBytesTransferred,
LARGE_INTEGER StreamSize,
LARGE_INTEGER StreamBytesTransferred,
DWORD dwStreamNumber,
DWORD dwCallbackReason,
HANDLE hSourceFile,
HANDLE hDestinationFile,
LPVOID lpData )
{
// Calculate the percentage here.
double Percentage = ( double(TotalBytesTransferred.QuadPart) /
double(TotalFileSize.QuadPart) ) * 100;
// Continue the file copy. It can also be stopped.
return PROGRESS_CONTINUE;
}
void CDlgDlg::OnCopy()
{
// Copy the file.
CopyFileEx( _T("c:\\Jijo\\Games.rar"),
_T("c:\\Jijo\\GamesBackup.rar"),
CopyProgressRoutine,
0,
FALSE,
0 );
}
![]()
CopyFIleEx() and MoveFileEx() are available only from Windows NT onwards. So don’t forget to add _WIN32_WINNT=0×0400 to project settings.
![]()
Targeted Audience – Beginners.
how to set color for static control text in dialog?
Aug 27th
![]()
Colors convey meanings too. For instance, if something is written in red – that means something to be cautious. If its in green, its treated as safe. Well, usually in window dialogs, the static control texts are black in color. Is it possible to color them to convey more meaning?

![]()
Yes! You have to handle WM_CTLCOLOR message. For each control, this message will be triggered and you’re free to do modification on your control. Have a look at the code snippet in MFC framework.
// Message Map
BEGIN_MESSAGE_MAP(CDlgDlg, CDialog)
...
ON_WM_CTLCOLOR()
END_MESSAGE_MAP()
HBRUSH CDlgDlg::OnCtlColor( CDC* pDC, CWnd* pWnd, UINT nCtlColor )
{
// Call base class version at first. Or else it will override your changes.
HBRUSH hbr = CDialog::OnCtlColor( pDC, pWnd, nCtlColor );
// Check whether which static label its.
if( pWnd->GetDlgCtrlID() == IDC_STATIC_OK )
{
// Set color as red.
pDC->SetTextColor( RGB( 255, 0, 0 ));
pDC->SetBkMode( TRANSPARENT );
}
return hbr;
}
![]()
Always call CDialog::OnCtlColor() at first. Or else it will override your modifications.
![]()
Targeted Audience – Intermediate.
How to check whether the window handle is valid?
Aug 24th
![]()
Since communication by messages are so easy, windows used to communicate with each other by using messages. For instance, if simple data blocks are to be transfered – WM_COPYDATA can be used. For all those instances the window should be alive. But how to know whether the current window handle is valid or whether it points to a dead window?
![]()
You can call the api – IsWindow() by passing window handle. If the handle points to a live window, then the function returns true else false. Have a look at the code snippet.
// The handle to be tested. HWND hWindow = GetSafeHwnd(); // Check whether the window is still there. BOOL bWindowAlive = IsWindow( hWindow );
![]()
While digging for the api, i found an interesting info. What about window handle re-cycling? For instance you have a window handle and you’re going to check that window by calling IsWindow() function. But in between that the real window is closed and a new window is created. Whether the window handler will be allocated to the new window?
Well, the answer is here. Have a look at it. Its interesting. The oldnewthing.
![]()
Targeted Audience – Beginners.
Microsoft Visual Studio 2008 Service Pack 1 Released!
Aug 21st

Visual Studio 2008 Service Pack 1 is released!
Download it from here.
Main features included in this service pack are -
Visual Studio 2008 SP1 features:
- Improved WPF designers
- SQL Server 2008 support
- ADO.NET Entity Designer
- Visual Basic and Visual C++ components and tools (including an MFC-based Office 2007 style
How to disable maximizing the dialog from Task manager?
Aug 20th
![]()
Its not mandatory for every windows citizen to have maximize button.
For instance, Windows calculator. But do you know that via taskmgr we could maximize any dialogs? Even you can maximize the dialog which doesn’t have maximize style. Its a master piece of QA team to make the dialog look weired. Well how to prevent it?
![]()
You can do it by handling – WM_GETMINMAXINFO message. Before resizing, this message is fired to the dialog to get the minimum and maximum window dimensions. Since, we don’t need to change our dimensions, we have to set the max dimensions as current window dimension. Have a look at the code snippet.
// Message map
BEGIN_MESSAGE_MAP(CRabbitDlg, CDialog)
...
ON_WM_GETMINMAXINFO()
END_MESSAGE_MAP()
void CRabbitDlg::OnGetMinMaxInfo( MINMAXINFO FAR* pMinMaxInfo )
{
// Window rect.
RECT rect = { 0 };
GetWindowRect( &rect );
CRect WindowRect( &rect );
// Set the maximum size. Used while maximizing.
pMinMaxInfo->ptMaxSize.x = WindowRect.Width();
pMinMaxInfo->ptMaxSize.y = WindowRect.Height();
// Set the x,y position after maximized.
pMinMaxInfo->ptMaxPosition.x = rect.left;
pMinMaxInfo->ptMaxPosition.y = rect.top;
}
![]()
There is one small known issue – if we maximize via taskmgr, the window remains same, but the title bar will be painted like maximized.
![]()
Targeted Audience – Intermediate.
How to handle F1 or help in application?
Aug 17th
![]()
Help is inevitable part of every windows application. The first function key – F1 itself is assigned as help in every application. Well, how to handle the user’s “Mayday” call in application?

![]()
Basically you’ve to handle the WM_HELP message. When you press F1 the WM_HELP message will be posted to your window. To handle this via MFC, Add ON_WM_HELPINFO() to message map and implement OnHelpInfo() in your dialog. Have a look at the following code snippet.
// Add ON_WM_HELPINFO() to your message map.
BEGIN_MESSAGE_MAP(CYourDialog, CDialog)
...
ON_WM_HELPINFO()
END_MESSAGE_MAP()
// Add this function to your dialog.
BOOL CYourDialog::OnHelpInfo( HELPINFO* HelpInfo)
{
// Handle your help request here.
return TRUE;
}
![]()
Well, the only help i used to use is MSDN. If I accidentally launch Windows help, I’ll kill it immediately by using taskmgr. I don’t know why I hate it.
![]()
Targeted Audience – Beginners.
How to set timer in mfc dialog?
Aug 12th
![]()
Timers are running everywhere! The moment when your machine starts, when your machine is turned off, in your heart, in your lungs, in your brain and even in your mind, its everywhere. Well, Software application are not exception to timers. We have to use timers in several situations.

But how to create timers and use it?
![]()
For creating timers you can use the api – SetTimer(). In SetTimer() you’ve to specify the time interval in milliseconds, a timer id as UINT, then TimerProc. For each tick of specified timer, windows will call the TimerProc. If you didn’t specify any TimerProc, then a WM_TIMER message will be posted to your window.
You can utilize the Timer ID to set multiple timers using the same TimeProc. You can use KillTimer() to remove the timer. Have a look at the code snippet. In the given code snippet, timer is handled by WM_TIMER message.
// Message Map
BEGIN_MESSAGE_MAP(CDlgDlg, CDialog)
...
ON_WM_TIMER()
END_MESSAGE_MAP()
...
// Timer ID constants.
const UINT ID_TIMER_MINUTE = 0x1001;
const UINT ID_TIMER_SECONDS = 0x1000;
// Start the timers.
void CDlgDlg::StartTimer()
{
// Set timer for Minutes.
SetTimer( ID_TIMER_MINUTE, 60 * 1000, 0 );
// Set timer for Seconds.
SetTimer( ID_TIMER_SECONDS, 1000, 0 );
}
// Stop the timers.
void CDlgDlg::StopTimer()
{
// Stop both timers.
KillTimer( ID_TIMER_MINUTE );
KillTimer( ID_TIMER_SECONDS );
}
// Timer Handler.
void CDlgDlg::OnTimer( UINT nIDEvent )
{
// Per minute timer ticked.
if( nIDEvent == ID_TIMER_MINUTE )
{
// Do your minute based tasks here.
}
// Per minute timer ticked.
if( nIDEvent == ID_TIMER_SECONDS )
{
// Do your seconds based tasks here.
}
}
![]()
“Today is the tomorrow you worried about yesterday” – So enjoy your day!
![]()
Targeted Audience – Beginners.
WeSeeTips crossed Milestone – 125+ tips and 25,000+ hits
Aug 10th
We Crossed 25,000+ Hits

Hurreyyyyy!!! We have crossed 25,000 hits! Its quite important milestone for us. Few months before when Weseetips started, the average hit was below 20 ~ 30. Most of the visitors were my friends.
But the growth was spontaneous, soon the daily average climbed to hundreds. I thank each and every one of you for watching my blog and for a true blogger, eternity is nothing but the satisfaction that he gets from being appreciated by others by reading his thoughts.
I know I’m Still a Kid!

I know its just the beginning of the journey. I’ve miles to go… And this 25K is just a footstep! Well, Several other blogs have impressed me and are my role models. I’m a great fan of TheOldNewThing. He used to tell a lot of interesting stuffs in his blog. Its one of the first blogs that thrilled me! Well, I was stunned, when i saw the hit count of my friend Sarath’s blog. He is going to cross 100,000 hits soon! Well, my journey has just started!!! May be one day, me too will become a “Old New Thing“.
At this moment, I dedicate this small milestone achievement to all important persons who were the turning points of my life.
- My Father (One who took me to the world of Computers)
- My Mother and Sister. (For everything)
- Abdul Nazar. K (One who hold my hands when i started walking in world of computers)
- Muhammed Ziyad. T.A (My C Guru, My mentor and My Well Wisher)
- Yeshwant Kanitkar ( The man who thinks out of box! He is my role model)
- John Robbins (Without him and his books, I’ll be zero in debugging)
- Charles Petzold – (With his books I started my HelloWorld in Windows Programming)
- Jeffrey Ritcher – (I’ve made my first design after reading his book!)
- Last but not least – She. (The heartbeat of my life. The one, who is behind all my achievements. Without her, I’ll be nothing.)
Feedback – Yes! I would like to hear from you!!!

It is not the strongest, nor the most intelligent that survives. It is the one that is the most adaptable to change.
~ Charles Darwin
Yes! I just want to know what do you think about WeSeeTips. Please spend a few minutes to post me a feedback! Well, I’ve prepared a feedback form which make you’re life easier. Just post your feedback as comment to this post.
Feedback
1) What all do you like about WeSeeTips?
2) How WeSeeTips can be more better? Improvement suggestions, Something that you don’t like… etc.
3) Misc – Some thing more to say?
Thanks a lot for your kindness to spend a few minutes for me in between your busy schedule. Well, guys watch on… Once again thanks for you all for visiting and reading my tips. Expect more stunning tips… See you…
For WeSeeTips
Jijo.
How to sort an array by using STL?
Aug 10th
![]()
Today I’ll tell a story… Its interesting!
Once upon a time, there lived a C++ Padawan. One day, he want to sort an array. He knows that STL have std::sort() function. But he want to sort an array, not an STL container. What a fool he is? Native array’s don’t have iterators. So how could he call the std::sort() function? Well, he is clever – he created a vector, copied his array to vector, then he called the sort() function. After sorting, he copied the data back to array.
The End!

But now he is ashamed of what he did. Because now he realize that he can use std::sort() directly on native array too! But how?
![]()
You can use array pointers direclty in STL algorithms. They act like iterators itself. Have a look at the code snippet.
// Integer array with 10 items.
int Array[10] = { 9,3,2,4,0,6,7,8,1,5 };
// Sort them.
std::sort( Array, Array + 10 );
![]()
Well, Can you guess – who was that C++ trainee? It was me.
![]()
Targeted Audience – Beginners.



