Windows API背景故障

奥列格·阿普里利科夫(Oleg Aprelikov)

设置窗口类的背景时,我有一个奇怪的故障。我将其设置为:

wcWndClass.hbrBackground = (HBRUSH) (COLOR_WINDOW + 1);

但是,当我测试游戏时,这是我看到的:

背景故障图像

在此处输入图片说明

Windows API或我的代码有故障吗?谢谢你的帮助。

编辑:我发现背景只绘制直到标题。因此,如果我将文本控件上移或下移,背景也会上移或下移。

// Game
// Destroyable!

#include "Game.h"


HRESULT Game::Initialize(INT nCmdShow) {

  WNDCLASSEX wcWndClass;
  RECT rcSettingsClientRect;

  wcWndClass.style = CS_HREDRAW | CS_VREDRAW;
  wcWndClass.hbrBackground = (HBRUSH) (COLOR_WINDOW + 1);
  wcWndClass.cbSize = sizeof(WNDCLASSEX);
  wcWndClass.cbWndExtra = sizeof(LONG_PTR);
  wcWndClass.cbClsExtra = 0;
  wcWndClass.lpszClassName = L"wcWndClass";
  wcWndClass.lpszMenuName = NULL;
  wcWndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  wcWndClass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
  wcWndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
  wcWndClass.hInstance = GetModuleHandle(NULL);
  wcWndClass.lpfnWndProc = Game::WindowProcedure;

  HRESULT hResult = RegisterClassEx(&wcWndClass) ? S_OK : E_UNEXPECTED;

  if (SUCCEEDED(hResult)) {

    RECT rcSettingsWndRect = {0, 0, 300, 300};
    AdjustWindowRectEx(&rcSettingsWndRect, WS_OVERLAPPEDWINDOW, FALSE, WS_EX_WINDOWEDGE);
    m_hSettingsWnd = CreateWindowEx(WS_EX_WINDOWEDGE, L"wcWndClass", L"Game Settings", WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX, CW_USEDEFAULT, CW_USEDEFAULT, rcSettingsWndRect.right - rcSettingsWndRect.left, rcSettingsWndRect.bottom - rcSettingsWndRect.top, NULL, NULL, GetModuleHandle(NULL), this);
    hResult = m_hSettingsWnd ? S_OK : E_UNEXPECTED;

  }

  if (SUCCEEDED(hResult)) {

    RECT rcGameWndRect = {0, 0, 300, 300};
    AdjustWindowRectEx(&rcGameWndRect, WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX, FALSE, WS_EX_WINDOWEDGE);
    m_hGameWnd = CreateWindowEx(WS_EX_WINDOWEDGE, L"wcWndClass", L"Gee Yoo Aye", WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX, CW_USEDEFAULT, CW_USEDEFAULT, rcGameWndRect.right - rcGameWndRect.left, rcGameWndRect.bottom - rcGameWndRect.top, NULL, NULL, GetModuleHandle(NULL), this);
    hResult = m_hGameWnd ? S_OK : E_UNEXPECTED;

  }

  if (SUCCEEDED(hResult)) {

    GetClientRect(m_hSettingsWnd, &rcSettingsClientRect);
    InitCommonControls();
    m_hTitleWnd = CreateWindowEx(NULL, L"Static", L"Gee Yoo Aye", WS_VISIBLE | WS_CHILD | SS_CENTER, 0, rcSettingsClientRect.bottom / 7, rcSettingsClientRect.right, rcSettingsClientRect.bottom, m_hSettingsWnd, (HMENU) TITLE, GetModuleHandle(NULL), NULL);
    hResult = m_hTitleWnd ? S_OK : E_UNEXPECTED;

  }

  if (SUCCEEDED(hResult)) {

    SendMessage(m_hTitleWnd, WM_SETFONT, (WPARAM) CreateFont(36, 0, 0, 0, FW_BOLD, FALSE, FALSE, FALSE, ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE, NULL), TRUE);
    m_hSizeSliderWnd = CreateWindowEx(NULL, TRACKBAR_CLASS, L"Board Size", WS_VISIBLE | WS_CHILD | TBS_AUTOTICKS, rcSettingsClientRect.right / 6, rcSettingsClientRect.bottom / 2, rcSettingsClientRect.right * 2 / 3, 25, m_hSettingsWnd, (HMENU) SIZE_SLIDER, GetModuleHandle(NULL), NULL);
    hResult = m_hSizeSliderWnd ? S_OK : E_UNEXPECTED;

  }

  if (SUCCEEDED(hResult)) {

    ShowWindow(m_hSettingsWnd, nCmdShow);
    UpdateWindow(m_hSettingsWnd);

  }

  return hResult;

}


WPARAM Game::RunMessageLoop() {

  MSG mMsg;

  while (GetMessage(&mMsg, NULL, 0, 0)) {

    TranslateMessage(&mMsg);
    DispatchMessage(&mMsg);

  }

  return mMsg.wParam;

}


LRESULT CALLBACK Game::WindowProcedure(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {

  LRESULT lResult;

  if (uMsg == WM_CREATE) {

    LPCREATESTRUCT lpCreateStruct = (LPCREATESTRUCT) lParam;
    Game* pGame = (Game*) lpCreateStruct -> lpCreateParams;
    ::SetWindowLongPtr(hWnd, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(pGame));
    lResult = 1;

  } else {

    Game* pGame = reinterpret_cast<Game*>(static_cast<LONG_PTR>(::GetWindowLongPtr(hWnd, GWLP_USERDATA)));
    BOOL bHandled = FALSE;

    if (pGame) {

      switch (uMsg) {

        case WM_DESTROY:

        {

          PostQuitMessage(0);
          bHandled = TRUE;
          lResult = 1;
          break;

        }

      }

    }

    if (!bHandled) {

      return DefWindowProc(hWnd, uMsg, wParam, lParam);

    }

  }

  return lResult;

}
Struct = (LPCREATESTRUCT) lParam;
    Game* pGame = (Game*) lpCreateStruct -> lpCreateParams;
    ::SetWindowLongPtr(hWnd, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(pGame));
    lResult = 1;

  } else {

    Game* pGame = reinterpret_cast<Game*>(static_cast<LONG_PTR>(::GetWindowLongPtr(hWnd, GWLP_USERDATA)));
    BOOL bHandled = FALSE;

    if (pGame) {

      switch (uMsg) {

        case WM_DESTROY:

        {

          PostQuitMessage(0);
          bHandled = TRUE;
          lResult = 1;
          break;

        }

      }

    }

    if (!bHandled) {

      return DefWindowProc(hWnd, uMsg, wParam, lParam);

    }

  }

  return lResult;

}
奥列格·阿普里利科夫(Oleg Aprelikov)

我发现了问题。文本控件绘制自己的背景,并且我将文本的宽度和高度设置得太高和太高,以至于几乎占据了整个窗口。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章