位图和位块传输(6)画框与会移动的小球
来源:互联网 发布:淘宝2016双十一 交易额 编辑:程序博客网 时间:2024/05/17 04:10
由一个很常用的技巧,这个方法使用"掩码"位图和一些光栅操作,掩码是一个单色位图,它的大小和你要显示的矩形位图大小相同。每个掩码像素对应要显示的位图上的一个像素,要显示出来的像素对应的掩码像素为白色(1)
下面来看看这个例子(来自Windows程序设计第五版圣经)
#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 windows 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 hBitmapImag,hBitmapMask;static HINSTANCE hInstance;static int cxClient,cyClient,cxBitmap,cyBitmap;BITMAP bitmap;HDC hdc,hdcMemImag,hdcMemMask;int x,y;PAINTSTRUCT ps;switch (uMsg){case WM_CREATE:hInstance = ((LPCREATESTRUCT) lParam)->hInstance ;// Load the original image and get its sizehBitmapImag = LoadBitmap (hInstance, MAKEINTRESOURCE(BITMAPID)) ;GetObject (hBitmapImag, sizeof (BITMAP), &bitmap) ;cxBitmap = bitmap.bmWidth ;cyBitmap = bitmap.bmHeight ;// Select the original image into a memory DChdcMemImag = CreateCompatibleDC (NULL) ;SelectObject (hdcMemImag, hBitmapImag) ;// Create the monochrome mask bitmap and memory DChBitmapMask = CreateBitmap (cxBitmap, cyBitmap, 1, 1, NULL) ;hdcMemMask = CreateCompatibleDC (NULL) ;SelectObject (hdcMemMask, hBitmapMask) ;// Color the mask bitmap black with a white ellipseSelectObject (hdcMemMask, GetStockObject (BLACK_BRUSH)) ;Rectangle (hdcMemMask, 0, 0, cxBitmap, cyBitmap) ;SelectObject (hdcMemMask, GetStockObject (WHITE_BRUSH)) ;Ellipse (hdcMemMask, 0, 0, cxBitmap, cyBitmap) ;// Mask the original imageBitBlt (hdcMemImag, 0, 0, cxBitmap, cyBitmap, hdcMemMask, 0, 0, SRCAND) ;DeleteDC (hdcMemImag) ;DeleteDC (hdcMemMask) ;return 0 ;case WM_SIZE:cxClient = LOWORD (lParam) ;cyClient = HIWORD (lParam) ;return 0 ;case WM_PAINT:hdc = BeginPaint (hwnd, &ps) ;// Select bitmaps into memory DCshdcMemImag = CreateCompatibleDC (hdc) ;SelectObject (hdcMemImag, hBitmapImag) ;hdcMemMask = CreateCompatibleDC (hdc) ;SelectObject (hdcMemMask, hBitmapMask) ;// Center imagex = (cxClient - cxBitmap) / 2 ;y = (cyClient - cyBitmap) / 2 ;// Do the bitbltsBitBlt (hdc, x, y, cxBitmap, cyBitmap, hdcMemMask, 0, 0, 0x220326) ;BitBlt (hdc, x, y, cxBitmap, cyBitmap, hdcMemImag, 0, 0, SRCPAINT) ;DeleteDC (hdcMemImag) ;DeleteDC (hdcMemMask) ;EndPaint (hwnd, &ps) ;return 0 ;case WM_DESTROY:DeleteObject (hBitmapImag) ;DeleteObject (hBitmapMask) ;PostQuitMessage (0) ;return 0 ;}return DefWindowProc (hwnd, uMsg, wParam, lParam) ;}
下面是会移动的小球
#include <windows.h>#define ID_TIMER 1 LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow){ static TCHAR szAppName[] = TEXT ("Bounce") ; 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 ("Bouncing Ball"), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, 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 iMsg, WPARAM wParam, LPARAM lParam){ static HBITMAP hBitmap ; static int cxClient, cyClient, xCenter, yCenter, cxTotal, cyTotal, cxRadius, cyRadius, cxMove, cyMove, xPixel, yPixel ; HBRUSH hBrush ; HDC hdc, hdcMem ; int iScale ; switch (iMsg) { case WM_CREATE: hdc = GetDC (hwnd) ; xPixel = GetDeviceCaps (hdc, ASPECTX) ; yPixel = GetDeviceCaps (hdc, ASPECTY) ; ReleaseDC (hwnd, hdc) ; SetTimer (hwnd, ID_TIMER, 50, NULL) ; return 0 ; case WM_SIZE: xCenter = (cxClient = LOWORD (lParam)) / 2 ; yCenter = (cyClient = HIWORD (lParam)) / 2 ; iScale = min (cxClient * xPixel, cyClient * yPixel) / 16 ; cxRadius = iScale / xPixel ; cyRadius = iScale / yPixel ; cxMove = max (1, cxRadius / 2) ; cyMove = max (1, cyRadius / 2) ; cxTotal = 2 * (cxRadius + cxMove) ; cyTotal = 2 * (cyRadius + cyMove) ; if (hBitmap) DeleteObject (hBitmap) ; hdc = GetDC (hwnd) ; hdcMem = CreateCompatibleDC (hdc) ; hBitmap = CreateCompatibleBitmap (hdc, cxTotal, cyTotal) ; ReleaseDC (hwnd, hdc) ; SelectObject (hdcMem, hBitmap) ; Rectangle (hdcMem, -1, -1, cxTotal + 1, cyTotal + 1) ; hBrush = CreateHatchBrush (HS_DIAGCROSS, 0L) ; SelectObject (hdcMem, hBrush) ; SetBkColor (hdcMem, RGB (255, 0, 255)) ; Ellipse (hdcMem, cxMove, cyMove, cxTotal - cxMove, cyTotal - cyMove) ; DeleteDC (hdcMem) ; DeleteObject (hBrush) ; return 0 ; case WM_TIMER: if (!hBitmap) break ; hdc = GetDC (hwnd) ; hdcMem = CreateCompatibleDC (hdc) ; SelectObject (hdcMem, hBitmap) ; BitBlt (hdc, xCenter - cxTotal / 2, yCenter - cyTotal / 2, cxTotal, cyTotal, hdcMem, 0, 0, SRCCOPY) ; ReleaseDC (hwnd, hdc) ; DeleteDC (hdcMem) ; xCenter += cxMove ; yCenter += cyMove ; if ((xCenter + cxRadius >= cxClient) || (xCenter - cxRadius <= 0)) cxMove = -cxMove ; if ((yCenter + cyRadius >= cyClient) || (yCenter - cyRadius <= 0)) cyMove = -cyMove ; return 0 ; case WM_DESTROY: if (hBitmap) DeleteObject (hBitmap) ; KillTimer (hwnd, ID_TIMER) ; PostQuitMessage (0) ; return 0 ; } return DefWindowProc (hwnd, iMsg, wParam, lParam) ;}
闪屏:
#include <windows.h>#define NUM 300LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow){ static int iKeep [NUM][4] ; HDC hdcScr, hdcMem ; int cx, cy ; HBITMAP hBitmap ; HWND hwnd ; int i, j, x1, y1, x2, y2 ; if (LockWindowUpdate (hwnd = GetDesktopWindow ())) { hdcScr = GetDCEx (hwnd, NULL, DCX_CACHE | DCX_LOCKWINDOWUPDATE) ; hdcMem = CreateCompatibleDC (hdcScr) ; cx = GetSystemMetrics (SM_CXSCREEN) / 10 ; cy = GetSystemMetrics (SM_CYSCREEN) / 10 ; hBitmap = CreateCompatibleBitmap (hdcScr, cx, cy) ; SelectObject (hdcMem, hBitmap) ; srand ((int) GetCurrentTime ()) ; for (i = 0 ; i < 2 ; i++) for (j = 0 ; j < NUM ; j++) { if (i == 0) { iKeep [j] [0] = x1 = cx * (rand () % 10) ; iKeep [j] [1] = y1 = cy * (rand () % 10) ; iKeep [j] [2] = x2 = cx * (rand () % 10) ; iKeep [j] [3] = y2 = cy * (rand () % 10) ; } else { x1 = iKeep [NUM - 1 - j] [0] ; y1 = iKeep [NUM - 1 - j] [1] ; x2 = iKeep [NUM - 1 - j] [2] ; y2 = iKeep [NUM - 1 - j] [3] ; } BitBlt (hdcMem, 0, 0, cx, cy, hdcScr, x1, y1, SRCCOPY) ; BitBlt (hdcScr, x1, y1, cx, cy, hdcScr, x2, y2, SRCCOPY) ; BitBlt (hdcScr, x2, y2, cx, cy, hdcMem, 0, 0, SRCCOPY) ; Sleep (10) ; } DeleteDC (hdcMem) ; ReleaseDC (hwnd, hdcScr) ; DeleteObject (hBitmap) ; LockWindowUpdate (NULL) ; } return FALSE ;}
自个试验去
因为慢着学习别的东西,没有太多时间来写自己的理解,匆忙写点笔记。
- 位图和位块传输(6)画框与会移动的小球
- Windows程序设计-位图和位块传输
- 位图和位块传输(3)之加载位图资源
- 第十四章 位图和Bitblt(位块传输)
- 位图和位块传输(1)BitBlt与PatBlt
- 位图和位块传输(5)之画图程序
- 第 14 章 位图和位块传输
- 位图和位块传输(4)位图画刷与画画
- 位图和位块传输(2)压缩拉伸之StretchBlt函数
- 14.3 位块传输
- 8位位图和24位位图的相互转换
- 移动的小球
- 移动的红色小球
- 移动的小球
- 移动的小球
- 移动的小球
- 飞翔的小球,小球自由移动
- 位向量/位图的定义和应用
- hdu 4046 Panda
- HDU-1012 小小的收获和对前辈的各种膜拜
- 【转】Android手机应用UI设计的10个建议
- 继承与动态内存分配——《c++primer plus第五版》
- 字符串数组的全排列到八皇后问题详解
- 位图和位块传输(6)画框与会移动的小球
- linux redhat Centos debian 破解root 密码 “Give root password for maintenance”
- c++ 内存分布(一)
- smd模型查看器
- 归回本质汇编系列(序言)
- c++内存分布(二)--虚函数和虚继承
- matlab2012一些函数删除后的替代解决方法及用到操作
- 839 - Not so Mobile--递归水题
- 右键菜单没有新建文本文档txt