本文讲述了c# winform 开发常用代码!具有很好的参考价值,希望对大家有所帮助。一起跟随六星小编过来看看吧,具体如下:
因为公司业务需要,做了一个客户端,时间比较紧迫,我尽量使用简单技术来实现,毕竟暂时没有了解其他的技术,不敢随便乱用,万一出现一个难以解决的问题,又得推倒重来。所以为了尽快实现,也为了提高性能,我只用到了很基础的一些控件
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
//不显示默认的标题栏
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
}
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();
}
}
private void App_Min(object sender, EventArgs e)
{
this.WindowState = FormWindowState.Minimized;
}
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;
}
}
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);
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);
}
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电子书、教程、项目接单、源码等等
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!