event handling - Programmer

This is a discussion on event handling - Programmer ; Hi everyone! I wrote a small aplication which creates a window and react on events like WM_KEYDOWN and WM_KEYUP untill an event WM_CLOSE shows. I didn't need window to be visible so I have created window with (0,0) coordinates and ...

+ Reply to Thread
Results 1 to 8 of 8

Thread: event handling

  1. event handling

    Hi everyone!

    I wrote a small aplication which creates a window and react on events
    like WM_KEYDOWN and WM_KEYUP untill an event WM_CLOSE shows. I didn't
    need window to be visible so I have created window with (0,0)
    coordinates and (0,0) width and height. But I still see the window in
    the taskbar. I have tried to create it with SW_HIDE option but then my
    application won't work. Does anyone have idea how to fix it?

    Here is the code:

    #include
    #include "stdio.h"

    const char g_szClassName[] = "myWindowClass";

    LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM
    lParam)
    {

    boolean processed;
    LRESULT result;

    result = 0;
    processed = false;

    switch (msg)
    {
    case WM_KEYDOWN:
    processed = true;
    printf("Key down\n");
    break;


    case WM_KEYUP:
    processed = true;
    printf("Key up\n");
    break;

    case WM_CLOSE:
    DestroyWindow(hwnd);
    PostQuitMessage(0);
    break;
    }

    if (!processed)
    result = DefWindowProc(hwnd, msg, wParam, lParam);

    return result;
    }

    int main()
    {
    WNDCLASSEX wc;
    HWND hwnd;
    MSG Msg;
    HINSTANCE hInstance = NULL;

    wc.cbSize = sizeof(WNDCLASSEX);
    wc.style = 0;
    wc.lpfnWndProc = WndProc;
    wc.cbClsExtra = 0;
    wc.cbWndExtra = 0;
    wc.hInstance = hInstance;
    wc.hIcon = NULL;
    wc.hCursor = NULL;
    wc.hbrBackground = NULL;
    wc.lpszMenuName = NULL;
    wc.lpszClassName = g_szClassName;
    wc.hIconSm = NULL;

    if(!RegisterClassEx(&wc))
    {
    MessageBox(NULL, "Window Registration Failed!", "Error!",
    MB_ICONEXCLAMATION | MB_OK);
    return 0;
    }

    hwnd = CreateWindow
    (
    g_szClassName,
    // TEXT("widhout text"),
    NULL,
    WS_POPUP,
    0, 0 , 0, 0,
    NULL,
    NULL,
    hInstance,
    NULL
    );
    if(hwnd == NULL)
    {
    MessageBox(NULL, "Window Creation Failed!", "Error!",
    MB_ICONEXCLAMATION | MB_OK);
    return 0;
    }

    ShowWindow(hwnd, SW_SHOWNORMAL);
    UpdateWindow(hwnd);

    while(GetMessage(&Msg, NULL, 0, 0) > 0)
    {
    TranslateMessage(&Msg);
    DispatchMessage(&Msg);
    }

    return Msg.wParam;
    }


  2. Re: event handling

    ginjasvinja wrote:
    > Hi everyone!
    >
    > I wrote a small aplication which creates a window and react on events
    > like WM_KEYDOWN and WM_KEYUP untill an event WM_CLOSE shows. I didn't
    > need window to be visible so I have created window with (0,0)
    > coordinates and (0,0) width and height. But I still see the window in
    > the taskbar. I have tried to create it with SW_HIDE option but then my
    > application won't work. Does anyone have idea how to fix it?


    Try creating the window with CreateWindowEx and the WS_EX_TOOLWINDOW
    extended style.

    --
    Scott McPhillips [VC++ MVP]


  3. Re: event handling

    It works! Thank you very much!


  4. Re: event handling

    It works! Thank you very much!


  5. Re: event handling

    Hi. it's me again. Is it possible to get some time limit for waitting?
    I want to program wait for some time for event(s) and then stop with
    getting messages. I've tried do read current date and to add it 5
    seconds. I also use one more variable which is difference beetwen limit
    time and current time. I 've tried to put the condition in while

    while((moreTime > 0) && (GetMessage(&Msg, NULL, 0, 0) > 0))

    but it won't work what I want. I am not sure what this line works. It
    looks like it waits for event even if time passed and when I put a key
    down it goes out of while. Is there any other way to do this?

    Code:

    #include
    #include "stdio.h"
    #include "time.h"

    const char g_szClassName[] = "myWindowClass";

    LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM
    lParam)
    {

    boolean processed;
    LRESULT result;

    result = 0;
    processed = false;

    switch (msg)
    {
    case WM_KEYDOWN:
    processed = true;
    printf("Key down\n");
    break;

    case WM_KEYUP:
    processed = true;
    printf("Key up\n");
    break;

    case WM_CLOSE:
    DestroyWindow(hwnd);
    PostQuitMessage(0);
    break;
    }

    if (!processed)
    result = DefWindowProc(hwnd, msg, wParam, lParam);

    return result;
    }

    int main()
    {
    WNDCLASSEX wc;
    HWND hwnd;
    MSG Msg;
    HINSTANCE hInstance = NULL;

    time_t currentTime,limitTime,moreTime;

    currentTime = time (NULL);
    limitTime = currentTime + 5;
    moreTime = limitTime - currentTime;
    printf("current %ld\n", currentTime);
    printf("limit %ld\n", limitTime);
    printf("moreTime %ld\n",moreTime);

    wc.cbSize = sizeof(WNDCLASSEX);
    wc.style = 0;
    wc.lpfnWndProc = WndProc;
    wc.cbClsExtra = 0;
    wc.cbWndExtra = 0;
    wc.hInstance = hInstance;
    wc.hIcon = NULL;
    wc.hCursor = NULL;
    wc.hbrBackground = NULL;
    wc.lpszMenuName = NULL;
    wc.lpszClassName = g_szClassName;
    wc.hIconSm = NULL;

    if(!RegisterClassEx(&wc))
    {
    MessageBox(NULL, "Window Registration Failed!", "Error!",
    MB_ICONEXCLAMATION | MB_OK);
    return 0;
    }

    hwnd = CreateWindowEx
    ( WS_EX_TOOLWINDOW,
    g_szClassName,
    // TEXT("beznadezan prozor"),
    NULL,
    WS_POPUP,
    0, 0 , 0, 0,
    NULL,
    NULL,
    hInstance,
    NULL
    );
    if(hwnd == NULL)
    {
    MessageBox(NULL, "Window Creation Failed!", "Error!",
    MB_ICONEXCLAMATION | MB_OK);
    return 0;
    }

    ShowWindow(hwnd, SW_SHOWNORMAL);
    UpdateWindow(hwnd);

    while((moreTime > 0) && (GetMessage(&Msg, NULL, 0, 0) > 0))
    {
    TranslateMessage(&Msg);
    DispatchMessage(&Msg);

    currentTime = time(NULL);
    moreTime = limitTime - currentTime;
    printf("moreTime %ld\n",moreTime);
    }

    return Msg.wParam;
    }


  6. Re: event handling

    "ginjasvinja" wrote in message
    news:1137683573.647967.31890@o13g2000cwo.googlegro ups.com...
    > Hi. it's me again. Is it possible to get some time limit for waitting?
    > I want to program wait for some time for event(s) and then stop with
    > getting messages. I've tried do read current date and to add it 5
    > seconds. I also use one more variable which is difference beetwen limit
    > time and current time. I 've tried to put the condition in while
    >
    > while((moreTime > 0) && (GetMessage(&Msg, NULL, 0, 0) > 0))
    >
    > but it won't work what I want. I am not sure what this line works. It
    > looks like it waits for event even if time passed and when I put a key
    > down it goes out of while. Is there any other way to do this?
    >


    Looks to me you could try a WM_TIMER message.
    When ready to start waiting:
    SetTimer (hwnd, identifier, elapse, custom_function);
    (see MSDN for exact parameters; steer clear of the custom function and just
    use the window handle)

    After "elapse" microsecs -- in your case 5000 -- a WM_TIMER is posted in
    your message loop.
    If you want to get rid of the timer do a KillTimer (hwnd, identifier) there.

    --
    [jongware]
    "It's not the despair... I can stand the despair. It's the hope."



  7. Re: event handling

    It works very well. Thank you for helping me!


  8. Re: event handling

    "ginjasvinja" wrote in message
    news:1138006437.535738.172590@z14g2000cwz.googlegr oups.com...
    > It works very well. Thank you for helping me!


    glad to be of service.
    .... this message will self-destruct in five seconds ...

    [jw]



+ Reply to Thread