C#的winform开发常用代码

因为公司业务需要,做了一个客户端,时间比较紧迫,我尽量使用简单技术来实现,毕竟暂时没有了解其他的技术,不敢随便乱用,万一出现一个难以解决的问题,又得推倒重来。所以为了尽快实现,也为了提高性能,我只用到了很基础的一些控件

1.不显示窗体标题栏

private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            //不显示默认的标题栏
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
             
        }

2.关闭窗口

 public void App_Close(object sender, EventArgs e)
        {
            DialogResult result = MessageBox.Show("确定要退出系统吗?", "退出系统", MessageBoxButtons.OKCancel);
            if (result == DialogResult.OK)
            {
                Environment.Exit(0);
                System.Diagnostics.Process.GetCurrentProcess().Kill();
            }


        }

3.最小化窗口

   private void App_Min(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Minimized;
        }

4.最大化窗口

   private void MaxWindow() {


            if (!maxable)
            {

                //最大化之后,不遮盖任务栏
                this.MaximumSize = new Size(Screen.PrimaryScreen.WorkingArea.Width, Screen.PrimaryScreen.WorkingArea.Height);
                this.WindowState = FormWindowState.Maximized;
                maxable = true;
            }
            else
            {
                this.WindowState = FormWindowState.Normal;
                maxable = false;
            }
        }

5.窗口移动


  private void App_MouseDown(object sender, MouseEventArgs e)
        {
            this._point.X = e.X;
            this._point.Y = e.Y;


        }


        private void App_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                Point p = MousePosition;
                p.Offset(-this._point.X, -this._point.Y);
                this.Location = p;
            }
        }

 this.toolbar_panel.MouseDown += new System.Windows.Forms.MouseEventHandler(App_MouseDown);
                this.toolbar_panel.MouseMove += new System.Windows.Forms.MouseEventHandler(App_MouseMove);

6.窗口居中

 public void ResetLocation()
        {
           
            Rectangle ScreenBox = System.Windows.Forms.Screen.GetWorkingArea(this);
            screenWidth = ScreenBox.Width;
            screenHeight = ScreenBox.Height;
            int winWidth = ScreenBox.Width / 2;


            int winHeight = ScreenBox.Height / 2;


            if (this.form_width > 0)
            {
                winWidth = this.form_width;
            } 


            if (this.form_height > 0)
            {
                winHeight = this.form_height; 
            } 
            this.Size = new System.Drawing.Size(winWidth, winHeight);
            // this.StartPosition = System.Windows.Forms.FormStartPosition.WindowsDefaultLocation;


            this.Location = new Point((ScreenBox.Width - winWidth) / 2, (ScreenBox.Height - winHeight) / 2);
        }

7.判断是否联网

try {

                    Ping p = new Ping();
                    PingReply pr = null;
                    //ping百度
                    pr = p.Send("119.75.218.45");
                    if (pr.Status != IPStatus.Success)
                    {
                        //未联网
                        state = -1;
                    }
                    else
                    {
                        //已联网
                        state = 1;
                    }
                }
                catch (Exception ex) { }
                finally { }


基本上上面这些就已经够了。代码只是为了实现功能,真正体现能力的,是如何组织代码,就像打dota需要大局观,能够设计一个很合理,能够复用,达到更高效率的漂亮的系统,那才叫实力。 也可以理解为系统架构师,当然我目前还没达到这种层次,不过人总得有点梦想,有点追求。 

希望上面这些能够对大家有所帮助,记录下这点点滴滴,以后再看也许会有更多的感悟,希望大家能够喜欢,如果有什么错误,或者有更好的改进意见,请留下您的宝贵意见,希望大牛多指点。感谢大家的支持!



    

你可能感兴趣的:(C#的winform开发常用代码)