程式碼如下:
HBITMAPholdbitmap,hbitmap;
HDChdc,hscreendc,hmemdc;
hdc=GetWindowDC(hwnd);//獲得整個hwnd指向的視窗的DC
GetWindowRect(hwnd,&rect);//獲得視窗矩形
hscreendc=CreateDC("DISPLAY",NULL,NULL,NULL);//獲得整個螢幕的裝置DC
hmemdc=CreateCompatibleDC(hdc);//建立與裝置DC相容的記憶體DC
hbitmap=CreateCompatibleBitmap(hscreendc,GetDeviceCaps(hdcScreen,HORZRES),GetDeviceCaps(hdcScreen,VERTRES));//建立與螢幕DC相容的點陣圖
holdbitmap=(HBITMAP)SelectObject(hmemdc,hbitmap);//將hbitmap指向的點陣圖選入記憶體DC,返回值為原來的點陣圖holdbitmap,先儲存下來後面恢復
BitBlt(hmemdc,0,0,rect.right-rect.left,rect.bottom-rect.top,hscreendc,0,0,SRCCOPY);//將裝置DC的圖象複製到記憶體DC,這樣就把螢幕圖象儲存到hbitmap所指向的BITMAP結構體中了
程式碼如下:
HBITMAPholdbitmap,hbitmap;
HDChdc,hscreendc,hmemdc;
hdc=GetWindowDC(hwnd);//獲得整個hwnd指向的視窗的DC
GetWindowRect(hwnd,&rect);//獲得視窗矩形
hscreendc=CreateDC("DISPLAY",NULL,NULL,NULL);//獲得整個螢幕的裝置DC
hmemdc=CreateCompatibleDC(hdc);//建立與裝置DC相容的記憶體DC
hbitmap=CreateCompatibleBitmap(hscreendc,GetDeviceCaps(hdcScreen,HORZRES),GetDeviceCaps(hdcScreen,VERTRES));//建立與螢幕DC相容的點陣圖
holdbitmap=(HBITMAP)SelectObject(hmemdc,hbitmap);//將hbitmap指向的點陣圖選入記憶體DC,返回值為原來的點陣圖holdbitmap,先儲存下來後面恢復
BitBlt(hmemdc,0,0,rect.right-rect.left,rect.bottom-rect.top,hscreendc,0,0,SRCCOPY);//將裝置DC的圖象複製到記憶體DC,這樣就把螢幕圖象儲存到hbitmap所指向的BITMAP結構體中了