C#中Load等常见方法的介绍

来源:互联网 发布:mac os 启动慢 编辑:程序博客网 时间:2024/05/20 00:12

窗体常见方法的说明

案例一

随机更换主界面背景

目的:每次打开窗体时,相应窗体的主界面不同。

代码:

        private void Form1_Load(object sender, EventArgs e)

        {

            Random random = new Random();

            int i = random.Next(imageList1.Images.Count);

            this.BackgroundImage=imageList1.Images[i];

         }

 

案例二如何创建图形皮肤窗体

目的:可以根据自己的要求选择图片做窗体的背景

       //皮肤一

        private void button1_Click(object sender, EventArgs e)

        {

            this.BackgroundImage = Example.Properties.Resources.Show1;

        }

        //皮肤二

        private void button2_Click(object sender, EventArgs e)

        {

            this.BackgroundImage = Example.Properties.Resources.Show2;

        }

案例三如何自动闪烁应用程序窗体

目的:窗体可以根据设置的时间频率进行闪烁

主要介绍了如何使用Windows API函数FlashWindows(),参数说明:HWND:表示闪烁窗体句柄

      BOOL: 表示闪烁状态

WINDOWS系统中,系统API函数是标准C语言的方式提供,主要放在DLL中,在.NET中,系统调用C语言的API函数,要使用命名空间System.Runtime.InteropServices,如果要使用API函数,要做三步:

l      命名空间

l      导入动态库

l      申明public static extern bool FlashWindow(IntPtr hWnd,bool bInvert);

l      使用       

        [DllImport("User32")]

        public static extern bool FlashWindow(IntPtr hWnd,bool bInvert);        

        //开始闪烁

        private void button1_Click(object sender, EventArgs e)

        {

            int MyCount,MyTimes,MyTime;

            try

            {

               MyTimes=System.Convert.ToInt16(this.textBox1.Text);

               MyTime=System.Convert.ToInt16(this.textBox2.Text);

               for(MyCount=0;MyCount<MyTimes;MyCount++)

               {

                   FlashWindow(this.Handle,true);

                   System.Threading.Thread.Sleep(MyTime);

               }

            }

            catch(Exception MyEx)

            {

                System.Windows.Forms.MessageBox.Show(MyEx.Message,"信息提示",

MessageBoxButtons.OK,MessageBoxIcon.Information);

            }

        }

 

原创粉丝点击