我正在制作一个必须无边界的控制台应用程序;为了实现这一点,我已经改变了控制台窗口样式和窗口区域,如下所示
在某些时候,我遇到一个问题,使得控制台窗口通常大小约为2x1个字符,或者有时完全错误(不可见的客户区域,某些部分为白色,某些部分透明,随机边框等)
有人告诉我,将ShowWindow(hWnd, SW_HIDE);
添加到它现在所在的行可以解决问题。它确实。
现在我想弄清楚为什么问题首先存在,如果还有其他方法可以阻止它?
我被告知问题可能发生,因为Windows试图在我的程序的同时访问窗口属性(位置,样式,大小等)。不过,我不知道这是否属实。
#include <Windows.h>
#include <iostream>
int main()
{
HANDLE hCon = GetStdHandle(STD_OUTPUT_HANDLE);
SMALL_RECT srWnd = {0, 0, 1, 1};
SetConsoleWindowInfo(hCon, 1, &srWnd);
COORD cBuffSize = {81, 26};
SetConsoleScreenBufferSize(hCon, cBuffSize);
srWnd.Top = 0;
srWnd.Right = 80;
srWnd.Bottom = 25;
srWnd.Left = 0;
SetConsoleWindowInfo(hCon, 1, &srWnd);
// When the next two lines are moved so that they are the first two lines inside main(), the window gets bugged.
HWND hWnd = GetConsoleWindow();
ShowWindow(hWnd, SW_HIDE); // Or if you just remove this line
RECT rClnt;
GetClientRect(hWnd, &rClnt);
SetWindowLong(hWnd, GWL_STYLE, WS_POPUP);
LONG exStyle = GetWindowLong(hWnd, GWL_EXSTYLE);
exStyle &= ~(WS_EX_CLIENTEDGE | WS_EX_APPWINDOW);
SetWindowLongPtr(hWnd, GWL_EXSTYLE, exStyle | WS_EX_TOOLWINDOW);
rClnt.right += 1;
HRGN rgnClnt = CreateRectRgnIndirect(&rClnt);
SetWindowRgn(hWnd, rgnClnt, 1);
RECT rScrn;
GetWindowRect(GetDesktopWindow(), &rScrn);
SetWindowPos(hWnd, HWND_TOPMOST, rScrn.right / 2 - rClnt.right / 2, rScrn.bottom / 2 - rClnt.bottom / 2, 0, 0, SWP_NOSIZE | SWP_FRAMECHANGED);
ShowWindow(hWnd, SW_SHOW);
std::cin.get();
return 0;
}
答案 0 :(得分:0)
以下是Windows 8的工作示例。但是每次移动/调整/恢复控制台时都需要刷新窗口区域。
#include <windows.h>
#include <tchar.h>
#include <conio.h>
#include <dwmapi.h>
//#pragma comment(lib, "uxtheme.lib")
#pragma comment(lib, "dwmapi.lib")
int _tmain(int argc, _TCHAR* argv[])
{
HWND hWnd = GetConsoleWindow();
DWMNCRENDERINGPOLICY policy = DWMNCRP_DISABLED;
DwmSetWindowAttribute(hWnd, DWMWA_NCRENDERING_POLICY, &policy, sizeof(DWMNCRENDERINGPOLICY));
//SetThemeAppProperties(0);
//SetWindowThemeNonClientAttributes(hWnd, STAP_VALIDBITS, 0);
//SetWindowPos(hWnd, NULL, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE | SWP_NOZORDER | SWP_FRAMECHANGED);
//RedrawWindow(hWnd, NULL, NULL, RDW_INVALIDATE | RDW_UPDATENOW);
RECT rClnt, rWnd;
GetClientRect(hWnd, &rClnt);
GetWindowRect(hWnd, &rWnd);
POINT pt = {0,0};
MapWindowPoints(hWnd, NULL, &pt, 1);
OffsetRect(&rClnt, pt.x-rWnd.left, pt.y-rWnd.top);
HRGN rgnClnt = CreateRectRgnIndirect(&rClnt);
SetWindowRgn(hWnd, rgnClnt, 1);
_getch();
return 0;
}