Invalid Handle when using SetConsoleWindowInfo

白昼怎懂夜的黑 提交于 2020-01-05 04:19:08

问题


I'm new to C++ and decided to challenge myself with a small console game. To avoid typical flickering. Fromm what I got from MSDN docs I should be using a Console Buffer, but I took it easy and started from simple things like changing Window title and resizing it. The small program I wrote was meant to do just that, but for some reason I get Error Code 6 (should be "invalid handle") when I execute the SetConsoleWindowInfo.

Anyone who can point me in the right direction with this? Thank you in advance

#include <windows.h>
#include <stdio.h>
#include <iostream>

HANDLE  wHandle, bHandle;

SMALL_RECT wSize = { 0,0,100,100 };

int main() {
    wHandle = GetConsoleWindow();
    if (wHandle == NULL) {
        printf("Handle is Null");
    }
    SetConsoleTitle(L"NewTitle");
    if (!SetConsoleWindowInfo(wHandle, TRUE, &wSize)) {
        printf("SetConsoleWindowInfo (%d)\n", GetLastError());

    }

    getchar();
    return 0;
}

回答1:


Maybe this will help:

#include <windows.h>
#include <stdio.h>
#include <iostream>

HANDLE  wHandle, bHandle;

//SMALL_RECT wSize = { 0,0,100,100 }; // If SetConsoleWindow fails with code 87, then this is too big!
SMALL_RECT wSize = { 0,0,60,20 }; // Works on my screen!

int main() {
//  wHandle = GetConsoleWindow(); 
    wHandle = GetStdHandle(STD_OUTPUT_HANDLE); // See comment by RbMm
    if (wHandle == NULL) {
        printf("Handle is Null");
    }
//  SetConsoleTitle(L"NewTitle"); // Don't use a wide character string!
    SetConsoleTitle("NewTitle");
    if (!SetConsoleWindowInfo(wHandle, TRUE, &wSize)) {
        printf("SetConsoleWindowInfo (%d)\n", GetLastError());
    }
    getchar();
    return 0;
}

Feel free to ask, if you don't understand anything I've changed (or why I've changed it), but the comments address some of the issues.



来源:https://stackoverflow.com/questions/57753333/invalid-handle-when-using-setconsolewindowinfo

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!