如何在 GDI 中翻译简单的位图
How to translate a simple bitmap in GDI
我希望能够在屏幕上移动图像。这是我拥有的显示图像的代码,但它始终显示从原点开始的图像。
void drawBitmap(HDC hdc, int x1, int y1, int x2, int y2, LPWSTR path) {
HBITMAP bmp = (HBITMAP)::LoadImage(NULL, path, IMAGE_BITMAP, SCREEN_WIDTH, SCREEN_HEIGHT, LR_LOADFROMFILE);
HGDIOBJ B1 = CreatePatternBrush(bmp);
SelectObject(hdc, B1);
Rectangle(hdc, x1, y1, x2, y2);
DeleteObject(B1);
}
谢谢Barmak Shemirani的回答。对于任何对我的代码现在是什么样子感到好奇的人,我已经用回答我问题的人的代码替换了原始代码,现在看起来像这样:
HBITMAP hbitmap = (HBITMAP)LoadImage(NULL, path, IMAGE_BITMAP, x2-x1, y2-y1, LR_LOADFROMFILE);
HDC memdc = CreateCompatibleDC(hdc);
SelectObject(memdc, hbitmap);
BitBlt(hdc, x1, y1, x2, y2, memdc, 0, 0, SRCCOPY);
DeleteDC(memdc);
创建一个子窗口并覆盖WM_NCHITTEST
以返回HTCAPTION
,这将导致子窗口在单击时在主窗口中移动。
接下来,只需在子窗口中绘制位图
#include "windows.h"
LRESULT CALLBACK wndProc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp)
{
if (msg == WM_DESTROY)
{
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, msg, wp, lp);
}
LRESULT CALLBACK bitmapProc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp)
{
if (msg == WM_NCHITTEST)
{
return HTCAPTION;
}
if (msg == WM_PAINT)
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hwnd, &ps);
RECT rc;
GetClientRect(hwnd, &rc);
HBITMAP hbitmap = (HBITMAP)LoadImage(NULL, L"c:\test\test.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
HDC memdc = CreateCompatibleDC(hdc);
HBITMAP saveOldBitmap = (HBITMAP)SelectObject(memdc, hbitmap);
BitBlt(hdc, 0, 0, rc.right, rc.bottom, memdc, 0, 0, SRCCOPY);
SelectObject(memdc, saveOldBitmap); //add this
DeleteObject(hbitmap); //*** add this, important
DeleteDC(memdc);
EndPaint(hwnd, &ps);
}
return DefWindowProc(hwnd, msg, wp, lp);
}
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE, LPTSTR, int)
{
WNDCLASSEX wcex = { sizeof(WNDCLASSEX) };
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.hInstance = hInstance;
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1);
wcex.lpszClassName = L"mainWnd-bitmapWnd";
wcex.lpfnWndProc = wndProc;
if (!RegisterClassEx(&wcex))
{
MessageBox(0, L"error 1", 0, 0);
}
HWND hmain = CreateWindow(wcex.lpszClassName, L"w32", WS_CLIPCHILDREN | WS_VISIBLE | WS_OVERLAPPEDWINDOW, 0, 0, 300, 200, 0, 0, hInstance, 0);
wcex.lpszClassName = L"bitmapWnd";
wcex.lpfnWndProc = bitmapProc;
if (!RegisterClassEx(&wcex))
{
MessageBox(0, L"error 2", 0, 0);
}
CreateWindow(wcex.lpszClassName, L"w32", WS_VISIBLE | WS_CHILD | WS_BORDER, 0, 0, 50, 50, hmain, 0, hInstance, 0);
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int)msg.wParam;
}
更改 X/Y 在BitBlt
中的位置以在其他位置打印位图
相关文章:
- C / C++ 移位/偏移/向左或向右移动位图?
- 如何在快板的屏幕中显示子位图的绘制?
- CreateDIBSection为同一图像返回不一致的位图位值
- C++ 位图中的 ttc 字体
- 使用 GDI+ 旋转位图,然后转换为 HDC
- 难以从 CImageList 设置菜单项位图
- 如何将位图拉伸到父面板(wxWidgets 自定义)
- 如何使用 freetype2 访问单色位图中的像素状态
- 将位图 (bmp) 转换为具有透明度的 png (Windows c++)
- 安卓工作室 |CPP 文件错误错误: 位图库中对"AndroidBitmap_unlockPixels"的未定义引用
- 位图到垫子/2D 数组
- Gdiplus位图没有Alpha通道
- 如何从路径字符串加载Gdiplus::位图
- 在C++中使用Gdiplus创建透明位图
- 用C++压缩内存中的位图
- C++gdi::内存中的位图到PNG图像
- LoadImage 函数不适用于 ImageMagick 创建的位图图像
- 使用for循环制作位图的最简单和最有效的方法
- 如何在 GDI 中翻译简单的位图
- c++中的简单位图图形