Re: Clipboard problem

Tech-Archive recommends: Fix windows errors by optimizing your registry



"David Lowndes" <davidl@xxxxxxxxxxxxxxx> wrote in message
news:3aelh1d4ok0qtcpgl7c320oat7efm2ub6d@xxxxxxxxxx
> >I'm trying to use:
>>
>>keybd_event(VK_SNAPSHOT, 0, 0, 0);
>>keybd_event(VK_SNAPSHOT, 0, KEYEVENTF_KEYUP, 0);
>>
>>to take a screenshot, and use:
>>
>>Clipboard::GetImage();
>>
>>to retrieve it, but it returns a nullptr. It's strange though because I
>>know
>>the screenshot is taken since I can paste just fine in mspaint. If I press
>>print screen manually, the GetImage() method works as it should.
>
> James,
>
> I'm wondering if there's some timing issue coming into effect.
>
> Have you tried splitting up the keyboard event creation of the screen
> shot and the call to GetImage so they're 2 manually instigated events
> (say 2 separate push buttons on a form), and see if that makes any
> difference.

Well, one of the problems was that I didn't leave enough time between the
keystroke and the clipboard query, but there were other issues as well. For
some reason, using the code during a BackgroundWorker::DoWork failed, but
using it during a Form::Load worked. Another problem was that as I ran the
code in a loop a nullptr would come up if I switched the active window as it
was taking the shots. So the code was working like I wanted, but when I
alt-tabbed from an app window to VS, it'd give me a NullReferenceException,
and I'd grumble thinking it happened on the first pass. The problem worked
the other way too (i.e. from VS/system tray looping ss'ing app to
full-screen game), and the exception actually would occur on the first pass
and confuse me even more. It took me a long time to get my program working
though everything feels rather fragile, and I still don't get why things are
the way they are.


.


Quantcast