[RESOLVED] how can i call LRESULT CALLBACK WindowProc() function? - Page 2
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 3 FirstFirst 123 LastLast
Results 16 to 30 of 33

Thread: [RESOLVED] how can i call LRESULT CALLBACK WindowProc() function?

  1. #16
    Join Date
    Apr 2009
    Posts
    660

    Re: how can i call LRESULT CALLBACK WindowProc() function?

    Quote Originally Posted by VictorN View Post
    Well, I never worked with console windows... Therefore I ask you: are you sure these messages come to console?
    Did you check it (with Spy++ or some other tool?)
    i don't know use the Spy++ but i have it

  2. #17
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,415

    Re: how can i call LRESULT CALLBACK WindowProc() function?

    Then learn how to use it and do use!
    Victor Nijegorodov

  3. #18
    Join Date
    Apr 1999
    Posts
    27,429

    Re: how can i call LRESULT CALLBACK WindowProc() function?

    Quote Originally Posted by Cambalinho View Post
    mouse, keyboard, paint, move, what is possible
    whats why i wanted use that function for i get more easy the messages
    But as I stated, you must take those messages you intercepted and give them to the original console window procedure when you are done with them. You can't just throw the messages away as your code is doing now.

    Regards,

    Paul McKenzie

  4. #19
    Join Date
    Apr 2009
    Posts
    660

    Re: how can i call LRESULT CALLBACK WindowProc() function?

    Quote Originally Posted by Paul McKenzie View Post
    But as I stated, you must take those messages you intercepted and give them to the original console window procedure when you are done with them. You can't just throw the messages away as your code is doing now.

    Regards,

    Paul McKenzie
    Code:
    #include <iostream>
    #include <windows.h>
    #include <conio.h>
    #include <string>
    using namespace std;
    
    LRESULT CALLBACK ConsoleWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
    {    
        switch(msg)
        {
            case WM_DESTROY:
                MessageBox(NULL, "The window was destroyed", "bye", MB_OK);
                break;
            case WM_CREATE:
                MessageBox(NULL, "The window was created", "hello", MB_OK);
        }
        return CallWindowProc((WNDPROC ) ConsoleWndProc, hWnd, msg, wParam, lParam);
    }
    
    int main()
    {
        string a;
        cout << GetConsoleWindow() << endl;//the value is diferent from 0(zero)\NULL
        SetLastError(0);
        cout << SetWindowLong(GetConsoleWindow(), GWL_WNDPROC, (LONG)ConsoleWndProc);//here i recive 0(zero)
        cout << GetLastError();
        cin >> a;
        return 0;
    }
    but how can i do it, if the SetWindowLong() function is give problems?
    i have sure about the GetConsoleWindow(), because i get the HDC from it and works fine. i have used the HDC

  5. #20
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,450

    Re: how can i call LRESULT CALLBACK WindowProc() function?

    but i get 1 error "Error code: 5 - Access is denied" on GetLastError()
    can you advice me?
    Yes - you can't do this with consoles! Windows won't let you subclass a console window.

    What exactly are you trying to achieve?
    Last edited by 2kaud; September 3rd, 2013 at 04:30 PM.
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  6. #21
    Join Date
    Apr 2009
    Posts
    660

    Re: how can i call LRESULT CALLBACK WindowProc() function?

    Quote Originally Posted by 2kaud View Post
    Yes - you can't do this with consoles! Windows won't let you subclass a console window.

    What exactly are you trying to achieve?
    just get the messages for add events in my class
    i'm trying another code but i don't know how works

    Code:
    MSG msg;
        BOOL bRet;
        //...
        while( (bRet = GetMessage( &msg, NULL, 0, 0 )) != 0)
        { 
            if (bRet == -1)
            {
                // handle the error and possibly exit
            }
            else
            {
                TranslateMessage(&msg); 
                DispatchMessage(&msg); 
            }
        }
    i belive these is the other way, but isn't working
    can you advice?
    i only need get the messages from console

  7. #22
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,450

    Re: how can i call LRESULT CALLBACK WindowProc() function?

    i only need get the messages from console
    Just because you need them, doesn't mean the console is going to give them! As far as I am aware, you don't get access to the console messages. A console is not like other windows and can't be treated as such.

    Again, why do you want them? What are you going to use them for?

    Have a look at PeekConsoleInput and ReadConsoleInput
    http://msdn.microsoft.com/en-us/libr...=vs.85%29.aspx
    http://msdn.microsoft.com/en-us/libr...=vs.85%29.aspx

    These functions give you access to certain console events - mouse move etc.
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  8. #23
    Join Date
    Apr 2009
    Posts
    660

    Re: how can i call LRESULT CALLBACK WindowProc() function?

    Quote Originally Posted by 2kaud View Post
    Just because you need them, doesn't mean the console is going to give them! As far as I am aware, you don't get access to the console messages. A console is not like other windows and can't be treated as such.

    Again, why do you want them? What are you going to use them for?

    Have a look at PeekConsoleInput and ReadConsoleInput
    http://msdn.microsoft.com/en-us/libr...=vs.85%29.aspx
    http://msdn.microsoft.com/en-us/libr...=vs.85%29.aspx

    These functions give you access to certain console events - mouse move etc.
    thank you
    i will try the code hehehe
    works fine and these code seems easy to understand. thanks for all thanks
    thanks to all

  9. #24
    Join Date
    Apr 2009
    Posts
    660

    Re: how can i call LRESULT CALLBACK WindowProc() function?

    i belive the MOUSE_EVENT_RECORD struture isn't completed.
    http://msdn.microsoft.com/en-us/libr...(v=vs.85).aspx
    why? because when i release the mouse button, the message is "button press"

    Code:
    VOID MouseEventProc(MOUSE_EVENT_RECORD mer)
    {
    #ifndef MOUSE_HWHEELED
    #define MOUSE_HWHEELED 0x0008
    #endif
        printf("Mouse event: ");
    
        switch(mer.dwEventFlags)
        {
            case 0:
    
                if(mer.dwButtonState == FROM_LEFT_1ST_BUTTON_PRESSED)
                {
                    printf("left button press \n");
                }
                else if(mer.dwButtonState == RIGHTMOST_BUTTON_PRESSED)
                {
                    printf("right button press \n");
                }
                else
                {
                    printf("button press\n");
                }
                break;
            case DOUBLE_CLICK:
                printf("double click\n");
                break;
            case MOUSE_HWHEELED:
                printf("horizontal mouse wheel\n");
                break;
            case MOUSE_MOVED:
                printf("mouse moved\n");
                break;
            case MOUSE_WHEELED:
                printf("vertical mouse wheel\n");
                break;
            default:
                printf("unknown\n");
                break;
        }
    }
    so what is the const for release?
    (edit i mistake 1 thing: i mean the:
    Code:
     else
                {
                    printf("button press\n");
                }
    is activated
    Last edited by Cambalinho; September 4th, 2013 at 04:46 AM.

  10. #25
    Join Date
    Apr 2009
    Posts
    660

    Re: [RESOLVED] how can i call LRESULT CALLBACK WindowProc() function?

    fiz 2 alteraçoes necessarias no codigo:
    Code:
    VOID MouseEventProc(MOUSE_EVENT_RECORD mer){
    #ifndef MOUSE_HWHEELED
    #define MOUSE_HWHEELED 0x0008
    #endif
        printf("Mouse event: ");
    
    
        switch(mer.dwEventFlags)
        {
            case 0:
    
    
                if(mer.dwButtonState == FROM_LEFT_1ST_BUTTON_PRESSED)
                {
                    printf("left button press \n");
                }
                else if(mer.dwButtonState == RIGHTMOST_BUTTON_PRESSED)
                {
                    printf("right button press \n");
                }
                else if(mer.dwButtonState == 4)
                {
                    printf("middle button press \n");
                }
                else if(mer.dwButtonState !=0)
                {
                    printf("another button press \n");
                }
                else
                {
                    printf("button release\n"); //see these line. is release and not press;)
                }
                break;
            case DOUBLE_CLICK:
                printf("double click\n");
                break;
            case MOUSE_HWHEELED:
                printf("horizontal mouse wheel\n");
                break;
            case MOUSE_MOVED:
                printf("mouse moved\n");
                break;
            case MOUSE_WHEELED:
                printf("vertical mouse wheel\n");
                break;
            default:
                printf("unknown\n");
                break;
        }
    }
    see the comment and the back 'else if', now the middle button is detected. and is release button and not press button
    thanks for all

  11. #26
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,415

    Re: how can i call LRESULT CALLBACK WindowProc() function?

    Quote Originally Posted by Cambalinho View Post
    i belive the MOUSE_EVENT_RECORD struture isn't completed.
    http://msdn.microsoft.com/en-us/libr...(v=vs.85).aspx
    why? because when i release the mouse button, the message is "button press"
    From the article you refer to:
    Remarks
    Mouse events are placed in the input buffer when the console is in mouse mode (ENABLE_MOUSE_INPUT).

    Mouse events are generated whenever the user moves the mouse, or presses or releases one of the mouse buttons. Mouse events are placed in a console's input buffer only when the console group has the keyboard focus and the cursor is within the borders of the console's window.
    So it looks like there is no any const for "release"
    Victor Nijegorodov

  12. #27
    Join Date
    Apr 2009
    Posts
    660

    Re: how can i call LRESULT CALLBACK WindowProc() function?

    Quote Originally Posted by VictorN View Post
    From the article you refer to:So it looks like there is no any const for "release"
    but if i relase the button, i recive '0'(zero)

  13. #28
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,450

    Re: [RESOLVED] how can i call LRESULT CALLBACK WindowProc() function?

    dwButtonState of 0 indicates that the last button pressed has been released. So you need to keep a record of which button has been pressed and then when you get 0 you know that button has been released!
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  14. #29
    Join Date
    Apr 2009
    Posts
    660

    Re: [RESOLVED] how can i call LRESULT CALLBACK WindowProc() function?

    Quote Originally Posted by 2kaud View Post
    dwButtonState of 0 indicates that the last button pressed has been released. So you need to keep a record of which button has been pressed and then when you get 0 you know that button has been released!
    that's why i update it again
    i update the code again:
    Code:
    VOID MouseEventProc(MOUSE_EVENT_RECORD mer)
    {
    #ifndef MOUSE_HWHEELED
    #define MOUSE_HWHEELED 0x0008
    #endif
        printf("Mouse event: ");
    
        switch(mer.dwEventFlags)
        {
            case 0:
    
                if(mer.dwButtonState == FROM_LEFT_1ST_BUTTON_PRESSED)
                {
                    printf("left button press \n");
                }
                else if(mer.dwButtonState == RIGHTMOST_BUTTON_PRESSED)
                {
                    printf("right button press \n");
                }
                else if(mer.dwButtonState == 4)
                {
                    printf("middle button press \n");
                }
                else if(mer.dwButtonState !=0)
                {
                    printf("another button press \n");
                }
                else if (mer.dwButtonState ==0)
                {
                    printf("button release\n");
                }
                else
                {
                    printf("another event detected\n");
                }
                break;
            case DOUBLE_CLICK:
                printf("double click\n");
                break;
            case MOUSE_HWHEELED:
                printf("horizontal mouse wheel\n");
                break;
            case MOUSE_MOVED:
                printf("mouse moved\n");
                break;
            case MOUSE_WHEELED:
                printf("vertical mouse wheel\n");
                break;
            default:
                printf("unknown\n");
                break;
        }
    }

    thanks for all

  15. #30
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,450

    Re: [RESOLVED] how can i call LRESULT CALLBACK WindowProc() function?

    Code:
    else
        {
             printf("another event detected\n");
        }
    You do realise that this code will never execute? Previously you have a test for a non-zero value, then you have a test for a zero value so one of those conditions must be true!


    You can therefore simplify the tests slightly

    Code:
    #define BUTTON_RELEASE 0
    
    if(mer.dwButtonState == FROM_LEFT_1ST_BUTTON_PRESSED)
                {
                    printf("left button press \n");
                }
                else if(mer.dwButtonState == RIGHTMOST_BUTTON_PRESSED)
                {
                    printf("right button press \n");
                }
                else if(mer.dwButtonState == FROM_LEFT_2ND_BUTTON_PRESSED)
                {
                    printf("middle button press \n");
                }
                else if(mer.dwButtonState == BUTTON_RELEASE)
                {
                    printf("button release\n");
                }
                else
                {
                    printf("another event detected\n");
                }
    IMO I prefer not to have nested if statements like this and would code this as

    Code:
    #define BUTTON_RELEASE 0
    
    switch (mer.dwButtonState) {
         case BUTTON_RELEASE:
              puts("button release");
              break;
    
         case FROM_LEFT_1ST_BUTTON_PRESSED:
              puts("left button press");
              break;
    
         case RIGHTMOST_BUTTON_PRESSED:
              puts("right button press");
              break;
    
         case FROM_LEFT_2ND_BUTTON_PRESSED:
              puts("middle button press");
              break;
    
         default:
              puts("another button press");
              break;
    }
    I would also use a #define (or a const int as appropriate) to define constants (such as the 0 for the button release test) to make the program more readable and more easily maintainable in the future.
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

Page 2 of 3 FirstFirst 123 LastLast

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center