Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux 程式設計 >> Linux 程式設計

Winform 自繪按鈕:用座標區域與 GraphicsPath 模擬按下效果

有時候窗體已經有一張完整的背景圖,圖上「畫」了幾顆按鈕,這時再擺真正的 Button 控制項反而顯得突兀。比較漂亮的解法是讓窗體自己承擔按鈕的工作:先定義每顆按鈕的 clickable 範圍,再靠幾個事件把「按下」與「彈起」的圖片貼上去。

做法可以拆成三個步驟。首先量出各按鈕的左上角座標與尺寸,準備好每顆按鈕的 normal 與 pressed 兩張圖;接著用 GraphicsPath 把座標與尺寸組合成一個個封閉區域;最後用四個 bool 旗標記錄目前哪顆按鈕正被按住。

一、按鈕參數與旗標宣告

/// <summary>
    /// 即時監控、酒位展示、酒櫃介紹、網路訂購四個按鈕的位置參數
    /// </summary>
    int[,] BtnLocation = new int[4, 2]{{637,161},{683,269},{714,374},{637,478}};
    /// <summary>
    /// 四個按鈕的 Size 參數
    /// </summary>
    Size BtnSize = new Size(285,92);
    /// <summary>
    /// 即時監控按鈕是否按下的標誌
    /// </summary>
    bool FlageMouseDownRealTime = false;
    /// <summary>
    /// 酒位展示按鈕是否按下的標誌
    /// </summary>
    bool FlageMouseDownWinePos = false;
    /// <summary>
    /// 酒櫃介紹按鈕是否按下的標誌
    /// </summary>
    bool FlageMouseDownCupIntrol = false;
    /// <summary>
    /// 網路訂購按鈕是否按下的標誌
    /// </summary>
    bool FlageMouseDownNetPurchase = false;
    /// <summary>
    /// 即時監控按鈕的位置範圍
    /// </summary>
    GraphicsPath gpRealTime = new GraphicsPath();
    /// <summary>
    /// 酒位展示按鈕的位置範圍
    /// </summary>
    GraphicsPath gpWinePos = new GraphicsPath();
    /// <summary>
    /// 酒櫃介紹按鈕的位置範圍
    /// </summary>
    GraphicsPath gpCupIntrol = new GraphicsPath();
    /// <summary>
    /// 網路訂購按鈕的位置範圍
    /// </summary>
    GraphicsPath gpNetPurchase = new GraphicsPath();

    Graphics dc = null;

二、建立四個按鈕的命中範圍

這段方法把每顆按鈕的四個角點組成矩形線段,加進各自的 GraphicsPath。之後只要呼叫 IsVisible(e.Location) 就能判斷滑鼠是否落在按鈕上,比手動比較座標區間乾淨得多。

/// <summary>
    /// 取得四個按鈕的區域範圍
    /// </summary>
    private void GetButtonGraphicPath()
    {
        FlageMouseDownRealTime = false;
        FlageMouseDownWinePos = false;
        FlageMouseDownCupIntrol = false;
        FlageMouseDownNetPurchase = false;

        Point[] pt = new Point[]{
                new Point(BtnLocation[0,0],BtnLocation[0,1]),
                new Point(BtnLocation[0,0]+BtnSize.Width,BtnLocation[0,1]),
                new Point(BtnLocation[0,0]+BtnSize.Width,BtnLocation[0,1]+BtnSize.Height),
                new Point(BtnLocation[0,0],BtnLocation[0,1]+BtnSize.Height)
            };
        gpRealTime.AddLines(pt);
        pt = new Point[]{
                new Point(BtnLocation[1,0],BtnLocation[1,1]),
                new Point(BtnLocation[1,0]+BtnSize.Width,BtnLocation[1,1]),
                new Point(BtnLocation[1,0]+BtnSize.Width,BtnLocation[1,1]+BtnSize.Height),
                new Point(BtnLocation[1,0],BtnLocation[1,1]+BtnSize.Height)
            };
        gpWinePos.AddLines(pt);
        pt = new Point[]{
                new Point(BtnLocation[2,0],BtnLocation[2,1]),
                new Point(BtnLocation[2,0]+BtnSize.Width,BtnLocation[2,1]),
                new Point(BtnLocation[2,0]+BtnSize.Width,BtnLocation[2,1]+BtnSize.Height),
                new Point(BtnLocation[2,0],BtnLocation[2,1]+BtnSize.Height)
            };
        gpCupIntrol.AddLines(pt);
        pt = new Point[]{
                new Point(BtnLocation[3,0],BtnLocation[3,1]),
                new Point(BtnLocation[3,0]+BtnSize.Width,BtnLocation[3,1]),
                new Point(BtnLocation[3,0]+BtnSize.Width,BtnLocation[3,1]+BtnSize.Height),
                new Point(BtnLocation[3,0],BtnLocation[3,1]+BtnSize.Height)
            };
        gpNetPurchase.AddLines(pt);

    }

三、初始化:掛上背景圖並綁定事件

這裡用一個 Panel 鋪滿整個窗體當作繪圖畫布,把背景圖畫上去之後取得 Graphics 物件,再依序掛上 MouseDown、MouseMove、MouseUp、MouseClick 與 MouseDoubleClick。

 private void InitialButtonsStyle()
    {
        GetButtonGraphicPath();

        pxControl.Location = new Point(0,0);
        pxControl.Size = new Size(1024,768);
        SetBackGroudImg();
        this.Controls.Add(pxControl);
        dc = pxControl.CreateGraphics();
        pxControl.MouseDoubleClick +=new MouseEventHandler(this.Main_MouseDoubleClick);
        pxControl.MouseDown+=new MouseEventHandler(this.pxControl_MouseDown);
        pxControl.MouseUp += new MouseEventHandler(this.pxControl_MouseUp);
        pxControl.MouseMove += new MouseEventHandler(this.pxControl_MouseMove);
        pxControl.MouseClick += new MouseEventHandler(this.pxControl_MouseClick);

  }

四、按下:換上 pressed 圖並立旗標

/// <summary>
    /// 按下四個按鈕時的狀態變化
    /// </summary>
    private void pxControl_MouseDown(object sender,MouseEventArgs e)
    {
        if (gpRealTime.IsVisible(e.Location))
        {
            using(Image imgTemp = Image.FromFile(strWineRealStatusDownPath))
            {
                dc.DrawImage(imgTemp, BtnLocation[0, 0], BtnLocation[0, 1], BtnSize.Width,BtnSize.Height);
                FlageMouseDownRealTime = true;
            }
        }
        if (gpWinePos.IsVisible(e.Location))
        {
            using (Image imgTemp = Image.FromFile(strWinePosQueryDownPath))
            {
                dc.DrawImage(imgTemp, BtnLocation[1, 0], BtnLocation[1, 1], BtnSize.Width, BtnSize.Height);
                FlageMouseDownWinePos = true;
            }
        }
        if (gpCupIntrol.IsVisible(e.Location))
        {
            using (Image imgTemp = Image.FromFile(strWineCupIntroDownPath))
            {
                dc.DrawImage(imgTemp, BtnLocation[2, 0], BtnLocation[2, 1], BtnSize.Width, BtnSize.Height);
                FlageMouseDownCupIntrol = true;
            }
        }
        if (gpNetPurchase.IsVisible(e.Location))
        {
            using (Image imgTemp = Image.FromFile(strWinePurchaseDownPath))
            {
                dc.DrawImage(imgTemp, BtnLocation[3, 0], BtnLocation[3, 1], BtnSize.Width, BtnSize.Height);
                FlageMouseDownNetPurchase = true;
            }
        }
    }

五、移動:拖曳出去要自動復原

這一段是整個做法的關鍵。滑鼠若停在按鈕範圍內就維持按下狀態,一旦移出範圍且該按鈕先前被按著,就換回 normal 圖並清掉旗標,模擬「按著不放但滑鼠移開會取消點擊」的真實手感。

/// <summary>
    /// 離開四個按鈕時的狀態變化
    /// </summary>
    private void pxControl_MouseMove(object sender, MouseEventArgs e)
    {
        if (gpRealTime.IsVisible(e.Location))
        {
            if (FlageMouseDownRealTime)
            {
                using (Image imgTemp = Image.FromFile(strWineRealStatusDownPath))
                {
                   dc.DrawImage(imgTemp, BtnLocation[0, 0], BtnLocation[0, 1], BtnSize.Width, BtnSize.Height);
                }
            }
        }
        else
        {
            if (FlageMouseDownRealTime)
            {
                using (Image imgTemp = Image.FromFile(strWineRealStatusUpPath))
                {
                   dc.DrawImage(imgTemp, BtnLocation[0, 0], BtnLocation[0, 1], BtnSize.Width, BtnSize.Height);
                }
                FlageMouseDownRealTime = false;
            }
        }
        if (gpWinePos.IsVisible(e.Location))
        {
            if (FlageMouseDownWinePos)
            {
                using (Image imgTemp = Image.FromFile(strWinePosQueryDownPath))
                {
                   dc.DrawImage(imgTemp, BtnLocation[1, 0], BtnLocation[1, 1], BtnSize.Width, BtnSize.Height);
                }
            }
        }
        else
        {
            if (FlageMouseDownWinePos)
            {
                using (Image imgTemp = Image.FromFile(strWinePosQueryUpPath))
                {
                   dc.DrawImage(imgTemp, BtnLocation[1, 0], BtnLocation[1, 1], BtnSize.Width, BtnSize.Height);
                }
                FlageMouseDownWinePos = false;
            }
        }
        if (gpCupIntrol.IsVisible(e.Location))
        {
            if (FlageMouseDownCupIntrol)
            {
                using (Image imgTemp = Image.FromFile(strWineCupIntroDownPath))
                {
                   dc.DrawImage(imgTemp, BtnLocation[2, 0], BtnLocation[2, 1], BtnSize.Width, BtnSize.Height);
                }
            }
        }
        else
        {
            if (FlageMouseDownCupIntrol)
            {
                using (Image imgTemp = Image.FromFile(strWineCupIntroUpPath))
                {
                   dc.DrawImage(imgTemp, BtnLocation[2, 0], BtnLocation[2, 1], BtnSize.Width, BtnSize.Height);
                }
                FlageMouseDownCupIntrol = false;
            }
        }
        if (gpNetPurchase.IsVisible(e.Location))
        {
            if (FlageMouseDownNetPurchase)
            {
                using (Image imgTemp = Image.FromFile(strWinePurchaseDownPath))
                {
                   dc.DrawImage(imgTemp, BtnLocation[3, 0], BtnLocation[3, 1], BtnSize.Width, BtnSize.Height);
                }
            }
        }
        else
        {
            if (FlageMouseDownNetPurchase)
            {
                using (Image imgTemp = Image.FromFile(strWinePurchaseUpPath))
                {
                   dc.DrawImage(imgTemp, BtnLocation[3, 0], BtnLocation[3, 1], BtnSize.Width, BtnSize.Height);
                }
                FlageMouseDownNetPurchase = false;
            }
        }
    }

六、放開:復原外觀並清除旗標

/// <summary>
    /// 放開四個按鈕時的狀態變化
    /// </summary>
    private void pxControl_MouseUp(object sender, MouseEventArgs e)
    {
        if (gpRealTime.IsVisible(e.Location))
        {
            using (Image imgTemp = Image.FromFile(strWineRealStatusUpPath))
            {
                dc.DrawImage(imgTemp, BtnLocation[0, 0], BtnLocation[0, 1], BtnSize.Width, BtnSize.Height);
                FlageMouseDownRealTime = false;
            }
        }
        if (gpWinePos.IsVisible(e.Location))
        {
            using (Image imgTemp = Image.FromFile(strWinePosQueryUpPath))
            {
                dc.DrawImage(imgTemp, BtnLocation[1, 0], BtnLocation[1, 1], BtnSize.Width, BtnSize.Height);
                FlageMouseDownWinePos = false;
            }
        }
        if (gpCupIntrol.IsVisible(e.Location))
        {
            using (Image imgTemp = Image.FromFile(strWineCupIntroUpPath))
            {
                dc.DrawImage(imgTemp, BtnLocation[2, 0], BtnLocation[2, 1], BtnSize.Width, BtnSize.Height);
                FlageMouseDownCupIntrol = false;
            }
        }
        if (gpNetPurchase.IsVisible(e.Location))
        {
            using (Image imgTemp = Image.FromFile(strWinePurchaseUpPath))
            {
                dc.DrawImage(imgTemp, BtnLocation[3, 0], BtnLocation[3, 1], BtnSize.Width, BtnSize.Height);
                FlageMouseDownNetPurchase = false;
            }
        }
    }

七、點擊:轉發給原本的事件處理常式

既然畫面上沒有真正的 Button,點擊的商業邏輯就得自己接回來。這裡用 else if 串起四個判斷,命中哪個區域就呼叫對應的既有事件方法,好處是原本的邏輯完全不必改寫。

  /// <summary>
    /// 點擊四個按鈕時的事件變化
    /// </summary>
    private void pxControl_MouseClick(object sender, MouseEventArgs e)
    {
        if (gpRealTime.IsVisible(e.Location))
        {
            btnRealStatus_Click(null, null);
        }
        else if (gpWinePos.IsVisible(e.Location))
        {
            btnWinePosQuery_Click(null, null);
        }
        else if (gpCupIntrol.IsVisible(e.Location))
        {
            btnWineCupIntro_Click(null, null);
        }
        else if (gpNetPurchase.IsVisible(e.Location))
        {
            btnPurchase_Click(null, null);
        }
    }

實作上的提醒

GraphicsPath 用完記得 Dispose,Image.FromFile 每次呼叫都會開啟檔案,放在事件裡高頻呼叫時建議改成预先載入並快取,避免磁碟 I/O 拖累回應速度。另外旗標命名拼成 Flage 只是習慣問題,若想更貼近 C# 慣例,改成 isMouseDownRealTime 這類寫法可讀性會更好。若日後要支援高 DPI 或視窗縮放,座標寫死的方式就必須改成比例換算,這點在規劃階段值得先想清楚。

Copyright © Linux教程網 All Rights Reserved