定时器
资源中,所有windows窗体内,可以找到定时器控件,将控件加入到窗体。在定时器控件的属性设置中,Name代表定时器的名字(如timer1),Interval代表定时器的间隔时间(1000代表1s),在属性的事件里,可以设置定时器响应函数的名字,并且回车生成函数,也可以直接点击定时器,生成并进入函数。使用时,timer1.Start();开启定时器,timer1.Stop();取消定时器。
屏幕大小
//获取工作区大小
Rectangle rect = new Rectangle();
rect = Screen.GetWorkingArea(this);
///获取整个屏幕大小,如果需要全屏,设置ResizeMode为NoResize;
Rectangle rect = System.Windows.Forms.SystemInformation.VirtualScreen;
控件大小/位置
this.**.Left = 0;
this.**.Top = 0;
this.**.Width = 1000;
this.**.Height = 500;
消息
- 截获消息
protected override void DefWndProc(ref Message m)
{
switch (m.Msg)
{
case 0x0400 + 1:
break;
default:
base.DefWndProc(ref m);
break;
}
}
-
鼠标消息
string s = string.Format(“{0},{1}”, e.X, e.Y); -
键盘消息
if (e.KeyCode == Keys.A && e.Control){ 同时按下control和a }
Keys类定义了所有的按键,对于control,alt这类的键,只能用e.Control这种布尔值进行判断,不能用KeyCode和Keys进行比较。
MDI
主窗口Name属性为默认的Form1
1)设置主窗口IsMdiContain属性为true
2)创建新的winform窗口(form2),在代码中修改构造函数
public Form2(Form1 parent)
{
InitializeComponent();
this.MdiParent = parent;
}
3)使用
Form2 child = new Form2(this);
child.Show();
限制输入数字
lvds1.PreviewTextInput += new TextCompositionEventHandler(EditPreviewTextInput);
void EditPreviewTextInput(object sender, TextCompositionEventArgs e)
{
e.Handled = false;
if (!isNumberic(e.Text)) e.Handled = true;
}
public bool isNumberic(string _string)
{
if (string.IsNullOrEmpty(_string))
return false;
foreach (char c in _string)
{
if (!char.IsDigit(c))
return false;
}
return true;
}