Then go to the Win32 Error Codes adn look for the code with "number 6". It is
Quote:
ERROR_INVALID_HANDLEThe handle is invalid
Printable View
Then go to the Win32 Error Codes adn look for the code with "number 6". It is
Quote:
ERROR_INVALID_HANDLEThe handle is invalid
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 ();
}
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.
Note you should always test for errors after calling a function! - omitted in main here for clarityCode:#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;
}
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!Quote:
why they complicate so many in C++ with API functions
NO!! A console handle is not a window handle or a device context. DC is not used in console programming.Code:console.WindowHandle =hwndFound;
console.WindowDC =GetDC( hwndFound);
ConsoleDC=console.WindowDC;
ConsoleHandle=console.WindowHandle;
COORD consize;
GetConsoleSize(ConsoleHandle, consize);
SetConsoleScreenBufferSize(ConsoleHandle, consize);
To get a handle to the console being used, use GetStdHandle as per my example.
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.Quote:
if you don't use DC, you can't use graphics lol
i build a game in console heheheheh
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(...).Quote:
what is the diference between HWND and HANDLE?
sorry to both but i can't rate you again... thanks for all.. thanks
Why go through all the above if all you want is the HWND of the console? Just useCode:#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;
Simples!:DCode:console.WindowHandle = GetConsoleWindow();
No - English. Simples is from a famous TV commercial featuring Meerkats!Quote:
are you portuguese?
http://www.google.co.uk/search?q=com...w=1244&bih=862