Results 1 to 9 of 9

Thread: Timer in Console...

  1. #1
    AnT
    Guest

    Timer in Console...

    How do I manipulate the WM_TIMER message in a console program? Here is my code:
    PHP Code:
    #include <iostream.h>
    #include <windows.h>
    void sleep(int milliseconds);
    void time();
    int main()
    {
        
    HWND myhwnd;
        
    myhwnd FindWindow("ConsoleWindowClass"NULL);
        
    SetTimer(myhwnd11000NULL);
        
    cout << "Hello." << endl;
        
    cout << "Exiting in 2 seconds.";
        
    sleep(2000);
        return 
    0;
    }
    void sleep(int milliseconds)
    {
        
    Sleep(milliseconds);
    }
    void time()
    {
        
    HWND myhwnd;
        
    myhwnd FindWindow("ConsoleWindowClass"NULL);
        
    MessageBox(myhwnd"Hello, this is a timer""MOO!!"NULL);

    I know there isn't much about timers in there, but if you all could just tell me how to manipulate WM_TIMER, I'd be grateful.

  2. #2
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    You need to setup a TimerProc callback
    PHP Code:
    [i]From MSDN[/i]
    VOID CALLBACK TimerProc(
      
    HWND hwnd,         // handle to window
      
    UINT uMsg,         // WM_TIMER message
      
    UINT_PTR idEvent,  // timer identifier
      
    DWORD dwTime       // current system time
    ); 
    Then pass the address of TimerProc into SetTimer.

    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  3. #3
    AnT
    Guest
    Can you give me an example of how to do this? (Just fiddling with C++, really a complete newb...)

  4. #4
    AnT
    Guest
    So no one has done this before?

  5. #5
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    You define whatever you want the timer to do in the TimerProc function (or whatever your function is called), then give SetTimer() a pointer to that function so it can call it whenever the timer goes off.

    Crptcblade already gave you the function declaration, you just need to define its content:

    Code:
    VOID CALLBACK TimerProc(
      HWND hwnd,         // handle to window
      UINT uMsg,         // WM_TIMER message
      UINT_PTR idEvent,  // timer identifier
      DWORD dwTime       // current system time
    )
    {
      // your code here
    }
    Harry.

    "From one thing, know ten thousand things."

  6. #6
    AnT
    Guest
    Ok I fixed the error but the message box in the function is not showing up. Here is my code:
    PHP Code:
    //Includes
    #include <iostream.h>
    #include <windows.h>

    //Globals
    HWND myhwnd=FindWindow("ConsoleWindowClass"NULL);
    //Function Prototypes
    void sleep(int milliseconds);
    VOID CALLBACK TimerProc(HWND hwndUINT uMsgUINT_PTR idEventDWORD dwTime);
    int main()
    {
        
    SetTimer(myhwnd11000, (TIMERPROC) &TimerProc);
        
    cout << "Hello." << endl;
        
    MessageBox(myhwnd"Wow, I can make message boxes...haha""WEEEEE!!!"MB_OK MB_ICONSTOP);
        
    cout << "Exiting in 2 seconds.\n";
        
    //sleep(2000);
        
    return 0;
    }
    void sleep(int milliseconds)
    {
        
    Sleep(milliseconds);
    }
    VOID CALLBACK TimerProc(
      
    HWND hwnd,         // handle to window
      
    UINT uMsg,         // WM_TIMER message
      
    UINT_PTR idEvent,  // timer identifier
      
    DWORD dwTime       // current system time
    )
    {
      
    MessageBox(myhwnd"HELLO""HELLO"MB_OK);


  7. #7
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    That doesn't work unless GetMessage is called - so it doesn't work in console apps.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  8. #8
    AnT
    Guest
    Ok, so how do I make certain events fire off at a certain incrememnt in a console app?

  9. #9
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    You can create a high-performance timer, it's events are fired in a seperate thread.
    Basically you don't have timers in console apps, they are designed for linear execution.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width