窗口界面编程之一:VB实现简单异形窗口
一、运行效果图(在Win8里的运行效果,在XP里运行就不能体现出来,因为我使用的XP的界面效果)
二、编译环境:Visual Basic 6.0 (SP6)
三、实现原理:通过区域合并
四、使用API函数:CreateRectRgn、CreateRoundRectRgn、CombineRgn、SetWindowRgn,其声明及说明如下: 函数:CreateRectRgn
五、开始动手:创建一个无标题栏无边框的窗口,载入一张图片作为窗口背景,使用Image控件作为按钮,在窗口加截函数中处理窗口区域,在窗口鼠标按下函数中加入移动消息,使用API函数CloseWindow实现最小化。
Private Declare Function CreateRectRgn Lib "gdi32" (ByVal X1 As Long,ByVal Y2 As Long) As Long Private Declare Function CreateRoundRectRgn Lib "gdi32" (ByVal X1 As Long,ByVal Y3 As Long) As Long Private Declare Function CombineRgn Lib "gdi32" (ByVal hDestRgn As Long,ByVal nCombineMode As Long) As Long Private Declare Function SetWindowRgn Lib "user32" (ByVal hwnd 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) '创建一个空的区域 rect = CreateRectRgn(0,642,480) '以左上角(0,0)为坐标,创建一个矩形区域 rgn = CreateRoundRectRgn(0,640,491,10,10) '以左上角(0,0)为坐标,创建一个弧度为10的圆角矩形区域 CombineRgn winxpstyle,rect,RGN_AND '将rect、rgn以相交的方式(RGN_AND)进行合并到winxpstyle区域 DeleteObject rect '删除区域对像 DeleteObject rgn '删除区域对像 SetWindowRgn Me.hwnd,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,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,Y As Single) ImageClose3.Visible = True ImageClose2.Visible = False End Sub Private Sub ImageClose1_MouseMove(Button As Integer,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,Y As Single) ImageMin3.Visible = True ImageMin2.Visible = False End Sub Private Sub ImageMin1_MouseMove(Button As Integer,Y As Single) ImageMin2.Visible = True ImageMin1.Visible = False End Sub 工程下载地址:http://download.csdn.net/detail/snowren3074/6469381 水梦雪 SnowEmail3074@163.com (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |