【问题标题】:Draw bitmap over 3 buttons in C++ Win32 Application在 C++ Win32 应用程序中的 3 个按钮上绘制位图
【发布时间】:2011-01-12 22:40:37
【问题描述】:

我有一个 win32 应用程序,我放置在主窗口中(使用 CreateWindowEx 创建)3 个按钮。我想将它们分开两行,所以我想在每个按钮之间放置一个位图,但是当我尝试在 WM_PAINT 内绘制图像时,它会将其放置在最后一个按钮之后,如果 X 和 Y 坐标为 0,0 也是如此。为什么?我是否应该放置一个静态控件并使用 STM_SETIMAGE 设置图像背景?

编辑 1

hdc = BeginPaint(hWnd, &ps);
HDC hdcMem;
hdcMem = CreateCompatibleDC(hdc);
BITMAP bm;
HBITMAP oldbitmap;

GetObject(test, sizeof(bm), &bm);
oldbitmap = (HBITMAP)SelectObject(hdcMem, test);

BitBlt(hdc, 10, 10, bm.bmWidth, bm.bmHeight, hdcMem, 0, 0, SRCCOPY);  

SelectObject(hdcMem, oldbitmap);  
DeleteObject(oldbitmap);
DeleteDC(hdcMem);

EndPaint(hWnd, &ps);

编辑 2

ImageHost.org http://g.imagehost.org/0076/image-example.png

【问题讨论】:

  • 我添加了用于绘制位图图像的代码
  • 您能否发布您渲染的内容和您期望渲染的内容的屏幕截图?
  • 添加,顶部图像显示当前如何渲染窗口,底部图像拍摄应如何渲染窗口。黑线是我将在每个按钮之间添加的图像(它们是图像的红色部分)
  • 仍然没有足够的上下文来了解您在做什么。屏幕截图没有显示任何按钮,也没有创建按钮的代码。
  • 按钮的创建方式与我创建黑线 (CreateWindow) 的方式相同。按钮为 3,并覆盖了图像的所有红色部分

标签: c++ winapi


【解决方案1】:

您的最后一条评论引发了危险。

您无需创建窗口即可绘制黑线。似乎对窗口系统的工作方式存在一些误解。

所以,为了构建这个:

|--------------------------------|
|MainWindow    B                 |
|              l                 |
|  |--------|  a  |--------|     |
|  |Button1 |  c  |Button2 |     |
|  |        |  k  |        |     |
|  |        |     |        |     |
|  |        |  B  |        |     |
|  |        |  a  |        |     |
|  |--------|  r  |--------|     |
|--------------------------------|

您将首先调用 CreateWindow 来创建 MainWindow,然后进入您的消息循环(GetMessage、Translate、Dispatch 等)。在主窗口的 WM_CREATE 处理程序中,您将使用消息处理程序的 window 参数作为按钮的父窗口创建 Button1 和 Button2。

要绘制黑条,您需要在主窗口的消息处理程序中为 WM_ERASEBKGND 消息添加一个处理程序,并在该处理程序中清除客户区(通过调用 DefWindowProc)然后绘制黑条。

按钮的绘制应由按钮窗口的 WM_ERASEBKGND/WM_PAINT 处理程序处理,而不是由主窗口处理。

编辑:这是一个显示两个按钮和一个黑条的简单程序:

#define WIN32_MEAN_AND_LEAN
#include <windows.h>

// a helper function to create two buttons
void CreateButtons (HWND parent)
{
  // create two button
  // Here, I've used the standard button class. If you want to have red buttons,
  // either create a new class for the buttons and implement all the functionality
  // yourself, or sub class the standard button and override the drawing functions.
  CreateWindowEx (0,
                  TEXT ("BUTTON"),
                  TEXT ("Button1"),
                  WS_CHILD | WS_VISIBLE,
                  10,
                  10,
                  100,
                  50,
                  parent,
                  0,
                  reinterpret_cast <HINSTANCE> (GetWindowLongPtr (parent, GWL_HINSTANCE)),
                  0);

  CreateWindowEx (0,
                  TEXT ("BUTTON"),
                  TEXT ("Button2"),
                  WS_CHILD | WS_VISIBLE,
                  140,
                  10,
                  100,
                  50,
                  parent,
                  0,
                  reinterpret_cast <HINSTANCE> (GetWindowLongPtr (parent, GWL_HINSTANCE)),
                  0);

}

LRESULT CALLBACK MainWindowProc (HWND window, UINT message, WPARAM w_param, LPARAM l_param)
{
  LRESULT
    result;

  bool
    use_default_proc = true;

  switch (message)
  {
  case WM_CREATE:
    // when the window is created, create the two buttons!
    CreateButtons (window);
    break;

  case WM_DESTROY:
    // when the window is finished with, call PostQuitMessage to exit the message loop
    PostQuitMessage (0);
    use_default_proc = false;
    result = 0;
    break;

  case WM_ERASEBKGND:
    // here we draw the black line between the two buttons using a solid colour filled rectangle.
    // this can just as easily be done in the WM_PAINT handler
    {
      // DefWindowProc will clear the window using the WNDCLASSEX.hbrBackground member
      result = DefWindowProc (window, message, w_param, l_param);
      use_default_proc = false;

      // get the DC to draw with
      HDC
        dc = reinterpret_cast <HDC> (w_param);

      // draw the black bar
      RECT
        rect = {120, 0, 130, 70};

      FillRect (dc, &rect, reinterpret_cast <HBRUSH> (GetStockObject (BLACK_BRUSH)));
    }
    break;
  }

  if (use_default_proc)
  {
    result = DefWindowProc (window, message, w_param, l_param);
  }

  return result;
}

int __stdcall WinMain (HINSTANCE instance, HINSTANCE previous_instance, LPSTR command_line, int show_command)
{
  // create a window class for the main window
  WNDCLASSEX
    window_class = 
    {
      sizeof window_class,
      CS_HREDRAW | CS_VREDRAW,
      MainWindowProc,
      0,
      0,
      instance,
      0,
      LoadCursor (0, IDC_ARROW),
      reinterpret_cast <HBRUSH> (COLOR_WINDOW + 1),
      0,
      TEXT ("MainWindowClass"),
      0
    };

  // register the window class
  RegisterClassEx (&window_class);

  // create the main window
  HWND
    window = CreateWindowEx (WS_EX_APPWINDOW,
                             TEXT ("MainWindowClass"),
                             TEXT ("Example Window"),
                             WS_OVERLAPPEDWINDOW | WS_VISIBLE,
                             CW_USEDEFAULT,
                             CW_USEDEFAULT,
                             CW_USEDEFAULT,
                             CW_USEDEFAULT,
                             0,
                             0,
                             instance,
                             0);

  bool
    quit = false;

  while (!quit)
  {
    MSG
      message;

    int
      error_code;

    switch (GetMessage (&message, 0, 0, 0))
    {
    case 0: // WM_QUIT processed
      quit = true;
      break;

    case -1: // an error
      error_code = GetLastError ();
      quit = true;
      break;

    default: // pass the message on to the window...
      TranslateMessage (&message);
      DispatchMessage (&message);
      break;
    }
  }

  return 0;
}

【讨论】:

  • 这是我想做的事,你能发一个非常简单的例子来说明代码应该是怎样的吗?
猜你喜欢
  • 1970-01-01
  • 2010-12-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-12
  • 1970-01-01
相关资源
最近更新 更多