位元影像和位塊傳輸(6)畫框與會移動的小球

來源:互聯網
上載者:User

由一個很常用的技巧,這個方法使用"掩碼"位元影像和一些光柵操作,掩碼是一個單色位元影像,它的大小和你要顯示的矩形位元影像大小相同。每個掩碼像素對應要顯示的位元影像上的一個像素,要顯示出來的像素對應的掩碼像素為白色(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 ;}

自個實驗去

因為慢著學習別的東西,沒有太多時間來寫自己的理解,匆忙寫點筆記。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.