0

Working on a C# program which will take a screenshot for the user when a button is clicked, the issue I have is, if the Start Menu is open, clicking the button on the WinForms app closes the Start Menu before taking the screenshot, the method for capturing the screen I am using:

Bitmap bmp = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
Graphics gr = Graphics.FromImage(bmp);
gr.CopyFromScreen(0, 0, 0, 0, bmp.Size);

This is bound to the button click:

onClick(object sender, EventArgs e) {

My thoughts are I would need to somehow make the button clickable without gaining focus?

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
  • 1
    If it can be done, it will probably be through WndProc (i.e. handling the click event at a lower level). Windows 10's version of Snipping Tool's solution to this is to allow a delay to be set so that it works in this order: 1) click the "New" button to trigger the screenshot 2) Snipping Tool waits X seconds and takes screenshot. In the time between you can open the menu you want to screenshot. – ProgrammingLlama Nov 05 '18 at 04:41
  • This could be a viable option, although not ideal. Just tried Snipping Tool and it doesn't allow taking a "Snip" inclusive of the Start Menu :( –  Nov 05 '18 at 04:49
  • You need to register a HotKey: [Capture a keyboard keypress in the background](https://stackoverflow.com/questions/15413172/capture-a-keyboard-keypress-in-the-background). Use a Function Key (F11-F12), it won't interfere with Start Menu. See the example, it's registering the F12 key. – Jimi Nov 05 '18 at 06:46
  • @Jimi Also a viable option, I had thought of this but would rather use a button if possible, thanks Jimi, this is last resort :) –  Nov 05 '18 at 07:00
  • Another thing. If you're targeting Windows 8+, take a look at the [Desktop Duplication Api](https://learn.microsoft.com/en-us/windows/desktop/direct3ddxgi/desktop-dup-api). Those use the DirectX Graphics Infrastructure (DXGI). Here's a nice [C# implementation on GitHub](https://github.com/jasonpang/desktop-duplication-net). See the description on [MSDN on DXGI](https://learn.microsoft.com/en-us/windows/desktop/direct3ddxgi/dx-graphics-dxgi) – Jimi Nov 05 '18 at 07:45
  • Or, if your C++ is good enough, on CodePrject: [Desktop Screen Capture on Windows via Windows Desktop Duplication API](https://www.codeproject.com/Tips/1116253/Desktop-Screen-Capture-on-Windows-via-Windows-Desk) – Jimi Nov 05 '18 at 07:51
  • @HansPassant I’m quite aware of “Print Screen”... I’m talking about a software button, I made this quite clear in my original question. –  Nov 05 '18 at 10:29
  • You liked Jimi's comment, seemed like a good idea to mention that such a hotkey already exists. Google "c# clipboard monitor" to find code that makes that key useful. – Hans Passant Nov 05 '18 at 10:41
  • @HansPassant I can see your point of view, as stated that would be last resort, I’m looking for a solution that would allow a button press (software) that doesn’t steal focus from the Start Menu, still looking into WndProc –  Nov 05 '18 at 10:45

0 Answers0