转:重绘panel边框方法

今天搜索到这个帖子,赶紧收藏了~~

http://delphi.ktop.com.tw/board.php?cid=169&fid=1220&tid=101360

 

NET 的 Panel 控件非常陽春, 本以為升級到 VS2010 會不會增強一些屬性, 結果看起來跟 VS2003 的 Panel 完全一樣, 沒有改進 , 我還是用我習慣的 VS2003 就好了
.NET 的 Panel 控件外觀只有三種樣式選擇 : BorderStyle = None (無外框) , FixedSingle (單線外框), Fixed3D (立體) , 而立體也只有凹下去的 Style, 沒有凸出來的 Style ; 圖中最上面三種樣式即為標準的 .NET Panel 控件樣式
 
我們今天來實作可以像 Delphi / C++Builder 中的 Panel 有更多的外觀 , 也就是圖中下面六種樣式, 看起來是否更活潑呢 ? 

转:重绘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


  1. private void Custmer_PanelPaint(object sender, System.Windows.Forms.PaintEventArgs e, int BevelOuter, int BevelInner, int BorderWidth)  
  2. {  
  3.     Panel pnl=(Panel)sender;  
  4.    
  5.     switch (BevelOuter*10+BevelInner)  
  6.     {  
  7.        case 10 : //外框為凸起; 無內框  
  8.        
  9.         //BevelOuter (bvRaised)  
  10.         e.Graphics.DrawLine(new Pen(SystemColors.ControlLightLight),0,0,0,pnl.Height-2);  //左  
  11.         e.Graphics.DrawLine(new Pen(SystemColors.ControlLightLight),0,0,pnl.Width-2,0);   //上  
  12.         e.Graphics.DrawLine(new Pen(SystemColors.ControlDark),pnl.Width-2,pnl.Height-2,0,pnl.Height-2); //下  
  13.         e.Graphics.DrawLine(new Pen(SystemColors.ControlDark),pnl.Width-2,pnl.Height-2,pnl.Width-2,0);  //右  
  14.        
  15.         break;  
  16.        case 11 : //外框為凸起; 內框凸起  
  17.        
  18.         //BevelOuter (bvRaised)  
  19.         e.Graphics.DrawLine(new Pen(SystemColors.ControlLightLight),0,0,0,pnl.Height-2);  
  20.         e.Graphics.DrawLine(new Pen(SystemColors.ControlLightLight),0,0,pnl.Width-2,0);    
  21.         e.Graphics.DrawLine(new Pen(SystemColors.ControlDark),pnl.Width-2,pnl.Height-2,0,pnl.Height-2);  
  22.         e.Graphics.DrawLine(new Pen(SystemColors.ControlDark),pnl.Width-2,pnl.Height-2,pnl.Width-2,0);  
  23.        
  24.         if (BorderWidth>0)  
  25.         {  
  26.           //BevelInner (bvRaised)  
  27.           e.Graphics.DrawLine(new Pen(SystemColors.ControlLightLight),BorderWidth+1,BorderWidth+1,BorderWidth+1,pnl.Height-(BorderWidth+3));  
  28.           e.Graphics.DrawLine(new Pen(SystemColors.ControlLightLight),BorderWidth+1,BorderWidth+1,pnl.Width-(BorderWidth+3),BorderWidth+1);    
  29.           e.Graphics.DrawLine(new Pen(SystemColors.ControlDark),pnl.Width-(BorderWidth+3),pnl.Height-(BorderWidth+3),BorderWidth+1,pnl.Height-(BorderWidth+3));  
  30.           e.Graphics.DrawLine(new Pen(SystemColors.ControlDark),pnl.Width-(BorderWidth+3),pnl.Height-(BorderWidth+3),pnl.Width-(BorderWidth+3),BorderWidth+1);  
  31.         }   
  32.        
  33.         break;  
  34.    
  35.        case 12 : //外框為凸起; 內框凹下  
  36.        
  37.         //BevelOuter (bvRaised)  
  38.         e.Graphics.DrawLine(new Pen(SystemColors.ControlLightLight),0,0,0,pnl.Height-2);  
  39.         e.Graphics.DrawLine(new Pen(SystemColors.ControlLightLight),0,0,pnl.Width-2,0);    
  40.         e.Graphics.DrawLine(new Pen(SystemColors.ControlDark),pnl.Width-2,pnl.Height-2,0,pnl.Height-2);  
  41.         e.Graphics.DrawLine(new Pen(SystemColors.ControlDark),pnl.Width-2,pnl.Height-2,pnl.Width-2,0);  
  42.        
  43.         if (BorderWidth>0)  
  44.         {  
  45.           //BevelInner (bvLowered)  
  46.           e.Graphics.DrawLine(new Pen(SystemColors.ControlDark),BorderWidth,BorderWidth,BorderWidth,pnl.Height-(BorderWidth*1+2));  
  47.           e.Graphics.DrawLine(new Pen(SystemColors.ControlDark),BorderWidth,BorderWidth,pnl.Width-(BorderWidth*1+2),BorderWidth);    
  48.           e.Graphics.DrawLine(new Pen(SystemColors.ControlLightLight),pnl.Width-(BorderWidth+2),pnl.Height-(BorderWidth+2),BorderWidth,pnl.Height-(BorderWidth+2));  
  49.           e.Graphics.DrawLine(new Pen(SystemColors.ControlLightLight),pnl.Width-(BorderWidth+2),pnl.Height-(BorderWidth+2),pnl.Width-(BorderWidth+2),BorderWidth);  
  50.         }  
  51.        
  52.              
  53.         break;    
  54.        case 20 : //外框為凹下; 無內框  
  55.         //BevelOuter (bvLowered)  
  56.         e.Graphics.DrawLine(new Pen(SystemColors.ControlDark),0,0,0,pnl.Height-0);  
  57.         e.Graphics.DrawLine(new Pen(SystemColors.ControlDark),0,0,pnl.Width-0,0);    
  58.         e.Graphics.DrawLine(new Pen(SystemColors.ControlLightLight),pnl.Width-1,pnl.Height-1,0,pnl.Height-1);  
  59.         e.Graphics.DrawLine(new Pen(SystemColors.ControlLightLight),pnl.Width-1,pnl.Height-1,pnl.Width-1,0);  
  60.        
  61.         break;      
  62.        case 21 : //外框為凹下; 內框凸起  
  63.        
  64.         //BevelOuter (bvLowered)  
  65.         e.Graphics.DrawLine(new Pen(SystemColors.ControlDark),0,0,0,pnl.Height-0);  
  66.         e.Graphics.DrawLine(new Pen(SystemColors.ControlDark),0,0,pnl.Width-0,0);    
  67.         e.Graphics.DrawLine(new Pen(SystemColors.ControlLightLight),pnl.Width-1,pnl.Height-1,0,pnl.Height-1);  
  68.         e.Graphics.DrawLine(new Pen(SystemColors.ControlLightLight),pnl.Width-1,pnl.Height-1,pnl.Width-1,0);  
  69.    
  70.         if (BorderWidth>0)  
  71.         {  
  72.           //BevelInner (bvRaised)  
  73.           e.Graphics.DrawLine(new Pen(SystemColors.ControlLightLight),BorderWidth,BorderWidth,BorderWidth,pnl.Height-BorderWidth);  
  74.           e.Graphics.DrawLine(new Pen(SystemColors.ControlLightLight),BorderWidth,BorderWidth,pnl.Width-BorderWidth,BorderWidth);    
  75.           e.Graphics.DrawLine(new Pen(SystemColors.ControlDark),pnl.Width-(BorderWidth+1),pnl.Height-(BorderWidth+1),BorderWidth,pnl.Height-(BorderWidth+1));  
  76.           e.Graphics.DrawLine(new Pen(SystemColors.ControlDark),pnl.Width-(BorderWidth+1),pnl.Height-(BorderWidth+1),pnl.Width-(BorderWidth+1),BorderWidth);  
  77.         }  
  78.    
  79.         break;  
  80.      
  81.        case 22 : //外框為凹下; 內框凹下  
  82.        
  83.         //BevelOuter (bvLowered)  
  84.         e.Graphics.DrawLine(new Pen(SystemColors.ControlDark),0,0,0,pnl.Height-0);  
  85.         e.Graphics.DrawLine(new Pen(SystemColors.ControlDark),0,0,pnl.Width-0,0);    
  86.         e.Graphics.DrawLine(new Pen(SystemColors.ControlLightLight),pnl.Width-1,pnl.Height-1,0,pnl.Height-1);  
  87.         e.Graphics.DrawLine(new Pen(SystemColors.ControlLightLight),pnl.Width-1,pnl.Height-1,pnl.Width-1,0);  
  88.    
  89.         if (BorderWidth>0)  
  90.         {  
  91.         //BevelInner (bvLowered)  
  92.         e.Graphics.DrawLine(new Pen(SystemColors.ControlDark),BorderWidth+1,BorderWidth+1,BorderWidth+1,pnl.Height-(BorderWidth*1+2));  
  93.         e.Graphics.DrawLine(new Pen(SystemColors.ControlDark),BorderWidth+1,BorderWidth+1,pnl.Width-(BorderWidth*1+2),BorderWidth+1);    
  94.         e.Graphics.DrawLine(new Pen(SystemColors.ControlLightLight),pnl.Width-(BorderWidth+2),pnl.Height-(BorderWidth+2),BorderWidth,pnl.Height-(BorderWidth+2));  
  95.         e.Graphics.DrawLine(new Pen(SystemColors.ControlLightLight),pnl.Width-(BorderWidth+2),pnl.Height-(BorderWidth+2),pnl.Width-(BorderWidth+2),BorderWidth);  
  96.         }   
  97.    
  98.         break;  
  99.     }  
  100. }