【小5聊】Winform窗体开发基础知识积累

13 篇文章 1 订阅
本文介绍了如何在Windows窗体应用中实现各种美化效果和自定义功能,包括去除顶部工具栏、实现无边框窗体的自定义移动、添加运行图标、设置无边框窗体的阴影效果、使Label背景透明、最小化窗体到任务栏和系统托盘。通过这些技巧,开发者可以提升应用的用户体验和交互设计。
摘要由CSDN通过智能技术生成

整理经常用到的一些功能知识点

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、

未完待续

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

全栈小5

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值