-
August 19th, 2013, 02:58 PM
#16
Re: about console size
Originally Posted by Cambalinho
number 6.
Then go to the Win32 Error Codes adn look for the code with "number 6". It is
Victor Nijegorodov
-
August 19th, 2013, 02:59 PM
#17
Re: about console size
Originally Posted by Cambalinho
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)
-
August 19th, 2013, 03:04 PM
#18
-
August 19th, 2013, 03:11 PM
#19
Re: about console size
Originally Posted by 2kaud
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 ();
}
-
August 19th, 2013, 03:23 PM
#20
Re: about console size
Originally Posted by Cambalinho
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)
-
August 19th, 2013, 03:32 PM
#21
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)
-
August 19th, 2013, 03:36 PM
#22
Re: about console size
Originally Posted by 2kaud
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?
-
August 19th, 2013, 03:57 PM
#23
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)
-
August 19th, 2013, 04:00 PM
#24
Re: about console size
Originally Posted by Cambalinho
what is the diference between HWND and HANDLE?
Why don't you want to read MSDN about HWND and HANDLE?
Victor Nijegorodov
-
August 19th, 2013, 04:01 PM
#25
Re: about console size
Originally Posted by 2kaud
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
-
August 19th, 2013, 04:04 PM
#26
Re: about console size
Originally Posted by VictorN
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
-
August 19th, 2013, 04:07 PM
#27
Re: [RESOLVED] about console size
sorry to both but i can't rate you again... thanks for all.. thanks
-
August 19th, 2013, 04:29 PM
#28
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)
-
August 19th, 2013, 04:32 PM
#29
Re: [RESOLVED] about console size
Originally Posted by 2kaud
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?
-
August 19th, 2013, 04:41 PM
#30
Re: [RESOLVED] about console size
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)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|