介面開發之Flat3DButton (轉)

worldblog發表於2007-12-12
介面開發之Flat3DButton (轉)[@more@]


自己是否想過重畫,現在用強大的VB來實現吧。

下例就是簡單的利用VB中的Commanutton改變成Flat3DButton風格。其實就是利用VB的SubClass去處理父視窗的WM_DRAWITEM訊息。

1. 建立一個標準EXE工程,加入Command1和Command2,將Command1的Style屬性設為Graphical。

2. 加入模組,取名SubClass_Flat3DButton,貼進程式碼:

Option Explicit

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'Copyright 2002 40Star, All Rights Reserved.
'
'E-  :
'Distribution:你可以完全自由隨便的使用這段程式碼,不管你用於任何目的
'  在於交流和學習
'  如有任何請和我聯絡
'
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

Private Declare Function GetParent Lib "user32" _
  (ByVal hWnd As Long) As Long

Private Declare Function GetWindowLong Lib "user32" Alias _
  "GetWindowLongA" (ByVal hWnd As Long, _
  ByVal nIndex As Long) As Long

Private Declare Function SetWindowLong Lib "user32" Alias _
  "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex _
  As Long, ByVal dwNewLong As Long) As Long
 
Private Declare Function CallWindowProc Lib "user32" Alias _
  "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal _
  hWnd As Long, ByVal Msg As Long, ByVal wParam As _
  Long, ByVal lParam As Long) As Long
 
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" _
  (Destination As Any, As Any, ByVal Length As Long)
 
Const GWL_WNDPROC = (-4&)

Dim PrevWndProc&

Private Const WM_DESTROY = &H2
Private Const WM_DRAWITEM = &H2B

Private Type RECT
  Left As Long
  Top As Long
  Right As Long
  Bottom As Long
End Type

Private Type DRAWITEMSTRUCT
  CtlType As Long
  CtlID As Long
  itemID As Long
  itemAction As Long
  itemState As Long
  hwndItem As Long
  hdc As Long
  rcItem As RECT
  itemData As Long
End Type

' Owner draw constants
Private Const ODT_BUTTON = 4
' Owner draw actions
Private Const ODA_DRAWENTIRE = &H1
Private Const ODA_ = &H2
Private Const ODA_FOCUS = &H4
' Owner draw state
Private Const ODS_SELECTED = &H1
Private Const ODS_GRAYED = &H2
Private Const ODS_DISABLED = &H4
Private Const ODS_CHECKED = &H8
Private Const ODS_FOCUS = &H10

Private Declare Function GetWindowText Lib "user32" Alias _
  "GetWindowTextA" (ByVal hWnd As Long, ByVal lpString As String, _
  ByVal cch As Long) As Long

'Various GDI painting-related functions
Private Declare Function Select Lib "gdi32" (ByVal hdc As Long, ByVal hObject As Long) As Long

Private Declare Function CreateSolidBrush Lib "gdi32" (ByVal crColor As Long) As Long

Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long

Private Declare Function GetSylor Lib "user32" (ByVal nIndex As Long) As Long
' Color Types
Const CTLCOLOR_MSGBOX = 0
Const CTLCOLOR_EDIT = 1
Const CTLCOLOR_LISTBOX = 2
Const CTLCOLOR_BTN = 3
Const CTLCOLOR_DLG = 4
Const CTLCOLOR_SCROLLBAR = 5
Const CTLCOLOR_STATIC = 6
Const CTLCOLOR_MAX = 8  '  three bits max

Const COLOR_SCROLLBAR = 0
Const COLOR_BACKGROUND = 1
Const COLOR_ACTIVECAPTION = 2
Const COLOR_INACTIVECAPTION = 3
Const COLOR_MENU = 4
Const COLOR_WINDOW = 5
Const COLOR_WINDOWFRAME = 6
Const COLOR_MENUTEXT = 7
Const COLOR_WINDOWTEXT = 8
Const COLOR_CAPTIONTEXT = 9
Const COLOR_ACTIVEBORDER = 10
Const COLOR_INACTIVEBORDER = 11
Const COLOR_APPWORKSPACE = 12
Const COLOR_HIGHLIGHT = 13
Const COLOR_HIGHLIGHTTEXT = 14
Const COLOR_BTNFACE = 15
Const COLOR_BTNSHADOW = 16
Const COLOR_GRAYTEXT = 17
Const COLOR_BTNTEXT = 18
Const COLOR_INACTIVECAPTIONTEXT = 19
Const COLOR_BTNHIGHLIGHT = 20

Private Declare Function FillRect Lib "user32" (ByVal hdc As Long, lpRect As RECT, ByVal hBrush As Long) As Long

Private Declare Function FrameRect Lib "user32" (ByVal hdc As Long, lpRect As RECT, ByVal hBrush As Long) As Long

Private Declare Function RoundRect Lib "gdi32" (ByVal hdc As Long, ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long, ByVal X3 As Long, ByVal Y3 As Long) As Long

'pen
Private Declare Function CreatePen Lib "gdi32" (ByVal nPenStyle As Long, ByVal nWidth As Long, ByVal crColor As Long) As Long
'  Pen Styles
Const PS_SOLID = 0
Const PS_DASH = 1  '  -------
Const PS_DOT = 2  '  .......
Const PS_DASHDOT = 3  '  _._._._
Const PS_DASHDOTDOT = 4  '  _.._.._
Const PS_NULL = 5
Const PS_INSFRAME = 6
Const PS_USERSTYLE = 7
Const PS_ALTERNATE = 8
Const PS_STYLE_MASK = &HF

Private Declare Function MoveToEx Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, lpPoint As POINTAPI) As Long

Private Declare Function Lo Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long) As Long

Private Type POINTAPI
  x As Long
  y As Long
End Type

Private Declare Function DrawText Lib "user32" Alias "DrawTextA" _
  (ByVal hdc As Long, ByVal lpStr As String, ByVal nCount As Long, _
  lpRect As RECT, ByVal wFormat As Long) As Long

Private Const DT_SINGLELINE = &H20
Private Const DT_CENTER = &H1
Private Const DT_VCENTER = &H4

Private Declare Function SetTextColor Lib "gdi32" (ByVal hdc As Long, _
  ByVal crColor As Long) As Long
 
Private Declare Function SetBkMode Lib "gdi32" (ByVal hdc As Long, _
  ByVal nBkMode As Long) As Long
 
Private Const TRANSPARENT = 1


Private Sub DrawButton(ByVal hWnd As Long, ByVal hdc As Long, _
rct As RECT, ByVal nState As Long)

  Dim P As POINTAPI
  Dim s As String
  Dim hbr As Long
  Dim hpen As Long
 
  hbr = CreateSolidBrush(GetSysColor(COLOR_BTNFACE))
  SelectObject hdc, hbr
  FillRect hdc, rct, hbr
  DeleteObject hbr
 
  '畫文字時背景為透明狀
  SetBkMode hdc, TRANSPARENT
  '得到Button的Caption
  s = String$(255, 0)
  GetWindowText hWnd, s, 255
  s = Left$(s, InStr(s, Chr$(0)) - 1)
  '根據Button的Enabled狀態進行重畫
  If (nState And ODS_DISABLED) = ODS_DISABLED Then
  '畫內側3D效果->亮色
  hpen = CreatePen(PS_SOLID, 1, GetSysColor(COLOR_BTNHIGHLIGHT))
  SelectObject hdc, hpen
  MoveToEx hdc, rct.Left, rct.Top, P
  LineTo hdc, rct.Right, rct.Top
  MoveToEx hdc, rct.Left, rct.Top, P
  LineTo hdc, rct.Left, rct.Bottom
  DeleteObject hpen
  '畫內側3D效果->暗色
  hpen = CreatePen(PS_SOLID, 1, GetSysColor(COLOR_BTNSHADOW))
  SelectObject hdc, hpen
  MoveToEx hdc, rct.Left, rct.Bottom - 1, P
  LineTo hdc, rct.Right, rct.Bottom - 1
  MoveToEx hdc, rct.Right - 1, rct.Top, P
  LineTo hdc, rct.Right - 1, rct.Bottom
  DeleteObject hpen
  '畫陰影文字
  rct.Left = rct.Left + 1
  rct.Right = rct.Right + 1
  rct.Bottom = rct.Bottom + 1
  rct.Top = rct.Top + 1
  SetTextColor hdc, GetSysColor(COLOR_BTNHIGHLIGHT)
  DrawText hdc, s, Len(s), rct, DT_CENTER Or DT_SINGLELINE Or DT_VCENTER
  rct.Left = rct.Left - 1
  rct.Right = rct.Right - 1
  rct.Bottom = rct.Bottom - 1
  rct.Top = rct.Top - 1
  SetTextColor hdc, GetSysColor(COLOR_GRAYTEXT)
  DrawText hdc, s, Len(s), rct, DT_CENTER Or DT_SINGLELINE Or DT_VCENTER
  Exit Sub
  End If
  '按下Button時重畫
  If (nState And ODS_SELECTED) = ODS_SELECTED Then
  '畫外圍黑框
  hbr = CreateSolidBrush(GetSysColor(COLOR_BTNTEXT))
  SelectObject hdc, hbr
  FrameRect hdc, rct, hbr
  DeleteObject hbr
  hbr = CreateSolidBrush(GetSysColor(COLOR_BTNSHADOW))
  SelectObject hdc, hbr
  rct.Left = rct.Left + 1
  rct.Right = rct.Right - 1
  rct.Bottom = rct.Bottom - 1
  rct.Top = rct.Top + 1
  FrameRect hdc, rct, hbr
  DeleteObject hbr
 
  rct.Left = rct.Left + 1
  rct.Right = rct.Right + 1
  rct.Bottom = rct.Bottom + 1
  rct.Top = rct.Top + 1
  SetTextColor hdc, GetSysColor(COLOR_BTNTEXT)
  DrawText hdc, s, Len(s), rct, DT_CENTER Or DT_SINGLELINE Or DT_VCENTER
  Exit Sub
  End If
  'Button得到焦點時重畫
  If (nState And ODS_FOCUS) = ODS_FOCUS Then
  '畫外圍黑框
  hbr = CreateSolidBrush(GetSysColor(COLOR_BTNTEXT))
  SelectObject hdc, hbr
  FrameRect hdc, rct, hbr
  DeleteObject hbr
  '畫內側3D效果->亮色
  hpen = CreatePen(PS_SOLID, 1, GetSysColor(COLOR_BTNHIGHLIGHT))
  SelectObject hdc, hpen
  MoveToEx hdc, rct.Left + 1, rct.Top + 1, P
  LineTo hdc, rct.Right - 1, rct.Top + 1
  MoveToEx hdc, rct.Left + 1, rct.Top + 1, P
  LineTo hdc, rct.Left + 1, rct.Bottom - 1
  DeleteObject hpen
  '畫內側3D效果->暗色
  hpen = CreatePen(PS_SOLID, 1, GetSysColor(COLOR_BTNSHADOW))
  SelectObject hdc, hpen
  MoveToEx hdc, rct.Left + 1, rct.Bottom - 2, P
  LineTo hdc, rct.Right - 1, rct.Bottom - 2
  MoveToEx hdc, rct.Right - 2, rct.Top + 1, P
  LineTo hdc, rct.Right - 2, rct.Bottom - 1
  DeleteObject hpen
 
  SetTextColor hdc, GetSysColor(COLOR_BTNTEXT)
  DrawText hdc, s, Len(s), rct, DT_CENTER Or DT_SINGLELINE Or DT_VCENTER
  Else
  '畫內側3D效果->亮色
  hpen = CreatePen(PS_SOLID, 1, GetSysColor(COLOR_BTNHIGHLIGHT))
  SelectObject hdc, hpen
  MoveToEx hdc, rct.Left, rct.Top, P
  LineTo hdc, rct.Right, rct.Top
  MoveToEx hdc, rct.Left, rct.Top, P
  LineTo hdc, rct.Left, rct.Bottom
  DeleteObject hpen
  '畫內側3D效果->暗色
  hpen = CreatePen(PS_SOLID, 1, GetSysColor(COLOR_BTNSHADOW))
  SelectObject hdc, hpen
  MoveToEx hdc, rct.Left, rct.Bottom - 1, P
  LineTo hdc, rct.Right, rct.Bottom - 1
  MoveToEx hdc, rct.Right - 1, rct.Top, P
  LineTo hdc, rct.Right - 1, rct.Bottom
  DeleteObject hpen
  '畫陰影文字
  SetTextColor hdc, GetSysColor(COLOR_BTNTEXT)
  DrawText hdc, s, Len(s), rct, DT_CENTER Or DT_SINGLELINE Or DT_VCENTER
  End If
End Sub

Private Function SubWndProc(ByVal hWnd As Long, ByVal Msg As Long, _
  ByVal wParam As Long, ByVal lParam As Long) _
  As Long
  Dim di As DRAWITEMSTRUCT
  If Msg = WM_DESTROY Then Tenate (hWnd)
  '處理自畫訊息
  If Msg = WM_DRAWITEM Then
  CopyMemory di, ByVal lParam, Len(di)
  '判斷是自畫Button
  If di.CtlType = ODT_BUTTON Then
 
  DrawButton di.hwndItem, di.hdc, di.rcItem, di.itemState
  '不返回VB的預設Button繪製過程
  SubWndProc = 1
  Exit Function
  End If
 
  End If
  SubWndProc = CallWindowProc(PrevWndProc, hWnd, Msg, wParam, lParam)
End Function

Public Sub Init(hWnd As Long)
  PrevWndProc = SetWindowLong(hWnd, GWL_WNDPROC, AddressOf SubWndProc)
End Sub

Public Sub Terminate(hWnd As Long)
  Call SetWindowLong(hWnd, GWL_WNDPROC, PrevWndProc)
End Sub
' -- 模組結束 -- '

3. Form1中的程式碼:

Option Explicit

Private Sub Form_Load()
Call Init(Me.hWnd)
End Sub

Private Sub Form_Unload(Cancel As Integer)
Call Terminate(Me.hWnd)
End Sub

4. 結束語

怎麼樣,看到兩個Button之間的不同了麼

本程式在 + 中透過。


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10752043/viewspace-992035/,如需轉載,請註明出處,否則將追究法律責任。

相關文章