Winform持续更新,留着自己看

1.窗体属性窗体居中

2.窗体属性窗体居中

            //窗体居中
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;

3.窗体去掉最小化、最大化

            //去掉最小化,去掉最大化
            MinimizeBox = false;
            MaximizeBox = false;

4.设置窗体不可拉伸

设置窗体的FormBorderStyle属性,将默认的”Sizable”改为” FixedSingle”或” Fixed3D”即可。

5.C#实现Form窗口最大化(最小化)

this.WindowState = FormWindowState.Maximized;//最大化:
this.WindowState = FormWindowState.Normal;//原始大小:
this.WindowState = FormWindowState.Minimized;//最小化:

6.Panel控件增加滚动条

AutoScroll=True
AutoScrollMiniSize 450,450 //设置逻辑区域尺寸,如果它大于控件尺寸就会出现滚动条。

7.WINFORM 中改变窗体大小

this.Size = new System.Drawing.Size(100, 100);

8.WINFORM 中动态添加按钮

        private void button1_Click(object sender, EventArgs e)
        {
            
            Button btn = new Button();
            btn.Width = 30;
            btn.Height = 30;
            btn.Text = "123";
            panel1.Controls.Add(btn);
        }

9.c#鼠标在控件上面,然后显示文字

先添加toolTip控件到界面

然后每个控件的属性会多一项 ToolTip

第一种:直接给里面加文字

第二种:

private void pictureBox_topmost_MouseHover(object sender, EventArgs e)
        {
            if (this.TopMost == true)
            {
                toolTip_jmt.SetToolTip(pictureBox_topmost, "取消置顶");
            }
            else 
            {
                toolTip_jmt.SetToolTip(pictureBox_topmost, "置顶");
            }
           
        }

10.winform TextBox设置透明

1,方法1:设置背景色与父容器的背景色一致;
在这里插入图片描述
2,方法2:自定义控件;

10.C# 鼠标点击移动窗体代码,可以实现无边框窗体的拖动

private static bool IsDrag = false;
private int enterX;
private int enterY;
private void setForm_MouseDown(object sender, MouseEventArgs e)
{
IsDrag = true;
enterX = e.Location.X;
enterY = e.Location.Y;
}

private void setForm_MouseUp(object sender, MouseEventArgs e)
{
IsDrag = false;
enterX = 0;
enterY = 0;
}

private void setForm_MouseMove(object sender, MouseEventArgs e)
{
if (IsDrag)
{
Left += e.Location.X - enterX;
Top += e.Location.Y - enterY;
}
}

11.Windform C# pictureBox 更换背景图片

       private void pbRun_Click(object sender, EventArgs e)
        {
            string pbName = pbRun.Tag.ToString();
            if (pbRun.Tag.ToString() == "0")
            {
                string localFilePath = @"../../Source/启动.png";
                Image Img = Image.FromFile(localFilePath);
                Image bmp = new Bitmap(Img);
                Img.Dispose();
                pbRun.Image = bmp;
                pbRun.SizeMode = PictureBoxSizeMode.StretchImage;
                pbRun.Tag = "1";
            }
            else
            {
                string localFilePath = @"../../Source/suspend.png";
                Image Img = Image.FromFile(localFilePath);
                Image bmp = new Bitmap(Img);
                Img.Dispose();
                pbRun.Image = bmp;
                pbRun.SizeMode = PictureBoxSizeMode.StretchImage;
                pbRun.Tag = "0";
            }
        }

12.c# datagridview列宽自适应设置

在这里插入图片描述

13.winform中改变DataGridview的列标题字体颜色

            DataGridViewCellStyle style = new DataGridViewCellStyle();
            style.ForeColor = Color.Red;
            foreach (DataGridViewColumn col in this.dataGridView1.Columns)
            {
                col.HeaderCell.Style = style;
            }
            //也可以在属性中更改ColumnHeadersDefaultCellStyle
            this.dataGridView1.EnableHeadersVisualStyles = false;
			//datagridview第一列空白隐藏掉
            this.dataGridView1.RowHeadersVisible = false;

14.C#窗体中的DataGridView取消选中条

 private void DataGridViewDetail_RowStateChanged(object sender, DataGridViewRowStateChangedEventArgs e)
        {
            //设置没有选中条
            e.Row.Selected = false;
        }

在这里插入图片描述

15.C# WINFORM 鼠标右键弹出菜单


private void Form1_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {//不要给窗口设置菜单,不然右键也有效
                contextMenuStrip1.Show(e.X + this.Left, e.Y + this.Top);
            }
        }

16.点击最小化的时候软件图标放到托盘中

        private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (WindowState == FormWindowState.Minimized)
            {
               
                //还原窗体显示    
                WindowState = FormWindowState.Normal;
                //激活窗体并给予它焦点
                this.Activate();
                //任务栏区显示图标
                this.ShowInTaskbar = true;
                //托盘区图标隐藏
                notifyIcon1.Visible = false;
            }
        }

        private void MainForm1_SizeChanged(object sender, EventArgs e)
        {
            //判断是否选择的是最小化按钮
            if (WindowState == FormWindowState.Minimized)
            {
                notifyIcon1.Icon = new Icon("DT.ico");
                //隐藏任务栏区图标
                this.ShowInTaskbar = false;
                //图标显示在托盘区
                notifyIcon1.Visible = true;
            }
        }
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

金增辉

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

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

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

打赏作者

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

抵扣说明:

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

余额充值