win32SDK中使用bmp和png點陣圖
阿新 • • 發佈:2019-01-05
Win32SDK.h
#pragma once #include <windows.h> #include <tchar.h> #include <assert.h> #include <GdiPlus.h> #include <AtlImage.h> #pragma comment(lib,"gdiplus.lib") //名稱空間 using namespace Gdiplus; //繪畫模式 #define DRAW_MODE_SPREAD 0 //平鋪模式 #define DRAW_MODE_CENTENT 1 //居中模式 #define DRAW_MODE_ELONGGATE 2 //拉伸模式 //全域性變數定義 CImage m_Imagebmp; //bmp圖片 Image * m_pImage=NULL; //png圖片 HWND g_hwnd; //視窗控制代碼 TCHAR szWindowClass[]=TEXT("哈哈"); //全域性函式定義 //視窗回撥 LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); //建立事件 bool OnCreate(HWND hwnd); //繪畫背景 VOID DrawViewImage(HDC DC, CImage & BitImage, BYTE cbDrawMode); //繪畫PNG影象 bool DrawImagePng(HDC DC, INT nXPos, INT nYPos);
Win32SDK.cpp
#include "Win32SDK.h" int APIENTRY _tWinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPTSTR lpCmdLine,int nCmdShow) { WNDCLASSEX wcex={0}; wcex.cbSize = sizeof(WNDCLASSEX); wcex.style = CS_HREDRAW | CS_VREDRAW; wcex.lpfnWndProc = WndProc; wcex.cbClsExtra = 0; wcex.cbWndExtra = 0; wcex.hInstance = hInstance; wcex.hIcon = LoadIcon(NULL, MAKEINTRESOURCE(IDI_APPLICATION)); wcex.hCursor = LoadCursor(NULL, IDC_ARROW); wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); wcex.lpszMenuName = NULL; wcex.lpszClassName = szWindowClass; wcex.hIconSm = LoadIcon(NULL, MAKEINTRESOURCE(IDI_APPLICATION)); RegisterClassEx(&wcex); HWND hWnd; hWnd = CreateWindow(szWindowClass, TEXT("標題"), WS_OVERLAPPEDWINDOW,0, 0, 600, 400, NULL, NULL, hInstance, NULL); if (!hWnd)return FALSE; ShowWindow(hWnd, nCmdShow); UpdateWindow(hWnd); MSG msg={0}; while (GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return (int) msg.wParam; } LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { int wmId, wmEvent; PAINTSTRUCT ps; HDC hdc; switch (message) { case WM_CREATE: OnCreate(hWnd);break; case WM_COMMAND: { wmId = LOWORD(wParam); wmEvent = HIWORD(wParam); // 分析選單選擇: switch (wmId) { case IDOK:break; default: return DefWindowProc(hWnd, message, wParam, lParam); } } break; case WM_PAINT: { hdc = BeginPaint(hWnd, &ps); // TODO: 在此新增任意繪圖程式碼... DrawViewImage(hdc,m_Imagebmp,DRAW_MODE_CENTENT); DrawImagePng(hdc,0,0); EndPaint(hWnd, &ps); } break; case WM_DESTROY: PostQuitMessage(0); break; default: return DefWindowProc(hWnd, message, wParam, lParam); } return 0; } bool OnCreate(HWND hwnd) { g_hwnd=hwnd; //視窗居中 int x=GetSystemMetrics(SM_CXSCREEN); int y=GetSystemMetrics(SM_CYSCREEN); RECT WindowRect; GetWindowRect(hwnd,&WindowRect); int cx=(x-WindowRect.right)/2; int cy=(y-WindowRect.bottom)/2; SetWindowPos(hwnd,NULL,cx,cy,NULL,NULL,SWP_SHOWWINDOW|SWP_NOSIZE); //載入 bmp檔案 m_Imagebmp.Load(TEXT("res\\bmp\\VIEW_BACK.bmp")); //載入 png檔案 CT2CW strFileName(TEXT("res\\png\\READY.png")); m_pImage=Image::FromFile((LPCWSTR)strFileName); //錯誤判斷 if ((m_pImage==NULL)||(m_pImage->GetLastStatus()!=Ok)) { try { delete m_pImage; } catch (...) { assert(FALSE); } m_pImage=NULL; return false; } return true; } //繪畫背景 VOID DrawViewImage( HDC DC, CImage & BitImage, BYTE cbDrawMode ) { //獲取位置 CRect rcClient; GetClientRect(g_hwnd,&rcClient); //無效區域 RECT rcDirty={0}; GetClipBox(DC,&rcDirty); //繪畫點陣圖 switch (cbDrawMode) { case DRAW_MODE_SPREAD: //平鋪模式 { //載入資源 INT nImageWidth=BitImage.GetWidth(); INT nImageHeight=BitImage.GetHeight(); //繪畫點陣圖 for (INT nXPos=0;nXPos<rcClient.right;nXPos+=nImageWidth) { for (INT nYPos=0;nYPos<rcClient.bottom;nYPos+=nImageHeight) { BitImage.BitBlt(DC,nXPos,nYPos); } } return; } case DRAW_MODE_CENTENT: //居中模式 { //變數定義 INT nImageWidth=BitImage.GetWidth(); INT nImageHeight=BitImage.GetHeight(); //位置計算 INT nXPos=(rcClient.Width()-BitImage.GetWidth())/2; INT nYPos=(rcClient.Height()-BitImage.GetHeight())/2; //繪畫點陣圖 BitImage.BitBlt(DC,nXPos,nYPos); return; } case DRAW_MODE_ELONGGATE: //拉伸模式 { //變數定義 INT nImageWidth=BitImage.GetWidth(); INT nImageHeight=BitImage.GetHeight(); //繪畫點陣圖 BitImage.StretchBlt(DC,0,0,rcClient.Width(),rcClient.Height(),0,0,nImageWidth,nImageHeight); return; } } return; } //繪畫PNG影象 bool DrawImagePng( HDC DC, INT nXPos, INT nYPos ) { //載入判斷 assert(m_pImage!=NULL); if (m_pImage==NULL) return false; //建立螢幕 assert(DC!=NULL); Graphics graphics(DC); //獲取屬性 INT nImageWidth=m_pImage->GetWidth(); INT nImageHeight=m_pImage->GetHeight(); //構造位置 RectF rcDrawRect; rcDrawRect.X=(REAL)nXPos; rcDrawRect.Y=(REAL)nYPos; rcDrawRect.Width=(REAL)nImageWidth; rcDrawRect.Height=(REAL)nImageHeight; //繪畫影象 graphics.DrawImage(m_pImage,rcDrawRect,0,0,(REAL)nImageWidth,(REAL)nImageHeight,UnitPixel); return true; }