【问题标题】:How to call StretchBlt by pressing a button? Program is not loading如何通过按下按钮调用 StretchBlt?程序未加载
【发布时间】:2021-06-17 09:28:48
【问题描述】:
case ID_BUTTON2:
    {
        StretchBlt(hdc, 100, rect.bottom, rect.right, -rect.bottom, hmem, 0, 0, bmp.bmWidth, bmp.bmHeight, SRCCOPY); // mirror vertically

        break;
    }

当我以这种方式调用它或尝试先释放 DC 时,程序只是没有运行。如果这是一个问题,只是不是处理内存分配的专家。那么有什么解决办法呢?

在 WM_PAINT 我有:

 case WM_PAINT:
    hdc = BeginPaint(hwnd, &ps); // Get DC handle
    GetClientRect(hwnd, &rect);  // Get client rectangle
    hmem = CreateCompatibleDC(hdc);
    hbmp = LoadImage(0, "\\image.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE); 
    SelectObject(hmem, hbmp);
    GetObject(hbmp, sizeof(BITMAP), &bmp);
    StretchBlt(hdc, 100, 0, rect.right, rect.bottom, hmem, 0, 0, bmp.bmWidth, bmp.bmHeight, SRCCOPY);

        if ((HWND)lParam == ID_BUTTON2) { // HOW TO MAKE IT WORK, I have read documentation, and googled a lot, just doesn't work

        if (HIWORD(wParam) == BN_CLICKED) {
            StretchBlt(hdc, 100, rect.bottom, rect.right, -rect.bottom, hmem, 0, 0, bmp.bmWidth, bmp.bmHeight, SRCCOPY);// mirror vertically
        }
    }

【问题讨论】:

  • 可能相关:别忘了致电EndPaint。另外,你为什么在 WM_PAINT 处理程序之外调用 StretchBlt
  • 如果用户按下按钮,我想通过改变目标坐标来改变位图。
  • 您仍然应该在您的 WM_PAINT 处理程序中执行此操作。你可以通过调用InvalidateRect来触发重绘。
  • 感谢您的回复,实际上我尝试了这样做,但现在我卡住了 5 个小时,无法触发 BT_CLICKED 成为 WM_PAINT 中的标志
  • 在 createWindowW 中定义了一个按钮 #define ID_BUTTON2 2 我有 (HMENU)ID_BUTTON2 如何触发在 WM_PAINT 中单击的按钮?我尝试用全局变量来做这件事,但它不起作用,没有错误,什么都没有

标签: c++ c windows winapi


【解决方案1】:

您需要处理WM_COMMAND 中的按钮按下。

下面是一些你可以参考的代码:

case WM_PAINT:
    hdc = BeginPaint(hwnd, &ps); // Get DC handle
    GetClientRect(hwnd, &rect);  // Get client rectangle
    hmem = CreateCompatibleDC(hdc);
    hbmp = (HBITMAP)LoadImage(0, L"\\image.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
    SelectObject(hmem, hbmp);
    GetObject(hbmp, sizeof(BITMAP), &bmp);
    StretchBlt(hdc, 100, 0, rect.right, rect.bottom, hmem, 0, 0, bmp.bmWidth, bmp.bmHeight, SRCCOPY);
    EndPaint(hwnd, &ps);
    return 0;
case WM_COMMAND:
    hdc = GetDC(hwnd);
    if (LOWORD(wParam) == ID_BUTTON2) { // HOW TO MAKE IT WORK, I have read documentation, and googled a lot, just doesn't work
        if (HIWORD(wParam) == BN_CLICKED) {
            StretchBlt(hdc, 100, rect.bottom, rect.right, -rect.bottom, hmem, 0, 0, bmp.bmWidth, bmp.bmHeight, SRCCOPY);// mirror vertically
        }
    }
    return 0;

【讨论】:

  • 嗨,这个答案能解决你的问题吗?如果您有任何问题,请随时告诉我,如果有帮助也请接受。
  • 嗨!谢谢你的回复我几乎失去了希望。是的,它确实有效,非常感谢你:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-07
  • 1970-01-01
相关资源
最近更新 更多