win32下handle转化为HBITMAP

来源:互联网 发布:阿唯得旗舰店淘宝 编辑:程序博客网 时间:2024/06/07 06:12
Code:
  1. //  handle to bitmap   
  2. HBITMAP hwndToHbitmap(HWND hwnd)   
  3. {   
  4.        
  5.     // get the DC of the handle   
  6.     HDC hDC = GetDC(hwnd);   
  7.        
  8.     // the rect struct    
  9.     RECT rect;   
  10.     // get the rect of the handle   
  11.     GetClientRect(hwnd, &rect);   
  12.   
  13.     // create a compatible dc for the hDC   
  14.     HDC hDCMem = CreateCompatibleDC(hDC);   
  15.   
  16.     // create a  compatible bitmap for the hDC   
  17.     HBITMAP hBitMap = CreateCompatibleBitmap(hDC, rect.right, rect.bottom);   
  18.   
  19.     // selects an object into a specified device context   
  20.     // the second parameter is als brush font pen and region   
  21.     // the first parameter is a handle point to the device context   
  22.     HBITMAP hOldMap = (HBITMAP)SelectObject(hDCMem, hBitMap);      
  23.        
  24.     //    
  25.     BitBlt(hDCMem, 0, 0, rect.right, rect.bottom, hDC, 0, 0, SRCCOPY);   
  26.     hBitMap = (HBITMAP)SelectObject(hDCMem, hOldMap);   
  27.     return hBitMap;   
  28. }  

 

原创粉丝点击