CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Jan 2009
    Location
    England
    Posts
    57

    [RESOLVED] Window_buffer_size_event

    I am trying to capture a resize event for a console window. Everything works in the code I pulled off MSDN site except the resize? Could someone point me in the right direction as to why I'm not trapping this one event type?

    Code:
    #include <windows.h>
    #include <stdio.h>
    
    
    HANDLE hStdin;
    DWORD fdwSaveOldMode;
    
    
    VOID ErrorExit(LPSTR);
    VOID KeyEventProc(KEY_EVENT_RECORD);
    VOID MouseEventProc(MOUSE_EVENT_RECORD);
    VOID ResizeEventProc(WINDOW_BUFFER_SIZE_RECORD);
    
    
    int main(VOID)
    {
        DWORD cNumRead, fdwMode, i;
        INPUT_RECORD irInBuf[128];
        int counter=0;
    
    
        // Get the standard input handle.
        hStdin = GetStdHandle(STD_INPUT_HANDLE);
        if (hStdin == INVALID_HANDLE_VALUE)
            ErrorExit("GetStdHandle");
    
    
        // Save the current input mode, to be restored on exit.
        if (! GetConsoleMode(hStdin, &fdwSaveOldMode) )
            ErrorExit("GetConsoleMode");
    
    
        // Enable the window and mouse input events.
        fdwMode = ENABLE_WINDOW_INPUT | ENABLE_MOUSE_INPUT;
        if (! SetConsoleMode(hStdin, fdwMode) )
            ErrorExit("SetConsoleMode");
    
    
        // Loop to read and handle the next 100 input events.
        while (counter++ <= 100)
        {
            // Wait for the events.
    
    
            if (! ReadConsoleInput(
                    hStdin,      // input buffer handle
                    irInBuf,     // buffer to read into
                    128,         // size of read buffer
                    &cNumRead) ) // number of records read
                ErrorExit("ReadConsoleInput");
    
    
            // Dispatch the events to the appropriate handler.
            for (i = 0; i < cNumRead; i++)
            {
                switch(irInBuf[i].EventType)
                {
                    case KEY_EVENT: // keyboard input
                        KeyEventProc(irInBuf[i].Event.KeyEvent);
                        break;
    
    
                    case MOUSE_EVENT: // mouse input
                        MouseEventProc(irInBuf[i].Event.MouseEvent);
                        break;
    
    
                    case WINDOW_BUFFER_SIZE_EVENT: // scrn buf. resizing
                        ResizeEventProc( irInBuf[i].Event.WindowBufferSizeEvent );
                        break;
    
    
                    case FOCUS_EVENT:  // disregard focus events
    
    
                    case MENU_EVENT:   // disregard menu events
                        break;
    
    
                    default:
                        ErrorExit("Unknown event type");
                        break;
                }
            }
        }
    
    
        // Restore input mode on exit.
        SetConsoleMode(hStdin, fdwSaveOldMode);
    
    
        return 0;
    }
    
    
    VOID ErrorExit (LPSTR lpszMessage)
    {
        fprintf(stderr, "%s\n", lpszMessage);
    
    
        // Restore input mode on exit.
    
    
        SetConsoleMode(hStdin, fdwSaveOldMode);
    
    
        ExitProcess(0);
    }
    
    
    VOID KeyEventProc(KEY_EVENT_RECORD ker)
    {
        printf("Key event: ");
    
    
        if(ker.bKeyDown)
            printf("key pressed\n");
        else printf("key released\n");
    }
    
    
    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;
        }
    }
    
    
    VOID ResizeEventProc(WINDOW_BUFFER_SIZE_RECORD wbsr)
    {
        printf("Resize event\n");
        printf("Console screen buffer is %d columns by %d rows.\n", wbsr.dwSize.X, wbsr.dwSize.Y);
    }
    Last edited by Gerald Bates; October 1st, 2014 at 11:12 AM.
    What the mind can conceive it can achieve.

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: Window_buffer_size_event

    Please post your code so that we can have a look at it and advise.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3
    Join Date
    Jan 2009
    Location
    England
    Posts
    57

    Re: Window_buffer_size_event

    Quote Originally Posted by 2kaud View Post
    Please post your code so that we can have a look at it and advise.
    Sorry, I have had issues with my internet connection. Post has now been edited.
    What the mind can conceive it can achieve.

  4. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: Window_buffer_size_event

    Your resize code works correctly as expected. However I don't think the way it works is the way you expect it to work. You correctly get an event when the screen buffer is resized. However, just resizing the console screen doesn't resize the console buffer. The two are somewhat independent of each other. You have the console screen buffer and then you have the console window which shows a part of the console buffer - that's how console screen scrolling works. The scrolling moves the console window over the console buffer to display different parts of it. The console buffer can be much larger than the console window (but not smaller). The WINDOW_BUFFER_SIZE_EVENT is called when the size of the buffer is changed - not the size/position of the window.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  5. #5
    Join Date
    Jan 2009
    Location
    England
    Posts
    57

    Re: Window_buffer_size_event

    Thanks 2kaud, You are right that I thought it was capturing a console resize. I needed to trap this so I can change the screen buffer if the console window size changed. I going to test another idea and see if I can capture a change in console size then I know to change the screen buffer size accordingly. Will post again with my idea once done.
    What the mind can conceive it can achieve.

  6. #6
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: Window_buffer_size_event

    Note that you can only change the console screen size up to a maximum the size of the console buffer. You can't resize the console screen to be bigger than the buffer although the window can be smaller than the buffer. So if you are thinking of being able to resize the console window to be bigger than the buffer, capture the increase change in console size and then resize the buffer, then this is a no-no. If you manage to capture the console window size reduction and then change the screen buffer to match the new console window size, you won't be able to make the console window bigger again as that would first mean increasing the size of the screen buffer before the window resize.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  7. #7
    Join Date
    Jan 2009
    Location
    England
    Posts
    57

    Re: Window_buffer_size_event

    Quote Originally Posted by 2kaud View Post
    Note that you can only change the console screen size up to a maximum the size of the console buffer. You can't resize the console screen to be bigger than the buffer although the window can be smaller than the buffer. So if you are thinking of being able to resize the console window to be bigger than the buffer, capture the increase change in console size and then resize the buffer, then this is a no-no. If you manage to capture the console window size reduction and then change the screen buffer to match the new console window size, you won't be able to make the console window bigger again as that would first mean increasing the size of the screen buffer before the window resize.
    Yes, I have learned this the hard way. Spent hours trying to get this to work to no avail. I could not get the console size to get bigger at all, neither could I find a way of detecting the console size change.

    Many thanks to you all for your help, appreciated.
    What the mind can conceive it can achieve.

  8. #8
    Join Date
    Oct 2022
    Posts
    1

    Re: [RESOLVED] Window_buffer_size_event

    I wrote this function to deal with a need for capturing the console window (not buffer) resize event.
    Called from the right place in your program, the window resize event can be captured within millisecs of the event itself.
    Good luck to you.

    Code:

    Code:
    #include <windows.h>
    /*----------------------------------------------------------------------------------------
     
    	check_console_window_resize_event()
    
    	2022.10.05 - Created by Greg Spears and placed in the public domain.
    			   - Tested -- use at your own risk.
    
    	Params: none
    	Returns: TRUE if the console window has changed size.  FALSE if not.
    
    	USAGE: Best practice is to call the function repeatedly from your main application 
    	loop. 	Preferably a place where the function can be called several times per second 
    	throughout the program's run time.
    *----------------------------------------------------------------------------------------*/
    int check_console_window_resize_event(void)
    {
    static int old_screen_w=0, old_screen_h=0;
    static HANDLE hConOut=NULL;   /* variables declared static hold their value between function calls.*/
    int current_screen_w, current_screen_h;
    int window_resized = FALSE;
    CONSOLE_SCREEN_BUFFER_INFO csbi;
    SECURITY_ATTRIBUTES sa;
    
    	if(!hConOut)
    	{
    		/* First call -- get the window handle one time and save it*/
    		sa.nLength = sizeof(sa);
    		sa.lpSecurityDescriptor = NULL;
    		sa.bInheritHandle = TRUE;
    		/* Using CreateFile we get the true console handle, avoiding any redirection.*/
    		hConOut = CreateFile( TEXT("CONOUT$"),
    		     GENERIC_READ | GENERIC_WRITE,
    		     FILE_SHARE_READ | FILE_SHARE_WRITE,
    		     &sa, OPEN_EXISTING, (DWORD)0, (HANDLE)0 );
    	}
    	if(!hConOut) /* actually, this is a bad error, let your app handle the error as needed*/
    		return FALSE;
    
    	GetConsoleScreenBufferInfo( hConOut, &csbi );
    	current_screen_w  = csbi.srWindow.Right - csbi.srWindow.Left + 1;
    	current_screen_h  = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
    
    	if(!old_screen_w && !old_screen_h)
    	{
    		/* Execution comes here if this is first time this function is called.  
    		** Initialize the static variables and bail...*/
    		old_screen_w = current_screen_w;
    		old_screen_h = current_screen_h;
    		return FALSE;
    	}
    
    	/* At last the real work of this function can be realized...*/
    	if(current_screen_w != old_screen_w || current_screen_h != old_screen_h)
    	{
    		old_screen_w = current_screen_w;
    		old_screen_h = current_screen_h;
    		window_resized = TRUE;
    	}
    
    	return window_resized;
    }
    Last edited by VictorN; October 6th, 2022 at 06:48 AM. Reason: adding CODE tags

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

Featured