日期:2014-05-18  浏览次数:20767 次

在设置Form的MinimumSize属性时,如果使Form不被激活?
用Refactor看了Form的源码,在设置MinimumSize属性时,最终会调用系统的SetWindowPos方法,并且nFlags中没有加SWP_NOACTIVATE,应该是这个方法导致Form被激活的?请问有什么办法在设置MinimumSize属性时不激活窗口?我试过了捕获WM_ACTIVATE消息,却不管用。请高手帮忙!

------解决方案--------------------
最小化时Form的Enabled=false可以吗?
------解决方案--------------------
不知道是否可行,不过你可以考虑保存前次激活窗口,最小化后再次激活前次窗口。
------解决方案--------------------
捕获WM_GETMINMAXINFO消息自己来处理。
------解决方案--------------------
是想在设置MinimumSize时禁止执行Activated里的代码?
最简单的办法,变通一下好了,加个委托,以及焦点重新指定一下
C# code

Form1:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication66
{
    public partial class Form1 : Form
    {
        Form2 F = null;
        bool Cancel = false;

        public Form1()
        {
            InitializeComponent();

            (F = new Form2(new EventHandler(ActivatedControl))).Show();
        }

        void ActivatedControl(object sender, EventArgs e)
        {
            if (Cancel)
                throw new Exception("CancelContinue");
        }

        void button1_Click(object sender, EventArgs e)
        {
            try
            {
                Cancel = true;
                F.MinimumSize = new Size(100, 100);
                this.Focus();
            }
            finally
            {
                this.Focus();
            }
        }
    }
}

Form2:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace WindowsApplication66
{
    public partial class Form2 : Form
    {
        int Count = 0;
        EventHandler FActivatedControl = null;

        public Form2(EventHandler EH)
        {
            InitializeComponent();

            FActivatedControl = EH;
        }

        void Form2_Activated(object sender, EventArgs e)
        {
            try
            {
                if (FActivatedControl != null)
                    FActivatedControl(null, null);
                this.Text = "第" + (++Count).ToString() + "激活";
            }
            catch (Exception ex)
            {
                if (ex.Message == "CancelContinue")
                    return;
            }
        }

        void Form2_Deactivate(object sender, EventArgs e)
        {
            try
            {
                if (FActivatedControl != null)
                    FActivatedControl(null, null);

                // 如果失去焦点事件里也有代码要控制
            }
            catch (Exception ex)
            {
                if (ex.Message == "CancelContinue")
                    return;
            }
        }

        void Form2_Shown(object sender, EventArgs e)
        {
            try
            {
                if (FActivatedControl != null)
                    FActivatedControl(null, null);

                // 如果显示窗口事件里也有代码要控制
            }
            catch (Exception ex)
            {
                if (ex.Message == "CancelContinue")
                    return;
            }
        }
    }
}

------解决方案--------------------
学习!