C#开发winform小知识点

main函数

一个WinForm窗体应用只能有一个Main函数,这也就是程序的入口函数。

 [STAThread]
 static void Main()
  {
      Application.EnableVisualStyles();
      Application.SetCompatibleTextRenderingDefault(false);
      
   }

弹出新窗口

Applicationrun方法可以启动一个UI线程,弹出新窗体,比如

Application.Run(new TestForm());

窗体这间传递参数

窗体类可以当作普通类,可以在构造函数里传递参数。
比如某个弹出框Alert.cs构造函数如下:

public Alert(string contentStr, string titleStr)
 {
      this.contentStr = contentStr;
      if (!string.IsNullOrEmpty(titleStr)) this.titleStr = titleStr;
      InitializeComponent();
  }

那么可以new Alert(“确定删除?”,“提示”);这样传递参数

子窗体向父窗体回传参数

当子窗体使用ShowDialog()弹出时,线程会阻塞,直到子窗体关闭.在子窗体里设置this.DialogResult = DialogResult.OK;后窗体就会关闭,那父窗体就能通过子窗口实例拿到DialogResult。比如

SearchInfoForm searchInfo = new SearchInfoForm();
 if(searchInfo.ShowDialog() == DialogResult.OK)
  {
      //确定
  }else if if(searchInfo.ShowDialog() == DialogResult.Cancel)
  {
      //取消
  }

子窗体向父窗体回传参数二

上面的方法只要适用弹出Confirm,单击确定和取消不同的逻辑。如果想取其它数据,就要用到c#的ref特性了。
ref关键字用于将方法内的变量改变后带出方法外。

c# winForm 禁止缩放

在Form类下面有一个FormBorderStyle的字段,我们可以通过设置它的值来让窗体不能被拉大拉小。FormBorderStyle的值设置为FormBorderStyle.FixedSingle或Fixed3D时,窗体大小是不能被改变的。
当然,还有一种情况,我们也应该要考虑到,那就是窗体最大化。所以,我们要将窗体最大化的功能去掉,即this.MaximizeBox = false;

继承

派生类对象允许转换为基类对象;但是不允许基类对象转换为派生类对象。

反射获得对象的属性名和属性值


public void LocalStorageVo(object sender)
{
   Type t = sender.GetType();//获得该类的Type
     //再用Type.GetProperties获得PropertyInfo[],然后就可以用foreach 遍历了
   foreach (PropertyInfo pi in t.GetProperties())
   {
       object value = pi.GetValue(sender, null);//用pi.GetValue获得值
       if(null == value)
       {
           value = new object();
       }
       //获得属性的名字,后面就可以根据名字判断来进行些自己想要的操作
       string name = pi.Name;
       this.Write(name, value.ToString());
   }
}

事件

组件支持的事件都在如下图上
在这里插入图片描述

我们可以在右边直接写事件监听器,会自动添加事件方法。
如果想手动添加,则如下

this.txtUserName.Leave += new EventHandler(txtUserName_Leave);  //失去焦点事件。

定时器

using Timer = System.Timers.Timer;


private void addTime(){
	Timer aTimer= new Timer();
	aTimer.Elapsed += new System.Timers.ElapsedEventHandler(stepAssembly);
	// 设置引发时间的时间间隔 此处设置为1秒
	aTimer.Interval = 1000;
	aTimer.Enabled = true;
}

创建线程

//创建线程
public static void StartNewThread()
{
     //middle方法名
     // thread.Name:指定线程名
     ThreadStart ts = new ThreadStart(middle);
      Thread thread = new Thread(ts);
      thread.Name = "middle";
      thread.Start();
 }

 private void middle()
 {
//dosomthing

 }

去掉winForm的右上角关闭按钮

方法一

在组件的构造函数里放如下代码

this.ControlBox = false;

完成代码

public DownLoadingForm()
{
     InitializeComponent();
     this.ControlBox = false;
 }

方法二

使用组件在FormBoderstyle属性将它设为none。结果如下
在这里插入图片描述
效果如图
在这里插入图片描述

整个抬头按钮都取掉了。

代码指定BackgroundImage和Icon

//指定form窗体的Icon和BackgroundImage 
string myPath = FileUtil.GetAssemblyPath();
form.Icon = Icon.ExtractAssociatedIcon(myPath + "icon/favicon.ico");
form.BackgroundImage = Image.FromFile(myPath + "img/background.jpg");

FileUtil.GetAssemblyPath()是获得dll绝对路径代码

/// <summary>
        /// 获取Assembly的运行路径
        /// </summary>
        ///<returns></returns>
        public static string GetAssemblyPath()
        {
            string _CodeBase = System.Reflection.Assembly.GetExecutingAssembly().CodeBase;

            _CodeBase = _CodeBase.Substring(8, _CodeBase.Length - 8);    // 8是file:// 的长度

            string[] arrSection = _CodeBase.Split(new char[] { '/' });

            string _FolderPath = "";
            for (int i = 0; i < arrSection.Length - 1; i++)
            {
                _FolderPath += arrSection[i] + "/";
            }

            return _FolderPath;
        }

路径相关的操作System.IO.Path类

在C#中/是特殊字符,要表示它的话需要使用\。由于这种写法不方便,C#语言提供了@对其简化。只要在字符串前加上@即可直接使用</kbd>。

所以上面的路径在C#中应该表示为“Book”,@“\Tmp\Book”,@“C:\Tmp\Book”
但如果使用Path,操作起来会非常方便且正常。
比如合并2个路径,输出一个标准格式的路径,Path.Combine(string path1, string path2);这将非常有用。

键盘事件

参考C#键盘事件处理

private void FrmMain_Load(object sender, EventArgs e)
{
    this.KeyPreview = true;//获取或设置一个值,该值指示在将键事件传递到具有焦点的控件前,窗体是否将接收此键事件。
}

private void FrmMain_KeyUp(object sender, KeyEventArgs e)
{
    if (Keys.F1 == e.KeyCode)
    {
        //MessageBox.Show("您所按动的键是:" + e.KeyCode.ToString());
        Help.ShowHelp(this,@"C:\Users\HongYe\Desktop\RevitAPI.chm");
    }
}

--------------------------------------未完待续--------------------------------------------------------

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值