为什么没有使用 SelectObject 和 BitBlt 函数将屏幕截图绘制到我的窗口上?
我试图从屏幕的 0x0(左上角)位置获取 500x500 的屏幕截图并将其放入窗口中。
这是我的代码(hwnd
是我的窗口句柄):
HDC appDc = GetDC(hwnd);
HDC dc = GetDC(NULL);
HBITMAP bitmap = CreateCompatibleBitmap(dc, 500, 500);
HDC memoryDc = CreateCompatibleDC(dc);
SelectObject(memoryDc, bitmap);
BitBlt(appDc, 0, 0, 500, 500, dc, 0, 0, SRCCOPY);
ShowWindow(hwnd, SW_SHOW);
SetWindowText(hwnd, _T("Window"));
我在这里缺少什么?我在窗口内变黑,而不是屏幕截图。
编辑
它在我将 memoryDc
更改为 dc
后起作用 以前是 BitBlt(appDc, 0, 0, 500, 500, memoryDc, 0, 0, SRCCOPY); 但现在的问题是 SelectObject 不起作用。我的意思是它没有将图像放入 HBITMAP 中。但是 BitBlt 正在从 dc
复制到 appDc
I am trying to get a 500x500 screenshot from the 0x0 (top-left) position of screen and put it in a window.
Here is my code (hwnd
is my Window Handle):
HDC appDc = GetDC(hwnd);
HDC dc = GetDC(NULL);
HBITMAP bitmap = CreateCompatibleBitmap(dc, 500, 500);
HDC memoryDc = CreateCompatibleDC(dc);
SelectObject(memoryDc, bitmap);
BitBlt(appDc, 0, 0, 500, 500, dc, 0, 0, SRCCOPY);
ShowWindow(hwnd, SW_SHOW);
SetWindowText(hwnd, _T("Window"));
What am I missing here? I am getting black inside the window instead of the screen capture.
EDIT
It works after I changed memoryDc
to dc
It previously was BitBlt(appDc, 0, 0, 500, 500, memoryDc, 0, 0, SRCCOPY);
But now the problem is SelectObject is not working.I meant Its not putting the Image in HBITMAP. However BitBlt is copying from dc
to appDc
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,设备上下文似乎存在混淆。您从内存Dc 传输到appDc,但内存Dc 不包含任何内容 - 它已被创建为与dc 兼容,但这并不意味着它共享内容。另外,您在示例中没有释放 DC。
其次,您对 ShowWindow() 的调用似乎暗示该窗口以前不可见。如果是这种情况,那么之前“绘制”的任何内容实际上都没有被绘制,并且在窗口中将不可见。以位图形式捕获屏幕内容并在 WM_PAINT 期间显示它。
First, there seems to be a confusion with the device contexts. You blit from memoryDc to appDc, but memoryDc does not contain anything - it has been created to be compatible to dc, but that does not mean it shares the content. Also, you do not release the DCs in your example.
Second, your call to ShowWindow() seems to imply that the window has not been visible before. If that is the case, anything that has been "drawn" before has not actually been drawn and will not be visible in the window. Capture the screen content in a bitmap and display it during WM_PAINT.
由于您在代码块末尾为应用程序的窗口调用
ShowWindow
,因此我假设该窗口在此之前不可见。如果是这样,那么这就是您的问题,因为当不可见的窗口再次可见时,其客户区域总是重新绘制。这会导致使用该窗口的默认画笔(在您的情况下显然是黑色画笔)以及您在其设备上下文 (DC) 中绘制的任何内容(使用 BitBlt 函数)来擦除其背景丢失。
更好的方法是将屏幕捕获绘制到临时位图中。然后,只需保留此位图的副本,并在收到
WM_PAINT
消息时将其绘制到窗口上。Since you're calling
ShowWindow
for your application's window at the end of the code block, I assume that the window is not visible prior to that time.If so, then that is your problem because when an invisible window is made visible again, its client area is always repainted. This causes its background to be erased with the default brush for that window (apparently a black brush, in your case), and anything that you painted (using the
BitBlt
function) into its device context (DC) to be lost.A better approach would be to draw the screen capture into a temporary bitmap, instead. Then, just keep a copy of this bitmap around and paint it onto the window whenever you receive a
WM_PAINT
message.