CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 7 of 9 FirstFirst ... 456789 LastLast
Results 91 to 105 of 132
  1. #91
    Join Date
    May 2014
    Posts
    205

    Re: Capture of Window

    Another error:
    Code:
    void HandlePaint(HWND window, HDC hDC2)
    {
    	HDC dc;
    	PAINTSTRUCT ps;
    	RECT *rect;
    
    	if (GetUpdateRect(window, rect, FALSE))
    	{
    		dc = BeginPaint(window, &ps);
    		BitBlt(hDC2, 0, 0, 255, 255,
    			hDC, 0, 0, SRCCOPY);
    		EndPaint(window, &ps);
    	}
    }
    Produces:
    First-chance exception at 0x7e37a8f6 in WM_1_wndproc.exe: 0xC0000005: Access violation writing location 0x00000002.
    Unhandled exception at 0x7e37a8f6 in WM_1_wndproc.exe: 0xC0000005: Access violation writing location 0x00000002.

  2. #92
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: Capture of Window

    From MSDN
    Code:
    BOOL GetUpdateRect(
      _In_   HWND hWnd,
      _Out_  LPRECT lpRect,
      _In_   BOOL bErase
    );
    so you need
    Code:
    {
    	HDC dc;
    	PAINTSTRUCT ps;
    	RECT rect;
    
    	if (GetUpdateRect(window, &rect, FALSE))
    	{
    		dc = BeginPaint(window, &ps);
    		BitBlt(hDC2, 0, 0, 255, 255,
    			hDC, 0, 0, SRCCOPY);
    		EndPaint(window, &ps);
    	}
    }
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #93
    Join Date
    May 2014
    Posts
    205

    Re: Capture of Window

    No light on the end of tunnel.
    I found out, that I overlooked your code you gave me to test capturing was two parts. However when I run it now again with the second part implemented in "Learning WinAPI" still not correct size. I would appreciate some correction to the code
    Code:
    HWND HCapture = FindWindow(NULL, _T("Learning WInAPI"));
    Since I am never sure about the title. I still change between programs and doing mistakes in case while retyping. Is it possible to find window case insensitive and beginning just "learn"? But I wonder that the last bitmap was made when I have incorrect case. Should be "Learning WinAPI".

  4. #94
    Join Date
    May 2014
    Posts
    205

    Re: Capture of Window

    When I check your yesterday code of "Learning WinAPI" - why did you not made the drawing under WM_PRINTCLIENT? Should be more simple...

    Is it possible to realize my idea, to send the drawing to the device context of capturing program? My skills are too poor.

  5. #95
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: Capture of Window

    Since I am never sure about the title. I still change between programs and doing mistakes in case while retyping. Is it possible to find window case insensitive and beginning just "learn"?
    Yes, but it's not as simple as just using FindWindow(). You need to enumerate the top level windows which will give a handle to the next desktop window and then obtain the window title and then see if the title matches using the required criteria. It uses a callback function that is called for each top level window.

    I'll knock something up along these lines and post.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  6. #96
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: Capture of Window

    This will use the first window found that contains the text set for wndtitle irrespective of case. If more than one window would match wndtitle, the first one found is used.

    Code:
    #include <windows.h>
    #include <cstdio>
    #include <cstring>
    #include <fstream>
    #include <iostream>
    
    using namespace std;
    
    const int maxstr = 200;
    
    const char wndtitle[] = "wint";
    
    BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lparam)
    {
    char title[maxstr + 1];
    
    char *ch = title;
    
    	if (GetWindowText(hwnd, title, maxstr)) {
    		while (*ch = (char)tolower(*(ch++)));
    
    		if (strstr(title, wndtitle)) {
    			*(HWND*)lparam = hwnd;
    			SetLastError(ERROR_NO_MATCH);
    			return FALSE;
    		}
    	}
    
    	return TRUE;
    }
    
    int main()
    {
    HWND HCapture;
    
    	while (EnumWindows(EnumWindowsProc, (LPARAM)&HCapture));
    
    	if (GetLastError() != ERROR_NO_MATCH) {
    		cout << "Cannot find window\n";
    		return 1;
    	}
    
    //HWND HCapture = FindWindow(NULL, "wintest"); // get window handle
    //HWND HCapture = GetForegroundWindow();   //captures cmd window
    
    	if (!IsWindow(HCapture)) {
    		cout << "Bad find\n";
    		return 2;
    	}
    
    RECT rect, rectw; 
    
    	GetClientRect(HCapture, &rect);
    	GetWindowRect(HCapture, &rectw);
    
    LONG dx = rect.right - rect.left;
    LONG dy = rect.bottom - rect.top;
    
    const int fudgey = (rectw.bottom - rectw.top) - dy - 2 * GetSystemMetrics(SM_CYSIZEFRAME);
    const int fudgex = 0;
    
    // create BITMAPINFO structure
    // used by CreateDIBSection
    BITMAPINFO info = {0};
    
    	info.bmiHeader.biSize          = sizeof(BITMAPINFOHEADER);
    	info.bmiHeader.biWidth         = dx + fudgex;
    	info.bmiHeader.biHeight        = dy + fudgey;
    	info.bmiHeader.biPlanes        = 1;
    	info.bmiHeader.biBitCount      = 24;
    	info.bmiHeader.biCompression   = BI_RGB;
    
    HDC	HDevice = CreateCompatibleDC(NULL);
     
    BYTE*	memory = 0;
     
    HBITMAP	HBitmap = CreateDIBSection(HDevice, &info, DIB_RGB_COLORS, (void**)&memory, NULL, NULL);
    
    	SelectObject(HDevice, HBitmap);
     
    	if (!PrintWindow(HCapture, HDevice, /*PW_CLIENTONLY*/0))
    		return 2;
    
    	ReleaseDC(HCapture, HDevice);
    
    char *buffer = new char[50];
    
    	sprintf(buffer, "capture%d%d.bmp", dx, dy);
    
    ofstream file(buffer, ios::binary);
    
    	if (!file) {
    		DeleteObject(HBitmap);
    		return 1;
    	}
    
    // initialize bitmap file headers
    BITMAPFILEHEADER fileHeader = {0};
    BITMAPINFOHEADER infoHeader = {0};
    
    	fileHeader.bfType      = 0x4d42;
    	fileHeader.bfSize      = 0; //????
    	fileHeader.bfOffBits   = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
    
    	infoHeader.biSize          = sizeof(infoHeader);
    	infoHeader.biWidth         = dx + fudgex;
    	infoHeader.biHeight        = dy;
    	infoHeader.biPlanes        = 1;
    	infoHeader.biBitCount      = 24;
    	infoHeader.biCompression   = BI_RGB;
    
    	// save file headers
    	file.write((char*)&fileHeader, sizeof(fileHeader));
    	file.write((char*)&infoHeader, sizeof(infoHeader));
    
    // save 24-bit bitmap data
    int wbytes = (((24 * (dx) + 31) & (~31)) / 8);
    int tbytes = wbytes * dy;
    
    	file.write((char*)(memory), tbytes);
    	file.close();
    
    	// delete bitmap
    	DeleteObject(HBitmap);
    	return 0;
    }
    Last edited by 2kaud; May 12th, 2014 at 03:03 PM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  7. #97
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: Capture of Window

    you gave me to test capturing was two parts.
    Yes it was originally, but see my post #82 and #85. I moved away from any changes to the program producing the window to be captured. The code in #96 doesn't require any changes to the window producing program. The other program was simply a test program to produce a window containg an eclipse that could be captured This had a title of wintest - hence the use of wintest in the FindWindow().
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  8. #98
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Capture of Window

    Quote Originally Posted by crazy boy;2154787 I would appreciate some correction to the code
    [code
    HWND HCapture = FindWindow(NULL, _T("Learning WInAPI"));[/code]
    ... Is it possible to find window case insensitive and beginning just "learn"? But I wonder that the last bitmap was made when I have incorrect case. Should be "Learning WinAPI".
    You should never trust the FindWindow in your attempts to find out the correct window handle.
    There may be a lot of the windows with the same title. Note that the handle of the first found window is returned!
    There may be a lot of the windows with the same title and the same class name. Note that the handle of the first found window is returned!
    There may be nothing found in some localized Windows OS if one looks for a some predefined English string

    More info you will find here in the section Why FindWindow doesn't work.
    Victor Nijegorodov

  9. #99
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Capture of Window

    If you are launching the process that contains the target window, look into filtering by pid (process id) when using EnumWindows.

    That way, you are sure to find the window in the process you care about.

    I have a class with a FindWindowTimeout method that I wrote about 12 years ago. It allows you to do a partial title and partial class search (with or without a pid) to find a window.

    Let me know if you would like me to take the time to prep it to post.

  10. #100
    Join Date
    May 2014
    Posts
    205

    Re: Capture of Window

    I'll have to recheck tomorrow. But now I cannot make working your code:
    error C2664: 'GetWindowTextW' : cannot convert parameter 2 from 'char [201]' to 'LPWSTR'
    Why I got this message? I use Unicode, not ANSI.


    Quote Originally Posted by Arjay View Post
    Let me know if you would like me to take the time to prep it to post.
    Of sure you're welcome if you wanna help.

    PS: Does this forum support sending notifications of new post in thread on email? Asking because I am used to edit my post often while they are fresh.
    Last edited by crazy boy; May 12th, 2014 at 03:57 PM.

  11. #101
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Capture of Window

    Quote Originally Posted by crazy boy View Post
    I'll have to recheck tomorrow. But now I cannot make working your code:
    error C2664: 'GetWindowTextW' : cannot convert parameter 2 from 'char [201]' to 'LPWSTR'
    Why I got this message? I use Unicode, not ANSI.
    Is 'char [201]' UNICODE?

  12. #102
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: Capture of Window

    I use Unicode, not ANSI.
    I use ANSI. Change GetWindowText( to GetWindowTextA(
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  13. #103
    Join Date
    May 2014
    Posts
    205

    Re: Capture of Window

    OK I made it compiled. Thanks. I will try to continue tomorrow.

    #81 Separated codes; Target window responsive on WM_PRINTCLIENT. http://forums.codeguru.com/showthrea...05#post2154705

    Quote Originally Posted by 2kaud View Post
    Yes it was originally, but see my post #82 and #85. I moved away from any changes to the program producing the window to be captured. The code in #96 doesn't require any changes to the window producing program. The other program was simply a test program to produce a window containg an eclipse that could be captured This had a title of wintest - hence the use of wintest in the FindWindow().
    #82 http://forums.codeguru.com/showthrea...09#post2154709
    #85 http://forums.codeguru.com/showthrea...43#post2154743
    #96 http://forums.codeguru.com/showthrea...97#post2154797

    I need some time to check codes. Lots of work to do.
    Last edited by crazy boy; May 13th, 2014 at 04:56 AM.

  14. #104
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Capture of Window

    See the attached 2013 VC++ project. I wrote the original code about 12 years ago. This was part of a COM test automation engine which used Active Accessibility to automate the testing of applications.

    It has a funky mix of ComBSTR and Tstring (which is a typedef of std::string or std::wstring). Back then CString wasn't available outside of MFC (which it is now). ComBSTR was used for the COM interface level (and Tstring was used internally).

    If you actually use this code, clean it up by stripping out the ComBSTR, OLE_HANDLE (just use HWND), and Tstring (just use CString).

    Code:
    int _tmain(int argc, _TCHAR* argv[])
    {
    	HRESULT hr = S_OK;
    	OLE_HANDLE hWnd = NULL;
    	LONG lPid = 13296;  // Open up a few instances of Notepad and put one of the pids here
    
    	CWindow window;
    	if(SUCCEEDED(hr = window.FindWindowTimeout(
    		lPid, // Restrict by Process id
    		NULL, // Parent hwnd
    		CComBSTR("Notepad"), // Window class
    		CComBSTR("Untitled"), // Window title (partial title - full title is 'Untitled - Notepad')
    		30, // Find for 30 seconds
                    // Flags - look for visible window that exists with a partial title match and restrict by process id
    		FW_EXISTS | FW_PARTIAL_TITLE_MATCH | FW_USEPID | FW_VISIBLE,  
    		&hWnd)))
    	{
    		// Valid hWnd
    
    	}
    
    
    	return 0;
    }
    Attached Files Attached Files

  15. #105
    Join Date
    May 2014
    Posts
    205

    Re: Capture of Window

    Which function to use to detect if menu is present in the app? It's nonsense to recompile every time I change app. You also could help me if you would approve the code to support more titles. I am debugging 4 apps as target window: Learning WinAPI , Dialog App, AOK Trigger Studio and Map Viewer.

    To prevent using more files derived from capture3.cpp I want to change system of my tests. So I define
    Code:
    #define	CODE		81
    where number is number of your post. So now I need directive which will be used to determine if the
    CODE == 81 or if CODE >= 81 ...

    OK, so I have this code now, but when I debug it and no target window prepared it looks like it freeze. I looks that the while loop is infinite, which is not ideal. I'd suggest to test it just for second. So now I am at code 81 and I will do my way through all your codes.
    http://paste.ofcode.org/HpxJ3cbzkNUQL7zmqwUNrN
    Last edited by crazy boy; May 13th, 2014 at 05:37 AM.

Page 7 of 9 FirstFirst ... 456789 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured