自己是否想过重画控件,现在用强大的vb来实现吧。
下例就是简单的利用vb中的commandbutton改变成flat3dbutton风格。其实就是利用vb的subclass去处理父窗口的wm_drawitem消息。
1. 建立一个标准exe工程,加入command1和command2,将command1的style属性设为graphical。
2. 加入模块,取名subclass_flat3dbutton,贴进代码:
option explicit
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'copyright 2002 40star, all rights reserved.
'
'e-mail :40star@163.com
'distribution:你可以完全自由随便的使用这段代码,不管你用于任何目的
' 程序在于交流和学习
' 如有任何bug请和我联系
'
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
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, source 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_select = &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 selectobject 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 getsyscolor 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