| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1743 人关注过本帖
标题:[经验][特别奉送] VB实现图形形状窗体
只看楼主 加入收藏
ryu
Rank: 1
等 级:新手上路
帖 子:124
专家分:0
注 册:2006-2-12
收藏
 问题点数:0 回复次数:7 
[经验][特别奉送] VB实现图形形状窗体

先献上效果图,绝非ps

注意:

1. picture的autosize为true
2. picture加载的图片为最终窗体的形状
3. picture move 0,0
4. 图片的背景色为白
5. 代码集成了移动无边框窗体&TOP窗体
6. 如果发现错误请检查"回车"/"符号"


Option Explicit
Private Declare Function ReleaseCapture Lib "user32" () As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Long) As Long
Private 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 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 GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) 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 GetPixel Lib "gdi32" (ByVal hdc As Long, ByVal X As Long, ByVal y 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 Const SWP_NOSIZE = &H1
Private Const SWP_NOMOVE = &H2
Private Const HWND_TOPMOST = -1
Private Const HWND_NOTOPMOST = -2
Private Const RGN_XOR = 3
Private Const HTCAPTION = 2
Private Const WM_NCLBUTTONDOWN = &HA1
Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Dim Xs As Long

Private Sub Form_Load()
SetWindowPos hwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE
Me.CreatePictureform
End Sub

Function CreatePictureform()
On Error Resume Next
Dim hRgn As Long, hRect As RECT, hTempRgn As Long, tColour As Long, OldScaleMode As Integer, AbsoluteX As Long, AbsoluteY As Long
Dim Color As Long, Hrect1 As RECT
Dim xx As Long, yy As Long
Dim rtn As Long
Me.Picture = Me.Picture1
Me.Width = Me.Picture1.Width
Me.Height = Me.Picture1.Height
OldScaleMode = Me.ScaleMode
Me.AutoRedraw = True
Me.ScaleMode = 3
Color = vbWhite
rtn = GetWindowRect(Me.hwnd, hRect)
hRgn = CreateRectRgn(0, 0, hRect.Right, hRect.Bottom)
For AbsoluteX = 0 To Me.ScaleWidth
For AbsoluteY = 0 To Me.ScaleHeight
tColour = GetPixel(Me.hdc, AbsoluteX, AbsoluteY)
If tColour = Color Then
hTempRgn = CreateRectRgn(AbsoluteX, AbsoluteY, AbsoluteX + 1, AbsoluteY + 1)
rtn = CombineRgn(hRgn, hRgn, hTempRgn, RGN_XOR)
rtn = DeleteObject(hTempRgn)
End If
Next AbsoluteY
Next AbsoluteX
rtn = SetWindowRgn(Me.hwnd, hRgn, True)
DeleteObject hRgn
Me.ScaleMode = OldScaleMode
If Err Then
MsgBox Error, 16, Err
End If
End Function

Private Sub Picture1_MouseDown(Button As Integer, Shift As Integer, X As Single, y As Single)
If Button = 1 Then
Dim ReturnVal As Long
Xs = ReleaseCapture()
ReturnVal = SendMessage(hwnd, WM_NCLBUTTONDOWN, HTCAPTION, 0)
End If
End Sub

搜索更多相关主题的帖子: 图形 窗体 形状 经验 奉送 
2006-02-12 21:43
ryu
Rank: 1
等 级:新手上路
帖 子:124
专家分:0
注 册:2006-2-12
收藏
得分:0 
补充一下picture的BorderStyle=none
form的BorderStyle=none

本帖版权归ryu所有.如果引用本帖,请注明帖子的出处和作者;本帖如系引用,其版权归原作者所有.
2006-02-12 21:47
ryu
Rank: 1
等 级:新手上路
帖 子:124
专家分:0
注 册:2006-2-12
收藏
得分:0 
再补充一下
加载的图片只要是白色就会被"抠掉".

本帖版权归ryu所有.如果引用本帖,请注明帖子的出处和作者;本帖如系引用,其版权归原作者所有.
2006-02-13 12:51
griefforyou
Rank: 6Rank: 6
等 级:贵宾
威 望:27
帖 子:3336
专家分:0
注 册:2004-4-15
收藏
得分:0 
加点注释就更好了。

天津网站建设 http://www./
2006-02-13 22:13
ryu
Rank: 1
等 级:新手上路
帖 子:124
专家分:0
注 册:2006-2-12
收藏
得分:0 
Option Explicit
Private Const SWP_NOSIZE = &H1
Private Const SWP_NOMOVE = &H2
Private Const HWND_TOPMOST = -1
Private Const HWND_NOTOPMOST = -2
Private 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 Declare Function ReleaseCapture Lib "user32" () As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Long) As Long
Const HTCAPTION = 2
Const WM_NCLBUTTONDOWN = &HA1
Dim x As Long
'///以上为移动窗体的声明"用api骗windows"
...
...
...
Function CreatePictureform()
On Error Resume Next
Dim hRgn As Long, hRect As RECT, hTempRgn As Long, tColour As Long, OldScaleMode As Integer, AbsoluteX As Long, AbsoluteY As Long
Dim Color As Long, Hrect1 As RECT
Dim xx As Long, yy As Long
Dim rtn As Long
Frmmain.Picture = Frmmain.Picture1
Frmmain.Width = Frmmain.Picture1.Width
Frmmain.Height = Frmmain.Picture1.Height
OldScaleMode = Frmmain.ScaleMode
Frmmain.AutoRedraw = True
Frmmain.ScaleMode = 3
Rem 白色,透明图象的背景为白色,
Color = vbWhite
Rem 得到窗体的整个区域
rtn = GetWindowRect(Frmmain.hwnd, hRect)
Rem 创建一个矩形区域,大小为整个窗体大小
hRgn = CreateRectRgn(0, 0, hRect.Right, hRect.Bottom)
For AbsoluteX = 0 To Frmmain.ScaleWidth
For AbsoluteY = 0 To Frmmain.ScaleHeight
Rem 得到该点像素的颜色
tColour = GetPixel(Frmmain.hDC, AbsoluteX, AbsoluteY)
Rem tColour的参数可为任何RGB颜色(属于color会被去掉)
If tColour = Color Then
Rem 创建一个小矩形区域,此小矩形区域像素的颜色为白色,应该去掉
hTempRgn = CreateRectRgn(AbsoluteX, AbsoluteY, AbsoluteX + 1, AbsoluteY + 1)
Rem 通过组合函数,用异或关系将该点去掉
rtn = CombineRgn(hRgn, hRgn, hTempRgn, RGN_XOR)
Rem 删除临时区域
rtn = DeleteObject(hTempRgn)
End If
Next AbsoluteY
Next AbsoluteX
Rem 设置窗体为该图像窗体
rtn = SetWindowRgn(Frmmain.hwnd, hRgn, True)
DeleteObject hRgn
Frmmain.ScaleMode = OldScaleMode
If Err Then MsgBox Error, 16, Err
End Function

本帖版权归ryu所有.如果引用本帖,请注明帖子的出处和作者;本帖如系引用,其版权归原作者所有.
2006-02-13 22:44
ryu
Rank: 1
等 级:新手上路
帖 子:124
专家分:0
注 册:2006-2-12
收藏
得分:0 
红色代码可改成你的图片背景色的rgb值
Function CreatePictureform()
On Error Resume Next
Dim hRgn As Long, hRect As RECT, hTempRgn As Long, tColour As Long, OldScaleMode As Integer, AbsoluteX As Long, AbsoluteY As Long
Dim Color As Long, Hrect1 As RECT
Dim xx As Long, yy As Long
Dim rtn As Long
Me.Picture = Me.Picture1
Me.Width = Me.Picture1.Width
Me.Height = Me.Picture1.Height
OldScaleMode = Me.ScaleMode
Me.AutoRedraw = True
Me.ScaleMode = 3
Color = vbWhite
rtn = GetWindowRect(Me.hwnd, hRect)
hRgn = CreateRectRgn(0, 0, hRect.Right, hRect.Bottom)
For AbsoluteX = 0 To Me.ScaleWidth
For AbsoluteY = 0 To Me.ScaleHeight
tColour = GetPixel(Me.hdc, AbsoluteX, AbsoluteY)
If tColour = Color Then
hTempRgn = CreateRectRgn(AbsoluteX, AbsoluteY, AbsoluteX + 1, AbsoluteY + 1)
rtn = CombineRgn(hRgn, hRgn, hTempRgn, RGN_XOR)
rtn = DeleteObject(hTempRgn)
End If
Next AbsoluteY
Next AbsoluteX
rtn = SetWindowRgn(Me.hwnd, hRgn, True)
DeleteObject hRgn
Me.ScaleMode = OldScaleMode
If Err Then
MsgBox Error, 16, Err
End If

本帖版权归ryu所有.如果引用本帖,请注明帖子的出处和作者;本帖如系引用,其版权归原作者所有.
2006-02-23 17:58
xinfresh
Rank: 4
等 级:贵宾
威 望:13
帖 子:594
专家分:0
注 册:2006-1-13
收藏
得分:0 
好贴!感谢

E-mail:xinfresh@QQ:383094053校内:http:///getuser.do?id=234719042
2006-02-25 07:49
SunUniverse
Rank: 1
等 级:新手上路
帖 子:14
专家分:0
注 册:2006-2-26
收藏
得分:0 
其实就是API,API如果参数都弄明白就好做了
2006-02-26 09:30
快速回复:[经验][特别奉送] VB实现图形形状窗体
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.072322 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved