[Lazarus] How to hide pop-up menu before taking a screen snapshot?
dezlov at gmail.com
Sun Feb 7 15:01:46 CET 2021
I had a similar experience with taking a screenshot programmatically.
Simply waiting or processing application messages after hiding the form
often doesn't help, and results in a form being a part of the screenshot
I managed to work around it by forcing Windows to repaint everything on
the desktop by broadcasting WM_PAINT message, like so:
SendMessageTimeout(HWND_BROADCAST, WM_PAINT, 0, 0, SMTO_ABORTIFHUNG,
You might also need to disable transition effects (fade-in and fade-out)
by setting the DWMWA_TRANSITIONS_FORCEDISABLED flag on the form before
hiding it, and then unsetting it when showing the form again.
On 07/02/2021 09:37, Bo Berglund via lazarus wrote:
> I have worked on a tool to copy a region of the Windows desktop for use in
> illustrations. The tool consists of a transparent form which is supposed to be
> located on top of the area to copy.
> This form has a pop-up menu where there are 3 items to select from:
> - Copy image
> - Copy position
> - Close
> When I use the "Copy image" function the resulting image contains the selected
> menu item caption on top of the copied desktop bitmap...
> I have tried various ways to get rid of this but everything I tried has failed.
> So how can I hide the menu item caption in code before I make the image
> I even added a timer set to 50 ms interval and put the image copy in its ontimer
> event hoping that the menu click should have exited and the text disappeared
> before the image copy happened, but no go...
> Here is the way the copy is commanded via the popup menu:
> And here is a captured image where the popup caption is visible:
> As you can see the caption of the clicked menu item is still present in the
> screenshot image...
> Here is parts of the code:
> procedure TfrmMain.miCopyImageClick(Sender : TObject);
> //CopyScreenRect; //Moved to timer
> //miCopyImage.Visible := false; //Does not work
> //self.Refresh; //Does not work
> timCopyImg.Enabled := true; //make copy inside timer event?
> procedure TfrmMain.miCopyPosClick(Sender : TObject);
> SaveRectCoords; //Saving rect coordinates to clipboard (OK)
> procedure TfrmMain.timCopyImgTimer(Sender : TObject);
> timCopyImg.Enabled := false;
> miCopyImage.Visible := false; //Does not work, caption still present
> self.Refresh; //Does also not work to remove caption
> miCopyImage.Visible := true; //Put it back to enable next capture
> procedure TfrmMain.CopyScreenRect;
> MyCapture : TBgraBitmap;
> Clip: TRect;
> Clip := Bounds(Self.Left, Self.Top, Self.Width, Self.Height);
> Self.Visible := false; //Hide selection form before copy
> //Self.Hide; //Not working for popup menu item...
> MyCapture := TBgraBitmap.Create();
> Self.Visible := true; //Show selection form after copy is done
> //Self.Show; //Not working for popup menu item...
> on E: exception do
> Clipboard.AsText := E.Message;
> Even Self.Visible or Self.Hide do not remove the menuitem caption....
> What can I do?
> Lazarus 2.0.10 / fpc 3.2.0 on Windows 10
More information about the lazarus