C#开发winform小知识点

灰太狼 2022-04-08 08:19 449阅读 0赞

main函数

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

  1. [STAThread]
  2. static void Main()
  3. {
  4. Application.EnableVisualStyles();
  5. Application.SetCompatibleTextRenderingDefault(false);
  6. }

弹出新窗口

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

  1. Application.Run(new TestForm());

窗体这间传递参数

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

  1. public Alert(string contentStr, string titleStr)
  2. {
  3. this.contentStr = contentStr;
  4. if (!string.IsNullOrEmpty(titleStr)) this.titleStr = titleStr;
  5. InitializeComponent();
  6. }

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

子窗体向父窗体回传参数

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

  1. SearchInfoForm searchInfo = new SearchInfoForm();
  2. if(searchInfo.ShowDialog() == DialogResult.OK)
  3. {
  4. //确定
  5. }else if if(searchInfo.ShowDialog() == DialogResult.Cancel)
  6. {
  7. //取消
  8. }

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

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

c# winForm 禁止缩放

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

继承

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

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

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

事件

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

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

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

定时器

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

创建线程

  1. //创建线程
  2. public static void StartNewThread()
  3. {
  4. //middle方法名
  5. // thread.Name:指定线程名
  6. ThreadStart ts = new ThreadStart(middle);
  7. Thread thread = new Thread(ts);
  8. thread.Name = "middle";
  9. thread.Start();
  10. }
  11. private void middle()
  12. {
  13. //dosomthing
  14. }

去掉winForm的右上角关闭按钮

方法一

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

  1. this.ControlBox = false;

完成代码

  1. public DownLoadingForm()
  2. {
  3. InitializeComponent();
  4. this.ControlBox = false;
  5. }

方法二

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

整个抬头按钮都取掉了。

代码指定BackgroundImage和Icon

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

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

  1. /// <summary>
  2. /// 获取Assembly的运行路径
  3. /// </summary>
  4. ///<returns></returns>
  5. public static string GetAssemblyPath()
  6. {
  7. string _CodeBase = System.Reflection.Assembly.GetExecutingAssembly().CodeBase;
  8. _CodeBase = _CodeBase.Substring(8, _CodeBase.Length - 8); // 8是file:// 的长度
  9. string[] arrSection = _CodeBase.Split(new char[] { '/' });
  10. string _FolderPath = "";
  11. for (int i = 0; i < arrSection.Length - 1; i++)
  12. {
  13. _FolderPath += arrSection[i] + "/";
  14. }
  15. return _FolderPath;
  16. }

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

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

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

键盘事件

参考C#键盘事件处理

  1. private void FrmMain_Load(object sender, EventArgs e)
  2. {
  3. this.KeyPreview = true;//获取或设置一个值,该值指示在将键事件传递到具有焦点的控件前,窗体是否将接收此键事件。
  4. }
  5. private void FrmMain_KeyUp(object sender, KeyEventArgs e)
  6. {
  7. if (Keys.F1 == e.KeyCode)
  8. {
  9. //MessageBox.Show("您所按动的键是:" + e.KeyCode.ToString());
  10. Help.ShowHelp(this,@"C:\Users\HongYe\Desktop\RevitAPI.chm");
  11. }
  12. }

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

发表评论

表情:
评论列表 (有 0 条评论,449人围观)

还没有评论,来说两句吧...

相关阅读

    相关 c++知识

    1.在C++中,每个程序必须包含一个main()的入口函数,只有这样,这个项目才能运行. 2.函数包括:函数名,参数,代码,返回值. 3.return 是C++的预定义语句

    相关 c语言知识

    1,%p表示输出指针变量的值,也就是内存地址 2,指针的话本身大小是定死的,只占用4个字节,无论它指向的变量占几个字节 3,如果在方法间传递参数,那必须要传递地址而非值

    相关 知识

    进制转换: 二进制转八进制: 从小数点开始向左为整数部分,向右为小数部分 “三位一体” ,不足补0 例如:10001001. 1011 划分完:010  001  0