位图和位块传输(3)之加载位图资源
2012-08-10 20:29 java线程例子 阅读(211) 评论(0) 编辑 收藏 举报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 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 ) { 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 name TEXT("this is title"), // window name WS_OVERLAPPEDWINDOW, // window style CW_USEDEFAULT, // horizontal position of window CW_USEDEFAULT, // vertical position of window CW_USEDEFAULT, // window width CW_USEDEFAULT, // window height NULL, // handle to parent or owner window NULL, // menu handle or child identifier hInstance, // handle to application instance NULL // 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 window UINT uMsg, // message identifier WPARAM wParam, // first message parameter LPARAM 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 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 ) { 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 name TEXT("this is title"), // window name WS_OVERLAPPEDWINDOW, // window style CW_USEDEFAULT, // horizontal position of window CW_USEDEFAULT, // vertical position of window CW_USEDEFAULT, // window width CW_USEDEFAULT, // window height NULL, // handle to parent or owner window NULL, // menu handle or child identifier hInstance, // handle to application instance NULL // 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 window UINT uMsg, // message identifier WPARAM wParam, // first message parameter LPARAM 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); }