Monday, May 24, 2010

Visual C++ programmers... Please help...?

I am using MFC in making a simple window. My main objective is to make mouse pointer disappear when it reaches 4 seconds. I solved it myself using the Win32 API but in MFC, I find myself stuck in setting up the time...





I surfed the web and found out that I have to use OnStartTimer, OnStopTimer and OnTimer and also by using the SetTimer... but when I tried to call any of these three functions e.g. OnStartTime... my compiler cannot recognize all above-stated functions... I tried to re-intialized them in class, though it creates no error and ran, but all timers that were set failed to perform there duties...





I am not asking or begging you for the entire codes or write the codes to supply my needs... I just want the idea or your own approach to my problem...





I am already out of ideas and can't think of any ways to solve the problem...





Please... anyone... help... :(

Visual C++ programmers... Please help...?
SetTimer is a member of the window class, so make sure you call it from inside a CWnd-derived object such as your view class (it causes a WM_TIMER message to be sent so you need a window to receive it. In that same window override the WM_TIMER handler (i.e. OnTimer) to catch the message then call SetCursor or whatever to disable the cursor.





Another technique is to use a worker thread, this will allow you to run your own function in a custom thread without holding up the main thread and causing your program to freeze. Your worker thread should go to sleep for the 4 seconds so that it doesn't tie up the CPU doing nothing, so do something like this:





UINT MyThreadProc(LPVOID pParam)


{


Sleep(4000); // 4000ms == 4 seconds


s_pMyWindowPtr-%26gt;SetCursor(NULL); // or whatever


return 0;


}





Kick off this function by calling AfxBeginThread, you'll need to change your project settings to link in the multithreaded MFC libraries. This page has more info about creating worker threads:





*** UPDATED ***





You don't call OnTimer yourself, Windows calls it and you need to catch it by implementing the message handler and adding an entry to the message map. It's been years since I've used MFC, so I'm a bit hazy on the interface specifics, but if you open up your window and go to its properties then you'll have a list of all functions that you can overload. OnTimer will be one of them, I think you can double-click on it or something to have DevStudio automatically create the function for you and add the message map entry.





If you're still stuck then post again here and I'll dig out some code for you from my old archives.
Reply:Did you put an entry for ON_WM_TIMER() in your message map? It needs that for OnTimer to be called.





BEGIN_MESSAGE_MAP(mainWindow, CFrameWnd)


ON_WM_TIMER()


END_MESSAGE_MAP()


No comments:

Post a Comment