Roger's Work Blog

一个不成熟的男人为了某种高尚的理想光荣的死去,一个成熟的男人为了某种高尚的理想卑贱的活着。

导航

统计

创建透明窗体

下面的例子实现了该功能。源代码/Files/roger1984/创建透明窗体.rar
>>步骤1----建立新工程。
>>步骤2----编写如下代码:

Private Declare Function GetWindowRect Lib "user32" (ByVal hWnd _
As Long, lpRECT As RECT) As Long
Private Declare Function GetClientRect Lib "user32" (ByVal hWnd _
As Long, lpRECT As RECT) 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 CreateRectRgn Lib "gdi32" (ByVal X1 As _
Long, ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long) _
As Long
Private Declare Function ScreenToClient Lib "user32" (ByVal hWnd _
As Long, lpPoint As POINTAPI) As Long
Private Declare Function SetWindowRgn Lib "user32" (ByVal hWnd As _
Long, ByVal hRgn As Long, ByVal bRedraw As Boolean) As Long

Const RGN_XOR = 3

Private Type POINTAPI
    x As Long
    Y As Long
End Type

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

Private rctClient As RECT, rctFrame As RECT
Private hClient As Long, hFrame As Long

Public Sub MakeTransparent(frm As Form)
    GetFrameClientRgn frm
    SetWindowRgn frm.hWnd, hFrame, True
End Sub

Private Sub GetFrameClientRgn(frm As Form)
    GetWindowRect frm.hWnd, rctFrame
    GetClientRect frm.hWnd, rctClient

'将窗口矩形坐标转换为屏幕坐标
Dim lpTL As POINTAPI, lpBR As POINTAPI
lpTL.x = rctFrame.Left
lpTL.Y = rctFrame.Top
lpBR.x = rctFrame.Right
lpBR.Y = rctFrame.Bottom
ScreenToClient frm.hWnd, lpTL
ScreenToClient frm.hWnd, lpBR
rctFrame.Left = lpTL.x
rctFrame.Top = lpTL.Y
rctFrame.Right = lpBR.x
rctFrame.Bottom = lpBR.Y
rctClient.Left = Abs(rctFrame.Left)
rctClient.Top = Abs(rctFrame.Top)
rctClient.Right = rctClient.Right + Abs(rctFrame.Left)
rctClient.Bottom = rctClient.Bottom + Abs(rctFrame.Top)
rctFrame.Right = rctFrame.Right + Abs(rctFrame.Left)
rctFrame.Bottom = rctFrame.Bottom + Abs(rctFrame.Top)
rctFrame.Top = 0
rctFrame.Left = 0

hClient = CreateRectRgn(rctClient.Left, rctClient.Top, _
rctClient.Right, rctClient.Bottom)
hFrame = CreateRectRgn(rctFrame.Left, rctFrame.Top, _
rctFrame.Right, rctFrame.Bottom)

CombineRgn hFrame, hClient, hFrame, RGN_XOR
End Sub

'在窗体代码中添加以下代码
Private Sub Form_Resize()
    MakeTransparent Me
End Sub

>>步骤3----编译运行,是不是看到效果了。

posted on 2005-12-15 21:00  Roger Yen  阅读(...)  评论(... 编辑 收藏