Windows API背景故障

时间:2016-06-05 09:20:42

标签: c++ winapi background

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

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

然而,当我测试我的游戏时,这就是我所看到的:

背景故障图片

enter image description here

这是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;

}

1 个答案:

答案 0 :(得分:0)

我发现了问题所在。文本控件绘制了它的&#39;拥有自己的背景,我将文字的宽度和高度设置得如此宽,高,几乎占据了整个窗口。