1. 程式人生 > >C# Form窗體的功能操作,無邊框窗體的移動,無邊框窗體的尺寸縮放,儲存和恢復窗體的尺寸和座標資訊

C# Form窗體的功能操作,無邊框窗體的移動,無邊框窗體的尺寸縮放,儲存和恢復窗體的尺寸和座標資訊

/// <summary>
/// 此類用於實現一些可用於Form窗體的功能操作
/// </summary>
class FormTool
{

    //using System.Runtime.InteropServices;  
    [DllImport("user32.dll")]
    public static extern bool ReleaseCapture();
    [DllImport("user32.dll")]
    public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);

    # region  無邊框窗體的移動

    // 在Form的MouseDown(object sender, MouseEventArgs e)事件中呼叫此函式,可控制窗體移動
    public static void MouseDown_MoveForm(Form form)
    {
        //常量  
        int WM_SYSCOMMAND = 0x0112;

        //窗體移動  
        int SC_MOVE = 0xF010;
        int HTCAPTION = 0x0002;

        ReleaseCapture();
        SendMessage(form.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);

        saveFormInfo(form);        // 儲存介面資訊
    }

    # endregion


    # region  無邊框窗體的尺寸縮放

    public static int formResizeMode = 0;       // 此處指定窗體尺寸修改的模式

    // 在Form的MouseDown(object sender, MouseEventArgs e)事件中呼叫此函式,可調整窗體大小
    public static void MouseDown_ResizeForm(Form form)
    {
        //常量
        int WM_SYSCOMMAND = 0x0112;

        ////改變窗體大小
        //int WMSZ_LEFT = 0xF001;
        //int WMSZ_RIGHT = 0xF002;
        //int WMSZ_TOP = 0xF003;
        //int WMSZ_TOPLEFT = 0xF004;
        //int WMSZ_TOPRIGHT = 0xF005;
        //int WMSZ_BOTTOM = 0xF006;
        //int WMSZ_BOTTOMLEFT = 0xF007;
        //int WMSZ_BOTTOMRIGHT = 0xF008;

        if (formResizeMode == 0) return;    // 若無縮放模式,則返回

        ReleaseCapture();
        SendMessage(form.Handle, WM_SYSCOMMAND, formResizeMode, 0);
        //SendMessage(form.Handle, WM_SYSCOMMAND, WMSZ_BOTTOM, 0);
        //SendMessage(this.Handle, WM_SYSCOMMAND, WMSZ_TOP, 0);
    }



    // 在處於最前端的控制元件的MouseMove事件中呼叫此函式,根據滑鼠位置設定對應的縮放模式

    public static void MouseMove(object sender, MouseEventArgs e)
    {
        Control ctrl = (Control)sender;             // 獲取滑鼠處的控制元件
        Rectangle client = ctrl.ClientRectangle;    // 獲取控制元件尺寸

        setSizeMode(client, new Point(e.X, e.Y), 10);   // 根據滑鼠在區域client中的位置設定縮放模式
        //MessageBox.Show("滑鼠座標:" + e.X + "," + e.Y + " 窗體尺寸" + client.Width + "," + client.Height);
    }

    // 根據座標 x,y 在rect上的位置控制尺寸調節模式
    private static void setSizeMode(Rectangle R, Point P, int W)
    {
        formResizeMode = getSizeMode(R, P, W);
    }

    // 根據座標 x,y 在rect上的位置控制尺寸調節模式
    private static int getSizeMode(Rectangle R, Point P, int W)
    {
        //改變窗體大小相關引數
        int WMSZ_LEFT = 0xF001;
        int WMSZ_RIGHT = 0xF002;
        int WMSZ_TOP = 0xF003;
        int WMSZ_TOPLEFT = 0xF004;
        int WMSZ_TOPRIGHT = 0xF005;
        int WMSZ_BOTTOM = 0xF006;
        int WMSZ_BOTTOMLEFT = 0xF007;
        int WMSZ_BOTTOMRIGHT = 0xF008;

        // 中間區域
        Rectangle main = new Rectangle(R.Left + W, R.Top + W, R.Width - 2 * W, R.Height - 2 * W);
        if (main.Contains(P)) return 0;

        // 左側區域
        Rectangle LeftRect = new Rectangle(R.Left + W, R.Top + W, W, R.Height - 2 * W);
        if (LeftRect.Contains(P)) return WMSZ_LEFT;

        // 右側區域
        Rectangle RightRect = new Rectangle(R.Right - W, R.Top + W, W, R.Height - 2 * W);
        if (RightRect.Contains(P)) return WMSZ_RIGHT;

        // 頂部區域
        Rectangle TopRect = new Rectangle(R.Left + W, R.Top, R.Width - 2 * W, W);
        if (TopRect.Contains(P)) return WMSZ_TOP;

        // 底部區域
        Rectangle BottomRect = new Rectangle(R.Left + W, R.Bottom - W, R.Width - 2 * W, W);
        if (BottomRect.Contains(P)) return WMSZ_BOTTOM;

        // 左上區域
        Rectangle TOPLEFT = new Rectangle(R.Left, R.Top, W, W);
        if (TOPLEFT.Contains(P)) return WMSZ_TOPLEFT;

        // 右上區域
        Rectangle TOPRIGHT = new Rectangle(R.Right - W, R.Top, W, W);
        if (TOPRIGHT.Contains(P)) return WMSZ_TOPRIGHT;

        // 左下區域
        Rectangle BOTTOMLEFT = new Rectangle(R.Left, R.Bottom - W, W, W);
        if (BOTTOMLEFT.Contains(P)) return WMSZ_BOTTOMLEFT;

        // 右下區域
        Rectangle BOTTOMRIGHT = new Rectangle(R.Right - W, R.Bottom - W, W, W);
        if (BOTTOMRIGHT.Contains(P)) return WMSZ_BOTTOMRIGHT;

        return 0;
    }

    # endregion


    # region 儲存和恢復窗體的尺寸和座標資訊

    // 儲存窗體屬性資訊到登錄檔中
    public static void saveFormInfo(Form form)
    {
        if (form == null) return;

        String formKey = form.Text; // 獲取窗體名稱
        Point P = form.Location;    // 獲取窗體form的座標位置

        string formInfo = form.Width + "_" + form.Height + "_" + P.X + "_" + P.Y;

        // 儲存窗體資訊到登錄檔
        Tool.RegistrySave(@"Scimence\TaskManager\Set\FormInfo", formKey, formInfo);
    }

    // 從登錄檔中獲取資訊並設定窗體屬性
    public static void RestoreFormInfo(Form form)
    {
        if (form == null) return;

        String formKey = form.Text; // 獲取窗體名稱

        // 獲取資訊
        string formInfo = Tool.RegistryStrValue(@"Scimence\TaskManager\Set\FormInfo", formKey);
        if (formInfo.Equals("")) saveFormInfo(form);
        else
        {
            string[] info = formInfo.Split('_');
            form.Width = Tool.toInt(info[0], form.Width);
            form.Height = Tool.toInt(info[1], form.Height);

            int x = Tool.toInt(info[2], form.Location.X);
            int y = Tool.toInt(info[3], form.Location.Y);
            form.Location = new Point(x, y);
        }
    }

    # endregion


    #region 控制窗體在桌面邊緣區域的自動隱藏

    private static Dictionary<string, Timer> DeactivateTimer = new Dictionary<string, Timer>();
    private static Timer getTimer(String formKey)
    {
        Timer timer;
        if (DeactivateTimer.ContainsKey(formKey)) timer = DeactivateTimer[formKey];
        else
        {
            timer = new Timer();
            DeactivateTimer.Add(formKey, timer);
        }

        return timer;
    }

    // 
    private void FloatMenu_Deactivate(object sender, EventArgs e)
    {
        MessageBox.Show("FloatMenu_Deactivate");
    }

    // 控制窗體在桌面邊緣的隱藏
    public static void BorderHide(Form form)
    {
        if (form == null) return;

        String formKey = form.Text; // 獲取窗體名稱
        getTimer(formKey);

        // 判定窗體form在桌面中的位置隱藏邏輯
        //if

    }

    //SystemInformation.WorkingArea.Width

    #endregion


    #region 控制Form2在Form1的下方停靠

    // 記錄Form的停靠資訊
    public static Dictionary<string, string> dockFormInfo = new Dictionary<string, string>();

    // 設定Form2停靠於Form1的下方
    public static void setDock(Form form1, Form form2)
    {
        Rectangle Rect1 = form1.ClientRectangle;
        Rectangle Rect2 = form2.ClientRectangle;

        if (Math.Abs(Rect1.Bottom - Rect2.Bottom) < 10 && Math.Abs(Rect1.Left - Rect2.Left) < 30)
        {

        }
    }


    #endregion

    // 設定form2自動顯示在form1的兩邊
    public static void AutoLocation_OnSide(Form form1, Form form2)
    {
        int x = form1.Location.X + form1.Width + 10;
        if (x > SystemInformation.WorkingArea.Width - form2.Width) x = form1.Location.X - form2.Width - 10;
        form2.Location = new Point(x, form1.Location.Y);
    }
}