首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> C語言 >> 關於C語言

C# 自繪視窗邊框與標題列:滑鼠互動基礎架構與繪圖邏輯 (上)

     編輯:關於C語言

視窗區域劃分與繪圖準備

定義視窗各區域常數(邊框、標題列、系統按鈕)並使用 TextureBrush 繪製皮膚:

namespace MouseSkin
{
    private void DrawMiddle_Left(Graphics g)
    {
        using (Brush brush = new TextureBrush(Middle_Left, new Rectangle(0, 0, Middle_Left.Width, Middle_Left.Height)))
        {
            g.FillRectangle(brush, 0, TITLE_WIDTH, Middle_Left.Width, Height - Bottom_Middle.Height - TITLE_WIDTH);
        }
    }
}

滑鼠動作抽象基類

定義 MouseAction 抽象類別,所有具體操作(縮放、拖曳)皆繼承此類:

public abstract class MouseAction
{
    public abstract void Action(int screenX, int screenY, Form form);
}

// 右邊框縮放範例
public class MouseSizeRight : MouseAction
{
    private int _originX;
    public MouseSizeRight(int originX) { _originX = originX; }
    public override void Action(int screenX, int screenY, Form form)
    {
        form.Width = screenX - _originX;
        form.Invalidate();
    }
}

// 視窗拖曳
public class MouseDrag : MouseAction
{
    private int _offsetX, _offsetY;
    public MouseDrag(int hitX, int hitY) { _offsetX = hitX; _offsetY = hitY; }
    public override void Action(int screenX, int screenY, Form form)
    {
        form.Location = new Point(screenX - _offsetX, screenY - _offsetY);
    }
}

滑鼠按下事件初始化

在 MouseDown 中記錄點擊座標,並根據區域建立對應的 MouseAction 實例:

private int LEFT = 5, RIGHT = 5, BOTTOM = 5, TOP = 5, TITLE_WIDTH = 45;
private int x, y; // 暫存座標
private MouseAction mouse; // 當前回應物件

// MouseDown 事件
x = e.X;
y = e.Y;
// 依座標判斷區域並 new 對應 MouseAction 給 mouse 欄位

此架構將滑鼠行為封裝為獨立類別,符合開放封閉原則,易於擴展新的互動模式(如多點觸控、自訂調整手柄)。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved