1. 程式人生 > >【Visual C++】遊戲開發筆記十四 遊戲畫面繪圖 四 華麗的CImage類

【Visual C++】遊戲開發筆記十四 遊戲畫面繪圖 四 華麗的CImage類

詳細註釋的原始碼如下

#include "stdafx.h"  #include "atlimage.h"//全域性變數宣告  HINSTANCE hInst;  HBITMAP bg;        //宣告一個位圖物件,用於儲存背景圖  HDC  mdc;        //宣告一個記憶體DC"mdc",用來暫存點陣圖//全域性函式宣告  ATOM     MyRegisterClass(HINSTANCE hInstance)BOOL     InitInstance(HINSTANCE, int)LRESULT CALLBACK    WndProc(HWND, UINT, WPARAM, LPARAM)
void     MyPaint(HDC hdc)////****Winmain函式,程式入口點函式**************************************  int APIENTRY WinMain(HINSTANCE hInstance,                       HINSTANCE hPrevInstance,                       LPSTR     lpCmdLine,                       int       nCmdShow)  {  MSG msg;  MyRegisterClass(hInstance);  if
(!InitInstance (hInstance, nCmdShow))   {  return FALSE;  }  //訊息迴圈  while (GetMessage(&msg, NULL, 0, 0))   {  TranslateMessage(&msg);  DispatchMessage(&msg);  }  return msg.wParam;  }  //****設計一個視窗類,類似填空題,使用視窗結構體*************************  ATOM MyRegisterClass(HINSTANCE hInstance)  {  WNDCLASSEX wcex;  wcex.cbSize = sizeof
(WNDCLASSEX);   wcex.style   = CS_HREDRAW | CS_VREDRAW;  wcex.lpfnWndProc    = (WNDPROC)WndProc;  wcex.cbClsExtra  = 0;  wcex.cbWndExtra  = 0;  wcex.hInstance   = hInstance;  wcex.hIcon   = LoadIcon(NULL,IDI_APPLICATION);  wcex.hCursor     = NULL;  wcex.hCursor     = LoadCursor(NULL, IDC_ARROW);  wcex.hbrBackground  = (HBRUSH)(COLOR_WINDOW+1);  wcex.lpszMenuName   = NULL;  wcex.lpszClassName  = "canvas";  wcex.hIconSm     = NULLreturn RegisterClassEx(&wcex);  }  //****初始化函式*************************************  // 1.建立與視窗DC相容的記憶體DC  // 2.從檔案載入背景圖及透明的洋蔥頭 BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)  {  HWND hWnd;  HDC hdc;  hInst = hInstance;  hWnd = CreateWindow("canvas", "淺墨的繪圖視窗" , WS_OVERLAPPEDWINDOW,  CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);  if (!hWnd)  {  return FALSE;  }  MoveWindow(hWnd,10,10,600,444,true);  ShowWindow(hWnd, nCmdShow);  UpdateWindow(hWnd);  hdc = GetDC(hWnd);                     //獲得視窗DC  mdc = CreateCompatibleDC(hdc);           //建立與視窗相容的記憶體DC(mdc)  bg = (HBITMAP)LoadImage(NULL,"bg.bmp",IMAGE_BITMAP,600,444,LR_LOADFROMFILE);   MyPaint(hdc);  ReleaseDC(hWnd,hdc);  return TRUE;  }  //****自定義繪圖函式*********************************  //透明貼圖  void MyPaint(HDC hdc)  {  SelectObject(mdc,bg);  BitBlt(hdc,0,0,600,450,mdc,0,0,SRCCOPY);    //先將背景圖貼到顯示視窗中  CImage image;      //定義一個CImage物件,用於透明貼圖CString str;      //定義一個CString物件,用於存放檔名字串str="onion.png";              //將字串賦值為檔名image.Load(str);                    //在image中載入影象檔案image.Draw(hdc,120,180,85,113,0,0,85,113);    //呼叫Draw進行透明貼圖//或者為image.TransparentBlt(hdc, 120, 180, 85, 113,CLR_INVALID );//呼叫TransparentBlt進行透明貼圖image.Destroy();}  //****訊息處理函式**********************************  LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)  {  PAINTSTRUCT ps;  HDC hdc;  switch (message)  {  case WM_PAINT:   //視窗重繪訊息  hdc = BeginPaint(hWnd, &ps);  MyPaint(hdc);  EndPaint(hWnd, &ps);  breakcase WM_DESTROY:     //視窗結束訊息  DeleteDC(mdc);  DeleteObject(bg);  PostQuitMessage(0);  breakdefault:     //其他訊息  return DefWindowProc(hWnd, message, wParam, lParam);     }     return 0;  }