代码之家  ›  专栏  ›  技术社区  ›  JohnSaps

类型隐藏不阻止窗体包含在屏幕转储中

  •  1
  • JohnSaps  · 技术社区  · 6 年前

    问题所在

    我正在使用以下代码执行屏幕转储。即使我用 this.Hide 表单仍包含在屏幕转储中,我不希望这样。

    this.Hide(); //Hide to not include this form in the screen dump
    
    try
    {
        Rectangle bounds = Screen.GetBounds(Point.Empty);
    
        using (Bitmap bitmap = new Bitmap(bounds.Width, bounds.Height))
        {
            using (Graphics g = Graphics.FromImage(bitmap))
            {
                g.CopyFromScreen(Point.Empty, Point.Empty, bounds.Size);
            }
    
            bitmap.Save(fileName, ImageFormat.Png);
        }
    }
    catch (Exception exc)
    {
        MessageBox.Show(LanguageMessages.MsgTextErrorScreenDump + 
            Utilities.DoubleNewLine() + exc.ToString(), 
            LanguageMessages.MsgCaptionErrorScreenDump, 
            MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
    finally
    {
        this.Show(this.Owner);
    }
    

    我所尝试的: 隐藏表单后添加以下内容,但没有任何区别:

    • 睡眠(1000);
    • 这重新绘制();
    • 这无效();

    我的问题是: 为什么 这隐藏 不是真的隐藏表单,从而防止它与上述代码一起包含在屏幕转储中?

    1 回复  |  直到 6 年前
        1
  •  1
  •   LarsTech    6 年前

    您可以使用计时器进行作弊,这将给您足够的“时间”来隐藏表单:

    private System.Windows.Forms.Timer hideTimer = null;
    

    整整一秒钟似乎就足够了:

    this.Hide();
    hideTimer = new System.Windows.Forms.Timer() { Interval = 1000 };
    hideTimer.Tick += (ts, te) => {
      hideTimer.Stop();
      hideTimer.Dispose();
      Rectangle bounds = Screen.GetBounds(this);
      using (Bitmap bitmap = new Bitmap(bounds.Width, bounds.Height)) {
        using (Graphics g = Graphics.FromImage(bitmap)) {
          g.CopyFromScreen(bounds.Location, Point.Empty, bounds.Size);
        }
        bitmap.Save(fileName, ImageFormat.Png);
      }
      this.Show(this.Owner);
    };
    hideTimer.Start();