c++ 已经得到qrcode 创建一个子窗口把qrcode生成图片显示在窗口上 不使用qt
时间: 2024-02-21 16:57:14 浏览: 26
你可以使用 Windows API 中的 GDI+ 库来实现这个功能。以下是一个简单的示例程序,可以参考一下:
```c++
#include <windows.h>
#include <gdiplus.h>
#pragma comment (lib,"Gdiplus.lib")
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
// 初始化 GDI+
Gdiplus::GdiplusStartupInput gdiplusStartupInput;
ULONG_PTR gdiplusToken;
Gdiplus::GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
// 创建窗口
HWND hwnd;
WNDCLASS wc = { 0 };
wc.lpfnWndProc = WndProc;
wc.hInstance = hInstance;
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wc.lpszClassName = L"QRCodeWindow";
if (!RegisterClass(&wc))
return 1;
hwnd = CreateWindow(wc.lpszClassName, L"QR Code", WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, 640, 480, NULL, NULL, hInstance, NULL);
if (!hwnd)
return 2;
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
// 创建 GDI+ 图片
Gdiplus::Bitmap bitmap(L"qrcode.png", FALSE);
// 进入消息循环
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
// 释放 GDI+
Gdiplus::GdiplusShutdown(gdiplusToken);
return (int)msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hwnd, &ps);
// 使用 GDI+ 绘制图片
Gdiplus::Graphics graphics(hdc);
Gdiplus::Bitmap bitmap(L"qrcode.png", FALSE);
graphics.DrawImage(&bitmap, 0, 0);
EndPaint(hwnd, &ps);
break;
}
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, message, wParam, lParam);
}
return 0;
}
```
这个程序会创建一个窗口,然后在窗口上显示指定的 QR Code 图片。你可以根据需要修改窗口的大小和位置,以及图片的文件名。