简体   繁体   English

如何在MDI表单C#Win Apps中将SDI Windows图像最小化时捕获SDI Windows图像

[英]How to capture SDI windows image when it is minimize in MDI form c# win apps

in windows 7 one feature is very good that when any apps is minimize and when user point mouse cursor on that minimize for then the image of that form is show in hover popup. 在Windows 7中,一个功能非常好,当最小化任何应用程序时,当用户将鼠标指针指向最小化时,该窗体的图像就会在悬停弹出窗口中显示。 so if i want to do it in my MDI form that when any SDI is minimize in MDI form and when user point mouse on that minimize form then the form image will show in hover popup. 因此,如果我想以我的MDI形式进行操作,则当任何SDI以MDI形式最小化时,并且当用户将鼠标指向该最小化形式时,该表单图像将在悬停弹出窗口中显示。 how to accomplish it in windows application through c# 如何通过C#在Windows应用程序中完成它

I have not tried this my self, but if I would think that the most likely solution for an MDI application would be to create the bitmap image of the child window prior to it being minimized and then use that image to show the last state of the window. 我还没有尝试过,但是如果我认为MDI应用程序最可能的解决方案是在最小化子窗口之前创建子窗口的位图图像,然后使用该图像显示子窗口的最后状态。窗口。

You will also need to handle a few of the ' WM_NC* ' messages to detect that the mouse is hovering over the minimized window and then render your cached image in a popup. 您还需要处理一些“ WM_NC * ”消息,以检测鼠标悬停在最小化的窗口上方,然后在弹出窗口中呈现缓存的图像。

UPDATE: 更新:

Here is a quick and dirty proof of concept, it is not perfect the tooltip does not follow all the standard tooltip functionality etc. but it should be enough to get you started. 这是一个快速而肮脏的概念证明,它并不完美,工具提示没有遵循所有标准的工具提示功能等,但是它足以使您入门。 The code could also be refactored to be more reusable, but at this stage you could derive your MDI child windows from this MDIChild and you would have this basic tooltip functionality. 还可以将代码重构为更可重用,但是在此阶段,您可以从此MDIChild派生MDI子窗口,并且您将拥有此基本工具提示功能。 Of course there will be issues if windows are overlapping etc. 当然,如果窗口重叠等会出现问题。

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;
  }
}

Here is the TooltipForm.designer.cs 这是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;
  }
}

And the TooltipForm.cs 还有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;
      }
    }
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM