Member Avatar for tawes01

I want to have a live clock in the statusbar.
When I put SetWindowText before the switch in the callback, like this:

BOOL CALLBACK MainDlgProc(HWND hDlg, UINT wMsg, WPARAM wParam, LPARAM lParam) {
   time_t rawtime;
   struct tm *timeinfo;
   time(&rawtime);
   timeinfo = localtime(&rawtime);
   strftime(thetimeis,79,"Current Date and Time: %A, %B %d, %Y %I:%M:%S",timeinfo);
   SetWindowText(GetDlgItem(hDlg,IDC_STATUSBAR),thetimeis);
   switch(wMsg) {
   	case WM_INITDIALOG:
         //other stuff, continues...

it makes it look like this:

but if I just remove the SetWindowText, i.e.

BOOL CALLBACK MainDlgProc(HWND hDlg, UINT wMsg, WPARAM wParam, LPARAM lParam) {
   time_t rawtime;
   struct tm *timeinfo;
   time(&rawtime);
   timeinfo = localtime(&rawtime);
   strftime(thetimeis,79,"Current Date and Time: %A, %B %d, %Y %I:%M:%S",timeinfo);
//   SetWindowText(GetDlgItem(hDlg,IDC_STATUSBAR),thetimeis);
   switch(wMsg) {
   	case WM_INITDIALOG:
         //other stuff, continues...

it makes it look like this, which is how I want it to look:

except I want the time in the statusbar. thetimeis is a global variable:

char thetimeis[80];

What is causing this, and how can I fix it?
I'm using dev-c++ 4.9.9.2 on a windows 7.

You can try SetWindowText() in WM_PAINT or WM_CREATE.

This is because:
1. your code has caused an internal error in your windows application. The MainDlgProc callback function is called when your dialog window is being created; therefore, the 1st time when you call SetWindowsText, your windows is being created but has not been created yet, you can actually examine what messages you will receive during the dialogbox creation by debugging. There're actually a lot of intermediate stages before final creation of the dialogbox. Calling SetWindowsText before your dialog window is completely set up causes some system internal error.

2. SetWindowsText will send WM_SETTEXT msg to the dialog box which again calls MainDlgProc which you call SetWindowsText again and again... This will run into an infinite recursion (a calls b, b calls a, ...) and causing stack overflow. Window has internally done a safety check to prevent this kind of callback function re-entry, but nonetheless, will lead to some errors

So the proper way is to create a Timer and associate it to this DialogBox by calling SetTimer during WM_INITDIALOG and pass the dialog window handle to it, and call SetWindowsText in the Timer message.

Member Avatar for tawes01

Ok, how do you create a timer?

Ok, how do you create a timer?

Download Win32 API reference from http://www.carabez.com/downloads/win32api_big.zip
In the SDK, find the function SetTimer and see how to use it

UINT SetTimer(

    HWND hWnd,	// handle of window for timer messages
    UINT nIDEvent,	// timer identifier
    UINT uElapse,	// time-out value
    TIMERPROC lpTimerFunc 	// address of timer procedure
);
...
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.