C#学习笔记(1)

C# 学习过程中随笔记录, 留待后续查询

EX001 设置窗口最大最小值

设置完 FormLoad 事件的响应之后, 通过控制窗口的两个属性, 控制窗口的最大和最小

1
2
3
4
5
6
private void Form1_Load(Object sender, EventArgs e)
{
this.MaximumSize = new Size(400, 400);

this.MinimumSize = new Size(300, 300);
}

EX002 设置窗口指定(Z序)

窗口的 TopMost 为 窗口置顶

1
2
3
4
5
//非属性, 代码也可实现
this.TopMost = true;

//此代码无法实现置顶, 设置为False 之后 窗口不可显
this.SetTopLevel(false);

EX003 动态创建窗口, 设置窗口位置

创建新窗口 为

1
2
Form2 frm = new Form2();
frm.Show();

但此窗口为非模态窗口, 为一个新窗口, 为新窗口类使用
在创建新窗口时, 可以指定窗口的显示位置

1
2
3
4
5
6
7
8
frm.StartPosition = FormStartPosition.CenterScreen;

// StartPosition 属性设置为 Manual时, Location 可以设置为具体的像素(X,Y)位置

// 设置窗口透明度
this.Opacity = 1.0f;
// 设置窗口背景颜色
this.BackColor = Color.LightPink;

EX006 控件在不同窗口移动

每个窗口都有自己的类, 则所有关于窗口的操作事件,由自己的类分管,
给窗口添加控件时,只需调用方法 this.Controls.Add(objControl);即可
给窗口移除控件时,只需调用方法 this.Controls.Remove(objControl);即可
问题: 是否相当于在对方类 拷贝构造一个对象? 还是给当前对象传递过去?

每一个可视控件都有所有者和Parent属性, 所有者是控件创建时指定的所属对象

EX007 窗口标题对齐

设置窗口标题的字体对齐
Form.RightToLeftYes或者No 区分 左对齐或右对齐

EX008 无边框相关

ControlBox 为设置窗口是否有边框
this.ControlBox = this.ControlBox == true ? false : true;
Tips: 设置为无边框时,不能有标题, 否则仅仅是隐藏了三个窗口控制按钮

EX009 - 无边框移动

设置窗口为无边框状态, 但此时要自行实现鼠标的 按下 移动 弹起 离开事件,
方能实现无边框窗口移动
this.FormBorderStyle = FormBorderStyle.None;

按下 记录当前鼠标位置 MousePosition 标记开始移动窗口
移动 判断是否进入移动模式 计算移动距离 调整窗口的 位置
弹起与离开 重置标记量 重置鼠标位置
Tips: 应仅相应 左键, 判断鼠标事件
MouseEventArgs e.Button == MouseButtons.Left

EX012 - 窗口变化,同步控件属性

设置窗口时, 控件同步设置大小

EX013 - 设置窗口背景为透明, 则不规则窗口

先设置窗口为无边框, 然后设置 窗口的什么颜色为去除状态

1
2
this.FormBorderStyle = FormBorderStyle.None;
this.TransparencyKey = Color.White;

EX014 - 带分割栏的窗口

通过SplitContainer控件实现

EX015 定时器

设置属性 或者生成对象

1
2
3
4
5
private System.Windows.Forms.Timer timer1;
this.timer1.Interval = 200;
this.components = new System.ComponentModel.Container();
this.timer1 = new System.Windows.Forms.Timer(this.components);
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);

EX016 导入User32的API

使用系统库, 然后倒入API 使用时转换为对应的数据即可

1
2
3
using System.Runtime.InteropServices;
[DllImport("user32")]
private static ## EXtern long FlashWindow(IntPtr handle, bool bInvert);

EX017 动画显示窗口

通过 Win32API: AnimateWindow 实现动画效果

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
using System.Runtime.InteropServices;

public partial class NativeConstants
{

[DllImport("user32")]
public static ## EXtern bool AnimateWindow(System.IntPtr hWnd, uint dwTime, uint dwFlags);

public const int AW_HOR_POSITIVE = 0x00000001;
public const int AW_HOR_NEGATIVE = 0x00000002;
public const int AW_VER_POSITIVE = 0x00000004;
public const int AW_VER_NEGATIVE = 0x00000008;
public const int AW_CENTER = 0x00000010;
public const int AW_HIDE = 0x00010000;
public const int AW_ACTIVATE = 0x00020000;
public const int AW_SLIDE = 0x00040000;
public const int AW_BLEND = 0x00080000;
}

// Form_Closed
NativeConstants.AnimateWindow(this.Handle, 500,
NativeConstants.AW_CENTER | NativeConstants.AW_HIDE);

// Form_Load
NativeConstants.AnimateWindow(this.Handle, 500,
NativeConstants.AW_CENTER );
文章目录
  1. 1. EX001 设置窗口最大最小值
  2. 2. EX002 设置窗口指定(Z序)
  3. 3. EX003 动态创建窗口, 设置窗口位置
  4. 4. EX006 控件在不同窗口移动
  5. 5. EX007 窗口标题对齐
  6. 6. EX008 无边框相关
  7. 7. EX009 - 无边框移动
  8. 8. EX012 - 窗口变化,同步控件属性
  9. 9. EX013 - 设置窗口背景为透明, 则不规则窗口
  10. 10. EX014 - 带分割栏的窗口
  11. 11. EX015 定时器
  12. 12. EX016 导入User32的API
  13. 13. EX017 动画显示窗口
|