非常有用常用的技术点二——WinForm

1、自动删除文件夹内的文件

public AutoDeleteFile()
{
   //使用线程来执行操作
   Thread threadDelete = new Thread(new ThreadStart(DeleteFile));
   threadDelete.IsBackground = true;
   threadDelete.Start();
}
private void DeleteFile()
{
    string strPath = Application.StartupPath + "\\" + _strfolder;
    if (!Directory.Exists(strPath))
    {
        LogFrame.LogForm(strPath + "此目录不存在!");
        return;
    }
    try
    {
        DirectoryInfo TheFolder = new DirectoryInfo(strPath);
        //遍历文件
        foreach (FileInfo NextFile in TheFolder.GetFiles())
        {
            TimeSpan ts = DateTime.Now - NextFile.LastWriteTime;
            if (ts.Days >= 7)
            {
                NextFile.Delete();
                LogFrame.LogForm("[" + NextFile.Name + "] delete successful ");
            }
        }
    }
    catch (Exception ex)
    {
        LogFrame.LogForm("DeleteLogFile()->EX:" + ex.ToString());
    }
}

2、点击控件移动窗体

常用于窗体属性FormBorderStyle设置为None的时候又要拖动窗体时使用。窗体设置为None时,必定需要自定义关闭,最大化,最小化等功能,此时,可以通过pen来画出三个图标,或者通过添加图片的方式进而通过点击事件来实现。

点击控件移动窗口

public class DragSetter
{
	///con控件名称
    public void Register(Control con)
    {
        con.MouseDown += new MouseEventHandler(con_MouseDown);
        con.MouseMove += new MouseEventHandler(con_MouseMove);
        con.MouseUp += new MouseEventHandler(con_MouseUp);
    }

    private bool IsOnDraggiong = false;
    private Point PtDown = Point.Empty;

    private void con_MouseDown(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left)
        {
            IsOnDraggiong = true;
            PtDown = e.Location;
        }
    }

    private void con_MouseMove(object sender, MouseEventArgs e)
    {
        if (IsOnDraggiong)
        {
            Control con = sender as Control;
            Form frm = con.FindForm();
            frm.Left += (e.X - PtDown.X);
            frm.Top += (e.Y - PtDown.Y);
        }
    }

    private void con_MouseUp(object sender, MouseEventArgs e)
    {
        if (IsOnDraggiong)
        {
            IsOnDraggiong = false;
            PtDown = Point.Empty;
        }
    }
}

//声明
private DragSetter DragSetter = new DragSetter();

//调用
DragSetter.Register(panelTitle);
DragSetter.Register(picTitle);
DragSetter.Register(labTitle);

通过pen来画关闭,最大化,最小化控件

//在窗体中添加一个panel作为拖动窗体的控件,在控件中进行画图
panelTitle.Paint += new PaintEventHandler(panelTitle_Paint);
//通过对panel控件的单击,双击事件来处理窗口的变换
panelTitle.MouseClick += new MouseEventHandler(panelTitle_MouseClick);
panelTitle.MouseDoubleClick += new MouseEventHandler(panelTitle_MouseDoubleClick);


private Rectangle RectMin = Rectangle.Empty;
private Rectangle RectMax = Rectangle.Empty;
private Rectangle RectClose = Rectangle.Empty;
private void panelTitle_Paint(object sender, PaintEventArgs e)
{
	private int BtnSize = 11;
    private int BtnPad = 15;
	//创建一个线性渐变画笔,从控件的最上面到最下面,颜色由blue渐变为green
    using (System.Drawing.Drawing2D.LinearGradientBrush lb = new System.Drawing.Drawing2D.LinearGradientBrush(
      new Point(0, 0), new Point(0, panelTitle.Height), Color.Blue, Color.Green))
    {
    	//填充区域:整个panel区域
        e.Graphics.FillRectangle(lb, 0, 0, panelTitle.Width, panelTitle.Height);
    }
	//确认关闭,最大化,最小化图形的位置和大小
    RectClose = new Rectangle(panelTitle.Width - BtnPad * 1 - BtnSize * 1, panelTitle.Height / 2 - BtnSize / 2 + 1, BtnSize, BtnSize);
    RectMax = new Rectangle(panelTitle.Width - BtnPad * 2 - BtnSize * 2, panelTitle.Height / 2 - BtnSize / 2 + 1, BtnSize, BtnSize);
    RectMin = new Rectangle(panelTitle.Width - BtnPad * 3 - BtnSize * 3, panelTitle.Height / 2 - BtnSize / 2 + 2, BtnSize, BtnSize);
    RectClose = GetZoomRect(RectClose);
    RectMax = GetZoomRect(RectMax);
    RectMin = GetZoomRect(RectMin);
}

private Rectangle GetZoomRect(Rectangle rect)
{
    int cx = rect.Left + rect.Width / 2;
    int cy = rect.Top + rect.Height / 2;
    int nw = Convert.ToInt32(rect.Width * 1.5);
    int nh = Convert.ToInt32(rect.Height * 1.5);
    return new Rectangle(cx - nw / 2, cy - nh / 2, nw, nh);
}

///通过双击来实现最大化和默认大小之间的相互转换
private void panelTitle_MouseDoubleClick(object sender, MouseEventArgs e)
{
     if (AllowResize)
     {
         if (this.WindowState == FormWindowState.Normal)
         {
             this.WindowState = FormWindowState.Maximized;
         }
         else if (this.WindowState == FormWindowState.Maximized)
         {
             this.WindowState = FormWindowState.Normal;
         }
     }
 }

///通过点击panel来确定最大化、最小化、和关闭事件
private void panelTitle_MouseClick(object sender, MouseEventArgs e)
{
    if (RectClose.Contains(e.Location))
    {
        this.Close();
    }
    if (RectMax.Contains(e.Location))
    {
        if (AllowResize)
        {
            if (this.WindowState == FormWindowState.Normal)
            {
                this.WindowState = FormWindowState.Maximized;
            }
            else if (this.WindowState == FormWindowState.Maximized)
            {
                this.WindowState = FormWindowState.Normal;
            }
        }
    }
    if (RectMin.Contains(e.Location))
    {
        this.Cursor = Cursors.Default;
        this.WindowState = FormWindowState.Minimized;
    }
}

3、窗体等比例缩放自适应

private void setTag(Control cons)
{
    foreach (Control con in cons.Controls)
    {
        con.Tag = con.Width + ":" + con.Height + ":" + con.Left + ":" + con.Top + ":" + con.Font.Size;
        if (con.Controls.Count > 0)
            setTag(con);
    }
}
private void setControls(float newx, float newy, Control cons)
{
    try
    {
        foreach (Control con in cons.Controls)
        {
            string[] mytag = con.Tag.ToString().Trim().Split(new char[] { ':' });
            float a = Convert.ToSingle(mytag[0]) * newx;
            con.Width = (int)a;
            a = Convert.ToSingle(mytag[1]) * newy;
            con.Height = (int)(a);
            a = Convert.ToSingle(mytag[2]) * newx;
            con.Left = (int)(a);
            a = Convert.ToSingle(mytag[3]) * newy;
            con.Top = (int)(a);
            Single currentSize = Convert.ToSingle(mytag[4]) * Math.Min(newx, newy);
            con.Font = new Font(con.Font.Name, currentSize, con.Font.Style, con.Font.Unit);
            if (con.Controls.Count > 0)
            {
                setControls(newx, newy, con);
            }
        }
    }
    catch (Exception ex)
    {
    }
}

你可能感兴趣的:(.net)