小5聊Winform窗体开发基础知识积累
Posted 小5聊
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了小5聊Winform窗体开发基础知识积累相关的知识,希望对你有一定的参考价值。
整理经常用到的一些功能知识点
1、去掉顶部工具栏
比如:最小化、最大化、以及关闭按钮
- 效果
- 代码
this.FormBorderStyle= FormBorderStyle.None
2、无边框自定义移动窗体
- 代码
#region 移动窗体
bool mouseDownFlag = false;
int valueX = 0;
int valueY = 0;
Point currentFormPoint = new Point(); //坐标点 - 窗体对象
Point currentMousePoint = new Point(); //坐标点 - 鼠标对象
private void Share_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
mouseDownFlag = true;
currentFormPoint = this.Location;
currentMousePoint = Control.MousePosition;
}
}
private void Share_MouseUp(object sender, MouseEventArgs e)
{
mouseDownFlag = false;
}
private void Share_MouseMove(object sender, MouseEventArgs e)
{
if (mouseDownFlag)
{
Point pt = Control.MousePosition;
valueX = currentMousePoint.X - pt.X;
valueY = currentMousePoint.Y - pt.Y;
this.Location = new Point(currentFormPoint.X - valueX, currentFormPoint.Y - valueY);
}
}
#endregion
3、添加运行图标
可以使用一些网络在线生成icon图标,然后直接上传即可
4、无边框窗体加阴影效果
- 效果
- 代码
#region 无边框窗体设置阴影效果
public Share()
{
m_aeroEnabled = false;
this.FormBorderStyle = FormBorderStyle.None;
InitializeComponent();
}
#endregion
[DllImport("dwmapi.dll")]
public static extern int DwmExtendFrameIntoClientArea(IntPtr hWnd, ref MARGINS pMarInset);
[DllImport("dwmapi.dll")]
public static extern int DwmSetWindowAttribute(IntPtr hwnd, int attr, ref int attrValue, int attrSize);
[DllImport("dwmapi.dll")]
public static extern int DwmIsCompositionEnabled(ref int pfEnabled);
private bool m_aeroEnabled; // variables for box shadow
private const int CS_DROPSHADOW = 0x00020000;
private const int WM_NCPAINT = 0x0085;
private const int WM_ACTIVATEAPP = 0x001C;
public struct MARGINS // struct for box shadow
{
public int leftWidth;
public int rightWidth;
public int topHeight;
public int bottomHeight;
}
private const int WM_NCHITTEST = 0x84; // variables for dragging the form
private const int HTCLIENT = 0x1;
private const int HTCAPTION = 0x2;
protected override CreateParams CreateParams
{
get
{
m_aeroEnabled = CheckAeroEnabled();
CreateParams cp = base.CreateParams;
if (!m_aeroEnabled)
cp.ClassStyle |= CS_DROPSHADOW;
return cp;
}
}
private bool CheckAeroEnabled()
{
if (Environment.OSVersion.Version.Major >= 6)
{
int enabled = 0;
DwmIsCompositionEnabled(ref enabled);
return enabled == 1 ? true : false;
}
return false;
}
protected override void WndProc(ref Message m)
{
switch (m.Msg)
{
case WM_NCPAINT: // box shadow
if (m_aeroEnabled)
{
var v = 2;
DwmSetWindowAttribute(Handle, 2, ref v, 4);
MARGINS margins = new MARGINS()
{
bottomHeight = 1,
leftWidth = 0,
rightWidth = 0,
topHeight = 0
};
DwmExtendFrameIntoClientArea(Handle, ref margins);
}
break;
default:
break;
}
base.WndProc(ref m);
if (m.Msg == WM_NCHITTEST && (int)m.Result == HTCLIENT) // drag the form
m.Result = (IntPtr)HTCAPTION;
}
5、Label标签背景透明
必须依赖于某一个父级才能实现背景透明
- 效果
- 代码
private void Share_Load(object sender, EventArgs e)
{
this.lbTime.ForeColor = Color.White; //System.Drawing.Color.FromName("#ffffff");
this.lbTime.BackColor = Color.Transparent;
this.lbTime.Parent = pictureBox; //pictureBox图片控件
DisplayTime();
}
#region 时间
private void DisplayTime()
{
try
{
Timer timer1 = new Timer();
timer1.Interval = 1000;
timer1.Enabled = true;
timer1.Tick += new EventHandler(timer1EventProcessor);//添加事件
}
catch(Exception ex)
{
LogHelper.Error(ex);
}
}
public void timer1EventProcessor(object source, EventArgs e)
{
this.lbTime.Text = DateTime.Now.ToString();
}
#endregion
6、最小化窗体到任务栏和系统盘
添加控件-notifyIcon,然后给控件添加icon图标
- 代码
//按钮点击时间 - 最小化窗体
private void panelMin_Click(object sender, EventArgs e)
{
if (this.WindowState == FormWindowState.Normal)
{
this.notifyIcon.Visible = true;
this.Visible = false;
this.ShowInTaskbar = true;
}
}
//点击图标,显示窗体
private void notifyIcon_Click(object sender, EventArgs e)
{
if (this.Visible == false)
{
this.Visible = true;
this.notifyIcon.Visible = false;
}
}
7、
未完待续
以上是关于小5聊Winform窗体开发基础知识积累的主要内容,如果未能解决你的问题,请参考以下文章
小5聊C# Winform窗体,程序点击运行第二次自动判断显示,解决不重复打开两个相同窗体