Bitmap and bit Block Transmission (6) small ball moved in the frame

Source: Internet
Author: User

By a very common technique, This method uses the "Mask" bitmap and some grating operations. The mask is a monochrome bitmap, and its size is the same as the size of the rectangle bitmap you want to display. Each mask pixel corresponds to a pixel on the bitmap to be displayed, and the mask pixel corresponding to the pixel to be displayed is white (1)

Next let's take a look at this example (from the fifth bible of Windows Programming)

#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) ;}

 

The following is the moving ball.

#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) ;}

 

Pop-up screen:

#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 ;}

Self-Testing

Because I learned other things slowly, I didn't have much time to write my own understanding, and I took some notes in a hurry.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.