窗口界面编程之一:VB实现简单异形窗口

一、运行效果图(在Win8里的运行效果,在XP里运行就不能体现出来,因为我使用的XP的界面效果)

窗口界面编程之一:VB实现简单异形窗口_第1张图片


二、编译环境:Visual Basic 6.0 (SP6)


三、实现原理:通过区域合并

窗口界面编程之一:VB实现简单异形窗口_第2张图片

四、使用API函数:CreateRectRgn、CreateRoundRectRgn、CombineRgn、SetWindowRgn,其声明及说明如下:

函数:CreateRectRgn 
声明:Declare Function CreateRectRgn Lib "gdi32" Alias "CreateRectRgn" (ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long) As Long 
说明:创建一个由点X1,Y1和X2,Y2描述的矩形区域 

函数:CreateRoundRectRgn 
声明:Declare Function CreateRoundRectRgn Lib "gdi32" Alias "CreateRoundRectRgn" (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 
说明:创建一个圆角矩形,该矩形由X1,Y1-X2,Y2确定,并由X3,Y3确定的椭圆描述圆角弧度 

函数:CombineRgn 
声明:Declare Function CombineRgn Lib "gdi32" Alias "CombineRgn" (ByVal hDestRgn As Long, ByVal hSrcRgn1 As Long, ByVal hSrcRgn2 As Long, ByVal nCombineMode As Long) As Long 
说明:将两个区域组合为一个新区域 

函数:SetWindowRgn 
声明:Declare Function SetWindowRgn Lib "user32" Alias "SetWindowRgn" (ByVal hWnd As Long, ByVal hRgn As Long, ByVal bRedraw As Boolean) As Long 
说明:这是那些很难有人注意到的对编程者来说是个巨大的宝藏的隐含的API函数中的一个。本函数允许您改变窗口的区域。通常所有窗口都是矩形的——窗口一旦存在就含有一个矩形区域。本函数允许您放弃该区域。这意味着您可以创建圆的、星形的窗口,也可以将它分为两个或许多部分——实际上可以是任何形状。


五、开始动手:创建一个无标题栏无边框的窗口,载入一张图片作为窗口背景,使用Image控件作为按钮,在窗口加截函数中处理窗口区域,在窗口鼠标按下函数中加入移动消息,使用API函数CloseWindow实现最小化。
程序源码:

 

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 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 CombineRgn Lib "gdi32" (ByVal hDestRgn As Long, ByVal hSrcRgn1 As Long, ByVal hSrcRgn2 As Long, ByVal nCombineMode As Long) As Long
Private Declare Function SetWindowRgn Lib "user32" (ByVal hwnd As Long, ByVal hRgn As Long, ByVal bRedraw As Boolean) As Long
Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Declare Function CloseWindow Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function ReleaseCapture Lib "user32" () As Long
Dim rect, rgn, winxpstyle, minwindow, message As Long     '声明变量
Private Const RGN_AND = 1
Private Const HTCAPTION = 2
Private Const WM_NCLBUTTONDOWN = &HA1

Private Sub Form_Load()
        ImageMin2.Visible = False
        ImageMin3.Visible = False
        ImageClose2.Visible = False
        ImageClose3.Visible = False
        winxpstyle = CreateRectRgn(0, 0, 0, 0)   '创建一个空的区域
        rect = CreateRectRgn(0, 0, 642, 480)     '以左上角(0,0)为坐标,创建一个矩形区域
        rgn = CreateRoundRectRgn(0, 0, 640, 491, 10, 10)   '以左上角(0,0)为坐标,创建一个弧度为10的圆角矩形区域
        CombineRgn winxpstyle, rect, rgn, RGN_AND    '将rect、rgn以相交的方式(RGN_AND)进行合并到winxpstyle区域
        DeleteObject rect            '删除区域对像
        DeleteObject rgn                  '删除区域对像
        SetWindowRgn Me.hwnd, winxpstyle, True    '将winxpstyle区域设置为窗口
        DeleteObject winxpstyle                '删除区域对像
        Form1.AutoRedraw = True                '设置自动重绘窗口
End Sub
     

Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
        ReleaseCapture
        SendMessage Me.hwnd, WM_NCLBUTTONDOWN, HTCAPTION, 0&       '支持无标题栏窗口的移动
End Sub

Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
        ImageMin1.Visible = True
        ImageClose1.Visible = True
        ImageMin2.Visible = False
        ImageMin3.Visible = False
        ImageClose2.Visible = False
        ImageClose3.Visible = False
End Sub

Private Sub ImageClose2_Click()
        Unload Me        '释放窗口,关闭程序
End Sub

Private Sub ImageClose1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
        ImageClose3.Visible = True
        ImageClose2.Visible = False
End Sub

Private Sub ImageClose1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
        ImageClose2.Visible = True
        ImageClose1.Visible = False
End Sub

Private Sub ImageMin2_Click()
        miniwindow = CloseWindow(Form1.hwnd)    '最小化窗口
End Sub

Private Sub ImageMin1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
        ImageMin3.Visible = True
        ImageMin2.Visible = False
End Sub

Private Sub ImageMin1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
        ImageMin2.Visible = True
        ImageMin1.Visible = False
End Sub


 

工程下载地址: http://download.csdn.net/detail/snowren3074/6469381
 

    水梦雪
[email protected]






 

你可能感兴趣的:(编程)