窗体切换特效

2021-02-04 18:16

阅读:587

标签:HERE   method   check   builder   path   插入   一行代码   rri   支持   

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不支持这个方法)
View Code

C#桌面磁性窗体

技术图片技术图片
转自:http://kb.cnblogs.com/a/1547320/
 
看到千千静听的窗口可以在接近屏幕边缘时贴在边缘上觉得不错,自己也有这个需要,所以写了这个方法,测试了感觉还蛮不错的,哈哈~
使用的时候只要在想应用的窗体的Form_Move(object sender,EventAges e)事件里面调用即可
ps:不过有时窗体可能会比较闪,这个可能是代码还有待改善,或者是在Form_Move事件里面来调用不大合适,反正功能是实现了,要是哪位有更好的方法,欢迎回复交流一下啊~ 
/// 
/// 磁性窗体函数
/// 
/// 窗体控件(一般传this即可)
/// 自定义的与屏幕边缘的距离
/// 是否在屏幕工作区进行该操作(true表示不包括任务栏,false则包括整个屏幕的范围)
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  0) || (Math.Abs(x) 0))  //Math.Abs(x)是取绝对值
    {
        form.Location = new Point(0, y);
    }

    //如果窗体的上边缘和屏幕上边缘的距离在用户定义的范围内,则执行上贴边
    if ((y  0) || (Math.Abs(y) 0))
    {
        form.Location = new Point(x, 0);
    }


    //窗体右边缘跟屏幕右边缘的距离
    int rightW = sW - form.Right;
    //窗体下边缘跟屏幕下边缘的距离
    int bottomW = sH - form.Bottom;

    //判断右边的情况
    if ((rightW 0))
    {
        form.Location = new Point(sW - form.Width, y);
    }
    //判断下边的情况
    if ((bottomW 10 && form.Bottom 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)
        {


评论


亲,登录后才可以留言!