简介:这里主要介绍的就是讲一个位图在窗体中进行缩放的操作,根据窗体大小的变化,来改变位图的大小;
关键函数:
StretchBlt();具体用法大家可以去msdn上查一下,我就不赘述了。
效果图如下:
代码如下:
代码如下:
#include
#include
#include
#include
HDC hdcmem;//设备内存环境句柄
HBITMAP hbitmap;//位图句柄
BITMAP bitmap;//BITMAP结构
LRESULT CALLBACK WndProc(
HWND hwnd, // handle to window
UINT uMsg, // message identifier
WPARAM wParam, // first message parameter
LPARAM lParam // second message parameter
);
int WINAPI WinMain(
HINSTANCE hInstance, // handle to current instance
HINSTANCE hPrevInstance, // handle to previous instance
LPSTR lpCmdLine, // command line
int nCmdShow // show state
)
{
WNDCLASS wndclass;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndclass.hInstance = hInstance;
wndclass.lpfnWndProc = WndProc;
wndclass.lpszClassName = "我的窗口";
wndclass.lpszMenuName = NULL;
wndclass.style = CS_HREDRAW | CS_VREDRAW;
RegisterClass(&wndclass); //注册窗口类
HWND hwnd;
hwnd = CreateWindow("我的窗口", "窗口", WS_OVERLAPPEDWINDOW,
0, 0, 1000, 600, NULL, NULL, hInstance, NULL);
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
MSG Msg;
while(GetMessage(&Msg, NULL, 0, 0))
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
return Msg.wParam;
}
LRESULT CALLBACK WndProc(
HWND hwnd, // handle to window
UINT uMsg, // message identifier
WPARAM wParam, // first message parameter
LPARAM lParam // second message parameter
)
{
HDC hdc;
PAINTSTRUCT ps;
HANDLE hb;
static int cxClient, cyClient, cxSource, cySource;
HDC hdcClient, hdcWindow ;
static RECT rect,rect1;
switch(uMsg)
{
case WM_CREATE:
hdc = GetDC(hwnd);
hdcmem = CreateCompatibleDC(hdc);//获取内存设备环境句柄
GetClientRect(GetDesktopWindow(), &rect);
ReleaseDC(hwnd, hdc);
break;
case WM_PAINT:
hdcClient = BeginPaint (hwnd, &ps) ;
hdcWindow = GetDC(NULL);
hb = LoadImage(NULL, "bk.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
SetStretchBltMode(hdcClient, COLORONCOLOR);
SelectObject(hdcmem, hb); //将位图选入内存设备
GetObject(hb, sizeof(BITMAP), &bitmap);
StretchBlt (hdcClient, 0, 0, cxClient, cyClient,
hdcmem, 0, 0, bitmap.bmWidth, bitmap.bmHeight, MERGECOPY);
EndPaint(hwnd, &ps);
ReleaseDC (hwnd, hdcWindow) ;
DeleteObject(hb);
break;
//case WM_QUIT://WM_QUIT 是关闭消息环的
case WM_CLOSE://WM_CLOSE 是关闭窗口的
DestroyWindow(hwnd);
break;
case WM_DESTROY://WM_DESTROY 是关闭程序的
DeleteDC(hdcmem);
PostQuitMessage(0);
break;
case WM_SIZE:
cxClient = LOWORD (lParam) ;
cyClient = HIWORD (lParam) ;
break;
default:
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
return 0;
}
资源链接:http://download.csdn.net/detail/u010084308/6705369