问题
I am developing an application in C# and want to know how to get the screen shot image of any window when we only know its window handle. The functionality I am trying to achieve is to be able to just copy the screen shot of any window (it can be a whole application or a control inside an application) when I only know its window handle.
Example: I am running a calculator application, and Spy++. When I use find window functionality of the Spy++ and hover my mouse over different buttons of the calculator, it show different window handles. Lets say I hover mouse over "9" button in the calculator. Now as I know its window handle, I want to get image of the "9" button only. If I have window handle of the complete application then I want to be able to get the screen shot of that application only. Similar to Alt+PrintScr.
回答1:
You'll need to use GetWindowRect.
Just like shown in this example.
Then you can use Graphics.CopyFromScreen as shown in this SO post c# Take a screenshot of specific area.
Putting it all together, you call GetWindowRect, which accepts an HWnd to get the rect of the window you want. Then you use those rect values with CopyFromScreen to capture your image.
来源:https://stackoverflow.com/questions/22540601/how-to-get-the-image-of-a-window-when-we-only-know-its-window-handle