全國最多中醫師線上諮詢網站-台灣中醫網
發文 回覆 瀏覽次數:5790
推到 Plurk!
推到 Facebook!

VS2003 C# - Panel 控件的外框美化

 
digitraveler
初階會員


發表:89
回覆:91
積分:46
註冊:2005-06-01

發送簡訊給我
#1 引用回覆 回覆 發表時間:2010-08-14 16:00:46 IP:111.254.xxx.xxx 訂閱
.NET 的 Panel 控件非常陽春, 本以為升級到 VS2010 會不會增強一些屬性, 結果看起來跟 VS2003 的 Panel 完全一樣, 沒有改進 , 我還是用我習慣的 VS2003 就好了
.NET 的 Panel 控件外觀只有三種樣式選擇 : BorderStyle = None (無外框) , FixedSingle (單線外框), Fixed3D (立體) , 而立體也只有凹下去的 Style, 沒有凸出來的 Style ; 圖中最上面三種樣式即為標準的 .NET Panel 控件樣式

我們今天來實作可以像 Delphi / C Builder 中的 Panel 有更多的外觀 , 也就是圖中下面六種樣式, 看起來是否更活潑呢 ?



■ 實作方法
在 Panel 控件的 OnPaint (重繪事件) 中畫出我們要的外框, 為了要讓各個 Panel 都可任意設定自己的外框, 又不想在每個 Panel 控件的 OnPaint (重繪事件) 寫一大堆 CODE , 所以我把共用的 CODE 包成一獨立函式 Custmer_PanelPaint() , 透過傳入參數的不同, 決定外框式樣, 以後有時間再把它包成控件

■ 重繪函式

傳入參數
BevelOuter 1:Panel外框為凸起 2:Panel外框為凹下
BevelInner 1:Panel內框為凸起 2:Panel內框為凹下 0:無內框
BorderWidth Panel 外框與內框之間距寬度

例 : 於 Panel 之 OnPaint 事件中 呼叫 Custmer_PanelPaint(sender, e, 2, 1, 1);
表 外框為凹下, 內框為凸起, 內外框間距為 1


[code c#]
private void Custmer_PanelPaint(object sender, System.Windows.Forms.PaintEventArgs e, int BevelOuter, int BevelInner, int BorderWidth)
{
Panel pnl=(Panel)sender;

switch (BevelOuter*10 BevelInner)
{
case 10 : //外框為凸起; 無內框

//BevelOuter (bvRaised)
e.Graphics.DrawLine(new Pen(SystemColors.ControlLightLight),0,0,0,pnl.Height-2); //左
e.Graphics.DrawLine(new Pen(SystemColors.ControlLightLight),0,0,pnl.Width-2,0); //上
e.Graphics.DrawLine(new Pen(SystemColors.ControlDark),pnl.Width-2,pnl.Height-2,0,pnl.Height-2); //下
e.Graphics.DrawLine(new Pen(SystemColors.ControlDark),pnl.Width-2,pnl.Height-2,pnl.Width-2,0); //右

break;
case 11 : //外框為凸起; 內框凸起

//BevelOuter (bvRaised)
e.Graphics.DrawLine(new Pen(SystemColors.ControlLightLight),0,0,0,pnl.Height-2);
e.Graphics.DrawLine(new Pen(SystemColors.ControlLightLight),0,0,pnl.Width-2,0);
e.Graphics.DrawLine(new Pen(SystemColors.ControlDark),pnl.Width-2,pnl.Height-2,0,pnl.Height-2);
e.Graphics.DrawLine(new Pen(SystemColors.ControlDark),pnl.Width-2,pnl.Height-2,pnl.Width-2,0);

if (BorderWidth>0)
{
//BevelInner (bvRaised)
e.Graphics.DrawLine(new Pen(SystemColors.ControlLightLight),BorderWidth 1,BorderWidth 1,BorderWidth 1,pnl.Height-(BorderWidth 3));
e.Graphics.DrawLine(new Pen(SystemColors.ControlLightLight),BorderWidth 1,BorderWidth 1,pnl.Width-(BorderWidth 3),BorderWidth 1);
e.Graphics.DrawLine(new Pen(SystemColors.ControlDark),pnl.Width-(BorderWidth 3),pnl.Height-(BorderWidth 3),BorderWidth 1,pnl.Height-(BorderWidth 3));
e.Graphics.DrawLine(new Pen(SystemColors.ControlDark),pnl.Width-(BorderWidth 3),pnl.Height-(BorderWidth 3),pnl.Width-(BorderWidth 3),BorderWidth 1);
}

break;

case 12 : //外框為凸起; 內框凹下

//BevelOuter (bvRaised)
e.Graphics.DrawLine(new Pen(SystemColors.ControlLightLight),0,0,0,pnl.Height-2);
e.Graphics.DrawLine(new Pen(SystemColors.ControlLightLight),0,0,pnl.Width-2,0);
e.Graphics.DrawLine(new Pen(SystemColors.ControlDark),pnl.Width-2,pnl.Height-2,0,pnl.Height-2);
e.Graphics.DrawLine(new Pen(SystemColors.ControlDark),pnl.Width-2,pnl.Height-2,pnl.Width-2,0);

if (BorderWidth>0)
{
//BevelInner (bvLowered)
e.Graphics.DrawLine(new Pen(SystemColors.ControlDark),BorderWidth,BorderWidth,BorderWidth,pnl.Height-(BorderWidth*1 2));
e.Graphics.DrawLine(new Pen(SystemColors.ControlDark),BorderWidth,BorderWidth,pnl.Width-(BorderWidth*1 2),BorderWidth);
e.Graphics.DrawLine(new Pen(SystemColors.ControlLightLight),pnl.Width-(BorderWidth 2),pnl.Height-(BorderWidth 2),BorderWidth,pnl.Height-(BorderWidth 2));
e.Graphics.DrawLine(new Pen(SystemColors.ControlLightLight),pnl.Width-(BorderWidth 2),pnl.Height-(BorderWidth 2),pnl.Width-(BorderWidth 2),BorderWidth);
}


break;
case 20 : //外框為凹下; 無內框
//BevelOuter (bvLowered)
e.Graphics.DrawLine(new Pen(SystemColors.ControlDark),0,0,0,pnl.Height-0);
e.Graphics.DrawLine(new Pen(SystemColors.ControlDark),0,0,pnl.Width-0,0);
e.Graphics.DrawLine(new Pen(SystemColors.ControlLightLight),pnl.Width-1,pnl.Height-1,0,pnl.Height-1);
e.Graphics.DrawLine(new Pen(SystemColors.ControlLightLight),pnl.Width-1,pnl.Height-1,pnl.Width-1,0);

break;
case 21 : //外框為凹下; 內框凸起

//BevelOuter (bvLowered)
e.Graphics.DrawLine(new Pen(SystemColors.ControlDark),0,0,0,pnl.Height-0);
e.Graphics.DrawLine(new Pen(SystemColors.ControlDark),0,0,pnl.Width-0,0);
e.Graphics.DrawLine(new Pen(SystemColors.ControlLightLight),pnl.Width-1,pnl.Height-1,0,pnl.Height-1);
e.Graphics.DrawLine(new Pen(SystemColors.ControlLightLight),pnl.Width-1,pnl.Height-1,pnl.Width-1,0);

if (BorderWidth>0)
{
//BevelInner (bvRaised)
e.Graphics.DrawLine(new Pen(SystemColors.ControlLightLight),BorderWidth,BorderWidth,BorderWidth,pnl.Height-BorderWidth);
e.Graphics.DrawLine(new Pen(SystemColors.ControlLightLight),BorderWidth,BorderWidth,pnl.Width-BorderWidth,BorderWidth);
e.Graphics.DrawLine(new Pen(SystemColors.ControlDark),pnl.Width-(BorderWidth 1),pnl.Height-(BorderWidth 1),BorderWidth,pnl.Height-(BorderWidth 1));
e.Graphics.DrawLine(new Pen(SystemColors.ControlDark),pnl.Width-(BorderWidth 1),pnl.Height-(BorderWidth 1),pnl.Width-(BorderWidth 1),BorderWidth);
}

break;

case 22 : //外框為凹下; 內框凹下

//BevelOuter (bvLowered)
e.Graphics.DrawLine(new Pen(SystemColors.ControlDark),0,0,0,pnl.Height-0);
e.Graphics.DrawLine(new Pen(SystemColors.ControlDark),0,0,pnl.Width-0,0);
e.Graphics.DrawLine(new Pen(SystemColors.ControlLightLight),pnl.Width-1,pnl.Height-1,0,pnl.Height-1);
e.Graphics.DrawLine(new Pen(SystemColors.ControlLightLight),pnl.Width-1,pnl.Height-1,pnl.Width-1,0);

if (BorderWidth>0)
{
//BevelInner (bvLowered)
e.Graphics.DrawLine(new Pen(SystemColors.ControlDark),BorderWidth 1,BorderWidth 1,BorderWidth 1,pnl.Height-(BorderWidth*1 2));
e.Graphics.DrawLine(new Pen(SystemColors.ControlDark),BorderWidth 1,BorderWidth 1,pnl.Width-(BorderWidth*1 2),BorderWidth 1);
e.Graphics.DrawLine(new Pen(SystemColors.ControlLightLight),pnl.Width-(BorderWidth 2),pnl.Height-(BorderWidth 2),BorderWidth,pnl.Height-(BorderWidth 2));
e.Graphics.DrawLine(new Pen(SystemColors.ControlLightLight),pnl.Width-(BorderWidth 2),pnl.Height-(BorderWidth 2),pnl.Width-(BorderWidth 2),BorderWidth);
}

break;
}
}

[/code]


■ 相關參考
http://tw.myblog.yahoo.com/bruce0211/article?mid=292&prev=-1&next=286


編輯記錄
digitraveler 重新編輯於 2010-08-14 16:08:48, 註解 無‧
sryang
尊榮會員


發表:39
回覆:762
積分:920
註冊:2002-06-27

發送簡訊給我
#2 引用回覆 回覆 發表時間:2010-08-14 17:30:06 IP:124.8.xxx.xxx 訂閱
感謝您的分享!

感覺上微軟吧 Win Form 的重點全擺在 WPF 上了,原有的 WinForm 歷經那麼多個版本還是一點長進也沒有,畫面繪製還是如此的慢
資源使用還是如此的兇。
------
歡迎參訪 "腦殘賤貓的備忘錄" http://maolaoda.blogspot.com/
系統時間:2024-05-01 15:15:47
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!