GetWindowDC(NULL) 失败
我有一个调用 GetWindowDC(NULL)
的函数。并且该函数由不同的线程调用。有时我看到 GetWindowDC(NULL)
返回 0x0
为什么?以及如何解决?该函数被正确地互斥保护。所以这不是问题。每次通话后我需要sleep()
几毫秒吗?为什么?
http://msdn.microsoft.com/en -us/library/dd144947%28v=vs.85%29.aspx
msdn说
返回值
如果函数成功,返回值是指定窗口的设备上下文的句柄。 如果函数失败,返回值为NULL,指示错误或hWnd参数无效。
但这指示错误意味着什么我无法理解
编辑:
是的,我正在释放所有 DC 并正确删除 DC
立即调用 GetLastError
retrns 0
尝试过。
if(hdc == 0x0)
qDebug() << GetLastError()
这是我的源代码。它工作,但工作超过 100 次(35-40 秒)后,它拒绝工作,并且 GetWindowDC(NULL)
返回 0
QPixmap Util::grabScreen(const DG::Rect* rect){
mutex.lock();
HDC hdc=GetWindowDC(NULL);
HWND win=WindowFromDC(hdc);
HDC cdc=CreateCompatibleDC(hdc);
HBITMAP temp=CreateCompatibleBitmap(hdc,rect->width,rect->height);
PAINTSTRUCT ps;
hdc=BeginPaint(win,&ps);
HBITMAP oldb=(HBITMAP)SelectObject(cdc,temp);
BitBlt(cdc,0,0,rect->width,rect->height,hdc,rect->top,rect->left,SRCCOPY);
SelectObject(cdc,oldb);
EndPaint(win,&ps);
char* buff;
buff = new char[rect->size()];
GetBitmapBits(temp,rect->size(),buff);
qDebug() << "temp" << temp;
if(temp == 0x0){
qDebug() << "hdc" << hdc;
}
DeleteDC(cdc);
ReleaseDC(NULL, hdc);
DeleteDC(hdc);
QPixmap pixmap = QPixmap::fromWinHBITMAP(temp);
//QPixmap pixmap = QPixmap::grabWidget(desktopWidget,rect->toQRect());
mutex.unlock();
return pixmap;
}
I've a function that calls GetWindowDC(NULL)
. and that function is called by different threads. sometimes I see GetWindowDC(NULL)
returning 0x0
Why ? and How to resolve ? the function is mutex gaurded properly. So thats not a Problem. Do I need to sleep()
few ms after each call ? and Why ?
http://msdn.microsoft.com/en-us/library/dd144947%28v=vs.85%29.aspx
msdn Says
Return Value
If the function succeeds, the return value is a handle to a device context for the specified window.
If the function fails, the return value is NULL, indicating an error or an invalid hWnd parameter.
but What this indicating an error means I couldn't understand
Edit:
and Ya I am releasing all the DCs and also Deleting the DCs properly
Giving an immediate call to GetLastError
retrns 0
tried.
if(hdc == 0x0)
qDebug() << GetLastError()
Here goes my source code. It Works but after working more than a 100 times (35-40 secs) it refuses to work and GetWindowDC(NULL)
returns 0
QPixmap Util::grabScreen(const DG::Rect* rect){
mutex.lock();
HDC hdc=GetWindowDC(NULL);
HWND win=WindowFromDC(hdc);
HDC cdc=CreateCompatibleDC(hdc);
HBITMAP temp=CreateCompatibleBitmap(hdc,rect->width,rect->height);
PAINTSTRUCT ps;
hdc=BeginPaint(win,&ps);
HBITMAP oldb=(HBITMAP)SelectObject(cdc,temp);
BitBlt(cdc,0,0,rect->width,rect->height,hdc,rect->top,rect->left,SRCCOPY);
SelectObject(cdc,oldb);
EndPaint(win,&ps);
char* buff;
buff = new char[rect->size()];
GetBitmapBits(temp,rect->size(),buff);
qDebug() << "temp" << temp;
if(temp == 0x0){
qDebug() << "hdc" << hdc;
}
DeleteDC(cdc);
ReleaseDC(NULL, hdc);
DeleteDC(hdc);
QPixmap pixmap = QPixmap::fromWinHBITMAP(temp);
//QPixmap pixmap = QPixmap::grabWidget(desktopWidget,rect->toQRect());
mutex.unlock();
return pixmap;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
根据 MSDN 你不应该对使用
GetDC
获取的句柄调用DeleteDC
。您应该只调用ReleaseDC
即可。According to MSDN you shouldn't call
DeleteDC
on an handle obtained usingGetDC
. You should just callReleaseDC
instead.不知道正在发生的事情的更多细节;这个场景对我来说听起来有点奇怪。
但是...
在您尝试获取 WindowDC 的同时,代码是否可能也在处理同一窗口的 WM_PAINT ?
我不确定您是否可以获得正在主动绘制的同一窗口的窗口 DC。
Without know more details on what is going on; the scenario sounds a bit odd to me.
However...
Is it possible that code is also processing a WM_PAINT for the same window at the same time you are trying to get the WindowDC ?
I'm not sure if you can obtain a window DC for the same window that is being actively painted.