Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

CWnd::CreateEx() and SetTimer()

Status
Not open for further replies.

sedawk

Programmer
Feb 5, 2002
247
US
Hi there,

I used CWnd::CreateEx() to create a window and later on I want to setup a timer to this window. Since CWnd::CreateEx() returns a BOOL value while SetTimer() needs the window handle to setup a timer.

I then think of GetSafeWnd() to have a window handle of the created window in order to use it in SetTimer(). But it seems doesn't work.

How can I obtain the window handle? The code I wrote simplified here:

UINT Result=CWnd:CreateEx(...);
HWND hwnd=GetSafeWnd();

SetTimer(hwnd,1000,(call back function));
 
Ok, I know how to do that now. Another thing I need help is:

I want to have multiple timers set and activated at different time:

SetTimer(TIMER1,1000,NULL); // Timer1 works at 1 sec
SetTimer(TIMER2,5000,NULL); // Timer2 works at 5 sec

if(nIDEvent==TIMER1)
do function1
else if(nIDEvent==TIMER2)
do function2

However, this doesn't seem to work. Any idea on how to set multiple timers?
 
Timers work only if thread is idle (it is caused WM_TIMER). Normally, You wish to use Timers to refresh any info in worked program - and it does not work while Your programm is busy. You should look MSDN samples to solve Your problem.
What is nIDEvent and where do You do the functions - in handler of WM_TIMER?
 
Think your declartion of TIMERID needs to be int. Change TIMER1 to just 1 and TIMER2 to 2. If that doesn't work look at the sample code below ..... Hope this helps.

Try calling the TIMERPROC function ..... Like so.....

// DEFINE AT THE BEGINNING OF THE CLASS .... MAKE IT
// A GLOBAL FUNCATION TO MAKE LIFE EASY
void CALLBACK TimerProc (HWND hWnd, UINT nMsg, UINT nTimerID, DWORD dwTimer);

//SET 3 TIMERS IN INIT....
// or somewhere you deem appropriate
SetTimer(hWnd, 1, 2000, TimerProc);
SetTimer(hWnd, 2, 4000, TimerProc);
SetTimer(hWnd, 3, 18000, TimerProc);

// if (nTimerID == 1) {
do this
}

if (nTimerID == 1) {
//do 1
}

if (nTimerID == 2) {
//do 2
}

if (nTimerID == 3) {
//do 3
}
 
Hi,

Thanks for your reply. The last reply made it work. The reason is I made a stupid mistake: I forgot in my *.h file define TIMER1 and TIMER2 to be an integer.

About what nIDEvent is, it is created by MFC ClassView automatically from classfunction, an integer UINT.

Another thing still not so clear from the first post is that how can I specify a HWND to MFC created window:

UINT Result=CWnd:CreateEx(...);

There is no way to specify a handle for the created window if I want to use:

SetTimer(hwnd,1,1000,(callback proc));

Since there is no hwnd. To this part, I still don't understand how MFC works
 
Try using CreateWindow or some other functions that returns. here's one example that was embedded in one of my code .....


BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;
hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL)
 
Hi Visitor1,

I know CreateWindow() can return and HWND. But my code is generated by MFC Template. If I have to modify all Create() function to CreateWindow() and assign handle notations, that would be too much. Is there other ways in MFC to solve this problem?

Thanks
 
I am sorry, but I don't know any other way as I am just getting the feel for visual C++ myself. maybe you outta post another query with "how to obtain a handle" and see if others respond to it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top