lz 你好
c语言要显示bmp位图需要使用win32的api , 具体如下:
BOOL BitBlt(
HDC hdcDest, // 位图显示目标设备环境中
int nXDest, // 位图显示在客户区的x坐标
int nYDest, // 位图显示在客户区的y坐标
int nWidth, // 位图显示的宽度
int nHeight, // 位图显示的长度
HDC hdcSrc, // 源设备环境(包含需要显示的bmp位图)
int nXSrc, // 在当前位图中显示的开始x位置
int nYSrc, // 在当前位图中显示的开始y位置
DWORD dwRop // 映射模式
);
以下是源代码:
//显示bmp位图
#include
#include"resource.h"
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
void DrawBrick();
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
PSTR szCmdLine,
int iCmdShow)
{
static TCHAR szAppName[] = TEXT("Bmp");
HWND hwnd;
MSG msg;
WNDCLASS wndclass;
wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = szAppName;
if(!RegisterClass(&wndclass))
{
MessageBox(NULL, TEXT("This program requires Windows NT!"),
szAppName, MB_ICONERROR);
return 0;
}
hwnd = CreateWindow(szAppName,
TEXT("Bmp Demo"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
754,
566,
NULL,
NULL,
hInstance,
NULL);
ShowWindow(hwnd, iCmdShow);
UpdateWindow(hwnd);
while(GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static HBITMAP hBitmap; //位图句柄 标示位图
static int cxBitmap, cyBitmap; //位图的长宽
BITMAP bitmap;
HDC hdc, hdcMem;
HINSTANCE hInstance;
PAINTSTRUCT ps;
switch(message)
{
case WM_CREATE:
hInstance = ((LPCREATESTRUCT)lParam)->hInstance; //获取窗口的实例句柄
hBitmap = LoadBitmap(hInstance, MAKEINTRESOURCE(IDB_BITMAP1)); //将位图加载到内存中
GetObject(hBitmap, sizeof(BITMAP), &bitmap);
cxBitmap = bitmap.bmWidth;//获取位图的长
cyBitmap = bitmap.bmHeight;//获取位图的宽
return 0 ;
case WM_PAINT:
hdc = BeginPaint(hwnd, &ps);
hdcMem = CreateCompatibleDC(hdc);//创建一个兼容于hdc设备环境描述表的hdcMem 主要是用于在内存中截图
SelectObject(hdcMem, hBitmap); //将位图选到hdcMem中
BitBlt(hdc, -1, -1, cxBitmap, cyBitmap, hdcMem, 0, 0, SRCCOPY);//绘制bmp位图
DeleteDC(hdcMem);
EndPaint(hwnd, &ps);
return 0;
case WM_DESTROY:
DeleteObject(hBitmap);
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, message, wParam, lParam);
}
程序运行效果:
希望能帮助你哈
ps:
附件是整个工程 , 用vs2008创建的项目 , 里面包含相应资源
单纯靠c语言是难以实现的老兄,要么用API,或者用MFC