首页 > 代码库 > winform(无边框窗体与timer)

winform(无边框窗体与timer)

一、无边框窗体

1、控制按钮如何制作
就是放置可以点击的控件,不局限于使用按钮或是什么别的,
只要放置的控件可以点击能触发点击事件就可以了

做的好看一点,就是鼠标移入(pictureBox1_MouseEnter),移出(pictureBox1_MouseLeave),按下( pictureBox1_MouseDown)三个事件会让按钮改变样式

如何获取图片的相对路径:
Application.StartupPath + "\\..\\..\\images\\btn_close_highlight.png"

\..\文件夹名称... 向上翻一个文件夹

2、如何让窗体动起来
调用窗体移动的API

如果有其它控件覆盖了窗体,那么写好鼠标按下的事件委托就可以了

//窗体移动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);
    }
}

调用窗体移动的API

3、如何让窗体有阴影

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);    

        private void SetShadow()//在构造函数中引用
        {
            SetClassLong(this.Handle, GCL_STYLE, GetClassLong(this.Handle, GCL_STYLE) | CS_DropSHADOW); 
        }

4.调整窗体大小

const int HTLEFT = 10;
const int HTRIGHT = 11;
const int HTTOP = 12;
const int HTTOPLEFT = 13;
const int HTTOPRIGHT = 14;
const int HTBOTTOM = 15;
const int HTBOTTOMLEFT = 0x10;
const int HTBOTTOMRIGHT = 17;

protected override void WndProc(ref Message m)
{
    switch (m.Msg)
    {
        case 0x0084:
            base.WndProc(ref m);
            Point vPoint = new Point((int)m.LParam & 0xFFFF,
                (int)m.LParam >> 16 & 0xFFFF);
            vPoint = PointToClient(vPoint);
            if (vPoint.X <= 5)
                if (vPoint.Y <= 5)
                    m.Result = (IntPtr)HTTOPLEFT;
                else if (vPoint.Y >= ClientSize.Height - 5)
                    m.Result = (IntPtr)HTBOTTOMLEFT;
                else m.Result = (IntPtr)HTLEFT;
            else if (vPoint.X >= ClientSize.Width - 5)
                if (vPoint.Y <= 5)
                    m.Result = (IntPtr)HTTOPRIGHT;
                else if (vPoint.Y >= ClientSize.Height - 5)
                    m.Result = (IntPtr)HTBOTTOMRIGHT;
                else m.Result = (IntPtr)HTRIGHT;
            else if (vPoint.Y <= 5)
                m.Result = (IntPtr)HTTOP;
            else if (vPoint.Y >= ClientSize.Height - 5)
                m.Result = (IntPtr)HTBOTTOM;
            break;
        case 0x0201://鼠标左键按下的消息 
            m.Msg = 0x00A1;//更改消息为非客户区按下鼠标 
            m.LParam = IntPtr.Zero;//默认值 
            m.WParam = new IntPtr(2);//鼠标放在标题栏内 
            base.WndProc(ref m);
            break; 
        default:
            base.WndProc(ref m);
            break;
    }

百度搜索 

二、后台创建控件

例:

//用户输入一个整数,点击此按钮
           //创建相应的按钮,数量等于用户输入的整数,名字从1开始,全部放放置到下面的Panel里
//1.获取用户输入的数字
int num =Convert.ToInt32(textBox1.Text);
//创建按钮
Button bt=new Botton();
for(int i=0;i<=num;i++)
{
bt.Text=i.tostring()
//3.如何放到Panel里去
flowLayoutPanel1.Controls.Add(btn);

}

除按钮以外后台还可以创建各种各样的控件

 三、用户控件

是由其它控件所组成的一种用户自定义控件
用户控件的主体与Panel相似,但是它却又是独立的一个类

注意:所有内部的控件访问权限都要修改为public

四、timer控件

timer在组件里:
Enabled - 此控件是否启用
Interval - 间隔时间,毫秒
Tick事件 - 间隔指定时间后要执行的代码段

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

winform(无边框窗体与timer)