绘按钮简例
小例子 程序代码:
.386 .Model Flat,Stdcall Option Casemap :None Include Windows.inc Include Kernel32.inc Include User32.inc Include Gdi32.inc Include /MASMPlus/Macros/Strings.Mac IncludeLib Kernel32.lib IncludeLib User32.lib IncludeLib Gdi32.lib .Data? hInstance dd ? lpOldButton dd ? btnState dd ? ; Button State ; -------------- ; Normal : FALSE ; Click : TRUE ; Hover : TRUE ; -------------- .Code BtnHover Proc Uses ebx _hDC:DWORD,_lpstRect:DWORD Local @hBrush,@hPen mov ebx,_lpstRect invoke CreateSolidBrush,00000000h ; 可以使用图片填充 mov @hBrush,eax invoke CreatePen,PS_SOLID,0,00f4c593h mov @hPen,eax invoke SelectObject,_hDC,@hBrush invoke SelectObject,_hDC,@hPen inc (RECT ptr [ebx]).left inc (RECT ptr [ebx]).top dec (RECT ptr [ebx]).right dec (RECT ptr [ebx]).bottom invoke Rectangle,_hDC,(RECT ptr [ebx]).left,(RECT ptr [ebx]).top,(RECT ptr [ebx]).right,(RECT ptr [ebx]).bottom invoke DeleteObject,@hBrush invoke DeleteObject,@hPen ret BtnHover EndP BtnNormal Proc Uses ebx _hDC:DWORD,_lpstRect:DWORD Local @hBrush,@hPen mov ebx,_lpstRect invoke CreateSolidBrush,0ffffffh ; 可以使用图片填充 mov @hBrush,eax invoke CreatePen,PS_SOLID,0,00f4c593h mov @hPen,eax invoke SelectObject,_hDC,@hBrush invoke SelectObject,_hDC,@hPen inc (RECT ptr [ebx]).left inc (RECT ptr [ebx]).top dec (RECT ptr [ebx]).right dec (RECT ptr [ebx]).bottom invoke Rectangle,_hDC,(RECT ptr [ebx]).left,(RECT ptr [ebx]).top,(RECT ptr [ebx]).right,(RECT ptr [ebx]).bottom invoke DeleteObject,@hBrush invoke DeleteObject,@hPen ret BtnNormal EndP NewBtnProc Proc _hWnd:DWORD,_uMsg:DWORD,_wParam:DWORD,_lParam:DWORD Local @stTme:TRACKMOUSEEVENT .if _uMsg == WM_MOUSEMOVE ; Mouse move msg .if (!btnState) lea ebx,@stTme assume ebx: ptr TRACKMOUSEEVENT mov [ebx].cbSize,sizeof TRACKMOUSEEVENT m2m [ebx].hwndTrack,_hWnd mov [ebx].dwFlags,TME_LEAVE or TME_HOVER mov [ebx].dwHoverTime,1 assume ebx: nothing invoke TrackMouseEvent,addr @stTme ; Send leave and hover Msg .endif .elseif _uMsg == WM_MOUSELEAVE ; Mouse leave msg mov btnState,FALSE invoke InvalidateRect,_hWnd,NULL,TRUE ; Redraw .elseif _uMsg == WM_MOUSEHOVER ; Mouse hover msg mov btnState,TRUE invoke InvalidateRect,_hWnd,NULL,TRUE ; Redraw .else invoke CallWindowProc,lpOldButton,_hWnd,_uMsg,_wParam,_lParam ret .endif xor eax,eax ret NewBtnProc EndP WndProc Proc Uses ebx edi esi _hWnd:DWORD,_uMsg:DWORD,_wParam:DWORD,_lParam:DWORD ; Dialog CallBack .if _uMsg == WM_INITDIALOG ; Dialog initialization invoke GetDlgItem,_hWnd,1001 ; SubCalss Button invoke SetWindowLong,eax,GWL_WNDPROC,offset NewBtnProc mov lpOldButton,eax .elseif _uMsg == WM_CLOSE ; Dialog terminate invoke EndDialog,_hWnd,NULL .elseif _uMsg == WM_DRAWITEM ; WM_DRAWITEM message ; wParam Control Identifier ; lParam lpDRAWITEMSTRUCT .if _wParam == 1001 mov ebx,_lParam assume ebx: ptr DRAWITEMSTRUCT .if [ebx].itemState & ODS_SELECTED ; Button State : Selected invoke BtnHover,[ebx].hdc,addr [ebx].rcItem .else .if (!btnState) invoke BtnNormal,[ebx].hdc,addr [ebx].rcItem .else invoke BtnHover,[ebx].hdc,addr [ebx].rcItem .endif .endif assume ebx: nothing .endif .elseif _uMsg == WM_COMMAND mov eax,_wParam and eax,0FFFFh .if eax == 1001 ; Button invoke MessageBox,0,$CTA0("你点击了按钮"),0,0 .endif .else mov eax,FALSE ret .endif mov eax,TRUE ret WndProc EndP Start Proc invoke GetModuleHandle,NULL ; Get the module handle mov hInstance,eax invoke DialogBoxParam,hInstance,1000,NULL,WndProc,NULL ; Dialog ret Start EndP End Start
Rc
程序代码:
#include <resource.h> 1000 DIALOG 0, 0, 227, 170 STYLE DS_CENTER | WS_POPUP | WS_CAPTION | WS_SYSMENU CAPTION "Button Example Works 2009" FONT 9, "宋体" { CONTROL "", 1001, BUTTON, BS_OWNERDRAW | WS_CHILD | WS_VISIBLE, 79, 63, 69, 29 }
MASMPlus Works:
DrawButton.rar
(66.11 KB)
[ 本帖最后由 sll0807 于 2009-11-1 23:44 编辑 ]