日期:2008-07-09  浏览次数:20518 次

先让大家看一下我要实现的窗体效果:




该窗体的背景色、圆角半径(下面代码中将圆角半径等于边框宽度,此为为般化处理,参考者可视具体需要而对代码略作修改,使二者具有不同的值,以获得不同的效果)由用户根据需要改变。上、左边框的色彩为白色(其实为DrawEdge过程中colTopLef决定,可设为其他值)到窗体背景色的渐变,下、右边框的色彩为RGB(132, 132, 132)(其实为DrawEdge过程中colBottomRight所决定,也可设为其他值)到窗体背景色的渐变。



要实现该效果,你的窗体(对任拥有hWnd属性的对象,也一样实用)应作如下设置:
1——BorderStyle=0,
2——ScaleMode=3 (所有GDI类API使用的长度单位都为Pixel,对应于该项的设置)。
3——AutoRedraw=True

主要使用了三个过程:
WindowShape ——'重塑窗体轮廓,入口函数,调用后两个函数
MakeRoundCorner——圆角
DrawEdge ——画外框

以下代码为要实现特效的窗体中的代码:
===============================================================
Private Sub Form_Load()
'调用入口函数实现效果
WindowShape hwnd, hdc, BackColor, ScaleWidth + 1, ScaleHeight + 1, 10
'将窗体置顶,
SetWindowPos Me.hwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOACTIVATE Or SWP_SHOWWINDOW Or SWP_NOMOVE Or SWP_NOSIZE
End Sub
===============================================================

以下为具体实现代码,可置于一个module中:
===============================================================
Option Explicit

Private Declare Function SetWindowRgn Lib "user32" (ByVal hwnd As Long, ByVal hRgn As Long, ByVal bRedraw As Boolean) As Long
Private Declare Function CombineRgn Lib "gdi32" (ByVal hDestRgn As Long, ByVal hSrcRgn1 As Long, ByVal hSrcRgn2 As Long, ByVal nCombineMode As Long) As Long
Private Declare Function CreateRoundRectRgn Lib "gdi32" (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
Private Declare Function CreateSolidBrush Lib "gdi32" (ByVal crColor As Long) As Long

Private Declare Function SelectObject Lib "gdi32" (ByVal hdc As Long, ByVal hObject As Long) As Long
Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
Private Declare Function StretchBlt Lib "gdi32" (ByVal hdc As Long, ByVal X As Long, ByVal Y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal nSrcWidth As Long, ByVal nSrcHeight As Long, ByVal dwRop As Long) As Long
Private Declare Function CopyRect Lib "user32" (lpDestRect As RECT, lpSourceRect As RECT) As Long

Private Declare Function GradientFill Lib "gdi32" Alias "GdiGradientFill" (ByVal hdc As Long, pVertex As TRIVERTEX, ByVal dwNumVertex As Long, pMesh As GRADIENT_RECT, ByVal dwNumMesh As Long, ByVal dwMode As Long) As Long
Private Declare Function GradientFillTriangle Lib "msimg32" Alias "GradientFill" (ByVal hdc As Long, pVertex As TRIVERTEX, ByVal dwNumVertex As Long, pMesh As GRADIENT_TRIANGLE, ByVal dwNumMesh As Long, ByVal dwMode As Long) As Long
Private Declare Function OleTranslateColor Lib "olepro32.dll" (ByVal OLE_COLOR As Long, ByVal hPalette As Long, pccolorref As Long) As Long
Private Declare Function RtlMoveMemory Lib "kernel32.dll" (Destination As Any, Source As Any, ByVal Length As Long) As Long
Public Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, ByVal X As Long, ByVal Y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long

Private Const RGN_AND = 1 '交集
Private Const RGN_COPY = 5 '覆盖
Private Const RGN_OR = 2 '并集
Private Const RGN_XOR = 3 '差集
Private Const RGN_DIFF = 4

Public Type RECT
left As Long
top As Long
right As Long
bottom As Long
End Type


Public Enum ESetWindowPosStyles
SWP_SHOWWINDOW = &H40
SWP_HIDEWINDOW = &H80
SW