• C#實現自定義外觀并可拖動和改變大小的窗口

    時間:2010年04月12日 點擊:2087
    自定義窗口在c#中只需重寫OnPaint方法就行了,不過有幾處關鍵之處要設置
    下面給出重會的方法,具體你只要修改下就行了
    protected override void OnPaint(PaintEventArgs e)
    {
    ResourceManager rm
    = new ResourceManager("SafeBox.Resources.Skin", Assembly.GetExecutingAssembly());
    Image TopLeft
    = (Image)(rm.GetObject("TopLeft"));
    Graphics g
    = e.Graphics;
    //畫標題欄左邊
    Brush brush = new TextureBrush(TopLeft, new Rectangle(0, 0, TopLeft.Width, TopLeft.Height));
    g.FillRectangle(brush,
    0, 0, TopLeft.Width, TopLeft.Height);
    //畫標題欄中間
    Brush MidBrush = new SolidBrush(Color.FromArgb(0, 163, 227));
    g.FillRectangle(MidBrush,
    1, TopLeft.Height - 1, DrawForm.ClientSize.Width, 1);
    //畫左邊框
    Brush BoardBrush = new SolidBrush(Color.FromArgb(78, 205, 255));
    g.FillRectangle(BoardBrush,
    0, 0, 2, DrawForm.ClientSize.Height);
    //畫右邊框
    g.FillRectangle(BoardBrush, DrawForm.ClientSize.Width - 2, 0, 2, DrawForm.ClientSize.Height);
    //畫上邊框
    g.FillRectangle(BoardBrush, 0, 0, DrawForm.ClientSize.Width, 2);
    //畫下邊框
    g.FillRectangle(BoardBrush, 0, DrawForm.ClientSize.Height - 2, DrawForm.ClientSize.Width, 2);
    base.OnPaint(e);
    }

    這樣寫好后窗口樣式變了,不過你首先得把窗口的 FormBoardStyle 設為None,但光這樣無法拖動窗口,
    下面是拖動和改變窗口的方法:重寫WndProc
    switch (Msg.Msg)
    {
    case 0x0084: //WM_NCHITTEST改變窗口大小
    {
    // 分解當前鼠標的坐標

    int nPosX = (Msg.LParam.ToInt32() & 65535);
    int nPosY = (Msg.LParam.ToInt32() >> 16);
    if (nPosX >= this.Left + this.Width - 6 && nPosY >= this.Top + this.Height - 6)
    {
    // 鼠標位置在窗體的右下角附近
    Msg.Result = new IntPtr(HTBOTTOMRIGHT);
    return;
    }
    else if (nPosX >= this.Left + this.Width - 2)
    {
    // 鼠標位置在窗體右側
    Msg.Result = new IntPtr(HTRIGHT);
    return;
    }
    else if (nPosY >= this.Top + this.Height - 2)
    {
    // 鼠標位置在窗體下方
    Msg.Result = new IntPtr(HTBOTTOM);
    return;
    }
    else if (nPosX <= this.Left + 2)
    {
    // 鼠標位置在窗體左側
    Msg.Result = new IntPtr(HTLEFT);
    return;
    }
    else if (nPosY <= this.Top + 2)
    {
    // 鼠標位置在窗體上側
    Msg.Result = new IntPtr(HTTOP);
    return;
    }
    // 以上只判斷鼠標位置是否在右側,右下角,下方,所以僅僅當鼠標指針在這三個位置時才會改動成改動大小的形狀,拖動后可改動大小。
    break;
    }
    case 0x0201://移動窗口
    {
    m.Msg
    = 0x00A1;//更改消息為非客戶區按下鼠標
    m.LParam = IntPtr.Zero;//默認值
    m.WParam = new IntPtr(2);//鼠標放在標題欄內
    }
    default:
    break;
    }

    使用以上方法時別忘了加入自定義常數哦:
    const int HTERROR = -2;
    const int HTTRANSPARENT = -1;
    const int HTNOWHERE = 0;
    const int HTCLIENT = 1;
    const int HTCAPTION = 2;
    const int HTSYSMENU = 3;
    const int HTGROWBOX = 4;
    const int HTSIZE = HTGROWBOX;
    const int HTMENU = 5;
    const int HTHSCROLL = 6;
    const int HTVSCROLL = 7;
    const int HTMINBUTTON = 8;
    const int HTMAXBUTTON = 9;
    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 = 16;
    const int HTBOTTOMRIGHT = 17;
    const int HTBORDER = 18;
    const int HTREDUCE = HTMINBUTTON;
    const int HTZOOM = HTMAXBUTTON;
    const int HTSIZEFIRST = HTLEFT;
    const int HTSIZELAST = HTBOTTOMRIGHT;

    好了,OK了吧,能拖動,能改變大小,而且外觀也自定義了,你移動看看,出問題了吧,窗口的殘留畫面去不了,不用急,在窗口的構造函數中加入:
    this.SetStyle(ControlStyles.ResizeRedraw, true);行了吧,又出問題了,窗口會閃,再設置下窗口的屬性:DoubleBuffered=true,ok了吧,,這個對控件雙緩存處理,這樣一個完美的自定義外觀并可拖動和改變大小的窗口就搞定了,,當然這不是標準的解決方法,標準的應該重繪標題欄等吧,等有時間有貼啦
    效果如下:
    智動軟件

    贊助商鏈接

    熱門內容

    相關內容

    聯系我們

    聯系方式
    亚洲AV口工动漫在线观看无码