3

ボーダレスでなければならないコンソール アプリケーションを作成しています。これを実現するために、以下に示すように、コンソール ウィンドウのスタイルとウィンドウ領域を変更しました。
ある時点で、コンソール ウィンドウが通常 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;
}
4

1 に答える 1

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;
}
于 2013-02-25T17:50:57.507 に答える