亲宝软件园·资讯

展开

C++绘制GDI位图 使用C++绘制GDI位图的基本编写实例

mig_davidli 人气:0
想了解使用C++绘制GDI位图的基本编写实例的相关内容吗,mig_davidli在本文为您仔细讲解C++绘制GDI位图的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:C++,GDI,GUI,下面大家一起来学习吧。

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一样,不能唯别人的模板是从,还是应该理解后加以修改,以更好的利用。

加载全部内容

相关教程
猜你喜欢
用户评论