如何在MDI格式c#win应用程序中最小化时捕获SDI窗口图像

时间:2011-03-29 08:01:29

标签: c# winforms

在Windows 7中,一个功能非常好,当任何应用程序最小化时,当用户指向鼠标光标时,最小化,然后在悬停弹出窗口中显示该表单的图像。因此,如果我想在我的MDI表单中执行此操作,当任何SDI在MDI格式中最小化并且当用户将鼠标指向最小化表单时,则表单图像将在悬停弹出窗口中显示。如何通过c#

在Windows应用程序中完成它

1 个答案:

答案 0 :(得分:1)

我没有试过这个我自己,但如果我认为MDI应用程序最可能的解决方案是在最小化之前创建子窗口的位图图像,然后使用该图像显示最后一个窗口的状态。

您还需要处理一些“WM_NC*”消息,以检测鼠标是否悬停在最小化窗口上,然后在弹出窗口中呈现缓存的图像。

<强>更新

这是一个快速而肮脏的概念证明,它不是完美的工具提示不遵循所有标准的工具提示功能等但它应该足以让你开始。代码也可以重构为更可重用,但在此阶段,您可以从此MDIChild派生MDI子窗口,您将拥有此基本工具提示功能。如果窗口重叠等,当然会出现问题。

using System;
using System.Drawing;
using System.Windows.Forms;

namespace MDITest
{
  public partial class MDIChild : Form
  {
    private static TooltipForm _tooltip = new TooltipForm();
    private static Form _lastForm;

    private Image _lastSnapshot;

    public MDIChild()
    {
      InitializeComponent();
    }

    protected override void WndProc(ref Message m)
    {
      if (m.Msg == WM_COMMAND && m.WParam.ToInt32() == SC_MINIMIZE)
      {
        OnMinimize(EventArgs.Empty);
      }
      else if (m.Msg == WM_NCMOUSEMOVE)
      {
        int x = m.LParam.ToInt32() & 0x0000ffff;
        int y = m.LParam.ToInt32() >> 16;
        OnNcMouseMove(new MouseEventArgs(MouseButtons.None, 0, x, y, 0));
      }
      base.WndProc(ref m);
    }

    protected virtual void OnNcMouseMove(MouseEventArgs e)
    {
      if (this.WindowState == FormWindowState.Minimized && _lastForm != this)
      {
        _lastForm = this;
        Point pt = MdiParent.PointToScreen(this.Location);
        ShowWindowTip(pt, _lastSnapshot);
      }
    }

    protected virtual void OnMinimize(EventArgs e)
    {
      _tooltip.Visible = false;
      if (_lastSnapshot == null)
      {
        _lastSnapshot = new Bitmap(100, 100);
      }

      using (Image windowImage = new Bitmap(ClientRectangle.Width, ClientRectangle.Height))
      using (Graphics windowGraphics = Graphics.FromImage(windowImage))
      using (Graphics tipGraphics = Graphics.FromImage(_lastSnapshot))
      {      
        Rectangle r = this.RectangleToScreen(ClientRectangle);
        windowGraphics.CopyFromScreen(new Point(r.Left, r.Top), Point.Empty, new Size(r.Width, r.Height));
        windowGraphics.Flush();

        float scaleX = 1;
        float scaleY = 1;
        if (ClientRectangle.Width > ClientRectangle.Height)
        {
          scaleY = (float)ClientRectangle.Height / ClientRectangle.Width;
        }
        else if (ClientRectangle.Height > ClientRectangle.Width)
        {
          scaleX = (float)ClientRectangle.Width / ClientRectangle.Height;
        }
        tipGraphics.DrawImage(windowImage, 0, 0, 100 * scaleX, 100 * scaleY);
      }
    }

    private static void ShowWindowTip(Point pt, Image image)
    {
      if (_tooltip.Visible)
      {
        _tooltip.Visible = false;
      }

      _tooltip.Image = image;
      _tooltip.Show();
      _tooltip.Location = new Point(pt.X, pt.Y - _tooltip.Height - 10);
    }

    private static int WM_NCMOUSEMOVE = 0x00A0;
    private static int WM_COMMAND = 0x0112;
    private static int SC_MINIMIZE = 0xf020;
  }
}

这是TooltipForm.designer.cs

namespace MDITest
{
  partial class TooltipForm
  {
    /// <summary>
    /// Required designer variable.
    /// </summary>
    private System.ComponentModel.IContainer components = null;

    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
    protected override void Dispose(bool disposing)
    {
      if (disposing && (components != null))
      {
        components.Dispose();
      }
      base.Dispose(disposing);
    }

    #region Windows Form Designer generated code

    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
      this.pictureBox1 = new System.Windows.Forms.PictureBox();
      ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
      this.SuspendLayout();
      // 
      // pictureBox1
      // 
      this.pictureBox1.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Center;
      this.pictureBox1.Dock = System.Windows.Forms.DockStyle.Fill;
      this.pictureBox1.Location = new System.Drawing.Point(0, 0);
      this.pictureBox1.Name = "pictureBox1";
      this.pictureBox1.Size = new System.Drawing.Size(134, 112);
      this.pictureBox1.TabIndex = 0;
      this.pictureBox1.TabStop = false;
      // 
      // TooltipForm
      // 
      this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
      this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
      this.ClientSize = new System.Drawing.Size(134, 112);
      this.ControlBox = false;
      this.Controls.Add(this.pictureBox1);
      this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow;
      this.MaximizeBox = false;
      this.MinimizeBox = false;
      this.Name = "TooltipForm";
      this.ShowIcon = false;
      this.ShowInTaskbar = false;
      this.Text = "TooltipForm";
      this.TopMost = true;
      ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
      this.ResumeLayout(false);

    }

    #endregion

    private System.Windows.Forms.PictureBox pictureBox1;
  }
}

和TooltipForm.cs

using System;
using System.Drawing;
using System.Windows.Forms;

namespace MDITest
{
  public partial class TooltipForm : Form
  {
    Timer _timer = new Timer();

    public TooltipForm()
    {
      InitializeComponent();
      TopLevel = true;
      _timer.Enabled = false;
      _timer.Interval = 5000;
      _timer.Tick += new EventHandler(_timer_Tick);
    }

    void _timer_Tick(object sender, EventArgs e)
    {      
      Visible = false;
    }

    protected override void SetVisibleCore(bool value)
    {
      if (value == true)
      {
        _timer.Start();
      }
      else
      {
        _timer.Stop();
      }
      base.SetVisibleCore(value);
    }

    public Image Image
    {
      get
      {
        return pictureBox1.Image;
      }
      set
      {
        pictureBox1.Image = value;
      }
    }
  }
}