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
  1. #16
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,395

    Re: about console size

    Quote Originally Posted by Cambalinho View Post
    number 6.
    Then go to the Win32 Error Codes adn look for the code with "number 6". It is
    ERROR_INVALID_HANDLE
    The handle is invalid
    Victor Nijegorodov

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

    Re: about console size

    Quote Originally Posted by Cambalinho View Post
    number 6.
    error 6 means that the handle is invalid - ie your consoleDC value is not a handle to a console. How did you obtain it?
    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. #18
    Join Date
    Apr 2009
    Posts
    1,355

    Re: about console size

    Quote Originally Posted by 2kaud View Post
    I gave you the code for this in post #2.
    why they complicate so many in C++ with API functions
    i get the same error
    6. i don't know what means

  4. #19
    Join Date
    Apr 2009
    Posts
    1,355

    Re: about console size

    Quote Originally Posted by 2kaud View Post
    error 6 means that the handle is invalid - ie your consoleDC value is not a handle to a console. How did you obtain it?
    yes i mistake it lol
    see the sub:
    Code:
    struct Window
    	{
    		HWND WindowHandle;
    		HDC WindowDC;
    	};
    
    HWND ConsoleHandle;
    	HDC ConsoleDC;
    	Window b;
    
    void GetConsoleHwnd(Window &console)
    	{	
    		#define MY_BUFSIZE 1024 // Buffer size for console window titles.
    		HWND hwndFound;         // This is what is returned to the caller.
    		char pszNewWindowTitle[MY_BUFSIZE]; // Contains fabricated
                                               // WindowTitle.
    		char pszOldWindowTitle[MY_BUFSIZE]; // Contains original
                                               // WindowTitle.
    
    		// Fetch current window title.
    	
    		GetConsoleTitle(pszOldWindowTitle, MY_BUFSIZE);
    
    		// Format a "unique" NewWindowTitle.
    
    		wsprintf(pszNewWindowTitle,"%d/%d",
                 GetTickCount(),
                 GetCurrentProcessId());
    
    		// Change current window title.
    
    		SetConsoleTitle(pszNewWindowTitle);
    
    		// Ensure window title has been updated.
    
    		Sleep(10);
    
    		// Look for NewWindowTitle.
    
    		hwndFound=FindWindow(NULL, pszNewWindowTitle);
    
    		// Restore original window title.
    
    		SetConsoleTitle(pszOldWindowTitle);
    		
    		console.WindowHandle =hwndFound;
    		console.WindowDC =GetDC( hwndFound);
    		ConsoleDC=console.WindowDC;
    		ConsoleHandle=console.WindowHandle; 
    		
    		
    		COORD    consize;
    
    GetConsoleSize(ConsoleHandle, consize);
    SetConsoleScreenBufferSize(ConsoleHandle, consize);
    		cout << GetLastError ();
    		
    	}

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

    Re: about console size

    Quote Originally Posted by Cambalinho View Post
    why they complicate so many in C++ with API functions
    i get the same error
    6. i don't know what means
    As explained, error 6 means that the value of the handle passed to the function SetConsoleWindowInfo as first parameter is not valid. As I asked before, how do you obtain this value? You have it named ConsoleDC but a console handle is nothing at all to do with a Device Context in the sense used in Windows gui programming!

    The program below when run from a console will set the buffer to be the same size as the console window and will therefore remove the scroll bars.

    Code:
    #define WINVER 0x0501
    #define _WIN32_WINNT WINVER
    
    #include <windows.h>
    
    DWORD GetConsoleSize(HANDLE sout, COORD& size)
    {
    CONSOLE_FONT_INFO	cfinfo;
    
    COORD		fsize;
    
    WINDOWINFO	winfo;
    
    	if (!GetCurrentConsoleFont(sout, FALSE, &cfinfo)) {
    		return (GetLastError());
    	}
    
    	fsize = GetConsoleFontSize(sout, cfinfo.nFont);
    	if (!fsize.X && !fsize.Y) {
    		return (GetLastError());
    	}
    
    	winfo.cbSize = sizeof(WINDOWINFO);
    	if (!GetWindowInfo(GetConsoleWindow(), &winfo)) {
    		return (GetLastError());
    	}
    
    	size.Y = (SHORT)((winfo.rcClient.bottom - winfo.rcClient.top) / fsize.Y);
    	size.X = (SHORT)((winfo.rcClient.right - winfo.rcClient.left) / fsize.X);
    
    	return (0);
    }
    
    int main()
    {
    HANDLE hcon = GetStdHandle(STD_OUTPUT_HANDLE);
    
    COORD	csize;
    
    	GetConsoleSize(hcon, csize);
    	SetConsoleScreenBufferSize(hcon, csize);
    	return 0;
    }
    Note you should always test for errors after calling a function! - omitted in main here for clarity

    why they complicate so many in C++ with API functions
    Windows/console programming is fairly complicated. That's why you need to learn how to do these things. You can't just sit down and write these kinds of programs 'on the hoof'! There are about 70 api functions alone just referring to console programming!
    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)

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

    Re: about console size

    Code:
    console.WindowHandle =hwndFound;
    console.WindowDC =GetDC( hwndFound);
    ConsoleDC=console.WindowDC;
    ConsoleHandle=console.WindowHandle; 
    		
    COORD    consize;
    
    GetConsoleSize(ConsoleHandle, consize);
    SetConsoleScreenBufferSize(ConsoleHandle, consize);
    NO!! A console handle is not a window handle or a device context. DC is not used in console programming.

    To get a handle to the console being used, use GetStdHandle as per my example.
    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. #22
    Join Date
    Apr 2009
    Posts
    1,355

    Re: about console size

    Quote Originally Posted by 2kaud View Post
    Code:
    console.WindowHandle =hwndFound;
    console.WindowDC =GetDC( hwndFound);
    ConsoleDC=console.WindowDC;
    ConsoleHandle=console.WindowHandle; 
    		
    COORD    consize;
    
    GetConsoleSize(ConsoleHandle, consize);
    SetConsoleScreenBufferSize(ConsoleHandle, consize);
    NO!! A console handle is not a window handle or a device context. DC is not used in console programming.

    To get a handle to the console being used, use GetStdHandle as per my example.
    if you don't use DC, you can't use graphics lol
    i build a game in console heheheheh
    what is the diference between HWND and HANDLE?

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

    Re: about console size

    if you don't use DC, you can't use graphics lol
    i build a game in console heheheheh
    Good luck! You don't normally do graphics (except character graphics) in a console window - thats why its called a console and is just text! If you want graphics, I suggest you create a normal windows program.

    what is the diference between HWND and HANDLE?
    In console terms, a lot! They are very different things. A HANDLE is a handle to either the input, output or error buffers whereas HWND is a handle to the console window itself. All the console api functions need a console handle (HANDLE) as opposed to a windows handle (HWND). To get console handles use GetStdHandle(...).
    Last edited by 2kaud; August 19th, 2013 at 04:01 PM.
    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)

  9. #24
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,395

    Re: about console size

    Quote Originally Posted by Cambalinho View Post
    what is the diference between HWND and HANDLE?
    Why don't you want to read MSDN about HWND and HANDLE?
    Victor Nijegorodov

  10. #25
    Join Date
    Apr 2009
    Posts
    1,355

    Re: about console size

    Quote Originally Posted by 2kaud View Post
    You don't do graphics (except character graphics) in a console window - thats why its called a console and is just text! If you want graphics, create a normal windows program.



    In console terms, a lot! They are very different things. A HANDLE is a handle to either the input, output or error buffers whereas HWND is a handle to the console window itself. All the console api functions need a console handle (HANDLE) as opposed to a windows handle (HWND). To get console handles use GetStdHandle(...).
    why my class have hwnd and dc(and handle now)? for the programmer be free

  11. #26
    Join Date
    Apr 2009
    Posts
    1,355

    Re: about console size

    Quote Originally Posted by VictorN View Post
    Why don't you want to read MSDN about HWND and HANDLE?
    i don't want be rude, but i don't like how they explain some things. ok.. my english is limited, but sometimes i don't understand what they said and very const aren't declared

  12. #27
    Join Date
    Apr 2009
    Posts
    1,355

    Re: [RESOLVED] about console size

    sorry to both but i can't rate you again... thanks for all.. thanks

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

    Re: [RESOLVED] about console size

    Code:
    #define MY_BUFSIZE 1024 // Buffer size for console window titles.
    HWND hwndFound;         // This is what is returned to the caller.
    char pszNewWindowTitle[MY_BUFSIZE]; // Contains fabricated WindowTitle.
    char pszOldWindowTitle[MY_BUFSIZE]; // Contains original WindowTitle.
    
    	// Fetch current window title.
    	GetConsoleTitle(pszOldWindowTitle, MY_BUFSIZE);
    
    	// Format a "unique" NewWindowTitle.
    	wsprintf(pszNewWindowTitle,"%d/%d", GetTickCount(), GetCurrentProcessId());
    
    	// Change current window title.
    	SetConsoleTitle(pszNewWindowTitle);
    
    	// Ensure window title has been updated.
    	Sleep(10);
    
    	// Look for NewWindowTitle.
    	hwndFound=FindWindow(NULL, pszNewWindowTitle);
    
    	// Restore original window title.
    	SetConsoleTitle(pszOldWindowTitle);
    
            console.WindowHandle =hwndFound;
    Why go through all the above if all you want is the HWND of the console? Just use

    Code:
    console.WindowHandle = GetConsoleWindow();
    Simples!
    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)

  14. #29
    Join Date
    Apr 2009
    Posts
    1,355

    Re: [RESOLVED] about console size

    Quote Originally Posted by 2kaud View Post
    Code:
    #define MY_BUFSIZE 1024 // Buffer size for console window titles.
    HWND hwndFound;         // This is what is returned to the caller.
    char pszNewWindowTitle[MY_BUFSIZE]; // Contains fabricated WindowTitle.
    char pszOldWindowTitle[MY_BUFSIZE]; // Contains original WindowTitle.
    
    	// Fetch current window title.
    	GetConsoleTitle(pszOldWindowTitle, MY_BUFSIZE);
    
    	// Format a "unique" NewWindowTitle.
    	wsprintf(pszNewWindowTitle,"%d/%d", GetTickCount(), GetCurrentProcessId());
    
    	// Change current window title.
    	SetConsoleTitle(pszNewWindowTitle);
    
    	// Ensure window title has been updated.
    	Sleep(10);
    
    	// Look for NewWindowTitle.
    	hwndFound=FindWindow(NULL, pszNewWindowTitle);
    
    	// Restore original window title.
    	SetConsoleTitle(pszOldWindowTitle);
    
            console.WindowHandle =hwndFound;
    Why go through all the above if all you want is the HWND of the console? Just use

    Code:
    console.WindowHandle = GetConsoleWindow();
    Simples!
    lol thanks for that
    are you portuguese?

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

    Re: [RESOLVED] about console size

    are you portuguese?
    No - English. Simples is from a famous TV commercial featuring Meerkats!

    http://www.google.co.uk/search?q=com...w=1244&bih=862
    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)

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
  •  





Click Here to Expand Forum to Full Width

Featured