#include <windows.h>
//函数声明
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);//窗口处理函数
void init();//初始化设备环境
void Render();//渲染函数
void CleanUp();//释放设备环境
//变量定义
HWND hwnd;
HDC hdc,hdcmen;
BITMAP bm;
HBITMAP hbm;
//主函数
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInst, LPSTR lpszCmdLine, int nCmdShow)
{
//设计窗口类
WNDCLASS wndclass = {
CS_HREDRAW | CS_VREDRAW,
WndProc,
0,
0,
hInstance,
LoadIcon(NULL, IDI_APPLICATION),
LoadCursor(NULL, IDC_ARROW),
(HBRUSH)GetStockObject(WHITE_BRUSH),
NULL,
TEXT("MYDEMO")
};
//注册窗口类
RegisterClass(&wndclass);
//创建窗口类
hwnd = CreateWindow(
TEXT("MYDEMO"),
TEXT("MyDemo"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hInstance,
NULL);
//显示窗口
ShowWindow(hwnd, nCmdShow);
//更新窗口
UpdateWindow(hwnd);
//初始化
init();
//消息循环
MSG Msg;
BOOL bRet;
PeekMessage(&Msg, NULL, 0, 0, PM_NOREMOVE);
while (Msg.message != WM_QUIT)
{
bRet = PeekMessage(&Msg, NULL, 0, 0, PM_REMOVE);
if (bRet)
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
Render();
}
CleanUp();
return Msg.wParam;
}
//窗口过程处理函数
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, message, wParam, lParam);
}
//初始化函数
void init()
{
if (!hwnd)
{
return;
}
//设备环境获取
hdc = GetDC(hwnd);
//获取兼容设备环境
hdcmen = CreateCompatibleDC(hdc);
//加载位图
hbm = (HBITMAP)LoadImage(
NULL,
L"flower.bmp",
IMAGE_BITMAP,
0,
0,
LR_LOADFROMFILE);
//将位图选入设备环境
SelectObject(hdcmen, hbm);
//获取位图大小
GetObject(hbm, sizeof(BITMAP), &bm);
}
//渲染
void Render()
{
if (hdc)
{
//正常位图输出
BitBlt(hdc, 200, 0, bm.bmWidth,bm.bmHeight,hdcmen, 0, 0, SRCCOPY);
//缩放模式设置,一般采用COLORONCOLOR该模式来处理彩色点阵图
SetStretchBltMode(hdc, COLORONCOLOR);
//位图缩放输出
/************************************************
这里在设备环境中从(0,0)坐标并以宽为200,高为 200输出该位图,
而该位图是从后备设备环境hdcmen中从(0,0)坐标开始,宽高为该位图大小的位图截取出来
从而实现缩放显示
*************************************************/
StretchBlt(hdc, 0, 0, 200, 200, hdcmen, 0, 0, bm.bmWidth,bm.bmHeight, SRCCOPY);
}
}
//清除设备环境
void CleanUp()
{
//清除位图相关
DeleteObject(hbm);
DeleteDC(hdcmen);
ReleaseDC(hwnd, hdc);
}