When does a window made in C++ update.

Hi. I'm pretty new to C++ but have been working with regular C for +20 years so this is a new world for me.

I have a question about update of content of a created window. I been trying to make a simple counter that just print out Test = 1 to 9 in a second. Program works fine but don't print anything out until after a second. The result is always the same. Test = 9. So where in the code does the printout actually happen?
Attached is the code of the printout loop. Thanks for the help. This is very different from C.

void Delay(long ulmilliSec)
{
clock_t set = clock();
while((clock() - set) < ulmilliSec); ///loop until time's up
}

void MainWindow::Paint( void )
{

int iCnt;
char csBuffer[] = "Test = 1"; // 8

PAINTSTRUCT ps;
HDC hdc = BeginPaint( hWnd, &ps );
for (iCnt = 0; iCnt < 9; iCnt++) {
TextOut( hdc, 20 /* X */, 20 /* Y */, csBuffer, 8 /* Number of char */);
csBuffer[7]++;
Delay(100);
}
EndPaint( hWnd, &ps );
}
Last edited on
...

well... how about this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <windows.h>
#include <conio.h>
using namespace std;

int main()
{
    char ch;
    int y = 0;
    for(int x = 1; x <= 9; x++)         //for x is 1-9
    {
        cout<< "Test = "<< x<< endl;
        Sleep(1000);               //pause for a sec
    }
    cout<< "PRESS ENTER";
    while(y == 0)                   //never ending loop allows us to create
    {                                      //a nice menu
        ch = _getch(); //get input
        if(ch == 0x0d)        //ch = enter
        {
            return 0;
        }
    }
    return 0;
}


put that into your compiler and see what you get. It should pause for a second after displaying each "Test = x" and then wait for you to press enter.
Last edited on
I agree that will work fine in console mode but that is not what I'm doing. The code you saw I copied is only a little piece of the actually program where I create a window and in that window I was trying just to have a number increment between 1 and 9. As far as I can see it does not do the printout in the textout function like a printf or cout. I'm trying to figure out where it actually happen. I have been trying to single step in debug mode but for some reason the program breaks down. I using Borland C++ ver 5.03 compiler and IDE. Thanks for your input anyway.
PS.

I don't know why the code not came out wit the right indent in the printout. Maybe I should replace the tabs with spaces. Ha Ha. Sorry about that. It can be tough to read that way.
Last edited on
Forgot to say. This is a Win32 GUI model and not a Win32 Console mode. That's why I'm not using cout (console out) function but the textout function.
closed account (DSLq5Di1)
Windows are event driven, do not use Sleep() in a GUI thread! Instead, create a timer SetTimer() which updates the text buffer and notifies the window to re-paint InvalideRect(). The paint handler should be responsible for painting only.

http://msdn.microsoft.com/en-us/library/ms644906
http://msdn.microsoft.com/en-us/library/dd145002
Thank you very much Sloppy9. That was exactly what I was looking for. I know it was event driven and thought the event was interrupt driven that's why I made my own delay and didn't use sleep. I guess I been working to much with microcontrollers. Ha Ha. Thanks again.
Topic archived. No new replies allowed.