page contents

c# winform 开发常用代码

本文讲述了c# winform 开发常用代码!具有很好的参考价值,希望对大家有所帮助。一起跟随六星小编过来看看吧,具体如下:

attachments-2022-10-QhboBo1k634771d6a3d13.png本文讲述了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需要大局观,能够设计一个很合理,能够复用,达到更高效率的漂亮的系统,那才叫实力。 也可以理解为系统架构师,当然我目前还没达到这种层次,不过人总得有点梦想,有点追求。 

更多相关技术内容咨询欢迎前往并持续关注六星社区了解详情。

长按或扫描下方二维码,免费获取 Python公开课和大佬打包整理的几百G的学习资料,内容包含但不限于Python电子书、教程、项目接单、源码等等

  

attachments-2022-10-kwwbZ9WG6347756cbf77c.jpg

  • 发表于 2022-10-13 10:03
  • 阅读 ( 561 )
  • 分类:C/C++开发

0 条评论

请先 登录 后评论
轩辕小不懂
轩辕小不懂

2403 篇文章

作家榜 »

  1. 轩辕小不懂 2403 文章
  2. Pack 1131 文章
  3. 小柒 1026 文章
  4. Nen 576 文章
  5. 王昭君 209 文章
  6. 文双 71 文章
  7. 小威 64 文章
  8. Cara 36 文章