无边框窗体和后台创建控件

时间:2021-09-01 19:31:47

最小化 最大化 关闭 按钮 不一定非要用按钮来做,

可以用图片 写事件,加上鼠标 移入移出 点击 来操作

MouseEnter-鼠标移入的时候发生的事件

private void pictureBox1_MouseEnter(object sender, EventArgs e) { pictureBox1.BackgroundImage = Image.FromFile(Application.StartupPath + "\\..\\..\\images\\btn_close_highlight.png"); }

MouseLeave-鼠标移出的时候发生的事件

private void pictureBox1_MouseLeave(object sender, EventArgs e) { pictureBox1.BackgroundImage = Image.FromFile(Application.StartupPath + "\\..\\..\\images\\btn_close_disable.png"); }

MouseDown-鼠标按下的时候发生的事件

//鼠标摁下时显示的图片 private void pictureBox1_MouseDown(object sender, MouseEventArgs e) { pictureBox1.BackgroundImage = Image.FromFile(Application.StartupPath + "\\..\\..\\images\\btn_close_down.png"); }


无边框的窗体 把FormBorderStyle 属性设为  none

如何获取图片的相对路径:

Application.StartupPath + "\\..\\..\\images\\btn_close_highlight.png"

默认的无边框是无法拖动窗口的 可以输入一下代码 来让窗口移动:

//窗体移动API
[DllImport("user32.dll")]
public static extern bool ReleaseCapture();
[DllImport("user32.dll")]
public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int IParam);
public const int WM_SYSCOMMAND = 0x0112;
public const int SC_MOVE = 0xF010;
public const int HTCAPTION = 0x0002;
[DllImport("user32")]
private static extern int SendMessage(IntPtr hwnd, int wMsg, int wParam, IntPtr lParam);
private const int WM_SETREDRAW = 0xB;

private void Form1_MouseDown(object sender, MouseEventArgs e)
{
 if (this.WindowState == FormWindowState.Normal)
 {
  ReleaseCapture();
  SendMessage(this.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);
 }

让窗体有 阴影,右下阴影(四边阴影暂无找到)

//两边阴影
private const int CS_DropSHADOW = 0x20000;
private const int GCL_STYLE = (-26);

[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern int SetClassLong(IntPtr hwnd, int nIndex, int dwNewLong);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern int GetClassLong(IntPtr hwnd, int nIndex);

timer控件:

----timer就是个线程,这个线程默认可以跨线程访问对象

有两个属性常用:

Enabled - 此控件是否启用

--timer1.Enabled=false;不可用
    --timer1.Enabled=true;可用

Interval - 间隔时间,毫秒

-------------

创建控件

PictureBox p = new PictureBox();//创建图片控件,实例化图片控件 //设置图片
p.BackgroundImage = Image.FromFile(Application.StartupPath + "\\dota_img5.jpg"); p.BackgroundImageLayout = ImageLayout.Stretch; TextBox tb = new TextBox();//创建textBox控件,实例化 flowLayoutPanel1.Controls.Add(p);//放入流式布局的集合中 flowLayoutPanel1.Controls.Add(tb);


改属性: foreach (Control ct in flowLayoutPanel1.Controls) { if (ct is TextBox) { ((TextBox)ct).Text = "123123"; } }

无边框窗体和后台创建控件