Creating a count down text in window

This is a discussion on Creating a count down text in window within the Windows Programming forums, part of the Platform Specific Boards category; Hi, I'm trying to display text into my window. My idea is to create a count down that displays in ...

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    53

    Creating a count down text in window

    Hi,

    I'm trying to display text into my window. My idea is to create a count down that displays in the window, using a for loop. I'm using the sleep() function to control the time.

    Basically I just want the text to be displayed in window as the count down decrements.

    It's suppose to go like this:
    count=3
    count=2
    count=1

    Here's my code:
    Code:
    #include <windows.h>
    #include <mmsystem.h>
    
    LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
    
    
    LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
    	HDC		hdc;
    	RECT	rect;
    	int count = 5;
    	int i;
    
    	switch (message){
    
    		case WM_PAINT:
    			hdc = BeginPaint(hwnd, &ps);
    			for(i=0; i<5; i++)
    			{
    				count --;
    				Sleep(1000);//causes the main thread to sleep for 1 seconds - allowing another thread to run
    				TextOut(hdc, 20, 20,TEXT("count = &#37;d\n",count),12);
    			}
    
    			
    			EndPaint(hwnd, &ps);
    			return 0;
    
    	}
    
    	return DefWindowProc(hwnd, message, wParam, lParam);
    }
    Last edited by vopo; 10-07-2007 at 02:58 PM. Reason: so people won't steal my code, for temporary purposes

  2. #2
    Registered User
    Join Date
    Mar 2007
    Posts
    416
    what problems are you having? maybe try TextOut() too. And of course you know making it sleep for any amount of time means the program won't respond for that time right

  3. #3
    Registered User
    Join Date
    Jul 2007
    Posts
    53
    I tried with textOut, but it only displays "count = &#37;d". But it doesn't show the count down.
    Last edited by vopo; 10-07-2007 at 01:25 AM.

  4. #4
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,059
    Code:
    #include <stdio.h>
    .
    .
    .
    char szTextOut[128] = {0};
    .
    .
    .
      //TextOut(hdc, 20, 20,TEXT("count = %d\n",count),12);
      memset(szTextOut, 0, sizeof szTextOut);
      sprintf(szTextOut,"count = %d", count);  
     TextOut(hdc, 20, 20,szTextOut, strlen(szTextOut));

  5. #5
    Registered User
    Join Date
    Jul 2007
    Posts
    53
    thanks for the help, but why does the text is displayed in another language?
    Last edited by vopo; 10-07-2007 at 03:08 PM.

  6. #6
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    1,678
    Try using unicode.
    A class that doesn't overload all operators just isn't finished yet. -- SmugCeePlusPlusWeenie
    A year spent in artificial intelligence is enough to make one believe in God. -- Alan J. Perlis

  7. #7
    Registered User
    Join Date
    Jul 2007
    Posts
    53
    How can I use unicode? Is there a special symbol I need to apply to my code?

  8. #8
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    1,678
    Turn char to TCHAR, and add #define UNICODE.
    A class that doesn't overload all operators just isn't finished yet. -- SmugCeePlusPlusWeenie
    A year spent in artificial intelligence is enough to make one believe in God. -- Alan J. Perlis

  9. #9
    Registered User
    Join Date
    Jul 2007
    Posts
    53
    I tried that but it didn't work

  10. #10
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,859
    You did not state what your app actually diaplays. My guess it just the last number?

    Your method will not work PROPERLY for a number of reasons as you want it to. Mainly due to the Sleep().



    I would use a timer. Call SetTimer() in the WM_INITDIALOG. The timer will execute every time period (1000 ms) until you call KillTimer().

    Have a static int in the callback (or member in C++). In the WM_TIMER increment the int and call for a paint with InvalidateRect() followed by UpdateWindow().

    In the WM_PAINT draw the text to the screen.

    Please note it MUST be a static variable.
    Last edited by novacain; 10-08-2007 at 02:38 AM.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  11. #11
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    1,678
    Actualy, the callback varibles really should be global.

    >> I tried that but it didn't work
    Sorry, I thought when you said "another language" you really meant like English, French, Spanish, ect...
    A class that doesn't overload all operators just isn't finished yet. -- SmugCeePlusPlusWeenie
    A year spent in artificial intelligence is enough to make one believe in God. -- Alan J. Perlis

  12. #12
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    1,678
    Also, "Last edited by vopo : Yesterday at 02:58 PM. Reason: so people won't steal my code, for temporary purposes".
    No one is going to do that, especially if it's not working.
    A class that doesn't overload all operators just isn't finished yet. -- SmugCeePlusPlusWeenie
    A year spent in artificial intelligence is enough to make one believe in God. -- Alan J. Perlis

  13. #13
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,859
    Quote Originally Posted by Yarin View Post
    Actualy, the callback varibles really should be global.
    Why use a global over a static local in this case?

    If global, the variable would have more scope than required, can cause confusion (if a variable local to a function has the same name), makes the code harder to read and (in this case) will not improve performance.

    IMO a static local is the right choice.

    IMO globals should only be used as a last resort unless you can justify their use. Just like goto, global variables, can usually be avoided by better design.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  14. #14
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    1,678
    Last resort? Globals never cause confusion.
    I'll show you the simple concept:
    Code:
    int var = 4;
    // here, var == 4
    int main(void)
    {
       int var = 3;
       // here, var == 3
       if(1)
       {
          int var = 2;
          // here, var == 2
       }
       return 0;
    }
    A class that doesn't overload all operators just isn't finished yet. -- SmugCeePlusPlusWeenie
    A year spent in artificial intelligence is enough to make one believe in God. -- Alan J. Perlis

  15. #15
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    1,678
    Further more, gotos can be freely used with no problems, so long as you know what your doing. This last resort stuff is bahh.
    A class that doesn't overload all operators just isn't finished yet. -- SmugCeePlusPlusWeenie
    A year spent in artificial intelligence is enough to make one believe in God. -- Alan J. Perlis

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. DirectX | Drawing text
    By gavra in forum Game Programming
    Replies: 4
    Last Post: 06-08-2009, 01:23 AM
  2. Profiler Valgrind
    By afflictedd2 in forum C++ Programming
    Replies: 4
    Last Post: 07-18-2008, 10:38 AM
  3. C or C++
    By AcerN30 in forum Game Programming
    Replies: 41
    Last Post: 05-30-2008, 07:57 PM
  4. Linking OpenGL in Dev-C++
    By linkofazeroth in forum Game Programming
    Replies: 4
    Last Post: 09-13-2005, 11:17 AM
  5. Grabbing Text from a Window
    By jimothygu in forum Windows Programming
    Replies: 4
    Last Post: 10-06-2003, 08:29 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21