为什么没有使用 SelectObject 和 BitBlt 函数将屏幕截图绘制到我的窗口上?

发布于 2024-10-29 08:40:30 字数 675 浏览 3 评论 0原文

我试图从屏幕的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

苍暮颜 2024-11-05 08:40:30

首先,设备上下文似乎存在混淆。您从内存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.

美羊羊 2024-11-05 08:40:30

由于您在代码块末尾为应用程序的窗口调用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文