位图和位块传输(3)之加载位图资源
来源:互联网 发布:淘宝韩国代购真假 编辑:程序博客网 时间:2024/05/18 18:01
Windows提供了两个能在位图被创建后取得和设置位图像素的函数
SetBitmapBits(hBitmap,cBytes,&bits);GetBitmapBits(hBitmap,cBytes,&bits);
使用GDI位图对象时,需要用到内存设备环境,要创建一个内存设备环境,必须有一个对应真实设备的环境句柄。假设句柄是hdc,则可以像下面这样创建一个内存设备环境
hdcMem=CreateCompatibleDC(hdc);
它和特定的真实设备"兼容"。
加载位图资源,LoadBitMap和LoadIcon和LoadCursor函数的语法一样
hBitmap=LoadBitmap(hInstance,szBitmapName);//知道位图ID用:hBitmap=LoadBitmap(hInstance,MAKEINTRESOURCE(BITMAPID));
下面是一个完整的例子
编好CPP文件,创建资源位图:
#include<windows.h>#include"resource.h"LRESULT CALLBACK WindowProc(HWND hwnd, // handle to windowUINT uMsg, // message identifierWPARAM wParam, // first message parameterLPARAM 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 ){static TCHAR szAppName[]=TEXT("leidemingzi");HWND hwnd;MSG msg;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_ERROR);wndclass.hInstance=hInstance;wndclass.lpfnWndProc=WindowProc;wndclass.lpszClassName=szAppName;wndclass.lpszMenuName=NULL;wndclass.style=CS_HREDRAW|CS_VREDRAW;if(!RegisterClass(&wndclass)){MessageBox(NULL,TEXT("the program require the window nt"),TEXT("tips"),MB_ICONERROR);return 0;}hwnd=CreateWindow(szAppName, // registered class nameTEXT("this is title"), // window nameWS_OVERLAPPEDWINDOW, // window styleCW_USEDEFAULT, // horizontal position of windowCW_USEDEFAULT, // vertical position of windowCW_USEDEFAULT, // window widthCW_USEDEFAULT, // window heightNULL, // handle to parent or owner windowNULL, // menu handle or child identifierhInstance, // handle to application instanceNULL// window-creation data);ShowWindow(hwnd,nCmdShow);UpdateWindow(hwnd);while(GetMessage(&msg,NULL,0,0)){TranslateMessage(&msg);DispatchMessage(&msg);}return msg.wParam;}LRESULT CALLBACK WindowProc(HWND hwnd, // handle to windowUINT uMsg, // message identifierWPARAM wParam, // first message parameterLPARAM lParam // second message parameter){static HBITMAP hBitmap;static int cxClient,cyClient,cxSource,cySource;BITMAP bitmap;HINSTANCE hInstance;HDC hdc,hdcMem;int x,y;PAINTSTRUCT ps;switch(uMsg){case WM_CREATE:hInstance=((LPCREATESTRUCT)lParam)->hInstance;hBitmap=LoadBitmap(hInstance,MAKEINTRESOURCE(BITMAPID));GetObject(hBitmap,sizeof(BITMAP),&bitmap);cxSource=bitmap.bmWidth;cySource=bitmap.bmHeight;return 0;case WM_SIZE:cxClient=LOWORD(lParam);cyClient=HIWORD(lParam);return 0;case WM_PAINT:hdc=BeginPaint(hwnd,&ps);hdcMem=CreateCompatibleDC(hdc);SelectObject(hdcMem,hBitmap);for(y=0;y<cyClient;y+=cySource)for(x=0;x<cxClient;x+=cxSource)BitBlt(hdc,x,y,cxSource,cySource,hdcMem,0,0,SRCCOPY);DeleteDC(hdcMem);EndPaint(hwnd,&ps);return 0;case WM_DESTROY:PostQuitMessage(0);return 0;}return DefWindowProc(hwnd,uMsg,wParam,lParam);}
效果就是一副墙:
下面代码可以达到同样的效果:
#include<windows.h>LRESULT CALLBACK WindowProc(HWND hwnd, // handle to windowUINT uMsg, // message identifierWPARAM wParam, // first message parameterLPARAM 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 ){static TCHAR szAppName[]=TEXT("leidemingzi");HWND hwnd;MSG msg;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_ERROR);wndclass.hInstance=hInstance;wndclass.lpfnWndProc=WindowProc;wndclass.lpszClassName=szAppName;wndclass.lpszMenuName=NULL;wndclass.style=CS_HREDRAW|CS_VREDRAW;if(!RegisterClass(&wndclass)){MessageBox(NULL,TEXT("the program require the window nt"),TEXT("tips"),MB_ICONERROR);return 0;}hwnd=CreateWindow(szAppName, // registered class nameTEXT("this is title"), // window nameWS_OVERLAPPEDWINDOW, // window styleCW_USEDEFAULT, // horizontal position of windowCW_USEDEFAULT, // vertical position of windowCW_USEDEFAULT, // window widthCW_USEDEFAULT, // window heightNULL, // handle to parent or owner windowNULL, // menu handle or child identifierhInstance, // handle to application instanceNULL// window-creation data);ShowWindow(hwnd,nCmdShow);UpdateWindow(hwnd);while(GetMessage(&msg,NULL,0,0)){TranslateMessage(&msg);DispatchMessage(&msg);}return msg.wParam;}LRESULT CALLBACK WindowProc(HWND hwnd, // handle to windowUINT uMsg, // message identifierWPARAM wParam, // first message parameterLPARAM lParam // second message parameter){static BITMAP bitmap={0,8,8,2,1,1};static BYTE bits[8][2]={0xFF,0,0x0C,0,0x0C,0,0x0C,0,0xFF,0,0xC0,0,0xC0,0,0xC0,0};static HBITMAP hBitmap;static int cxClient,cyClient,cxSource,cySource;HDC hdc,hdcMem;int x,y;PAINTSTRUCT ps;switch(uMsg){case WM_CREATE:bitmap.bmBits=bits;hBitmap=CreateBitmapIndirect(&bitmap);cxSource=bitmap.bmWidth;cySource=bitmap.bmHeight;return 0;case WM_SIZE:cxClient=LOWORD(lParam);cyClient=HIWORD(lParam);return 0;case WM_PAINT:hdc=BeginPaint(hwnd,&ps);hdcMem=CreateCompatibleDC(hdc);SelectObject(hdcMem,hBitmap);for(y=0;y<cyClient;y+=cySource)for(x=0;x<cxClient;x+=cxSource)BitBlt(hdc,x,y,cxSource,cySource,hdcMem,0,0,SRCCOPY);DeleteDC(hdcMem);EndPaint(hwnd,&ps);return 0;case WM_DESTROY:PostQuitMessage(0);return 0;}return DefWindowProc(hwnd,uMsg,wParam,lParam);}
- 位图和位块传输(3)之加载位图资源
- 位图和位块传输(5)之画图程序
- Windows程序设计-位图和位块传输
- 第十四章 位图和Bitblt(位块传输)
- 位图和位块传输(1)BitBlt与PatBlt
- 第 14 章 位图和位块传输
- 位图和位块传输(2)压缩拉伸之StretchBlt函数
- 位图和位块传输(4)位图画刷与画画
- 位图和位块传输(6)画框与会移动的小球
- 加载位图资源
- 位图和像素位
- 位图和像素位
- Linux文件系统分析之二(超级块,i节点位图和逻辑块位图)
- LoadBitmap加载位图资源、LoadImage从文件加载位图
- 8位位图和24位位图的相互转换
- 加载位图
- 加载位图
- 位图加载
- 未完成的水题~~~~~~~~~~~~~~~~~~·
- lIS 问题
- linux入门学习笔记
- 黑马程序员——迭代器Iterator
- 关于STL中heap的用法
- 位图和位块传输(3)之加载位图资源
- 一些MM问题
- #ifndef#define#endif的用法(整理) 原作者:icwk
- Irrlicht引擎源码剖析——第十七天
- 使用Apache FOP将XML导出成PDF
- error prj0019 running moc on "XXXX.h"
- 刷新动画效果
- #pragma pack(n)
- C++中string类的成员函数find_first_not_of()