• 窗体切换特效


    C# 实现磁性窗口(附源码和程序)

    实现并封装了磁性窗口类MagneticMagnager,实现磁性窗口仅仅需要调用一行代码:
    MagneticMagnager test2 = new MagneticMagnager(this, fm2, MagneticPosition.Top);
     
    插图:
     
    
     
    具体操作:
     
    1.新建winform项目MagneticForm,并添加磁性窗口操作类MagneticMagnager:
    [c-sharp] view plaincopy
    1. using System;  
    2. using System.Drawing;  
    3.   
    4. namespace System.Windows.Forms  
    5. {  
    6.     public class MagneticMagnager  
    7.     {  
    8.         MagneticPosition Pos;//位置属性  
    9.         Form MainForm, ChildForm;  
    10.         bool IsFirstPos;//是否第一次定位ChildForm子窗体  
    11.         public int step;//磁性子窗体ChildForm移动步长  
    12.         public Point LocationPt;//定位点  
    13.         delegate void LocationDel();//移动子窗体的委托  
    14.         public MagneticMagnager(Form _MainForm, Form _ChildForm, MagneticPosition _pos)  
    15.         {  
    16.             IsFirstPos = false;  
    17.             step = 20;  
    18.             MainForm = _MainForm;  
    19.             ChildForm = _ChildForm;  
    20.             Pos = _pos;  
    21.             MainForm.LocationChanged += new EventHandler(MainForm_LocationChanged);  
    22.             ChildForm.LocationChanged += new EventHandler(ChildForm_LocationChanged);  
    23.             MainForm.SizeChanged += new EventHandler(MainForm_SizeChanged);  
    24.             ChildForm.SizeChanged += new EventHandler(ChildForm_SizeChanged);  
    25.             ChildForm.Load+=new EventHandler(ChildForm_Load);  
    26.             MainForm.Load+=new EventHandler(MainForm_Load);  
    27.         }  
    28.         void ChildForm_Load(object sender, EventArgs e)  
    29.         {  
    30.             GetLocation();  
    31.         }  
    32.         void MainForm_Load(object sender, EventArgs e)  
    33.         {  
    34.             GetLocation();  
    35.         }  
    36.         void MainForm_LocationChanged(object sender, EventArgs e)  
    37.         {  
    38.             GetLocation();  
    39.         }  
    40.         void MainForm_SizeChanged(object sender, EventArgs e)  
    41.         {  
    42.             GetLocation();  
    43.         }  
    44.         void ChildForm_SizeChanged(object sender, EventArgs e)  
    45.         {  
    46.             GetLocation();  
    47.         }  
    48.         void GetLocation()//定位子窗体  
    49.         {  
    50.             if (ChildForm == null)  
    51.                 return;  
    52.             if (Pos == MagneticPosition.Left)  
    53.                 LocationPt = new Point(MainForm.Left - ChildForm.Width, MainForm.Top);  
    54.             else if (Pos == MagneticPosition.Top)  
    55.                 LocationPt = new Point(MainForm.Left, MainForm.Top - ChildForm.Height);  
    56.             else if (Pos == MagneticPosition.Right)  
    57.                 LocationPt = new Point(MainForm.Right, MainForm.Top);  
    58.             else if (Pos == MagneticPosition.Bottom)  
    59.                 LocationPt = new Point(MainForm.Left, MainForm.Bottom);  
    60.             ChildForm.Location = LocationPt;  
    61.         }  
    62.         void ChildForm_LocationChanged(object sender, EventArgs e)//当窗体位置移动后  
    63.         {  
    64.             if (!IsFirstPos)  
    65.             {  
    66.                 IsFirstPos = true;  
    67.                 return;  
    68.             }  
    69.             LocationDel del = new LocationDel(OnMove);//委托  
    70.             MainForm.BeginInvoke(del);//调用  
    71.         }  
    72.   
    73.         void OnMove()//移动子窗体  
    74.         {  
    75.             if (ChildForm.Left > LocationPt.X)  
    76.                 if (ChildForm.Left - LocationPt.X > step)  
    77.                     ChildForm.Left -= step;  
    78.                 else  
    79.                     ChildForm.Left = LocationPt.X;  
    80.             else if (ChildForm.Left < LocationPt.X)  
    81.                 if (ChildForm.Left - LocationPt.X < -step)  
    82.                     ChildForm.Left += step;  
    83.                 else  
    84.                     ChildForm.Left = LocationPt.X;  
    85.             if (ChildForm.Top > LocationPt.Y)  
    86.                 if (ChildForm.Top - LocationPt.Y > step)  
    87.                     ChildForm.Top -= step;  
    88.                 else  
    89.                     ChildForm.Top = LocationPt.Y;  
    90.   
    91.             else if (ChildForm.Top < LocationPt.Y)  
    92.                 if (ChildForm.Top - LocationPt.Y < -step)  
    93.                     ChildForm.Top += step;  
    94.                 else  
    95.                     ChildForm.Top = LocationPt.Y;  
    96.         }  
    97.     }  
    98.     public enum MagneticPosition//磁性窗体的位置属性  
    99.     {  
    100.         Left = 0,  
    101.         Top = 1,  
    102.         Right = 2,  
    103.         Bottom = 3  
    104.     }  
    105. }  
     
    2.添加MainForm主窗体,打开Program.cs文件,修改为如下 :
     
    [c-sharp] view plaincopy
    1. static void Main()  
    2. {  
    3.     Application.EnableVisualStyles();  
    4.     Application.SetCompatibleTextRenderingDefault(false);  
    5.     Application.Run(new MainForm());  
    6. }  
     
    3.添加ChildForm子窗体,不用写任何代码,当然实际中根据你自己的设计决定
    4.在MainForm中添加四个button,分别设置text属性为:"左磁性" "上磁性" "右磁性" 下磁性" ,并分别添加按钮点击事件,具体代码如下:
    [c-sharp] view plaincopy
    1. using System;  
    2. using System.Collections.Generic;  
    3. using System.ComponentModel;  
    4. using System.Data;  
    5. using System.Drawing;  
    6. using System.Linq;  
    7. using System.Text;  
    8. using System.Windows.Forms;  
    9.   
    10. namespace WindowsFormsApplication1  
    11. {  
    12.     public partial class MainForm : Form  
    13.     {  
    14.         public MainForm()  
    15.         {  
    16.             InitializeComponent();  
    17.         }  
    18.         ChildForm fm1, fm2, fm3, fm4;  
    19.         private void button1_Click(object sender, EventArgs e)  
    20.         {  
    21.             //左磁性  
    22.             if (fm1 == null)  
    23.             {  
    24.                 fm1 = new ChildForm();  
    25.                 MagneticMagnager test1 = new MagneticMagnager(this, fm1, MagneticPosition.Left);  
    26.                 fm1.Show();  
    27.             }  
    28.             else  
    29.             {  
    30.                 fm1.Close();  
    31.                 fm1 = null;  
    32.             }  
    33.         }  
    34.   
    35.         private void button4_Click(object sender, EventArgs e)  
    36.         {  
    37.             //上磁性  
    38.             if (fm2 == null)  
    39.             {  
    40.                 fm2 = new ChildForm();  
    41.                 MagneticMagnager test2 = new MagneticMagnager(this, fm2, MagneticPosition.Top);  
    42.                 fm2.Show();  
    43.             }  
    44.             else  
    45.             {  
    46.                 fm2.Close();  
    47.                 fm2 = null;  
    48.             }  
    49.         }  
    50.   
    51.         private void button3_Click(object sender, EventArgs e)  
    52.         {  
    53.             //右磁性  
    54.             if (fm3 == null)  
    55.             {  
    56.                 fm3 = new ChildForm();  
    57.                 MagneticMagnager test3 = new MagneticMagnager(this, fm3, MagneticPosition.Right);  
    58.                 fm3.Show();  
    59.             }  
    60.             else  
    61.             {  
    62.                 fm3.Close();  
    63.                 fm3 = null;  
    64.             }  
    65.         }  
    66.   
    67.         private void button2_Click(object sender, EventArgs e)  
    68.         {  
    69.             //下磁性  
    70.             if (fm4 == null)  
    71.             {  
    72.                 fm4 = new ChildForm();  
    73.                 MagneticMagnager test4 = new MagneticMagnager(this, fm4, MagneticPosition.Bottom);  
    74.                 fm4.Show();  
    75.             }  
    76.             else  
    77.             {  
    78.                 fm4.Close();  
    79.                 fm4 = null;  
    80.             }  
    81.         }  
    82.     }  
    83. }  
     
    View Code
    using System;
    using System.Drawing;
    
    namespace System.Windows.Forms
    {
        public class MagneticMagnager
        {
            MagneticPosition Pos;//位置属性
            Form MainForm, ChildForm;
            bool IsFirstPos;//是否第一次定位ChildForm子窗体
            public int step;//磁性子窗体ChildForm移动步长
            public Point LocationPt;//定位点
            delegate void LocationDel();//移动子窗体的委托
            public MagneticMagnager(Form _MainForm, Form _ChildForm, MagneticPosition _pos)
            {
                IsFirstPos = false;
                step = 20;
                MainForm = _MainForm;
                ChildForm = _ChildForm;
                Pos = _pos;
                MainForm.LocationChanged += new EventHandler(MainForm_LocationChanged);
                ChildForm.LocationChanged += new EventHandler(ChildForm_LocationChanged);
                MainForm.SizeChanged += new EventHandler(MainForm_SizeChanged);
                ChildForm.SizeChanged += new EventHandler(ChildForm_SizeChanged);
                ChildForm.Load+=new EventHandler(ChildForm_Load);
                MainForm.Load+=new EventHandler(MainForm_Load);
            }
            void ChildForm_Load(object sender, EventArgs e)
            {
                GetLocation();
            }
            void MainForm_Load(object sender, EventArgs e)
            {
                GetLocation();
            }
            void MainForm_LocationChanged(object sender, EventArgs e)
            {
                GetLocation();
            }
            void MainForm_SizeChanged(object sender, EventArgs e)
            {
                GetLocation();
            }
            void ChildForm_SizeChanged(object sender, EventArgs e)
            {
                GetLocation();
            }
            void GetLocation()//定位子窗体
            {
                if (ChildForm == null)
                    return;
                if (Pos == MagneticPosition.Left)
                    LocationPt = new Point(MainForm.Left - ChildForm.Width, MainForm.Top);
                else if (Pos == MagneticPosition.Top)
                    LocationPt = new Point(MainForm.Left, MainForm.Top - ChildForm.Height);
                else if (Pos == MagneticPosition.Right)
                    LocationPt = new Point(MainForm.Right, MainForm.Top);
                else if (Pos == MagneticPosition.Bottom)
                    LocationPt = new Point(MainForm.Left, MainForm.Bottom);
                ChildForm.Location = LocationPt;
            }
            void ChildForm_LocationChanged(object sender, EventArgs e)//当窗体位置移动后
            {
                if (!IsFirstPos)
                {
                    IsFirstPos = true;
                    return;
                }
                LocationDel del = new LocationDel(OnMove);//委托
                MainForm.BeginInvoke(del);//调用
            }
    
            void OnMove()//移动子窗体
            {
                if (ChildForm.Left > LocationPt.X)
                    if (ChildForm.Left - LocationPt.X > step)
                        ChildForm.Left -= step;
                    else
                        ChildForm.Left = LocationPt.X;
                else if (ChildForm.Left < LocationPt.X)
                    if (ChildForm.Left - LocationPt.X < -step)
                        ChildForm.Left += step;
                    else
                        ChildForm.Left = LocationPt.X;
                if (ChildForm.Top > LocationPt.Y)
                    if (ChildForm.Top - LocationPt.Y > step)
                        ChildForm.Top -= step;
                    else
                        ChildForm.Top = LocationPt.Y;
    
                else if (ChildForm.Top < LocationPt.Y)
                    if (ChildForm.Top - LocationPt.Y < -step)
                        ChildForm.Top += step;
                    else
                        ChildForm.Top = LocationPt.Y;
            }
        }
        public enum MagneticPosition//磁性窗体的位置属性
        {
            Left = 0,
            Top = 1,
            Right = 2,
            Bottom = 3
        }
    }
    
    
    
    
    
    
    
    
    
    
    
            static void Main()
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new MainForm());
            }
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    namespace WindowsFormsApplication1
    {
        public partial class MainForm : Form
        {
            public MainForm()
            {
                InitializeComponent();
            }
            ChildForm fm1, fm2, fm3, fm4;
            private void button1_Click(object sender, EventArgs e)
            {
                //左磁性
                if (fm1 == null)
                {
                    fm1 = new ChildForm();
                    MagneticMagnager test1 = new MagneticMagnager(this, fm1, MagneticPosition.Left);
                    fm1.Show();
                }
                else
                {
                    fm1.Close();
                    fm1 = null;
                }
            }
    
            private void button4_Click(object sender, EventArgs e)
            {
                //上磁性
                if (fm2 == null)
                {
                    fm2 = new ChildForm();
                    MagneticMagnager test2 = new MagneticMagnager(this, fm2, MagneticPosition.Top);
                    fm2.Show();
                }
                else
                {
                    fm2.Close();
                    fm2 = null;
                }
            }
    
            private void button3_Click(object sender, EventArgs e)
            {
                //右磁性
                if (fm3 == null)
                {
                    fm3 = new ChildForm();
                    MagneticMagnager test3 = new MagneticMagnager(this, fm3, MagneticPosition.Right);
                    fm3.Show();
                }
                else
                {
                    fm3.Close();
                    fm3 = null;
                }
            }
    
            private void button2_Click(object sender, EventArgs e)
            {
                //下磁性
                if (fm4 == null)
                {
                    fm4 = new ChildForm();
                    MagneticMagnager test4 = new MagneticMagnager(this, fm4, MagneticPosition.Bottom);
                    fm4.Show();
                }
                else
                {
                    fm4.Close();
                    fm4 = null;
                }
            }
        }
    }
    View Code

    C#后台调用前台javascript的五种方法

    C#后台调用前台javascript的五种方法
    转自:http://blog.csdn.net/joetao/article/details/3383280
     
     
    由于项目需要,用到其他项目组用VC开发的组件,在web后台代码无法访问这个组件,所以只好通过后台调用前台的javascript,从而操作这个组件。在网上找了找,发现有三种方法可以访问到前台代码:
    第一种,OnClientClick    (vs2003不支持这个方法)
    <asp:Button ID="Button1" runat="server" Text="Button"  OnClientClick="client_click()" OnClick="Button1_Click"  />
    client_click() 就是javascript的一个方法。
    第二种,Button1.Attributes.Add("onclick", "return Client_Click()");  
    “Client_Click() “是一个前台方法,可以替换成一般的脚本如:retrun confirm('确定删除吗?')
    第三种,是我自认为最灵活的一种,ClientScript.RegisterStartupScript
    例子:StringBuilder sb = new StringBuilder();
            sb.Append("<script language='javascript'>");
            sb.Append("Button2_onclick('" + serverPath + "')");
            sb.Append("</script>");
            ClientScript.RegisterStartupScript(this.GetType(), "LoadPicScript", sb.ToString());
    第四种. 用Response.Write方法写入脚本
    比如在你单击按钮后,先操作数据库,完了后显示已经完成,可以在最后想调用的地方写上
    Response.Write("<script type='text/javascript'>alert();</script>");
    这个方法有个缺陷就是不能调用脚本文件中的自定义的函数,只能调用内部函数,具体调用自定义的函数只能在Response.Write写上函数定义,比如Response.Write("<script type='text/javascript'>function myfun(){...}</script>");
    第五种 用ClientScript类动态添加脚本
        用法如下:在想调用某个javascript脚本函数的地方添加代码,注意要保证MyFun已经在脚本文件中定义过了。
        ClientScript.RegisterStartupScript(ClientScript.GetType(), "myscript", "<script>MyFun();</script>");
    这个方法比Response.Write更方便一些,可以直接调用脚本文件中的自定义函数。
     
    View Code

    C#桌面磁性窗体

    转自:http://kb.cnblogs.com/a/1547320/
     
    看到千千静听的窗口可以在接近屏幕边缘时贴在边缘上觉得不错,自己也有这个需要,所以写了这个方法,测试了感觉还蛮不错的,哈哈~
    使用的时候只要在想应用的窗体的Form_Move(object sender,EventAges e)事件里面调用即可
    ps:不过有时窗体可能会比较闪,这个可能是代码还有待改善,或者是在Form_Move事件里面来调用不大合适,反正功能是实现了,要是哪位有更好的方法,欢迎回复交流一下啊~ 
    /// <summary>
    /// 磁性窗体函数
    /// </summary>
    /// <param name="form">窗体控件(一般传this即可)</param>
    /// <param name="space">自定义的与屏幕边缘的距离</param>
    /// <param name="isWorkingArea">是否在屏幕工作区进行该操作(true表示不包括任务栏,false则包括整个屏幕的范围)</param>
    public void Form_Welt(Control form, int space, bool isWorkingArea)
    {
        //获取窗体的左上角的x,y坐标
        int x = form.Location.X;
        int y = form.Location.Y;
    
        int sW = 0;
        int sH = 0;
    
        if (isWorkingArea)
        {
            //获取屏幕的工作区(不包括任务栏)的宽度和高度
            sW = Screen.PrimaryScreen.WorkingArea.Width;
            sH = Screen.PrimaryScreen.WorkingArea.Height;
        }
        else
        {
            //获取整个屏幕(包括任务栏)的宽度和高度
            sW = Screen.PrimaryScreen.Bounds.Width;
            sH = Screen.PrimaryScreen.Bounds.Height;
        }
    
        //如果窗体的左边缘和屏幕左边缘的距离在用户定义的范围内,则执行左贴边
        if ((x <= space && x > 0) || (Math.Abs(x) <= space && x < 0))  //Math.Abs(x)是取绝对值
        {
            form.Location = new Point(0, y);
        }
    
        //如果窗体的上边缘和屏幕上边缘的距离在用户定义的范围内,则执行上贴边
        if ((y <= space && y > 0) || (Math.Abs(y) <= space && y < 0))
        {
            form.Location = new Point(x, 0);
        }
    
    
        //窗体右边缘跟屏幕右边缘的距离
        int rightW = sW - form.Right;
        //窗体下边缘跟屏幕下边缘的距离
        int bottomW = sH - form.Bottom;
    
        //判断右边的情况
        if ((rightW <= space && form.Right < sW) || (Math.Abs(rightW) <= space && rightW < 0))
        {
            form.Location = new Point(sW - form.Width, y);
        }
        //判断下边的情况
        if ((bottomW <= 10 && form.Bottom < sH) || (Math.Abs(bottomW) <= space && bottomW < 0))
        {
            form.Location = new Point(x, sH - form.Height);
        }
    }
     
    View Code

    Form1

    sing 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 WindowsApplication1
    {
        public partial class Form1 : Form
        {
            [DllImport(@"clayui_forcsharp.dll")]
            public static extern void CLAYUI_CSharp_Init(IntPtr handle);
    
            [DllImport(@"clayui_forcsharp.dll")]
            public static extern void CLAYUI_CSharp_Release();
    
            [DllImport(@"clayui_forcsharp.dll")]
            public static extern void CLAYUI_OnAnimation(IntPtr handle, int vert, int flag, int anitype, int invert);
    
            [DllImport(@"clayui_forcsharp.dll")]
            public static extern void Redraw(IntPtr handle, int usetime);
    
            [DllImport(@"clayui_forcsharp.dll")]
            public static extern int IsPlay();
    
            [DllImport(@"clayui_forcsharp.dll")]
            public static extern void CLAYUI_InitDialog2(IntPtr handle, IntPtr handle1);
    
            [DllImport(@"clayui_forcsharp.dll")]
            public static extern void MakeWindowTpt(IntPtr handle, int factor);
    
            [DllImport(@"clayui_forcsharp.dll")]
            public static extern void WinRedraw(IntPtr handle, int redraw);
    
            [DllImport(@"clayui_forcsharp.dll")]
            public static extern void desktomemdc1(IntPtr handle);
    
            public int m_isredraw = 1;
    
            public Form1()
            {
                InitializeComponent();
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                IntPtr handle = this.Handle;
                CLAYUI_CSharp_Init(handle);
            }
    
            protected override void WndProc(ref Message m)
            {
                base.WndProc(ref m);
            }
    
            protected override void OnPaint(PaintEventArgs e)
            {
                IntPtr handle = this.Handle;
                if (m_isredraw == 1)
                    base.OnPaint(e);
            }
    
            public void EnableControl(int isenable)
            {
                foreach (System.Windows.Forms.Control control in this.Controls)
                {
                    Form1.WinRedraw(control.Handle, isenable);
                }
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                IntPtr handle = this.Handle;
                IntPtr h1 = (IntPtr)0, h2 = (IntPtr)0;
                CLAYUI_OnAnimation(handle, 0, 1, 0, 0);
                Form2 f2 = new Form2();
                f2.m_f1 = this;
                f2.ShowDialog();
            }
    
            private void Form1_Shown(object sender, EventArgs e)
            {
                IntPtr handle = this.Handle;
            }
    
            private void Form1_Paint(object sender, PaintEventArgs e)
            {
                IntPtr handle = this.Handle;
            }
    
            private void Form1_FormClosed(object sender, FormClosedEventArgs e)
            {
                CLAYUI_CSharp_Release();
            }
    
            public void StartTimer()
            {
                timer1.Start();
            }
    
            private void timer1_Tick(object sender, EventArgs e)
            {
                IntPtr handle = this.Handle;
                if (IsPlay() == 0)
                {
                    EnableControl(1);
                    timer1.Stop();
                }
                else
                    Redraw(handle, 1);
            }
    
            private void button3_Click(object sender, EventArgs e)
            {
                IntPtr handle = this.Handle;
                IntPtr h1 = (IntPtr)0, h2 = (IntPtr)0;
                CLAYUI_OnAnimation(handle, 0, 2, 1, 0);
                Form2 f2 = new Form2();
                f2.m_f1 = this;
                f2.ShowDialog();
            }
    
            private void button4_Click(object sender, EventArgs e)
            {
                IntPtr handle = this.Handle;
                IntPtr h1 = (IntPtr)0, h2 = (IntPtr)0;
                CLAYUI_OnAnimation(handle, 0, 0, 4, 0);
                Form2 f2 = new Form2();
                f2.m_f1 = this;
                f2.ShowDialog();
            }
    
            private void button5_Click(object sender, EventArgs e)
            {
                IntPtr handle = this.Handle;
                IntPtr h1 = (IntPtr)0, h2 = (IntPtr)0;
                CLAYUI_OnAnimation(handle, 0, 0, 2, 0);
                Form2 f2 = new Form2();
                f2.m_f1 = this;
                f2.ShowDialog();
            }
    
            private void button6_Click(object sender, EventArgs e)
            {
                IntPtr handle = this.Handle;
                IntPtr h1 = (IntPtr)0, h2 = (IntPtr)0;
                CLAYUI_OnAnimation(handle, 0, 0, 3, 0);
                Form2 f2 = new Form2();
                f2.m_f1 = this;
                f2.ShowDialog();
            }
    
            private void button7_Click(object sender, EventArgs e)
            {
                IntPtr handle = this.Handle;
                IntPtr h1 = (IntPtr)0, h2 = (IntPtr)0;
                CLAYUI_OnAnimation(handle, 0, 0, 5, 0);
                Form2 f2 = new Form2();
                f2.m_f1 = this;
                f2.ShowDialog();
            }
    
            private void button8_Click(object sender, EventArgs e)
            {
                IntPtr handle = this.Handle;
                IntPtr h1 = (IntPtr)0, h2 = (IntPtr)0;
                CLAYUI_OnAnimation(handle, 0, 0, 6, 0);
                Form2 f2 = new Form2();
                f2.m_f1 = this;
                f2.ShowDialog();
            }
    
            private void button9_Click(object sender, EventArgs e)
            {
                IntPtr handle = this.Handle;
                IntPtr h1 = (IntPtr)0, h2 = (IntPtr)0;
                CLAYUI_OnAnimation(handle, 0, 0, 7, 0);
                Form2 f2 = new Form2();
                f2.m_f1 = this;
                f2.ShowDialog();
            }
    
            private void button10_Click(object sender, EventArgs e)
            {
                IntPtr handle = this.Handle;
                IntPtr h1 = (IntPtr)0, h2 = (IntPtr)0;
                CLAYUI_OnAnimation(handle, 0, 3, 8, 0);
                Form2 f2 = new Form2();
                f2.m_f1 = this;
                f2.ShowDialog();
            }
    
            private void button11_Click(object sender, EventArgs e)
            {
                IntPtr handle = this.Handle;
                IntPtr h1 = (IntPtr)0, h2 = (IntPtr)0;
                CLAYUI_OnAnimation(handle, 0, 0, 9, 0);
                Form2 f2 = new Form2();
                f2.m_f1 = this;
                f2.ShowDialog();
            }
    
            private void button12_Click(object sender, EventArgs e)
            {
                IntPtr handle = this.Handle;
                IntPtr h1 = (IntPtr)0, h2 = (IntPtr)0;
                CLAYUI_OnAnimation(handle, 0, 0, 10, 0);
                Form2 f2 = new Form2();
                f2.m_f1 = this;
                f2.ShowDialog();
            }
    
            private void button13_Click(object sender, EventArgs e)
            {
                IntPtr handle = this.Handle;
                IntPtr h1 = (IntPtr)0, h2 = (IntPtr)0;
                CLAYUI_OnAnimation(handle, 0, 0, 11, 0);
                Form2 f2 = new Form2();
                f2.m_f1 = this;
                f2.ShowDialog();
            }
    
            private void button14_Click(object sender, EventArgs e)
            {
                IntPtr handle = this.Handle;
                IntPtr h1 = (IntPtr)0, h2 = (IntPtr)0;
                CLAYUI_OnAnimation(handle, 0, 0, 12, 0);
                Form2 f2 = new Form2();
                f2.m_f1 = this;
                f2.ShowDialog();
            }
        }
    }
    View Code

    Form2

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    
    namespace WindowsApplication1
    {
        public partial class Form2 : Form
        {
            public Form1 m_f1;
            int m_isredraw = 1;
    
            public Form2()
            {
                InitializeComponent();
            }
    
            private void Form2_Load(object sender, EventArgs e)
            {
                IntPtr handle = this.Handle;
    
                Form1.MakeWindowTpt(handle, 0);
    
                m_isredraw = 0;
            }
    
            private void Form2_Shown(object sender, EventArgs e)
            {
                IntPtr handle = this.Handle;
                IntPtr handle1 = m_f1.Handle;
    
                Form1.CLAYUI_InitDialog2(handle, handle1);
    
                foreach (System.Windows.Forms.Control control in this.Controls)
                {
                    Form1.WinRedraw(control.Handle, 0);
                }
    
                timer1.Start();
            }
    
            protected override void OnPaint(PaintEventArgs e)
            {
                IntPtr handle = this.Handle;
                if (m_isredraw == 1)
                    base.OnPaint(e);
            }
    
            private void timer1_Tick(object sender, EventArgs e)
            {
                IntPtr handle = this.Handle;
                if (Form1.IsPlay() == 0)
                {
                    timer1.Stop();
                    foreach (System.Windows.Forms.Control control in this.Controls)
                    {
                        Form1.WinRedraw(control.Handle, 1);
                    }
                    Update();
                }
                else
                    Form1.Redraw(handle, 1);
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                IntPtr handle = this.Handle;
    
                m_f1.EnableControl(0);
                m_f1.m_isredraw = 0;
    
                Form1.desktomemdc1(handle);
                Form1.CLAYUI_OnAnimation(handle, 1, 1, 0, 0);
                m_f1.StartTimer();
    
                Form1.Redraw(m_f1.Handle, 0);
    
                Close();
            }
    
            private void button2_Click(object sender, EventArgs e)
            {
                IntPtr handle = this.Handle;
    
                m_f1.EnableControl(0);
                m_f1.m_isredraw = 0;
    
                Form1.desktomemdc1(handle);
                Form1.CLAYUI_OnAnimation(handle, 1, 2, 1, 0);
                m_f1.StartTimer();
    
                Form1.Redraw(m_f1.Handle, 0);
    
                Close();
            }
    
            private void button3_Click(object sender, EventArgs e)
            {
                IntPtr handle = this.Handle;
    
                m_f1.EnableControl(0);
                m_f1.m_isredraw = 0;
    
                Form1.desktomemdc1(handle);
                Form1.CLAYUI_OnAnimation(handle, 1, 0, 4, 0);
                m_f1.StartTimer();
    
                Form1.Redraw(m_f1.Handle, 0);
    
                Close();
            }
    
            private void button4_Click(object sender, EventArgs e)
            {
                IntPtr handle = this.Handle;
    
                m_f1.EnableControl(0);
                m_f1.m_isredraw = 0;
    
                Form1.desktomemdc1(handle);
                Form1.CLAYUI_OnAnimation(handle, 1, 0, 5, 0);
                m_f1.StartTimer();
    
                Form1.Redraw(m_f1.Handle, 0);
    
                Close();
            }
    
            private void button5_Click(object sender, EventArgs e)
            {
                IntPtr handle = this.Handle;
    
                m_f1.EnableControl(0);
                m_f1.m_isredraw = 0;
    
                Form1.desktomemdc1(handle);
                Form1.CLAYUI_OnAnimation(handle, 1, 0, 2, 0);
                m_f1.StartTimer();
    
                Form1.Redraw(m_f1.Handle, 0);
    
                Close();
            }
    
            private void button6_Click(object sender, EventArgs e)
            {
                IntPtr handle = this.Handle;
    
                m_f1.EnableControl(0);
                m_f1.m_isredraw = 0;
    
                Form1.desktomemdc1(handle);
                Form1.CLAYUI_OnAnimation(handle, 1, 0, 3, 0);
                m_f1.StartTimer();
    
                Form1.Redraw(m_f1.Handle, 0);
    
                Close();
            }
    
            private void button7_Click(object sender, EventArgs e)
            {
                IntPtr handle = this.Handle;
    
                m_f1.EnableControl(0);
                m_f1.m_isredraw = 0;
    
                Form1.desktomemdc1(handle);
                Form1.CLAYUI_OnAnimation(handle, 1, 0, 6, 0);
                m_f1.StartTimer();
    
                Form1.Redraw(m_f1.Handle, 0);
    
                Close();
            }
    
            private void button8_Click(object sender, EventArgs e)
            {
                IntPtr handle = this.Handle;
    
                m_f1.EnableControl(0);
                m_f1.m_isredraw = 0;
    
                Form1.desktomemdc1(handle);
                Form1.CLAYUI_OnAnimation(handle, 1, 0, 7, 0);
                m_f1.StartTimer();
    
                Form1.Redraw(m_f1.Handle, 0);
    
                Close();
            }
    
            private void checkBox1_CheckedChanged(object sender, EventArgs e)
            {
    
            }
    
            private void button9_Click(object sender, EventArgs e)
            {
                IntPtr handle = this.Handle;
    
                m_f1.EnableControl(0);
                m_f1.m_isredraw = 0;
    
                Form1.desktomemdc1(handle);
                Form1.CLAYUI_OnAnimation(handle, 1, 0, 9, 0);
                m_f1.StartTimer();
    
                Form1.Redraw(m_f1.Handle, 0);
    
                Close();
            }
    
            private void button10_Click(object sender, EventArgs e)
            {
                IntPtr handle = this.Handle;
    
                m_f1.EnableControl(0);
                m_f1.m_isredraw = 0;
    
                Form1.desktomemdc1(handle);
                Form1.CLAYUI_OnAnimation(handle, 1, 0, 10, 0);
                m_f1.StartTimer();
    
                Form1.Redraw(m_f1.Handle, 0);
    
                Close();
            }
    
            private void button11_Click(object sender, EventArgs e)
            {
                IntPtr handle = this.Handle;
    
                m_f1.EnableControl(0);
                m_f1.m_isredraw = 0;
    
                Form1.desktomemdc1(handle);
                Form1.CLAYUI_OnAnimation(handle, 1, 0, 11, 0);
                m_f1.StartTimer();
    
                Form1.Redraw(m_f1.Handle, 0);
    
                Close();
            }
    
            private void button12_Click(object sender, EventArgs e)
            {
                IntPtr handle = this.Handle;
    
                m_f1.EnableControl(0);
                m_f1.m_isredraw = 0;
    
                Form1.desktomemdc1(handle);
                Form1.CLAYUI_OnAnimation(handle, 1, 0, 12, 0);
                m_f1.StartTimer();
    
                Form1.Redraw(m_f1.Handle, 0);
    
                Close();
            }
        }
    }
    View Code

    WinForm磁性窗体的设计

    FrmClass.cs
     
    
    
    View Code 
     
      1 using System;
      2 using System.Collections.Generic;
      3 using System.Text;
      4 using System.Windows.Forms;//添加控件及窗体的定名空间
      5 using System.Drawing;//添加Point的定名空间
      6 using System.Collections;//为ArrayList添加定名空间
      7
      8 namespace MagnetismForm
      9 {
     10     class FrmClass
     11     {
     12         #region  磁性窗体-公共变量
     13         //记录窗体的隐蔽与显示
     14         public static bool Example_ListShow = false;
     15         public static bool Example_LibrettoShow = false;
     16         public static bool Example_ScreenShow = false;
     17
     18         //记录鼠标的当前地位
     19         public static Point CPoint;  //添加定名空间using System.Drawing;
     20         public static Point FrmPoint;
     21         public static int Example_FSpace = 10;//设置窗体间的间隔
     22
     23 //Frm_Play窗体的地位及大小
     24         public static int Example_Play_Top = 0;
     25         public static int Example_Play_Left = 0;
     26         public static int Example_Play_Width = 0;
     27         public static int Example_Play_Height = 0;
     28         public static bool Example_Assistant_AdhereTo = false;//帮助窗体是否磁性在一路
     29
     30 //Frm_ListBos窗体的地位及大小
     31         public static int Example_List_Top = 0;
     32         public static int Example_List_Left = 0;
     33         public static int Example_List_Width = 0;
     34         public static int Example_List_Height = 0;
     35         public static bool Example_List_AdhereTo = false;//帮助窗体是否与主窗体磁性在一路
     36
     37 //Frm_Libretto窗体的地位及大小
     38         public static int Example_Libretto_Top = 0;
     39         public static int Example_Libretto_Left = 0;
     40         public static int Example_Libretto_Width = 0;
     41         public static int Example_Libretto_Height = 0;
     42         public static bool Example_Libretto_AdhereTo = false;//帮助窗体是否与主窗体磁性在一路
     43
     44 //窗体之间的间隔差
     45         public static int Example_List_space_Top = 0;
     46         public static int Example_List_space_Left = 0;
     47         public static int Example_Libretto_space_Top = 0;
     48         public static int Example_Libretto_space_Left = 0;
     49         #endregion
     50
     51         #region  检测各窗体是否连接在一路
     52         /// <summary>
     53 /// 检测各窗体是否连接在一路
     54 /// </summary>
     55         public void FrmBackCheck()
     56         {
     57             bool Tem_Magnetism = false;
     58             //Frm_ListBos与主窗体
     59             Tem_Magnetism = false;
     60             if ((Example_Play_Top - Example_List_Top) == 061                 Tem_Magnetism = true;
     62             if ((Example_Play_Left - Example_List_Left) == 063                 Tem_Magnetism = true;
     64             if ((Example_Play_Left - Example_List_Left - Example_List_Width) == 065                 Tem_Magnetism = true;
     66             if ((Example_Play_Left - Example_List_Left + Example_List_Width) == 067                 Tem_Magnetism = true;
     68             if ((Example_Play_Top - Example_List_Top - Example_List_Height) == 069                 Tem_Magnetism = true;
     70             if ((Example_Play_Top - Example_List_Top + Example_List_Height) == 071                 Tem_Magnetism = true;
     72             if (Tem_Magnetism)
     73                 Example_List_AdhereTo = true;
     74
     75             //Frm_Libretto与主窗体
     76             Tem_Magnetism = false;
     77             if ((Example_Play_Top - Example_Libretto_Top) == 078                 Tem_Magnetism = true;
     79             if ((Example_Play_Left - Example_Libretto_Left) == 080                 Tem_Magnetism = true;
     81             if ((Example_Play_Left - Example_Libretto_Left - Example_Libretto_Width) == 082                 Tem_Magnetism = true;
     83             if ((Example_Play_Left - Example_Libretto_Left + Example_Libretto_Width) == 084                 Tem_Magnetism = true;
     85             if ((Example_Play_Top - Example_Libretto_Top - Example_Libretto_Height) == 086                 Tem_Magnetism = true;
     87             if ((Example_Play_Top - Example_Libretto_Top + Example_Libretto_Height) == 088                 Tem_Magnetism = true;
     89             if (Tem_Magnetism)
     90                 Example_Libretto_AdhereTo = true;
     91
     92             //两个辅窗体
     93             Tem_Magnetism = false;
     94             if ((Example_List_Top - Example_Libretto_Top) == 095                 Tem_Magnetism = true;
     96             if ((Example_List_Left - Example_Libretto_Left) == 097                 Tem_Magnetism = true;
     98             if ((Example_List_Left - Example_Libretto_Left - Example_Libretto_Width) == 099                 Tem_Magnetism = true;
    100             if ((Example_List_Left - Example_Libretto_Left + Example_Libretto_Width) == 0101                 Tem_Magnetism = true;
    102             if ((Example_List_Top - Example_Libretto_Top - Example_Libretto_Height) == 0103                 Tem_Magnetism = true;
    104             if ((Example_List_Top - Example_Libretto_Top + Example_Libretto_Height) == 0105                 Tem_Magnetism = true;
    106             if (Tem_Magnetism)
    107                 Example_Assistant_AdhereTo = true;
    108         }
    109         #endregion
    110
    111         #region  哄骗窗体上的控件移动窗体
    112         /// <summary>
    113 /// 哄骗控件移动窗体
    114 /// </summary>
    115 /// <param Frm="Form">窗体</param>
    116 /// <param e="MouseEventArgs">控件的移动事务</param>
    117         public void FrmMove(Form Frm, MouseEventArgs e)  //Form或MouseEventArgs添加定名空间using System.Windows.Forms;
    118         {
    119             if (e.Button == MouseButtons.Left)
    120             {
    121                 Point myPosittion = Control.MousePosition;//获取当前鼠标的屏幕坐标
    122                 myPosittion.Offset(CPoint.X, CPoint.Y);//重载当前鼠标的地位
    123                 Frm.DesktopLocation = myPosittion;//设置当前窗体在屏幕上的地位
    124             }
    125         }
    126         #endregion
    127
    128         #region  策画窗体之间的间隔差
    129         /// <summary>
    130 /// 策画窗体之间的间隔差
    131 /// </summary>
    132 /// <param Frm="Form">窗体</param>
    133 /// <param Follow="Form">跟从窗体</param>
    134         public void FrmDistanceJob(Form Frm, Form Follow)
    135         {
    136             switch (Follow.Name)
    137             {
    138                 case "Frm_ListBox":
    139                     {
    140                         Example_List_space_Top = Follow.Top - Frm.Top;
    141                         Example_List_space_Left = Follow.Left - Frm.Left;
    142                         break;
    143                     }
    144                 case "Frm_Libretto":
    145                     {
    146                         Example_Libretto_space_Top = Follow.Top - Frm.Top;
    147                         Example_Libretto_space_Left = Follow.Left - Frm.Left;
    148                         break;
    149                     }
    150             }
    151         }
    152         #endregion
    153
    154         #region  磁性窗体的移动
    155         /// <summary>
    156 /// 磁性窗体的移动
    157 /// </summary>
    158 /// <param Frm="Form">窗体</param>
    159 /// <param e="MouseEventArgs">控件的移动事务</param>
    160 /// <param Follow="Form">跟从窗体</param>
    161         public void ManyFrmMove(Form Frm, MouseEventArgs e, Form Follow)  //Form或MouseEventArgs添加定名空间using System.Windows.Forms;
    162         {
    163             if (e.Button == MouseButtons.Left)
    164             {
    165                 int Tem_Left = 0;
    166                 int Tem_Top = 0;
    167                 Point myPosittion = Control.MousePosition;//获取当前鼠标的屏幕坐标
    168                 switch (Follow.Name)
    169                 {
    170                     case "Frm_ListBox":
    171                         {
    172                             Tem_Top = Example_List_space_Top - FrmPoint.Y;
    173                             Tem_Left = Example_List_space_Left - FrmPoint.X;
    174                             break;
    175                         }
    176                     case "Frm_Libretto":
    177                         {
    178                             Tem_Top = Example_Libretto_space_Top - FrmPoint.Y;
    179                             Tem_Left = Example_Libretto_space_Left - FrmPoint.X;
    180                             break;
    181                         }
    182                 }
    183                 myPosittion.Offset(Tem_Left, Tem_Top);
    184                 Follow.DesktopLocation = myPosittion;
    185             }
    186         }
    187         #endregion
    188
    189         #region  对窗体的地位进行初始化
    190         /// <summary>
    191 /// 对窗体的地位进行初始化
    192 /// </summary>
    193 /// <param Frm="Form">窗体</param>
    194         public void FrmInitialize(Form Frm)
    195         {
    196             switch (Frm.Name)
    197             {
    198                 case "Frm_Play":
    199                     {
    200                         Example_Play_Top = Frm.Top;
    201                         Example_Play_Left = Frm.Left;
    202                         Example_Play_Width = Frm.Width;
    203                         Example_Play_Height = Frm.Height;
    204                         break;
    205                     }
    206                 case "Frm_ListBox":
    207                     {
    208                         Example_List_Top = Frm.Top;
    209                         Example_List_Left = Frm.Left;
    210                         Example_List_Width = Frm.Width;
    211                         Example_List_Height = Frm.Height;
    212                         break;
    213                     }
    214                 case "Frm_Libretto":
    215                     {
    216                         Example_Libretto_Top = Frm.Top;
    217                         Example_Libretto_Left = Frm.Left;
    218                         Example_Libretto_Width = Frm.Width;
    219                         Example_Libretto_Height = Frm.Height;
    220                         break;
    221                     }
    222             }
    223
    224         }
    225         #endregion
    226
    227         #region  存储各窗体的当前信息
    228         /// <summary>
    229 /// 存储各窗体的当前信息
    230 /// </summary>
    231 /// <param Frm="Form">窗体</param>
    232 /// <param e="MouseEventArgs">控件的移动事务</param>
    233         public void FrmPlace(Form Frm)
    234         {
    235             FrmInitialize(Frm);
    236             FrmMagnetism(Frm);
    237         }
    238         #endregion
    239
    240         #region  窗体的磁性设置
    241         /// <summary>
    242 /// 窗体的磁性设置
    243 /// </summary>
    244 /// <param Frm="Form">窗体</param>
    245         public void FrmMagnetism(Form Frm)
    246         {
    247             if (Frm.Name != "Frm_Play"248             {
    249                 FrmMagnetismCount(Frm, Example_Play_Top, Example_Play_Left, Example_Play_Width, Example_Play_Height, "Frm_Play");
    250             }
    251             if (Frm.Name != "Frm_ListBos"252             {
    253                 FrmMagnetismCount(Frm, Example_List_Top, Example_List_Left, Example_List_Width, Example_List_Height, "Frm_ListBos");
    254             }
    255             if (Frm.Name != "Frm_Libratto"256             {
    257                 FrmMagnetismCount(Frm, Example_Libretto_Top, Example_Libretto_Left, Example_Libretto_Width, Example_Libretto_Height, "Frm_Libratto");
    258             }
    259             FrmInitialize(Frm);
    260         }
    261         #endregion
    262
    263         #region  磁性窗体的策画
    264         /// <summary>
    265 /// 磁性窗体的策画
    266 /// </summary>
    267 /// <param Frm="Form">窗体</param>
    268 /// <param e="MouseEventArgs">控件的移动事务</param>
    269         public void FrmMagnetismCount(Form Frm, int top, int left, int width, int height, string Mforms)
    270         {
    271             bool Tem_Magnetism = false;//断定是否有磁性产生
    272             string Tem_MainForm = "";//姑且记录主窗体
    273             string Tem_AssistForm = "";//姑且记录辅窗体
    274
    275 //上方进行磁性窗体
    276             if ((Frm.Top + Frm.Height - top) <= Example_FSpace && (Frm.Top + Frm.Height - top) >= -Example_FSpace)
    277             {
    278                 //当一个主窗体不包含辅窗体时
    279                 if ((Frm.Left >= left && Frm.Left <= (left + width)) || ((Frm.Left + Frm.Width) >= left && (Frm.Left + Frm.Width) <= (left + width)))
    280                 {
    281                     Frm.Top = top - Frm.Height;
    282                     if ((Frm.Left - left) <= Example_FSpace && (Frm.Left - left) >= -Example_FSpace)
    283                         Frm.Left = left;
    284                     Tem_Magnetism = true;
    285                 }
    286                 //当一个主窗体包含辅窗体时
    287                 if (Frm.Left <= left && (Frm.Left + Frm.Width) >= (left + width))
    288                 {
    289                     Frm.Top = top - Frm.Height;
    290                     if ((Frm.Left - left) <= Example_FSpace && (Frm.Left - left) >= -Example_FSpace)
    291                         Frm.Left = left;
    292                     Tem_Magnetism = true;
    293                 }
    294
    295             }
    296
    297             //下面进行磁性窗体
    298             if ((Frm.Top - (top + height)) <= Example_FSpace && (Frm.Top - (top + height)) >= -Example_FSpace)
    299             {
    300                 //当一个主窗体不包含辅窗体时
    301                 if ((Frm.Left >= left && Frm.Left <= (left + width)) || ((Frm.Left + Frm.Width) >= left && (Frm.Left + Frm.Width) <= (left + width)))
    302                 {
    303                     Frm.Top = top + height;
    304                     if ((Frm.Left - left) <= Example_FSpace && (Frm.Left - left) >= -Example_FSpace)
    305                         Frm.Left = left;
    306                     Tem_Magnetism = true;
    307                 }
    308                 //当一个主窗体包含辅窗体时
    309                 if (Frm.Left <= left && (Frm.Left + Frm.Width) >= (left + width))
    310                 {
    311                     Frm.Top = top + height;
    312                     if ((Frm.Left - left) <= Example_FSpace && (Frm.Left - left) >= -Example_FSpace)
    313                         Frm.Left = left;
    314                     Tem_Magnetism = true;
    315                 }
    316             }
    317
    318             //左面进行磁性窗体
    319             if ((Frm.Left + Frm.Width - left) <= Example_FSpace && (Frm.Left + Frm.Width - left) >= -Example_FSpace)
    320             {
    321                 //当一个主窗体不包含辅窗体时
    322                 if ((Frm.Top > top && Frm.Top <= (top + height)) || ((Frm.Top + Frm.Height) >= top && (Frm.Top + Frm.Height) <= (top + height)))
    323                 {
    324                     Frm.Left = left - Frm.Width;
    325                     if ((Frm.Top - top) <= Example_FSpace && (Frm.Top - top) >= -Example_FSpace)
    326                         Frm.Top = top;
    327                     Tem_Magnetism = true;
    328                 }
    329                 //当一个主窗体包含辅窗体时
    330                 if (Frm.Top <= top && (Frm.Top + Frm.Height) >= (top + height))
    331                 {
    332                     Frm.Left = left - Frm.Width;
    333                     if ((Frm.Top - top) <= Example_FSpace && (Frm.Top - top) >= -Example_FSpace)
    334                         Frm.Top = top;
    335                     Tem_Magnetism = true;
    336                 }
    337             }
    338
    339             //右面进行磁性窗体
    340             if ((Frm.Left - (left + width)) <= Example_FSpace && (Frm.Left - (left + width)) >= -Example_FSpace)
    341             {
    342                 //当一个主窗体不包含辅窗体时
    343                 if ((Frm.Top > top && Frm.Top <= (top + height)) || ((Frm.Top + Frm.Height) >= top && (Frm.Top + Frm.Height) <= (top + height)))
    344                 {
    345                     Frm.Left = left + width;
    346                     if ((Frm.Top - top) <= Example_FSpace && (Frm.Top - top) >= -Example_FSpace)
    347                         Frm.Top = top;
    348                     Tem_Magnetism = true;
    349                 }
    350                 //当一个主窗体包含辅窗体时
    351                 if (Frm.Top <= top && (Frm.Top + Frm.Height) >= (top + height))
    352                 {
    353                     Frm.Left = left + width;
    354                     if ((Frm.Top - top) <= Example_FSpace && (Frm.Top - top) >= -Example_FSpace)
    355                         Frm.Top = top;
    356                     Tem_Magnetism = true;
    357                 }
    358             }
    359             if (Frm.Name == "Frm_Play"360                 Tem_MainForm = "Frm_Play";
    361             else
    362                 Tem_AssistForm = Frm.Name;
    363             if (Mforms == "Frm_Play"364                 Tem_MainForm = "Frm_Play";
    365             else
    366                 Tem_AssistForm = Mforms;
    367             if (Tem_MainForm == ""368             {
    369                 Example_Assistant_AdhereTo = Tem_Magnetism;
    370             }
    371             else
    372             {
    373                 switch (Tem_AssistForm)
    374                 {
    375                     case "Frm_ListBos":
    376                         Example_List_AdhereTo = Tem_Magnetism;
    377                         break;
    378                     case "Frm_Libratto":
    379                         Example_Libretto_AdhereTo = Tem_Magnetism;
    380                         break;
    381                 }
    382             }
    383         }
    384         #endregion
    385
    386         #region  恢复窗体的初始大小
    387         /// <summary>
    388 /// 恢复窗体的初始大小(当松开鼠标时,若是窗体的大小小于300*200,恢复初始状况)
    389 /// </summary>
    390 /// <param Frm="Form">窗体</param>
    391         public void FrmScreen_FormerlySize(Form Frm, int PWidth, int PHeight)
    392         {
    393             if (Frm.Width < PWidth || Frm.Height < PHeight)
    394             {
    395                 Frm.Width = PWidth;
    396                 Frm.Height = PHeight;
    397                 //Example_Size = false;
    398             }
    399         }
    400         #endregion
    401
    402     }
    403 }
     
     
    
    Frm_Play.cs
     
    
    
    View Code 
     
      1 using System;
      2 using System.Collections.Generic;
      3 using System.ComponentModel;
      4 using System.Data;
      5 using System.Drawing;
      6 using System.Linq;
      7 using System.Text;
      8 using System.Windows.Forms;
      9
     10 namespace MagnetismForm
     11 {
     12     public partial class Frm_Play : Form
     13     {
     14         public Frm_Play()
     15         {
     16             InitializeComponent();
     17         }
     18
     19         #region  公共变量
     20         FrmClass Cla_FrmClass = new FrmClass();
     21         public static Form F_List = new Form();
     22         public static Form F_Libretto = new Form();
     23         public static Form F_Screen = new Form();
     24         #endregion
     25
     26         private void Frm_Play_Load(object sender, EventArgs e)
     27         {
     28             //窗体地位的初始化
     29             Cla_FrmClass.FrmInitialize(this);
     30         }
     31
     32         private void panel_Title_MouseDown(object sender, MouseEventArgs e)
     33         {
     34
     35             int Tem_Y = 0;
     36             if (e.Button == MouseButtons.Left)//按下的是否为鼠标左键
     37             {
     38                 Cla_FrmClass.FrmBackCheck();//检测各窗体是否连在一路
     39                 Tem_Y = e.Y;
     40                 FrmClass.FrmPoint = new Point(e.X, Tem_Y);//获取鼠标在窗体上的地位,用于磁性窗体
     41                 FrmClass.CPoint = new Point(-e.X, -Tem_Y);//获取鼠标在屏幕上的地位,用于窗体的移动
     42                 if (FrmClass.Example_List_AdhereTo)//若是与frm_ListBox窗体相连接
     43                 {
     44                     Cla_FrmClass.FrmDistanceJob(this, F_List);//策画窗体的间隔差
     45                     if (FrmClass.Example_Assistant_AdhereTo)//两个辅窗体是否连接在一路
     46                     {
     47                         Cla_FrmClass.FrmDistanceJob(this, F_Libretto);//策画窗体的间隔差
     48                     }
     49                 }
     50                 if (FrmClass.Example_Libretto_AdhereTo)//若是与frm_Libretto窗体相连接
     51                 {
     52                     Cla_FrmClass.FrmDistanceJob(this, F_Libretto);//策画窗体的间隔差
     53                     if (FrmClass.Example_Assistant_AdhereTo)//两个辅窗体是否连接在一路
     54                     {
     55                         Cla_FrmClass.FrmDistanceJob(this, F_List);//策画窗体的间隔差
     56                     }
     57                 }
     58             }
     59
     60         }
     61
     62         private void panel_Title_MouseMove(object sender, MouseEventArgs e)
     63         {
     64             if (e.Button == MouseButtons.Left)//按下的是否为鼠标左键
     65             {
     66
     67                 Cla_FrmClass.FrmMove(this, e);//哄骗控件移动窗体
     68                 if (FrmClass.Example_List_AdhereTo)//若是frm_ListBox窗体与主窗体连接
     69                 {
     70
     71                     Cla_FrmClass.ManyFrmMove(this, e, F_List);//磁性窗体的移动
     72                     Cla_FrmClass.FrmInitialize(F_List);//对frm_ListBox窗体的地位进行初始化
     73                     if (FrmClass.Example_Assistant_AdhereTo)//若是两个子窗体连接在一路
     74                     {
     75                         Cla_FrmClass.ManyFrmMove(this, e, F_Libretto);
     76                         Cla_FrmClass.FrmInitialize(F_Libretto);
     77                     }
     78                 }
     79
     80                 if (FrmClass.Example_Libretto_AdhereTo)//若是frm_Libretto窗体与主窗体连接
     81                 {
     82                     Cla_FrmClass.ManyFrmMove(this, e, F_Libretto);
     83                     Cla_FrmClass.FrmInitialize(F_Libretto);
     84                     if (FrmClass.Example_Assistant_AdhereTo)
     85                     {
     86                         Cla_FrmClass.ManyFrmMove(this, e, F_List);
     87                         Cla_FrmClass.FrmInitialize(F_List);
     88                     }
     89                 }
     90                 Cla_FrmClass.FrmInitialize(this);
     91             }
     92         }
     93
     94         private void panel_Title_MouseUp(object sender, MouseEventArgs e)
     95         {
     96             Cla_FrmClass.FrmPlace(this);
     97         }
     98
     99         private void Frm_Play_Shown(object sender, EventArgs e)
    100         {
    101             //显示列表窗体
    102             F_List = new Frm_ListBox();
    103             F_List.ShowInTaskbar = false;
    104             FrmClass.Example_ListShow = true;
    105             F_List.Show();
    106             //显示歌词窗体
    107             F_Libretto = new Frm_Libretto();
    108             F_Libretto.ShowInTaskbar = false;
    109             FrmClass.Example_LibrettoShow = true;
    110             F_Libretto.Show();
    111             F_Libretto.Left = this.Left + this.Width;
    112             F_Libretto.Top = this.Top;
    113             //各窗体地位的初始化
    114             Cla_FrmClass.FrmInitialize(F_List);
    115             Cla_FrmClass.FrmInitialize(F_Libretto);
    116         }
    117
    118         private void panel_Close_Click(object sender, EventArgs e)
    119         {
    120             F_List.Close();
    121             F_List.Dispose();
    122             F_Libretto.Close();
    123             F_Libretto.Dispose();
    124             F_Screen.Close();
    125             F_Screen.Dispose();
    126             this.Close();
    127         }
    128
    129         private void panel_Title_Click(object sender, EventArgs e)
    130         {
    131             F_List.Focus();
    132             F_Libretto.Focus();
    133             this.Focus();
    134
    135         }
    136
    137     }
    138 }
     
     
    
    Frm_Play.designer.cs
     
    
    
    View Code 
     
     1 namespace MagnetismForm
     2 {
     3     partial class Frm_Play
     4     {
     5         /// <summary>
     6 /// 必须的设计器变量。
     7 /// </summary>
     8         private System.ComponentModel.IContainer components = null;
     9
    10         /// <summary>
    11 /// 清理所有正在应用的资料。
    12 /// </summary>
    13 /// <param name="disposing">若是应开释托管资料,为 true;不然为 false。</param>
    14         protected override void Dispose(bool disposing)
    15         {
    16             if (disposing && (components != null))
    17             {
    18                 components.Dispose();
    19             }
    20             base.Dispose(disposing);
    21         }
    22
    23         #region Windows 窗体设计器生成的代码
    24
    25         /// <summary>
    26 /// 设计器支撑所需的办法 - 不要
    27 /// 应用代码编辑器批改此办法的内容。
    28 /// </summary>
    29         private void InitializeComponent()
    30         {
    31             this.panel_Title = new System.Windows.Forms.Panel();
    32             this.panel_Close = new System.Windows.Forms.Panel();
    33             this.pictureBox1 = new System.Windows.Forms.PictureBox();
    34             this.panel_Title.SuspendLayout();
    35             ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
    36             this.SuspendLayout();
    37             //
    38 // panel_Title
    39 //
    40             this.panel_Title.BackColor = System.Drawing.Color.MediumBlue;
    41             this.panel_Title.BackgroundImage = global::MagnetismForm.Properties.Resources._1;
    42             this.panel_Title.Controls.Add(this.panel_Close);
    43             this.panel_Title.Dock = System.Windows.Forms.DockStyle.Top;
    44             this.panel_Title.Location = new System.Drawing.Point(00);
    45             this.panel_Title.Name = "panel_Title";
    46             this.panel_Title.Size = new System.Drawing.Size(29031);
    47             this.panel_Title.TabIndex = 0;
    48             this.panel_Title.MouseMove += new System.Windows.Forms.MouseEventHandler(this.panel_Title_MouseMove);
    49             this.panel_Title.Click += new System.EventHandler(this.panel_Title_Click);
    50             this.panel_Title.MouseDown += new System.Windows.Forms.MouseEventHandler(this.panel_Title_MouseDown);
    51             this.panel_Title.MouseUp += new System.Windows.Forms.MouseEventHandler(this.panel_Title_MouseUp);
    52             //
    53 // panel_Close
    54 //
    55             this.panel_Close.BackColor = System.Drawing.Color.Red;
    56             this.panel_Close.BackgroundImage = global::MagnetismForm.Properties.Resources.Close;
    57             this.panel_Close.Location = new System.Drawing.Point(2705);
    58             this.panel_Close.Name = "panel_Close";
    59             this.panel_Close.Size = new System.Drawing.Size(1818);
    60             this.panel_Close.TabIndex = 0;
    61             this.panel_Close.Click += new System.EventHandler(this.panel_Close_Click);
    62             //
    63 // pictureBox1
    64 //
    65             this.pictureBox1.Image = global::MagnetismForm.Properties.Resources._4;
    66             this.pictureBox1.Location = new System.Drawing.Point(031);
    67             this.pictureBox1.Name = "pictureBox1";
    68             this.pictureBox1.Size = new System.Drawing.Size(29089);
    69             this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
    70             this.pictureBox1.TabIndex = 1;
    71             this.pictureBox1.TabStop = false;
    72             //
    73 // Frm_Play
    74 //
    75             this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
    76             this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
    77             this.ClientSize = new System.Drawing.Size(290120);
    78             this.Controls.Add(this.pictureBox1);
    79             this.Controls.Add(this.panel_Title);
    80             this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
    81             this.Name = "Frm_Play";
    82             this.Text = "主窗体";
    83             this.Load += new System.EventHandler(this.Frm_Play_Load);
    84             this.Shown += new System.EventHandler(this.Frm_Play_Shown);
    85             this.panel_Title.ResumeLayout(false);
    86             ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
    87             this.ResumeLayout(false);
    88
    89         }
    90
    91         #endregion
    92
    93         private System.Windows.Forms.Panel panel_Title;
    94         private System.Windows.Forms.Panel panel_Close;
    95         private System.Windows.Forms.PictureBox pictureBox1;
    96     }
    97 }
     
     
    
    Frm_ListBox.cs
     
    
    
    View Code 
     
     1 using System;
     2 using System.Collections.Generic;
     3 using System.ComponentModel;
     4 using System.Data;
     5 using System.Drawing;
     6 using System.Linq;
     7 using System.Text;
     8 using System.Windows.Forms;
     9
    10 namespace MagnetismForm
    11 {
    12     public partial class Frm_ListBox : Form
    13     {
    14         public Frm_ListBox()
    15         {
    16             InitializeComponent();
    17         }
    18
    19         #region  公共变量
    20         FrmClass Cla_FrmClass = new FrmClass();
    21         #endregion
    22
    23         private void Frm_ListBox_Load(object sender, EventArgs e)
    24         {
    25             this.Left = FrmClass.Example_Play_Left;
    26             this.Top = FrmClass.Example_Play_Top + FrmClass.Example_Play_Height;
    27             Cla_FrmClass.FrmInitialize(this);
    28         }
    29
    30         private void panel_Title_MouseDown(object sender, MouseEventArgs e)
    31         {
    32             FrmClass.CPoint = new Point(-e.X, -e.Y);
    33         }
    34
    35         private void panel_Title_MouseMove(object sender, MouseEventArgs e)
    36         {
    37             FrmClass.Example_Assistant_AdhereTo = false;
    38             FrmClass.Example_List_AdhereTo = false;
    39             Cla_FrmClass.FrmMove(this, e);
    40         }
    41
    42         private void panel_Title_MouseUp(object sender, MouseEventArgs e)
    43         {
    44             Cla_FrmClass.FrmPlace(this);
    45         }
    46     }
    47 }
     
     
    
    Frm_ListBox.designer.cs
     
    
    
    View Code 
     
     1 namespace MagnetismForm
     2 {
     3     partial class Frm_ListBox
     4     {
     5         /// <summary>
     6 /// Required designer variable.
     7 /// </summary>
     8         private System.ComponentModel.IContainer components = null;
     9
    10         /// <summary>
    11 /// Clean up any resources being used.
    12 /// </summary>
    13 /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
    14         protected override void Dispose(bool disposing)
    15         {
    16             if (disposing && (components != null))
    17             {
    18                 components.Dispose();
    19             }
    20             base.Dispose(disposing);
    21         }
    22
    23         #region Windows Form Designer generated code
    24
    25         /// <summary>
    26 /// Required method for Designer support - do not modify
    27 /// the contents of this method with the code editor.
    28 /// </summary>
    29         private void InitializeComponent()
    30         {
    31             this.pictureBox1 = new System.Windows.Forms.PictureBox();
    32             this.panel_Title = new System.Windows.Forms.Panel();
    33             ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
    34             this.SuspendLayout();
    35             //
    36 // pictureBox1
    37 //
    38             this.pictureBox1.Image = global::MagnetismForm.Properties.Resources._4;
    39             this.pictureBox1.Location = new System.Drawing.Point(031);
    40             this.pictureBox1.Name = "pictureBox1";
    41             this.pictureBox1.Size = new System.Drawing.Size(29089);
    42             this.pictureBox1.TabIndex = 1;
    43             this.pictureBox1.TabStop = false;
    44             //
    45 // panel_Title
    46 //
    47             this.panel_Title.BackColor = System.Drawing.Color.MediumBlue;
    48             this.panel_Title.BackgroundImage = global::MagnetismForm.Properties.Resources._5;
    49             this.panel_Title.Dock = System.Windows.Forms.DockStyle.Top;
    50             this.panel_Title.Location = new System.Drawing.Point(00);
    51             this.panel_Title.Name = "panel_Title";
    52             this.panel_Title.Size = new System.Drawing.Size(29031);
    53             this.panel_Title.TabIndex = 0;
    54             this.panel_Title.MouseMove += new System.Windows.Forms.MouseEventHandler(this.panel_Title_MouseMove);
    55             this.panel_Title.MouseDown += new System.Windows.Forms.MouseEventHandler(this.panel_Title_MouseDown);
    56             this.panel_Title.MouseUp += new System.Windows.Forms.MouseEventHandler(this.panel_Title_MouseUp);
    57             //
    58 // Frm_ListBox
    59 //
    60             this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
    61             this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
    62             this.ClientSize = new System.Drawing.Size(290120);
    63             this.Controls.Add(this.pictureBox1);
    64             this.Controls.Add(this.panel_Title);
    65             this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
    66             this.Name = "Frm_ListBox";
    67             this.Text = "辅窗体1";
    68             this.Load += new System.EventHandler(this.Frm_ListBox_Load);
    69             ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
    70             this.ResumeLayout(false);
    71
    72         }
    73
    74         #endregion
    75
    76         private System.Windows.Forms.Panel panel_Title;
    77         private System.Windows.Forms.PictureBox pictureBox1;
    78     }
    79 }
     
     
    
    Frm_Libretto.cs
     
    
    
    View Code 
     
     1 using System;
     2 using System.Collections.Generic;
     3 using System.ComponentModel;
     4 using System.Data;
     5 using System.Drawing;
     6 using System.Linq;
     7 using System.Text;
     8 using System.Windows.Forms;
     9
    10 namespace MagnetismForm
    11 {
    12     public partial class Frm_Libretto : Form
    13     {
    14         public Frm_Libretto()
    15         {
    16             InitializeComponent();
    17         }
    18
    19         #region  公共变量
    20         FrmClass Cla_FrmClass = new FrmClass();
    21         #endregion
    22
    23         private void Frm_Libretto_Load(object sender, EventArgs e)
    24         {
    25             this.Top = FrmClass.Example_Play_Top;
    26             this.Left = FrmClass.Example_Play_Left + FrmClass.Example_Play_Width;
    27             Cla_FrmClass.FrmInitialize(this);
    28         }
    29
    30         private void panel_Title_MouseDown(object sender, MouseEventArgs e)
    31         {
    32             FrmClass.CPoint = new Point(-e.X, -e.Y);
    33         }
    34
    35         private void panel_Title_MouseMove(object sender, MouseEventArgs e)
    36         {
    37             FrmClass.Example_Assistant_AdhereTo = false;
    38             FrmClass.Example_Libretto_AdhereTo = false;
    39             Cla_FrmClass.FrmMove(this, e);
    40         }
    41
    42         private void panel_Title_MouseUp(object sender, MouseEventArgs e)
    43         {
    44             Cla_FrmClass.FrmPlace(this);
    45         }
    46     }
    47 }
     
     
    
    Frm_Libretto.designer.cs
     
    
    
    View Code 
     
     1 namespace MagnetismForm
     2 {
     3     partial class Frm_Libretto
     4     {
     5         /// <summary>
     6 /// Required designer variable.
     7 /// </summary>
     8         private System.ComponentModel.IContainer components = null;
     9
    10         /// <summary>
    11 /// Clean up any resources being used.
    12 /// </summary>
    13 /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
    14         protected override void Dispose(bool disposing)
    15         {
    16             if (disposing && (components != null))
    17             {
    18                 components.Dispose();
    19             }
    20             base.Dispose(disposing);
    21         }
    22
    23         #region Windows Form Designer generated code
    24
    25         /// <summary>
    26 /// Required method for Designer support - do not modify
    27 /// the contents of this method with the code editor.
    28 /// </summary>
    29         private void InitializeComponent()
    30         {
    31             this.pictureBox1 = new System.Windows.Forms.PictureBox();
    32             this.panel_Title = new System.Windows.Forms.Panel();
    33             ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
    34             this.SuspendLayout();
    35             //
    36 // pictureBox1
    37 //
    38             this.pictureBox1.Image = global::MagnetismForm.Properties.Resources._2;
    39             this.pictureBox1.Location = new System.Drawing.Point(031);
    40             this.pictureBox1.Name = "pictureBox1";
    41             this.pictureBox1.Size = new System.Drawing.Size(290209);
    42             this.pictureBox1.TabIndex = 1;
    43             this.pictureBox1.TabStop = false;
    44             //
    45 // panel_Title
    46 //
    47             this.panel_Title.BackColor = System.Drawing.Color.MediumBlue;
    48             this.panel_Title.BackgroundImage = global::MagnetismForm.Properties.Resources._5;
    49             this.panel_Title.Dock = System.Windows.Forms.DockStyle.Top;
    50             this.panel_Title.Location = new System.Drawing.Point(00);
    51             this.panel_Title.Name = "panel_Title";
    52             this.panel_Title.Size = new System.Drawing.Size(29031);
    53             this.panel_Title.TabIndex = 0;
    54             this.panel_Title.MouseMove += new System.Windows.Forms.MouseEventHandler(this.panel_Title_MouseMove);
    55             this.panel_Title.MouseDown += new System.Windows.Forms.MouseEventHandler(this.panel_Title_MouseDown);
    56             this.panel_Title.MouseUp += new System.Windows.Forms.MouseEventHandler(this.panel_Title_MouseUp);
    57             //
    58 // Frm_Libretto
    59 //
    60             this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
    61             this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
    62             this.ClientSize = new System.Drawing.Size(290240);
    63             this.Controls.Add(this.pictureBox1);
    64             this.Controls.Add(this.panel_Title);
    65             this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
    66             this.Name = "Frm_Libretto";
    67             this.Text = "Frm_Libretto";
    68             this.Load += new System.EventHandler(this.Frm_Libretto_Load);
    69             ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
    70             this.ResumeLayout(false);
    71
    72         }
    73
    74         #endregion
    75
    76         private System.Windows.Forms.Panel panel_Title;
    77         private System.Windows.Forms.PictureBox pictureBox1;
    78     }
    79 }
     
    View Code

    比如qq那样,拖到桌面边上,它会自动吸附在桌面边上,并且还会自动伸缩,(和windows的任务栏也类似)

    比如qq那样,拖到桌面边上,它会自动吸附在桌面边上,并且还会自动伸缩,(和windows的任务栏也类似) 
    两个窗口之间的吸附作用我也想了解,有谁知道怎么实现的是?? 怎样建立磁性窗体(转贴于:   :《程序员》2002年第01期) 
    
    
    实现磁性窗体基本上分为两步,第一步是实现当两个窗体靠近到一定距离以内时实现窗体间的粘贴操作,第二步是一定窗体时,同时移动与它粘在一起的其它窗体。 
    
    实现窗体的粘贴 
            实现粘贴的难点在于什么时候进行这个操作,假设有两个窗体Form1和Form2,移动Form2向Form1靠近,当Form2与Form1的最近距离小于distance时粘贴在一起。显然,应该在移动Form2的过程中进行判断,问题是在程序的什么位置插入判断代码呢? 
    
    
    合理的方法是利用系统产生的消息,但是利用什么消息呢?窗体在移动时会产生WM_WINDOWPOSCHANGING和WM_MOVING消息,移动结束后会产生WM_WINDOWPOSCHANGED和WM_MOVE消息。WM_WINDOWPOSCHANGING和WM_WINDOWPOSCHANGED消息的参数lParam是结构WINDOWPOS的指针,WINDOWPOS定义如下: 
            typedef   struct   _WINDOWPOS   { 
                    HWND   hwnd;   //   窗口句炳 
                    HWND   hwndInsertAfter;   //   窗口的Z顺序 
                    int     x;   //   窗口x坐标 
                    int     y;   //   窗口的y坐标 
                    int     cx;   //   窗口的宽度 
                    int     cy;   //   窗口的高度 
                    UINT   flags;   //   标志位,根据它设定窗口的位置 
            }   WINDOWPOS; 
            可以看出,WM_WINDOWPOSCHANGED消息不仅仅在窗口移动时产生,而且在它的Z顺序发生变化时产生,包括窗口的显示和隐藏。所以我认为这个消息不是最佳选择。 
            WM_MOVING和WM_MOVE消息的参数lParam是一个RECT结构指针,与WM_WINDOWPOSCHANGED消息相比较为单纯,我采用的即是这个消息。 
    
    
    -----------------------------"磁性窗体 ",到google找,很多.   
    有了以上的东西,用C#   的unsafe   code   ,调用API,不难实现. 
    
    
    ■■■■■   To   teach   a   fish   how   to   swim.   ■■■■■ 
    View Code

    简单的磁性窗体源程序

    最近在写一个小程序,顺便添加了一个磁性窗体功能。很简单,我却折腾了半天,现在把源代码奉献出来,供大家参考,不足之处希望指出。
    
    //全局变量:               
    
    
    
        private bool mouseDown = false; //鼠标是否按下
    
    
    
            private Point mouseLocation; //鼠标位置
    
    
    
    //鼠标移动事件 
    
    
    
        private void frmMain_MouseMove(object sender, MouseEventArgs e) 
    
    
    
            ...{ 
    
    
    
                    bool move = true;   //是否根据移动窗体
    
    
    
                    Point p1 = this.Location;  //获得当前窗体坐标
    
    
    
                    if (mouseDown ==true)  //检测鼠标是否按下
    
    
    
                    ...{ 
    
    
    
                            p1.Offset(e.Location.X - mouseLocation.X, e.Location.Y - mouseLocation.Y); //将p1按照鼠标移动偏移
    
    
    
                            if (Location.X <= 20 && e.Location.X - mouseLocation.X<=20)  //判断当前窗体横坐标是否小于指定吸附距离,并且鼠标横位移是否小于此距离(注意:鼠标的位移一定要等于或大于指定距离,否则窗体会产生晃动)
    
    
    
                            ...{ 
    
    
    
                                   move = false; //此时鼠标移动不会移动窗体
    
    
    
                                   if (!move)   //检测此时是否由鼠标控制窗体
    
    
    
                                         p1.X = 0; //将窗体吸附到边缘
    
    
    
                             }
    
    
    
                            if (Location.Y <= 20 && e.Location.Y - mouseLocation.Y<=20) //检测纵坐标(其他同上)
    
                            ...{
    
    
    
                                  move = false;
    
    
    
                                  if (!move)
    
    
    
                                      p1.Y = 0;
    
    
    
                           } 
    
    
    
                   this.Location = p1; //设置窗体位置为改变后的坐标
    
    
    
             }
    
    
    
    } 
    
    
    
    //鼠标按下事件 
    
    
    
        private void frmMain_MouseDown(object sender, MouseEventArgs e)
    
    
    
          ...{
    
    
    
                if (e.Button == MouseButtons.Left)  //判断是否按下鼠标左键
    
    
    
               ...{
    
    
    
                    mouseDown = true;  
    
    
    
                    mouseLocation = e.Location;  //获得此时的鼠标位置
    
    
    
               }
    
    
    
           }
    
    
    
    //鼠标松开事件
    
    
    
        private void frmMain_MouseUp(object sender, MouseEventArgs e)
    
    
    
          ...{
    
    
    
                 mouseDown = false;  
    
    
    
          }
    
     
    View Code

    今天编程的时候,遇到一个问题:在同一个窗体区域加载两个不同的窗体,每次只显示一个子窗体并能够对这两个子窗体做切换。

    今天编程的时候,遇到一个问题:在同一个窗体区域加载两个不同的窗体,每次只显示一个子窗体并能够对这两个子窗体做切换。
         对于这个问题用panel控件是非常简单的,只要每次清空panel控件上的子窗体,然后加载另一个子窗体即可。代码如下所示:
    panel1.Clear(); // 清空的是当前panel上的子窗体  
    panel1.Add(subtabcontrol);//subtabcontrol是另一个子窗体
     
    View Code

    两个问题

    两个问题
    1、当切换两窗体的焦点时,子窗体的Location属性为什么会发生变化?
    2、当最小化主窗体后,再恢复主窗体,子窗体却出不来了?
    主窗体的代码如下  
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
     
    namespace formlink
    {
        public partial class Form1 : Form
        {
            private Form2 f2;
            private bool Islink;
            private Point p;
            private int DeviationX, DeviationY;
     
            public Form1()
            {
                InitializeComponent();
                f2 = null;
                Islink = true;
            }
     
            private void Form1_Load(object sender, EventArgs e)
            {
                textBox1.Text = this.Location.X.ToString();
                textBox2.Text = this.Location.Y.ToString();
                textBox3.Text = this.Size.Width.ToString();
                textBox4.Text = this.Size.Height.ToString();
            }
     
            private void button1_Click(object sender, EventArgs e)
            {
                if (f2 == null)
                {
                    Form2 fm2 = new Form2(this);
                    fm2.Show();
                }
                else
                {
                    label5.Text = f2.Visible.ToString();//判断子窗体的visible属性
                }
            }
     
            private void Form1_LocationChanged(object sender, EventArgs e)
            {
                textBox1.Text = this.Location.X.ToString();
                textBox2.Text = this.Location.Y.ToString();
                if (f2 != null)
                {
                    if (Islink == true)//如果两窗体相连,子窗体跟随主窗体一起移动
                    {
                        f2.Set_Form2_Location(DeviationX, DeviationY);
                    }
                    else
                    {
                        f2.Set_Form2_NewLocation();//吸引子窗体
                    }
                }
            }
     
            public void Set_Form2(Form2 ptf2)
            {
                f2 = ptf2;
                this.Set_Deviation();
            }
     
            public void Set_Islink(bool ptlink)
            {
                Islink = ptlink;
            }
     
            private void Form1_Activated(object sender, EventArgs e)
            {
                this.Set_Deviation();
            }
     
            public void Set_Deviation()
            {
                if (f2 != null)
                {
                    DeviationX = f2.Location.X - this.Location.X;
                    DeviationY = f2.Location.Y - this.Location.Y;
                }
            }
        }
    }
     
     
    
    · 对我有用[0]· 丢个板砖[0]· 引用· 举报· 管理· TOP精华推荐:如何让gridview中的checkbox根据数据库情况默认选中?
    子窗体的代码如下,下帖还有using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;namespace formlink{ public partial class Form2 : Form { private Form1 f1; private Point MouseDownLocation, MouseStartDrag; private bool bLeft, bRight, bTop, bBottom, IsLink; private bool bTopLeft, bTopRight, bBottomLeft, bBottomRight; public Form2(Form1 ptf1) { InitializeComponent(); f1 = ptf1; } private void Form2_Load(object sender, EventArgs e) { textBox3.Text = this.Size.Width.ToString(); textBox4.Text = this.Size.Height.ToString(); this.Location = new Point(f1.Location.X + f1.Size.Width, f1.Location.Y); f1.Set_Form2(this); } private void Form2_FormClosed(object sender, FormClosedEventArgs e) { f1.Set_Form2(null); } public void Set_Form2_Location(int ptDeviationX,int ptDeviationY) { this.Location = new Point(f1.Location.X + ptDeviationX, f1.Location.Y + ptDeviationY); } private void button1_Click(object sender, EventArgs e) { this.Close(); }
    
    · · wangtong2010· (wangtong2010)· 等 级:· #5楼 得分:0回复于:2010-07-11 11:02:43
     
    
    
    private void Form2_MouseDown(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left) { MouseDownLocation = new Point(e.X, e.Y);//获取鼠标按下时的位置 MouseStartDrag = Control.MousePosition;//当开始拖拽鼠标时的位置 } }
    private void Form2_LocationChanged(object sender, EventArgs e)
      {
      textBox1.Text = this.Location.X.ToString();
      textBox2.Text = this.Location.Y.ToString();
      textBox3.Text = this.Size.Width.ToString();
      textBox4.Text = this.Size.Height.ToString();
      bLeft = this.Location.X + this.Size.Width == f1.Location.X && this.Location.Y + this.Size.Height > f1.Location.Y && this.Location.Y < f1.Location.Y + f1.Size.Height;
      //判断是否左侧相连
      bRight = this.Location.X == f1.Location.X + f1.Size.Width && this.Location.Y + this.Size.Height > f1.Location.Y && this.Location.Y < f1.Location.Y + f1.Size.Height;
      //判断是否右侧相连
      bTop = this.Location.Y + this.Size.Height == f1.Location.Y && this.Location.X + this.Size.Width > f1.Location.X && this.Location.X < f1.Location.X + f1.Size.Width;
      //判断是否上侧相连
      bBottom = this.Location.Y == f1.Location.Y + f1.Size.Height && this.Location.X + this.Size.Width > f1.Location.X && this.Location.X < f1.Location.X + f1.Size.Width;
      //判断是否下侧相连
      bTopLeft = this.Location.X + this.Size.Width == f1.Location.X && this.Location.Y + this.Size.Height == f1.Location.Y;
      //判断是否左上角相连
      bTopRight = this.Location.X == f1.Location.X + f1.Size.Width && this.Location.Y + this.Size.Height == f1.Location.Y;
      //判断是否右上角相连
      bBottomLeft = this.Location.X + this.Size.Width == f1.Location.X && this.Location.Y == f1.Location.Y + f1.Size.Height;
      //判断是否左下角相连
      bBottomRight = this.Location.X == f1.Location.X + f1.Size.Width && this.Location.Y == f1.Location.Y + f1.Size.Height;
      //判断是否右下角相连
      IsLink = bLeft || bRight || bTop || bBottom || bTopLeft || bTopRight || bBottomLeft || bBottomRight;
      //判断是否相连
      f1.Set_Islink(IsLink);
      //改变主窗体的连接信息
      }
    private void Form2_MouseMove(object sender, MouseEventArgs e)
      {
      if (e.Button == MouseButtons.Left)
      {
      Point p;
      p = Control.MousePosition;//获取当前鼠标桌面位置
      if (IsLink == true)//如果当前两个窗体连接上了
      {
      if (bLeft == true)//子窗体连在主窗体左侧
      {
      if (MouseStartDrag.X - p.X >= 20)//当拖拽量大于等于20时,两窗体分离
      {
      this.Location = new Point(this.Location.X - 20, p.Y - MouseDownLocation.Y);
      }
      else//否则子窗体沿主窗体边沿滑动
      {
      this.Location = new Point(this.Location.X, p.Y - MouseDownLocation.Y);
      }
      }
      else if (bRight == true)//子窗体连在主窗体右侧
      {
      if (p.X - MouseStartDrag.X >= 20)
      {
      this.Location = new Point(this.Location.X + 20, p.Y - MouseDownLocation.Y);
      }
      else
      {
      this.Location = new Point(this.Location.X, p.Y - MouseDownLocation.Y);
      }
      }
      else if (bTop == true)//子窗体连在主窗体上侧
      {
      if (MouseStartDrag.Y - p.Y >= 20)
      {
      this.Location = new Point(p.X - MouseDownLocation.X, this.Location.Y - 20);
      }
      else
      {
      this.Location = new Point(p.X - MouseDownLocation.X, this.Location.Y);
      }
      }
      else if (bBottom == true)//子窗体连在主窗体下侧
      {
      if (p.Y - MouseStartDrag.Y >= 20)
      {
      this.Location = new Point(p.X - MouseDownLocation.X, this.Location.Y + 20);
      }
      else
      {
      this.Location = new Point(p.X - MouseDownLocation.X, this.Location.Y);
      }
      }
      else if (bTopLeft == true)//子窗体连在主窗体左上角
      {
      if (p.X - MouseStartDrag.X <= -20 || p.Y - MouseStartDrag.Y <= -20)//当拖拽的方向向远离主窗体并超过20时,两窗体分离
      {
      this.Location = new Point(p.X - MouseDownLocation.X, p.Y - MouseDownLocation.Y);
      }
      else if (p.X - MouseStartDrag.X > 0 && p.Y - MouseStartDrag.Y <= 0)//否则子窗体滑向主窗体的一边沿
      {
      this.Location = new Point(p.X - MouseDownLocation.X, this.Location.Y);
      }
      else if (p.Y - MouseStartDrag.Y > 0 && p.X - MouseStartDrag.X <= 0)//否则子窗体滑向主窗体的一边沿
      {
      this.Location = new Point(this.Location.X, p.Y - MouseDownLocation.Y);
      }
      }
      else if (bTopRight == true)//子窗体连在主窗体右上角
      {
      if (p.X - MouseStartDrag.X >= 20 || p.Y - MouseStartDrag.Y <= -20)
      {
      this.Location = new Point(p.X - MouseDownLocation.X, p.Y - MouseDownLocation.Y);
      }
      else if (p.X - MouseStartDrag.X < 0 && p.Y - MouseStartDrag.Y <= 0)
      {
      this.Location = new Point(p.X - MouseDownLocation.X, this.Location.Y);
      }
      else if (p.Y - MouseStartDrag.Y > 0 && p.X - MouseStartDrag.X >= 0)
      {
      this.Location = new Point(this.Location.X, p.Y - MouseDownLocation.Y);
      }
      }
      else if (bBottomLeft == true)//子窗体连在主窗体左下角
      {
      if (p.X - MouseStartDrag.X <= -20 || p.Y - MouseStartDrag.Y >= 20)
      {
      this.Location = new Point(p.X - MouseDownLocation.X, p.Y - MouseDownLocation.Y);
      }
      else if (p.X - MouseStartDrag.X > 0 && p.Y - MouseStartDrag.Y >= 0)
      {
      this.Location = new Point(p.X - MouseDownLocation.X, this.Location.Y);
      }
      else if (p.Y - MouseStartDrag.Y < 0 && p.X - MouseStartDrag.X <= 0)
      {
      this.Location = new Point(this.Location.X, p.Y - MouseDownLocation.Y);
      }
      }
      else//子窗体连在主窗体右下角
      {
      if (p.X - MouseStartDrag.X >= 20 || p.Y - MouseStartDrag.Y >= 20)
      {
      this.Location = new Point(p.X - MouseDownLocation.X, p.Y - MouseDownLocation.Y);
      }
      else if (p.X - MouseStartDrag.X < 0 && p.Y - MouseStartDrag.Y >= 0)
      {
      this.Location = new Point(p.X - MouseDownLocation.X, this.Location.Y);
      }
      else if (p.Y - MouseStartDrag.Y < 0 && p.X - MouseStartDrag.X >= 0)
      {
      this.Location = new Point(this.Location.X, p.Y - MouseDownLocation.Y);
      }
      }
      }
      else//如果两窗体没有相连
      {
      if (f1.Location.X - 20 < this.Location.X + this.Size.Width && this.Location.X + this.Size.Width < f1.Location.X && this.Location.Y + this.Size.Height > f1.Location.Y && this.Location.Y < f1.Location.Y + f1.Size.Height)
      { //当子窗体在主窗体左侧并相距小于20时,子窗体贴向主窗体左侧
      this.Location = new Point(f1.Location.X - this.Size.Width, this.Location.Y);
      MouseStartDrag = Control.MousePosition;//重新设置拖拽起始点
      }
      else if (f1.Location.X + f1.Size.Width + 20 > this.Location.X && this.Location.X > f1.Location.X + f1.Size.Width && this.Location.Y + this.Size.Height > f1.Location.Y && this.Location.Y < f1.Location.Y + f1.Size.Height)
      {
      this.Location = new Point(f1.Location.X + f1.Size.Width, this.Location.Y);
      MouseStartDrag = Control.MousePosition;
      }
      else if (f1.Location.Y - 20 < this.Location.Y + this.Size.Height && this.Location.Y + this.Size.Height < f1.Location.Y && this.Location.X + this.Size.Width > f1.Location.X && this.Location.X < f1.Location.X + f1.Size.Width)
      {
      this.Location = new Point(this.Location.X, f1.Location.Y - this.Size.Height);
      MouseStartDrag = Control.MousePosition;
      }
      else if (f1.Location.Y + f1.Size.Height + 20 > this.Location.Y && this.Location.Y > f1.Location.Y + f1.Size.Height && this.Location.X + this.Size.Width > f1.Location.X && this.Location.X < f1.Location.X + f1.Size.Width)
      {
      this.Location = new Point(this.Location.X, f1.Location.Y + f1.Size.Height);
      MouseStartDrag = Control.MousePosition;
      }
      else if (this.Location.X >= f1.Location.X + f1.Size.Width && this.Location.X < f1.Location.X + f1.Size.Width + 20 && this.Location.Y + this.Size.Height <= f1.Location.Y && this.Location.Y + this.Size.Height + 20 > f1.Location.Y)
      {
      this.Location = new Point(f1.Location.X + f1.Size.Width, f1.Location.Y - this.Size.Height);
      MouseStartDrag = Control.MousePosition;
      }
      else if (this.Location.X >= f1.Location.X + f1.Size.Width && this.Location.X < f1.Location.X + f1.Size.Width + 20 && this.Location.Y >= f1.Location.Y + f1.Size.Height && this.Location.Y < f1.Location.Y + f1.Size.Height + 20)
      {
      this.Location = new Point(f1.Location.X + f1.Size.Width, f1.Location.Y + f1.Size.Height);
      MouseStartDrag = Control.MousePosition;
      }
      else if (this.Location.X + this.Size.Width <= f1.Location.X && this.Location.X + this.Size.Width > f1.Location.X - 20 && this.Location.Y >= f1.Location.Y + f1.Size.Height && this.Location.Y < f1.Location.Y + f1.Size.Height + 20)
      {
      this.Location = new Point(f1.Location.X - this.Size.Width, f1.Location.Y + f1.Size.Height);
      MouseStartDrag = Control.MousePosition;
      }
      else if (this.Location.X + this.Size.Width <= f1.Location.X && this.Location.X + this.Size.Width > f1.Location.X - 20 && this.Location.Y + this.Size.Height <= f1.Location.Y && this.Location.Y + this.Size.Height + 20 > f1.Location.Y)
      {
      this.Location = new Point(f1.Location.X - this.Size.Width, f1.Location.Y - this.Size.Height);
      MouseStartDrag = Control.MousePosition;
      }
      else
      {
      //子窗体自由移动
      this.Location = new Point(p.X - MouseDownLocation.X, p.Y - MouseDownLocation.Y);
      }
      }
      }
      } public void Set_Form2_NewLocation()
      {
      if (f1.Location.X - 20 < this.Location.X + this.Size.Width && this.Location.X + this.Size.Width < f1.Location.X && this.Location.Y + this.Size.Height > f1.Location.Y && this.Location.Y < f1.Location.Y + f1.Size.Height)
      {
      this.Location = new Point(f1.Location.X - this.Size.Width, this.Location.Y);
      f1.Set_Deviation();
      }
      else if (f1.Location.X + f1.Size.Width + 20 > this.Location.X && this.Location.X > f1.Location.X + f1.Size.Width && this.Location.Y + this.Size.Height > f1.Location.Y && this.Location.Y < f1.Location.Y + f1.Size.Height)
      {
      this.Location = new Point(f1.Location.X + f1.Size.Width, this.Location.Y);
      f1.Set_Deviation();
      }
      else if (f1.Location.Y - 20 < this.Location.Y + this.Size.Height && this.Location.Y + this.Size.Height < f1.Location.Y && this.Location.X + this.Size.Width > f1.Location.X && this.Location.X < f1.Location.X + f1.Size.Width)
      {
      this.Location = new Point(this.Location.X, f1.Location.Y - this.Size.Height);
      f1.Set_Deviation();
      }
      else if (f1.Location.Y + f1.Size.Height + 20 > this.Location.Y && this.Location.Y > f1.Location.Y + f1.Size.Height && this.Location.X + this.Size.Width > f1.Location.X && this.Location.X <= f1.Location.X + f1.Size.Width)
      {
      this.Location = new Point(this.Location.X, f1.Location.Y + f1.Size.Height);
      f1.Set_Deviation();
      }
      else if (this.Location.X >= f1.Location.X + f1.Size.Width && this.Location.X < f1.Location.X + f1.Size.Width + 20 && this.Location.Y + this.Size.Height <= f1.Location.Y && this.Location.Y + this.Size.Height + 20 > f1.Location.Y)
      {
      this.Location = new Point(f1.Location.X + f1.Size.Width, f1.Location.Y - this.Size.Height);
      f1.Set_Deviation();
      }
      else if (this.Location.X >= f1.Location.X + f1.Size.Width && this.Location.X < f1.Location.X + f1.Size.Width + 20 && this.Location.Y >= f1.Location.Y + f1.Size.Height && this.Location.Y < f1.Location.Y + f1.Size.Height + 20)
      {
      this.Location = new Point(f1.Location.X + f1.Size.Width, f1.Location.Y + f1.Size.Height);
      f1.Set_Deviation();
      }
      else if (this.Location.X + this.Size.Width <= f1.Location.X && this.Location.X + this.Size.Width > f1.Location.X - 20 && this.Location.Y >= f1.Location.Y + f1.Size.Height && this.Location.Y < f1.Location.Y + f1.Size.Height + 20)
      {
      this.Location = new Point(f1.Location.X - this.Size.Width, f1.Location.Y + f1.Size.Height);
      f1.Set_Deviation();
      }
      else if (this.Location.X + this.Size.Width <= f1.Location.X && this.Location.X + this.Size.Width > f1.Location.X - 20 && this.Location.Y + this.Size.Height <= f1.Location.Y && this.Location.Y + this.Size.Height + 20 > f1.Location.Y)
      {
      this.Location = new Point(f1.Location.X - this.Size.Width, f1.Location.Y - this.Size.Height);
      f1.Set_Deviation();
      }
      }
      }
    }
    1、当切换两窗体的焦点时,子窗体的Location属性为什么会发生变化?
    2、当最小化主窗体后,再恢复主窗体,子窗体却出不来了?
    
    问题1没重现
    问题2,你在Form1的Load中加一句:
    form.Owner = this;//设置owner
    C# code
     
    private void Form1_Load(object sender, EventArgs e)
    {
        //磁性窗体的页面加载事件《下》;
        _x = this.Location.X + this.Width;      //变量_x等于窗体x轴大小;
        _y = this.Location.Y;                   //变量_y等于窗体y轴大小;
        form.Owner = this;//设置owner
        form.Location = new Point(_x, _y); //初始化窗体的大小,相当于自定义;
        form.Show();
        //磁性窗体的页面加载事件《上》;//show出窗体;
    }
     
    View Code

    象QQ那样拖曳到屏幕边上时缩到边上去

    象QQ那样拖曳到屏幕边上时缩到边上去 
    private   void   Form1_MouseLeave(object   sender,   System.EventArgs   e)   { 
    const   int   j   =   5; //   要故意露出在右上的高度 
    
    if   (this.Top   <1)   { //如果当前X   已经是在最顶 
    //   并且Y   也已经在最右边 
    if   (this.Left   > =   Screen.PrimaryScreen.WorkingArea.Width   -   this.Width)   { 
    //   开始往上移 
    while(this.Top   > =   0-this.Height   +   j)   { 
    this.Top   --; 
    } 
    } 
    } 
    } 
    
    private   void   Form1_MouseEnter(object   sender,   System.EventArgs   e)   { 
    if   (this.Top   <0)   { 
    //   这里是移回正常位置的代码你接上面的逆着做就是了 
    } 
    }
    View Code

    用C#实现了一个切换当前活动窗口的功能

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Diagnostics;
    
    namespace testKMPlayerWinForm
    {
        public partial class Form1 : Form
        {
            [System.Runtime.InteropServices.DllImport("user32.dll")]
            public static extern bool SetForegroundWindow(IntPtr hWnd);
    
            System.Diagnostics.Process Proc;
            String pn;
            public Form1()
            {
                InitializeComponent();
    
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                Proc = new System.Diagnostics.Process();
                Proc.StartInfo.FileName = "C:/Program Files/The KMPlayer/KMPlayer.exe";
                Proc.Start();
                pn = Proc.ProcessName;
                System.Threading.Thread.Sleep(1000);
                SendKeys.Send("^u");
            }
    
            private void button2_Click(object sender, EventArgs e)
            {
                System.Diagnostics.Process[] p = System.Diagnostics.Process.GetProcessesByName(pn);
                if (p.Length > 0)
                {
                    SetForegroundWindow(p[0].MainWindowHandle);
                    System.Threading.Thread.Sleep(1000);
                    SendKeys.Send("^u");
                }
                else
                {
                    button1_Click(sender, e);
                }
            }
        }
    }
    View Code
  • 相关阅读:
    MySQL主从复制报错 Errno 1205
    MySQL添加索引优化SQL
    MySQL通过添加索引解决线上数据库服务器压力大问题
    手把手教你搭建MySQL双主MM+keepalived高可用架构
    SQLSERVER 维护计划无法删除
    Form表单中Post与Get方法的区别
    ASP.NET MVC中常用的ActionResult类型
    Web安全相关(五):SQL注入(SQL Injection)
    Web安全相关(四):过多发布(Over Posting)
    Web安全相关(三):开放重定向(Open Redirection)
  • 原文地址:https://www.cnblogs.com/blogpro/p/11458257.html
Copyright © 2020-2023  润新知