1 重写Form关闭按钮在
protected override void WndProc(ref Message m
){
const int WM_SYSCOMMAND
= 0x0112;
const int SC_CLOSE
= 0xF060;
if (m
.Msg
== WM_SYSCOMMAND
&& (int)m
.WParam
== SC_CLOSE
)
{
Application
.ExitThread();
return;
}
base.WndProc(ref m
);
}
2 退出系统
Application
.Exit();
this.Close();
Application
.ExitThread();
Environment
.Exit(0);
Process
.GetCurrentProcess().Kill();
3 路径
Winform获取应用程序的当前路径的方法集合,具体如下,值得收藏
string str
= this.GetType().Assembly
.Location
;
result
: X
:\xxx\xxx\xxx
.exe
(.exe文件所在的目录
+.exe文件名
)
string str
= System
.Diagnostics
.Process
.GetCurrentProcess().MainModule
.FileName
;
result
: X
:\xxx\xxx\xxx
.exe
(.exe文件所在的目录
+.exe文件名
)
string str
= System
.Environment
.CurrentDirectory
;
result
: X
:\xxx\xxx
(.exe文件所在的目录
)
string str
= System
.AppDomain
.CurrentDomain
.BaseDirectory
;
result
: X
:\xxx\xxx\
(.exe文件所在的目录
+"\"
)
string str
= System
.AppDomain
.CurrentDomain
.SetupInformation
.ApplicationBase
;
result
: X
:\xxx\xxx\
(.exe文件所在的目录
+"\"
)
string str
= System
.Windows
.Forms
.Application
.StartupPath
;
result
: X
:\xxx\xxx
(.exe文件所在的目录
)
string str
= System
.Windows
.Forms
.Application
.ExecutablePath
;
result
: X
:\xxx\xxx\xxx
.exe
(.exe文件所在的目录
+.exe文件名
)
string str
= System
.IO
.Directory
.GetCurrentDirectory();
result
: X
:\xxx\xxx
(.exe文件所在的目录
)
4 打开文件对话框
OpenFileDialog ofd
= new OpenFileDialog();
ofd
.Filter
= "Excel文件.xls;*.xlsx)|*.xls;*.xlsx|所有文件|*.*";
ofd
.ValidateNames
= true;
ofd
.CheckPathExists
= true;
ofd
.CheckFileExists
= true;
if (ofd
.ShowDialog() == DialogResult
.OK
)
{
string strFileName
= ofd
.FileName
;
}
5 保存文件对话框
SaveFileDialog sfd
= new SaveFileDialog();
sfd
.Filter
= "数据库备份文件(*.bak)|*.bak|数据文件(*.mdf)|*.mdf|日志文件(*.ldf)|*.ldf";
sfd
.FilterIndex
= 1;
sfd
.RestoreDirectory
= true;
sfd
.DefaultFileName
= "YourFileName";
if (sfd
.ShowDialog() == DialogResult
.OK
)
{
string localFilePath
= sfd
.FileName
.ToString();
string fileNameExt
= localFilePath
.Substring(localFilePath
.LastIndexOf("\\") + 1);
}
6 无边框移动
1.定义一个位置信息Point用于存储鼠标位置
private Point mPoint
;
2.给窗体等控件增加MouseDown和MouseMove事件
private void panel1_MouseDown(object sender
, MouseEventArgs e
)
{
mPoint
= new Point(e
.X
, e
.Y
);
}
private void panel1_MouseMove(object sender
, MouseEventArgs e
)
{
if (e
.Button
== MouseButtons
.Left
)
{
this.Location
= new Point(this.Location
.X
+ e
.X
- mPoint
.X
, this.Location
.Y
+ e
.Y
- mPoint
.Y
);
}
}
7 拖动窗体改变大小/双击最大化(无边框窗体)
[DllImport("user32.dll")]
public static extern bool ReleaseCapture();
[DllImport("user32.dll")]
public static extern bool SendMessage(IntPtr hwnd
, int wMsg
, int wParam
, int lParam
);
public const int WM_SYSCOMMAND
= 0x0112;
public const int SC_MOVE
= 0xF010;
public const int HTCAPTION
= 0x0002;
public const int SC_MINIMIZE
= 0xF020;
public const int SC_MAXIMIZE
= 0xF030;
public const int SC_RESTORE
= 0xF120;
public const int SC_SIZE
= 0xF000;
public const int LEFT
= 0x0001;
public const int RIGHT
= 0x0002;
public const int UP
= 0x0003;
public const int LEFTUP
= 0x0004;
public const int RIGHTUP
= 0x0005;
public const int BOTTOM
= 0x0006;
public const int LEFTBOTTOM
= 0x0007;
public const int RIGHTBOTTOM
= 0x0008;
private void panel2_MouseDown(object sender
, MouseEventArgs e
)
{
if (e
.Clicks
<= 1)
{
ReleaseCapture();
SendMessage(this.Handle
, WM_SYSCOMMAND
, SC_MOVE
+ HTCAPTION
, 0);
}
else if (e
.Clicks
== 2)
{
if (this.WindowState
== FormWindowState
.Maximized
)
{
ReleaseCapture();
SendMessage(this.Handle
, WM_SYSCOMMAND
, SC_RESTORE
, 0);
}
else if (this.WindowState
== FormWindowState
.Normal
)
{
ReleaseCapture();
SendMessage(this.Handle
, WM_SYSCOMMAND
, SC_MAXIMIZE
, 0);
}
}
}
private void pictureBox4_MouseDown(object sender
, MouseEventArgs e
)
{
if (e
.Clicks
<= 1)
{
this.Cursor
= Cursors
.SizeNWSE
;
ReleaseCapture();
SendMessage(this.Handle
, WM_SYSCOMMAND
, SC_SIZE
+ RIGHTBOTTOM
, 0);
}
}
8 窗体自适应
自适应类
using System
.Collections
.Generic
;
using System
.Windows
.Forms
;
namespace WindowsFormsApplication1
{
class AutoSizeFormClass
{
public struct controlRect
{
public int Left
;
public int Top
;
public int Width
;
public int Height
;
}
public List
<controlRect
> oldCtrl
= new List<controlRect>();
int ctrlNo
= 0;
public void controllInitializeSize(Control mForm
)
{
controlRect cR
;
cR
.Left
= mForm
.Left
; cR
.Top
= mForm
.Top
; cR
.Width
= mForm
.Width
; cR
.Height
= mForm
.Height
;
oldCtrl
.Add(cR
);
AddControl(mForm
);
}
private void AddControl(Control ctl
)
{
foreach (Control c
in ctl
.Controls
)
{
controlRect objCtrl
;
objCtrl
.Left
= c
.Left
; objCtrl
.Top
= c
.Top
; objCtrl
.Width
= c
.Width
; objCtrl
.Height
= c
.Height
;
oldCtrl
.Add(objCtrl
);
if (c
.Controls
.Count
> 0)
AddControl(c
);
}
}
public void controlAutoSize(Control mForm
)
{
if (ctrlNo
== 0)
{
controlRect cR
;
cR
.Left
= 0; cR
.Top
= 0; cR
.Width
= mForm
.PreferredSize
.Width
; cR
.Height
= mForm
.PreferredSize
.Height
;
oldCtrl
.Add(cR
);
AddControl(mForm
);
}
float wScale
= (float)mForm
.Width
/ (float)oldCtrl
[0].Width
;
float hScale
= (float)mForm
.Height
/ (float)oldCtrl
[0].Height
;
ctrlNo
= 1;
AutoScaleControl(mForm
, wScale
, hScale
);
}
private void AutoScaleControl(Control ctl
, float wScale
, float hScale
)
{
int ctrLeft0
, ctrTop0
, ctrWidth0
, ctrHeight0
;
foreach (Control c
in ctl
.Controls
)
{
ctrLeft0
= oldCtrl
[ctrlNo
].Left
;
ctrTop0
= oldCtrl
[ctrlNo
].Top
;
ctrWidth0
= oldCtrl
[ctrlNo
].Width
;
ctrHeight0
= oldCtrl
[ctrlNo
].Height
;
c
.Left
= (int)((ctrLeft0
) * wScale
);
c
.Top
= (int)((ctrTop0
) * hScale
);
c
.Width
= (int)(ctrWidth0
* wScale
);
c
.Height
= (int)(ctrHeight0
* hScale
);
ctrlNo
++;
if (c
.Controls
.Count
> 0)
AutoScaleControl(c
, wScale
, hScale
);
}
}
}
}
自适应
1.声明自适应类实例
AutoSizeFormClass asc
= new AutoSizeFormClass();
2,在初始化的时候
asc
.controllInitializeSize(this);
3,在SizeChanged事件中
asc
.controlAutoSize(this);
9 窗体快捷键的使用
1.响应 Form窗体的 KeyDown 事件
需要设置Form窗体的 KeyPreview 属性为 true (默认false)
private void MainForm_KeyDown(object sender
, KeyEventArgs e
)
{
switch (e
.KeyCode
)
{
case Keys
.F1
:
break;
case Keys
.Escape
:
break;
case Keys
.O
:
if (e
.Control
)
{
}
break;
case Keys
.Oemcomma
:
break;
case Keys
.OemPeriod
:
break;
case Keys
.Oem1
:
break;
case Keys
.Oem7
:
break;
case Keys
.Up
:
return true;
case Keys
.Down
:
return true;
}
2.重载 ProcessCmdKey 方法
protected override bool ProcessCmdKey(ref Message msg
, Keys keyData
)
{
switch (keyData
)
{
case Keys
.Up
:
return true;
case Keys
.Down
:
return true;
}
return false;
}
10.文件相关操作
创建文件夹
if (!Directory
.Exists("文件夹名称"))
{
Directory
.CreateDirectory("文件夹名称");
}