使用C++繪製GDI位元影像的基本編寫執行個體_C 語言

來源:互聯網
上載者:User

1、載入位元影像

2、建立相容DC

3、選擇之前的位元影像對象

4、用貼圖函數BitBlt()

HBITMAP bitmap=(HBITMAP)LoadImage(NULL,L"Name.bmp",IMAGE_BITMAP,high,length,LR_LOADFROMFILE); HWND tmp=CreateCompatiable(g_hdc); SelectObject(tmp,bitmap); BitBlt(g_hdc,0,0,high,length,tmp,0,0,SRCCOPY); 


Demo完整代碼:

#include <windows.h> #include <time.h>  const int TAR_HIGH=800; const int TAR_WEIGHT=600; const wchar_t TAR_TITLE[]=L"致我們不磨滅的青春和理想-建大英雄傳";  HDC g_hdc=NULL; HDC g_mdc=NULL; HBITMAP g_hbitmap=NULL;  void Game_Paint(HWND hwnd) {   SelectObject(g_mdc,g_hbitmap);   BitBlt(g_hdc,0,0,TAR_HIGH,TAR_WEIGHT,g_mdc,0,0,SRCCOPY); }  bool Game_Init(HWND hwnd) {   g_hdc=GetDC(hwnd);   //在這裡做初始化工作   g_hbitmap=(HBITMAP)LoadImage(NULL,L"content.bmp",IMAGE_BITMAP,TAR_HIGH,TAR_WEIGHT,LR_LOADFROMFILE);   //建立DC   g_mdc=CreateCompatibleDC(g_hdc);    Game_Paint(hwnd);   ReleaseDC(hwnd,g_hdc);   return 1; }  bool Game_Clear(HWND hwnd) {   DeleteObject(g_hbitmap);   DeleteDC(g_mdc);   return 1; }   LRESULT CALLBACK SdjzuProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam) {   switch(message)   {     PAINTSTRUCT paintstruct;   case WM_PAINT:     //新增裝置環境控制代碼調用     g_hdc=BeginPaint(hwnd,&paintstruct);     Game_Init(hwnd);     EndPaint(hwnd,&paintstruct);     ValidateRect(hwnd,NULL);     break;     case WM_KEYDOWN:       if(wParam=VK_ESCAPE)       DestroyWindow(hwnd);       break;   case WM_DESTROY:     PostQuitMessage(0);     break;   default:     return DefWindowProc(hwnd,message,wParam,lParam);   }   return 0; }   int WINAPI WinMain( __in HINSTANCE hInstance, __in_opt HINSTANCE hPrevInstance, __in LPSTR lpCmdLine, __in int nShowCmd ) {   WNDCLASSEX wndclass={0};   wndclass.cbSize=sizeof(WNDCLASSEX);   wndclass.style=CS_HREDRAW|CS_VREDRAW;   wndclass.lpfnWndProc=SdjzuProc;   wndclass.cbClsExtra=0;   wndclass.cbWndExtra=0;   wndclass.hInstance=hInstance;   wndclass.hIcon=(HICON)::LoadImage(NULL,L"tarico.ico",IMAGE_ICON,0,0,LR_DEFAULTSIZE|LR_LOADFROMFILE);   wndclass.hCursor=LoadCursor(NULL,IDC_ARROW);   wndclass.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);   wndclass.lpszMenuName=NULL;   wndclass.lpszClassName=L"sdjzuhero";    //註冊視窗    if(!RegisterClassEx(&wndclass))     return -1;   HWND hwnd=CreateWindow(L"sdjzuhero",TAR_TITLE,WS_OVERLAPPED|WS_CAPTION|WS_SYSMENU|WS_MINIMIZEBOX,CW_USEDEFAULT,CW_USEDEFAULT,TAR_HIGH,TAR_WEIGHT,NULL,NULL,hInstance,NULL);//第三個參數控制了視窗的樣式,合集為WS_OVERLAPPEDWINDOW   MoveWindow(hwnd,250,80,TAR_HIGH,TAR_WEIGHT,true);   ShowWindow(hwnd,nShowCmd);   UpdateWindow(hwnd);    //載入報錯模組    MSG msg={0};   while(msg.message!=WM_QUIT)   {     if(PeekMessage(&msg,0,0,0,PM_REMOVE))     {       TranslateMessage(&msg);       DispatchMessage(&msg);     }   }    UnregisterClass(L"sdjzuhero",wndclass.hInstance);    return 0; } 

剛開始試了幾次沒有成功,發現是在WndProc裡對函數的調用出錯了,沒有執行init部分,改成init之後問題解決。看來與ACM一樣,不能唯別人的模板是從,還是應該理解後加以修改,以更好的利用。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.