Why does BitBlt hang? - c++

I am writing C++ code to automatically take screenshots of my favorite game. Part of my code looks like this:
SelectObject(hCDC, shot);
BitBlt(hCDC, 0, 0, WIDTH, HEIGHT, hDDC, 0, 0, SRCCOPY);
SelectObject(hCDC, oldBitmap);
Bitmap shotBitmap(shot, NULL);
I had previously initialized the relevant variables, like this:
HDC hDDC = GetDC(handle);
HDC hCDC = CreateCompatibleDC(hDDC);
HBITMAP shot = CreateCompatibleBitmap(hDDC, WIDTH, HEIGHT);
HBITMAP oldBitmap = (HBITMAP) SelectObject(hCDC, shot);
For no apparent reason, the code hangs at the BitBlt function until something (but I don't know what) updates. The millisecond I click a button in the game, or click on a window outside the game, BitBlt finishes and the rest of the code executes flawlessly. Mysteriously, I've only run into this problem recently; it never happened before. Even more mysteriously, it only happens on my laptop, not on my friend's. What could be happening?

Related

The GDI text display problem (I just use GDI and not use Unity or DirectX)

I am writing a program game and want to show a text on the game's beginning.
I have checked the syntax of the program to guarantee there is no problem.
But the text can't display on the window.
I declare two global variables HDC:
HDC g_hdc, g_mdc;
The first is used to display, the second is used to buffer.
The text display code:
HFONT hFont = CreateFont(45, 0, 0, 0, 0, 0, 0, 0, GB2312_CHARSET, 0, 0, 0, 0, NULL);
SelectObject(g_hdc, hFont);
SetBkMode(g_hdc, TRANSPARENT);
wchar_t text1[] = L"Begin!";
SetTextColor(g_hdc, RGB(50, 255, 50));
TextOut(g_hdc, 0, 0, text1, wcslen(text1));
This code is included in the function init() so the code will be used when the windows create and just use only once.
When I run this program I just got this :
image
You can see the bitmap show correct but the text is not.
Can anyone tell me why?
I find why it can't display. I change the way of get HDC form PaintBegin() to GetDC() then the text begin to display. The PaintBegin() looks like only using when WM_PAINT message be caught.

Capture Screenshot from another desktop

I have created a second desktop using CreateDesktop and im not switching to it. Also i have created some processes in it like Explorer.exe and Winrar.exe. Next i have a code which takes Screenshot of current desktop to clipboard. Both CreateDesktop and Screenshot works, But Screenshot of that new desktop or window returns a black bitmap:
This is the screenshot for a window in a desktop which returns current desktop:
// hwnd is handle to winrar or ... created in a new desktop retrieved by EnumDesktopWindow
RECT rc;
GetClientRect(hwnd, &rc);
const HDC hScreenDC = GetDC(nullptr);
const HDC hMemoryDC = CreateCompatibleDC(hScreenDC);
const int width = GetDeviceCaps(hScreenDC, HORZRES);
const int height = GetDeviceCaps(hScreenDC, VERTRES);
const HBITMAP hBitmap = CreateCompatibleBitmap(hScreenDC, width, height);
HBITMAP(SelectObject(hMemoryDC, hBitmap));
BitBlt(hMemoryDC, 0, 0, width, height, hScreenDC, 0, 0, SRCCOPY);
OpenClipboard(nullptr);
EmptyClipboard();
SetClipboardData(CF_BITMAP, hBitmap);
CloseClipboard();
DeleteDC(hMemoryDC);
DeleteDC(hScreenDC);
I have implemented both this methods in c# but same thing happens there.
There are great resources like:
Capture screenshot of hidden desktop
take a screenshot of a desktop created using createdesktop api
C# – SCREEN CAPTURE WITH VISTA DWM (SHARED DIRECT3D SURFACE)
Window Contents Capturing using WM_PRINT Message
how to capture screen from another desktop?(CreateDesktop)
Also this is like a dead topic, No new article, Explanation or solution to it.
I have read most of them but no luck, This was my closest try i think. Also language doesnt matter for me: C#, C++, Python or ... .
I found the solution, It is interesting but no perfect, Just resolves my needs.
After CreateDesktop by calling OpenDesktop then SetThreadDesktop then using the screenshot code you get the screenshot of the window which is created inside CreateDesktop, Also no need for Creating Explorer.exe inside it if you just want the window:
CreateDesktopW(L"NewDesktop"); // CreateDesktop code here. This is my function
const HDESK Handle = OpenDesktopW(L"NewDesktop", 0, 0, GENERIC_ALL);
SetThreadDesktop(Handle);
// Above ScreenShot code here ...
The screenshot code needs a PrintWindow:
RECT rc;
GetClientRect(hwnd, &rc);
const HDC hScreenDC = GetDC(nullptr);
const HDC hMemoryDC = CreateCompatibleDC(hScreenDC);
const int width = GetDeviceCaps(hScreenDC, HORZRES);
const int height = GetDeviceCaps(hScreenDC, VERTRES);
const HBITMAP hBitmap = CreateCompatibleBitmap(hScreenDC, width, height);
HBITMAP(SelectObject(hMemoryDC, hBitmap));
BitBlt(hMemoryDC, 0, 0, width, height, hScreenDC, 0, 0, SRCCOPY);
/// ADDED CODE
PrintWindow(hWnd, hMemoryDC, PW_CLIENTONLY);
///
OpenClipboard(nullptr);
EmptyClipboard();
SetClipboardData(CF_BITMAP, hBitmap);
CloseClipboard();
DeleteDC(hMemoryDC);
DeleteDC(hScreenDC);
Mine worked with a winrar.exe window inside a inactive desktop. You can try this then paste it to paint to see the result.
There is just one thing, The whole area of the screenshot bitmap is black except the window handle that i want which is fine by me. I think i should get handle of every window from bottom to top in order then mix them up.
All additions to this are appreciated.

win32 picture control stop repainting

First, I have a picture control with a bitmap1 loaded in a dialog box:
SendMessage(hWnd, STM_SETIMAGE, IMAGE_BITMAP, (LPARAM)LoadImage(NULL, sbitmap1.c_str(), IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE));
Second, at some point I draw a bitmap2 in the picture control using StretchBlt.
hdcImage = GetDC(hWnd)
hMemDC = CreateCompatibleDC( hdcImage );
hOldbm = (HBITMAP)SelectObject( hMemDC, hbitmap2 );
SetStretchBltMode( hdcImage, COLORONCOLOR);
StretchBlt( hdcImage, left, top, width, height, hMemDC, 0, 0, w, h, SRCCOPY );
SelectObject( hMemDC, hOldbm );
The bitmap2 is successfully painted but in certain occasions (for example when I minimize the dialog) the picture control no longer shows bitmap2 but bitmap1 instead.
I think the problem is the repaint event. Is there a way to stop the repaint event or change the bitmap that this event is going to paint?
Edit:
Thanks #Mark and #Edward for your answers.
The problem was this:
after using StretchBlt you need to do SendMessage STM_SETIMAGE
for SendMessage STM_SETIMAGE use a global HBITMAP (preferably)
Something like this:
hbitmapglobal = (HBITMAP)CopyImage(hbitmap2, IMAGE_BITMAP, abs(width), abs(height), LR_COPYRETURNORG);
SendMessage(hWnd, STM_SETIMAGE, IMAGE_BITMAP, (LPARAM)hbitmapglobal);
CopyImage is the real simple way to make a copy of a HBITMAP. Take in consideration that this is a sample and hbitmapglobal must be freed at some point.
Normally all the painting occurs in the WM_PAINT handler.
I would suggest setting up (global) variables when you need to paint the second bitmap and invalidate the rectangle of the picture control.
Check for the variable in the WM_PAINT handler and do the painting there.

Win32 application. HBITMAP LoadImage fails to load anything

I'm writing a function that quickly draws an image of a menu for a game I'm making. I'm able to draw the background and text blocks just fine but I'm having trouble creating a bitmap image on the screen
bool menu::drawMenu(PAINTSTRUCT ps)
{
HWND hWnd = GetActiveWindow();
HDC hdc = GetDC(hWnd), hdcMem;
//Draw a new background
HPEN blackPen = CreatePen(PS_SOLID, 1, RGB(0, 0, 0));
HBRUSH blackBrush = CreateSolidBrush(RGB(0, 0, 0));
SelectObject(hdc, blackPen);
SelectObject(hdc, blackBrush);
Rectangle(hdc, 0, 0, 1080, 720);
//insert selection text
TextOut(hdc, 30, 0, L"New Game", 8);
TextOut(hdc, 30, 30, L"Exit Game", 9);
//draw arrow sprite
HBITMAP arrow = (HBITMAP)LoadImage(NULL, L"C:\\Users\\Tim\documents\\visual studio 2013\\Projects\\BoulderBisque\\BoulderBisque\\arrow.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
DWORD lastError = GetLastError();
if (arrow == NULL)
{
TextOut(hdc, 0, 60, L"Image Load Failed", 17);
return false;
}
hdcMem = CreateCompatibleDC(hdc);
if (hdcMem == NULL)
{
TextOut(hdc, 0, 90, L"Memory Creation Failed", 22);
return false;
}
SelectObject(hdcMem, arrow);
BitBlt(hdc, 0, choice * 30, 16, 16, hdcMem, 0, 0, SRCCOPY);
//cleanup
ReleaseDC(hWnd, hdc);
DeleteDC(hdcMem);
return true;
}
As of now arrow is NULL and I get the Load Image Failed textbox.
I am using the relative path of arrow.bmp I also tried using a full path, that didn't work either.
You may have noticed that this function is outside the WndProc. Everything else draws fine.
I tried running it in there too everything but the arrow.bmp loads.
What am I doing wrong to cause arrow.bmp to be NULL? I have other methods I plan to run in similar ways so getting this function to run would really be a big help.
EDIT* Whenever I give the full path name it still fails to load.
Also is this not the appropriate code for SO? This is my first question...
EDIT** The aditional '/'s haven't fixed the issue.
EDIT*** Using GetLastError, I found the error code to be 2, ERROR_FILE_NOT_FOUND
Your early versions of the question checked the return value of LoadImage but then did nothing more. The documentation says this:
If the function fails, the return value is NULL. To get extended error information, call GetLastError.
So, if the function fails, call GetLastError to find out why. When you do so, you obtain the error code ERROR_FILE_NOT_FOUND. Which is pretty conclusive. The filename that you specified does not exist.
Do note that the code in the latest update to the question calls GetLastError unconditionally. That is a mistake, one that is seen all too frequently here on Stack Overflow. The documentation only tells you to call GetLastError when the call to LoadImage fails. If the call to LoadImage succeeds, then the value return by GetLastError is meaningless. Error handling in Win32 is largely handled in the same way as LoadImage does it, but not always. So you have to read the documentation very carefully.
Perhaps instead of
C:\\Users\\Tim\documents\\...
you meant
C:\\Users\\Tim\\documents\\...
OK, now you've got the path right. The call to LoadImage returns NULL, but GetLastError is no longer helpful and returns ERROR_SUCCESS. Which is weird in itself.
I believe that the problem is that your image uses a format that LoadImage does not understand. I took your .bmp file, loaded it in Paint.net and then re-saved it. Once I did that, the re-saved image was loaded successfully.
Rather than trying to load this from a file it would make far more sense to link the image as a resource and load it that way.
I found through experimenting with Gimp, that your .bmp file must have the following attributes in order for it to work:
depth:24bit
"Do not show write color space information" box checked: Gimp export options
If that still doesn't work then try to "unblock" it: File options.

Copy part of one bitmap into an ImageList

Quick background: I have a TreeView which I have created using Windows API calls in C++ (Visual Studio 2008, though that shouldn't make a difference):
hTreeview = CreateWindowEx(0, WC_TREEVIEW, L"My Treeview", WS_CHILD | WS_VISIBLE | TVS_INFOTIP | TVS_NOHSCROLL | TVS_SHOWSELALWAYS | TVS_EDITLABELS | TVS_SINGLEEXPAND, m_tx, m_ty, m_tw, m_th, hWindow, (HMENU)2, hInstance, NULL);
I have successfully assigned an ImageList to it using:
m_hImageList = ImageList_Create(cx, cy, ILC_COLOR24, n, n);
TreeView_SetImageList(hTreeview, m_hImageList, TVSIL_NORMAL);
where cx, cy, and n are all specified (in this case, 18, 18, and 5, respectively). This all works fine, as I can see because now there is space set aside next to my items for the image.
What I am trying to accomplish is to then copy a subsection of another bitmap (from a file). The code that I have tried (but does not work) is this:
HBITMAP hSkin = (HBITMAP)LoadImage(NULL, szPathBmp, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE | LR_CREATEDIBSECTION);
for (long i = 0; i < n; i++)
{
HDC dcDest = CreateCompatibleDC(NULL);
HBITMAP hIcon = CreateCompatibleBitmap(dcDest, cx, cy);
HDC dcSrc = CreateCompatibleDC(NULL);
SelectObject(dcSrc, hSkin);
BitBlt(dcDest, 0, 0, cx, cy, dcSrc, x, y, SRCCOPY);
*pIcon = ImageList_Add(m_hImageList, hIcon, NULL);
DeleteObject(hIcon);
DeleteDC(dcSrc);
DeleteDC(dcDest);
}
DeleteObject(hSkin);
I have left out the error checking code for brevity, and it can be assumed that all of the listed variables have been set somewhere else in the program (forgive me for not providing a working source file, but this is a very large project and I have tried to include only the parts that are relevant).
All I get in the Treeview is black squares (which happens to be the background color of the Treeview), so I am assuming that something is going wrong with the last block of code--the one that loads the skin and tries to BitBlt a portion of it into a new bitmap to save to the ImageList. Can anyone either tell me what I'm doing wrong, or tell me a better way of accomplishing what I'm trying to do?
I am using C++ and the Windows API exclusively, no .NET, MFC, or Windows Form Designer.
Thanks in advance for your help, and if I've left anything out, I apologize; this is one of my first posts.
There are a few problems with your code.
Firstly, when you create a new DC it starts out with a monochrome bitmap in it, so your CreateCompatibleBitmap call will also produce a monochrome one. Instead, you probably want to create the bitmap based on the window or screen DC.
Secondly, you never actually select the bitmap into dcDest, so nothing will be drawn into hIcon anyway.
Thirdly, ImageList_Add will fail if the bitmap is currently selected into a device context, so you have to deselect hIcon from dcDest before you add the icon to the image list.
Lastly, you are also neglecting to save the original bitmaps and restore them, so this will also cause a GDI leak.
Try something like this:
HDC hdcWindow = GetDC(hWnd);
HDC dcDest = CreateCompatibleDC(hDCWindow);
HBITMAP hIcon = CreateCompatibleBitmap(hDCWindow, cx, cy);
HDC dcSrc = CreateCompatibleDC(NULL);
HGDIOBJ hOldSourceBmp = SelectObject(dcSrc, hSkin);
HGDIOBJ hOldDestBmp = SelectObject(dcDest, hIcon);
BitBlt(dcDest, 0, 0, cx, cy, dcSrc, x, y, SRCCOPY);
SelectObject(dcDest, hOldDestBmp);
SelectObject(dcSrc, hOldSourceBmp);
*pIcon = ImageList_Add(m_hImageList, hIcon, NULL);
DeleteObject(hIcon);
DeleteDC(dcSrc);
DeleteDC(dcDest);
ReleaseDC(hWnd, hDCWindow);