作为我开源的DeskPins 克隆(两个不同的链接,一个是原始链接,一个是GitHub上的克隆链接)的一部分,我需要允许用户与桌面进行交互。我想最简单的方法是打开一个新窗体并在其上绘制桌面内容。然后,我应该能够轻松设置鼠标指针,并向用户提供有关当前焦点窗口的视觉提示。
更复杂的替代方案是使用p/invoke来SetSystemCursor并注入自定义代码到其他窗口的WM_PAINT事件队列中(如果我的程序异常终止,则可能需要进行其他WinApi相关工作,例如光标清除将成为一个问题)。我宁愿不这样做。
我下面的代码是有效的,唯一的问题是屏幕闪烁。在我设置
有什么我可以做的,使它变得“平稳”,就像没有打开新窗口一样? 可以使用“冻结”效果 = 任何动画都会暂停。
更复杂的替代方案是使用p/invoke来SetSystemCursor并注入自定义代码到其他窗口的WM_PAINT事件队列中(如果我的程序异常终止,则可能需要进行其他WinApi相关工作,例如光标清除将成为一个问题)。我宁愿不这样做。
我下面的代码是有效的,唯一的问题是屏幕闪烁。在我设置
DoubleBuffered = true
之后(而不是屏幕闪烁,它变成了父窗体闪烁),但仍然很明显。所以现在每次打开叠加窗体时,我的窗体都会闪烁。有什么我可以做的,使它变得“平稳”,就像没有打开新窗口一样? 可以使用“冻结”效果 = 任何动画都会暂停。
public sealed partial class DesktopOverlayForm : Form
{
public DesktopOverlayForm()
{
InitializeComponent();
//make full screen
//https://dev59.com/eEvSa4cB1Zd3GeqPgbBl#2176683
Rectangle bounds = Screen.AllScreens
.Select(x => x.Bounds)
.Aggregate(Rectangle.Union);
this.Bounds = bounds;
//set desktop overlay image
this.BackgroundImage = MakeScreenshot();
}
/// <remarks>
/// Based on this answer on StackOverflow:
/// https://dev59.com/j3M_5IYBdhLWcg3w8H82
/// </remarks>
private static Bitmap MakeScreenshot()
{
Rectangle bounds = Screen.GetBounds(Point.Empty);
Bitmap image = new Bitmap(bounds.Width, bounds.Height);
using (Graphics g = Graphics.FromImage(image))
{
g.CopyFromScreen(Point.Empty, Point.Empty, bounds.Size);
}
return image;
}
private void DesktopOverlayForm_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Escape)
{
this.Close();
}
}
}