Home > Software engineering >  Why GetDC (HWND), get in HDC, after using a BitBlt is empty?
Why GetDC (HWND), get in HDC, after using a BitBlt is empty?

Time:09-26

There is an application called sabaki, under win10, run it later, it has a chessboard window, I use

HDC HDC=GetDC (HWND); Access to its window dc

Reoccupy BitBlt transferred to another image, then the image displayed, it was found that image completely black, empty,

CodePudding user response:

GetDC used during the WM_PAINT message, during the WM_PAINT message, use BenginPaint for DC

CodePudding user response:

It is said that DC is not cross thread!

CodePudding user response:

Try to copy the screen, and then capture part of the

 
BOOL CopyWnd (HWND hSrcWnd, HWND hDstWnd, BOOL bClientOnly)
{
BOOL bRet=FALSE;

HWND hDeskTop=NULL;
HDC hSrcDC=NULL;
HDC hMemDC=NULL;
HBITMAP hMemBmp=NULL;
HGDIOBJ hOldBmp=NULL;
HDC hDstDC=NULL;

Do
{
HDeskTop=GetDesktopWindow ();//get handle to the desktop

//handle to detect validity
if(! (hDeskTop
& & (hSrcWnd==NULL | | IsWindow (hSrcWnd))
& & IsWindow (hDstWnd)
& & 1))
{
BRet=1;
break;
}

//desktop DC
HSrcDC=GetDC (hDeskTop);
//Get Widht & amp; Height
Int nWidth=GetDeviceCaps (hSrcDC HORZRES);
Int nHeight=GetDeviceCaps (hSrcDC VERTRES);

//create a memory DC
HMemDC=CreateDC (_T (" DISPLAY "), NULL, NULL, NULL);
If (hMemDC==NULL)
{
BRet=2;
break;
}

//create a memory bitmap
HMemBmp=CreateBitmap (nWidth, nHeight, 1, 32, NULL);
If (hMemBmp==NULL)
{
BRet=3;
break;
}
//for memory bitmap
HOldBmp=SelectObject (hMemDC hMemBmp);

/*
if(! PrintWindow (hSrcWnd hMemDC, 0))
{
BRet=4;
break;
}
*/
//copy the desktop DC
if(! BitBlt (hSrcDC, 0, 0, nWidth, nHeight, hMemDC, 0, 0, SRCCOPY))
{
BRet=4;
break;
}

//get map area coordinates
The RECT rcSrc rcDst;
If (hSrcWnd==NULL)//empty by default for the entire screen
{
RcSrc. Left=rcSrc. Top=0;
RcSrc. Right=nWidth, rcSrc. Bottom=nHeight;
}
Else if (bClientOnly)
{
GetClientRect (hSrcWnd, & amp; RcSrc);
POINT lt={rcSrc. Left, rcSrc. Top};
POINT rb={rcSrc. Right, rcSrc bottom};
ClientToScreen (hSrcWnd, & amp; Lt);
ClientToScreen (hSrcWnd, & amp; Rb);
RcSrc. Left=lt. X, rcSrc. Top=lt y;
RcSrc. Right=rb. X, rcSrc. Bottom=rb. Y;
}
The else
{
GetWindowRect (hSrcWnd, & amp; RcSrc);
}
GetClientRect (hDstWnd, & amp; RcDst);

HDstDC=GetDC (hDstWnd);
If (hDstDC==NULL)
{
BRet=5;
break;
}

//set the zoom halftone
Int nOldMode=SetStretchBltMode (hDstDC, HALFTONE);
//zoom the map
StretchBlt (hDstDC,
0, 0, rcDst. Right - rcDst. RcDst. Bottom - rcDst. Left, top,
HMemDC,
RcSrc left, rcSrc. Top rcSrc. Right - rcSrc. RcSrc. Bottom - rcSrc. Left, top,
SRCCOPY);
//restore zoom mode
SetStretchBltMode (hDstDC nOldMode);

BRet=TRUE;

} while (0);

//end cleanup
If (hDstWnd & amp; & HDstDC)
{
ReleaseDC (hDstWnd hDstDC);
HDstDC=NULL;
}
If (hMemDC & amp; & HOldBmp)
{
SelectObject (hMemDC hOldBmp);
}
If (hMemBmp)
{
DeleteObject (hMemBmp);
HMemBmp=NULL;
}
If (hMemDC)
{
DeleteDC (hMemDC);
HMemDC=NULL;
}
If (hDeskTop & amp; & HSrcDC)
{
ReleaseDC (hDeskTop hSrcDC);
HSrcDC=NULL;
}

Return bRet.
}

Void CDlg1Dlg: : OnButton1 ()
{
Cstrings szHwnd;
GetDlgItemText (IDC_EDIT1 szHwnd);
HWND hSrcWnd=(HWND) _ttoi64 (szHwnd);//window handle
CopyWnd (hSrcWnd, GetDlgItem (IDC_STATIC1) - & gt; M_hWnd, 0);
}

  • Related