将WinForm背景设置为屏幕截图,窗口的一角出现黑框

时间:2017-11-15 14:05:31

标签: c# winforms screenshot

我有一个简单的winforms应用程序,当最大化时,会截取屏幕截图并设置表单的背景图像。如果我最小化形状然后最大化它我在顶角有一个黑盒子,我无法弄清楚它来自哪里。它不存在于表单的初始加载中。

这是从哪里来的?

这是带有VS 2017的.Net 4.7

enter image description here

private void Form1_Resize(object sender, EventArgs e)
{
    switch (WindowState)
    {
        case FormWindowState.Normal:
            break;
        case FormWindowState.Minimized:
            break;
        case FormWindowState.Maximized:
            Bitmap screenshot = new Bitmap(SystemInformation.VirtualScreen.Width, SystemInformation.VirtualScreen.Height);
            Size s = new Size(screenshot.Width, screenshot.Height);
            Graphics screenshotGraphics = Graphics.FromImage(screenshot);
            screenshotGraphics.CopyFromScreen(0, 0, 0, 0, s);
            this.BackgroundImage = screenshot;
            screenshotGraphics.Dispose();
            Invalidate();
            break;
        default:
            break;
    }
}

设计

private void InitializeComponent()
{
    this.SuspendLayout();
    this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
    this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
    this.ClientSize = new System.Drawing.Size(712, 594);
    this.ControlBox = false;
    this.DoubleBuffered = true;
    this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
    this.MaximizeBox = false;
    this.MinimizeBox = false;
    this.Name = "Form1";
    this.ShowIcon = false;
    this.SizeGripStyle = System.Windows.Forms.SizeGripStyle.Hide;
    this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
    this.Text = "Form1";
    this.TopMost = true;
    this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
    this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.Form1_KeyDown);
    this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseDown);
    this.MouseMove += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseMove);
    this.MouseUp += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseUp);
    this.Resize += new System.EventHandler(this.Form1_Resize);
    this.ResumeLayout(false);

}
  

更新

只有当我将FormBorderStyle设置为none时才会发生这种情况,但这就是我需要的方式。

0 个答案:

没有答案