#C++ Visual Studio 各位程序员上班摸鱼时速来打小菜
C++初学升进步者,可能很多细节内容没处理到位。
一个打印窗口背景的程序,求解!
第五行将不同格式图片转为BMP格式有问题,报错:
“ATL::Clmage::Load”:2个重载中没有一个可以转换所有参数类型
代码如下:
#include <iostream>
#include <Windows.h>
#include <atlimage.h>
//将不同格式图片转为BMP
bool convert2bmp(HBITMAP& bitmap, const std::string& url)
{
CImage img;
HRESULT ret = img.Load(url.c_str());
bitmap = img.Detach();
return true;
}
// 消息处理函数的实现
LRESULT CALLBACK PlayerWndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = ::BeginPaint(hwnd, &ps);
HDC s_hdcMem = CreateCompatibleDC(hdc);
HBITMAP hBitmap;
BITMAP s_bm;
convert2bmp(hBitmap, "./background/bg.png");//png,jpg,bmp均可
SelectObject(s_hdcMem, hBitmap);
GetObject(hBitmap, sizeof(s_bm), &s_bm);// 得到位图信息
RECT rect;
GetClientRect(hwnd, &rect);
//通过SetStretchBltMode的设置能使StretchBlt在缩放图像更加清晰
SetStretchBltMode(hdc, COLORONCOLOR);//STRETCH_HALFTONE
StretchBlt(hdc, 0, 0, rect.right, rect.bottom, s_hdcMem, 0, 0, s_bm.bmWidth, s_bm.bmHeight, SRCCOPY);
::EndPaint(hwnd, &ps);
return 0;
}
case WM_NCHITTEST: //实现窗口移动,与WM_LBUTTONDBLCLK冲突
{
UINT nHitTest;
nHitTest = ::DefWindowProc(hwnd, uMsg, wParam, lParam);
if (nHitTest == HTCLIENT && ::GetAsyncKeyState(MK_LBUTTON) < 0)
{
nHitTest = HTCAPTION;
}
return nHitTest;
}
case WM_DESTROY:
{
PostQuitMessage(0);
return 0;
}
case WM_KEYDOWN:
if (wParam == VK_ESCAPE)//"ECS键"退出
{
PostMessage(hwnd, WM_DESTROY, 0, 0);
return 0;
}
break;
}
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
//创建窗口
HWND CreateWin(double pos_x, double pos_y, double width, double height, HWND hwd, DWORD style)
{
HINSTANCE hinstance = (HINSTANCE)GetModuleHandle(NULL); // 得到进程实例句柄.
WNDCLASSEXA wcex;
wcex.cbSize = sizeof(WNDCLASSEXA);
wcex.style = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;
wcex.lpfnWndProc = PlayerWndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hinstance;
wcex.hIcon = LoadIcon(hinstance, MAKEINTRESOURCE(IDC_ICON));
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = CreateSolidBrush(RGB(0, 0, 0));
wcex.lpszMenuName = NULL;
wcex.lpszClassName = "player";
wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDC_ICON));
RegisterClassExA(&wcex);
// 创建窗口.
HWND hwnd = CreateWindowExA(0, "player", "", style, pos_x, pos_y, width, height, hwd, NULL, hinstance, NULL);
ShowWindow(hwnd, SW_SHOW);
return hwnd;
}
int main(int argc, char* argv[])
{
HWND hwnd = CreateWin(0, 0, 720, 576, NULL, WS_POPUP);
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
附带错误列表
自己也不知道怎么试,求程序员解答
想要达到的结果:程序能够正常运行