A basic authoring example of drawing an GDI bitmap using C++

  • 2020-05-07 20:01:30
  • OfStack

1. Load bitmaps

2. Establish DC compatibility

3. Select the previous bitmap object

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 complete code:


#include <windows.h> 
#include <time.h> 
 
const int TAR_HIGH=800; 
const int TAR_WEIGHT=600; 
const wchar_t TAR_TITLE[]=L" To our indelible youth and ideals - The hero of jianda "; 
 
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); 
  // Do the initialization here  
  g_hbitmap=(HBITMAP)LoadImage(NULL,L"content.bmp",IMAGE_BITMAP,TAR_HIGH,TAR_WEIGHT,LR_LOADFROMFILE); 
  // To establish 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: 
    // New device environment handle call  
    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"; 
 
  // Registration window  
 
  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);// The first 3 Three parameters control the style of the window WS_OVERLAPPEDWINDOW 
  MoveWindow(hwnd,250,80,TAR_HIGH,TAR_WEIGHT,true); 
  ShowWindow(hwnd,nShowCmd); 
  UpdateWindow(hwnd); 
 
  // Load the error reporting module  
 
  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; 
} 

After a few unsuccessful attempts at the beginning, I found that the function call in WndProc was wrong, and the init part was not executed. The problem was solved after init was changed to init. It seems that like ACM1, you can't only follow others' template, or you should understand and modify it to make better use of it.


Related articles: