
首先 建一个模块:
'透明效果
Public Declare Function SetLayeredWindowAttributes Lib "user32" (ByVal hwnd As Long, ByVal crKey As Long, ByVal bAlpha As Byte, ByVal dwFlags As Long) As Long
Public Const WS_EX_LAYERED = &H80000
Public Const GWL_EXSTYLE = (-20)
Public Const LWA_ALPHA = &H2
Public Const LWA_COLORKEY = &H1
Public Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
Public Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong 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
Public Const HWND_TOPMOST = -1
Public Const SWP_NOMOVE = &H2
Public Const SWP_NOSIZE = &H1
第二在form1窗体中,Picture1,Command1,caption属性“打开图片”,HScroll1,max=255,min=0,value=255,放多一个CommonDialog1控件,还有在程序目录放一张鸟.jpg的图片,图片自己找。
windowstate是2 最大化,maxbutton属性为false.
Dim Alpha As Integer '声明变量
Private Sub Form_Load()
Picture1.Picture = LoadPicture(App.Path &"\鸟.jpg")
Picture1.ZOrder 0
Form2.Show
Form2.Picture = LoadPicture(App.Path &"\鸟.jpg")
End Sub
Private Sub Form_Resize()
Picture1.Left = 0
Picture1.Top = 0
End Sub
Private Sub Form_Unload(Cancel As Integer)
Unload Form2
End Sub
Private Sub HScroll1_Change()
Alpha = HScroll1.Value
SetLayeredWindowAttributes Form2.hwnd, 0, Alpha, LWA_ALPHA
End Sub
Private Sub Command1_Click()
SetWindowPos Form2.hwnd, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE
CommonDialog1.ShowOpen
Form2.Picture = LoadPicture(CommonDialog1.FileName)
Form2.ZOrder 0
SetWindowPos Form2.hwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE
Dim rtn As Long
rtn = GetWindowLong(Form2.hwnd, GWL_EXSTYLE) ':取的窗口原先的样式
rtn = rtn Or WS_EX_LAYERED ':使窗体添加上新的样式WS_EX_LAYERED
SetWindowLong Form2.hwnd, GWL_EXSTYLE, rtn '把新的样式赋给窗体
SetLayeredWindowAttributes Form2.hwnd, 0, 255, LWA_ALPHA
HScroll1.Value = 255
End Sub
第三 增加一个form2,borderstyle设为none,代码如下
Private Sub Form_Resize()
Form2.Left = 5
Form2.Top = 380
Form2.Width = Form1.Picture1.Width
Form2.Height = Form1.Picture1.Height
End Sub
首先,把窗体的BorderStyle属性设为0
然后,输入以下代码:
Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal HWnd As Long, ByVal nIndex As Long) As LongPrivate Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal HWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Private Declare Function SetLayeredWindowAttributes Lib "user32" (ByVal HWnd As Long, ByVal crKey As Long, ByVal bAlpha As Byte, ByVal dwFlags As Long) As Long
Private Const WS_EX_LAYERED = &H80000
Private Const GWL_EXSTYLE = (-20)
Private Const LWA_ALPHA = &H2
Private Const LWA_COLORKEY = &H1
Private Sub Form_Load()
Me.AutoRedraw = True
Me.BackColor = &HFF0001
SetWindowLong Me.HWnd, GWL_EXSTYLE, GetWindowLong(Me.HWnd, GWL_EXSTYLE) Or WS_EX_LAYERED
SetLayeredWindowAttributes Me.HWnd, &HFF0001, 0, LWA_COLORKEY
End Sub
运行后就是全透明无边框的窗体了
环境:VS2008 .net2.0 C# winform问题:程序需要实现在主窗口上显示子窗口,子窗口上显示电压值,要求背景完全透明但文字不透明,可以拖动改变位置和大小;现在已经基本实现了该功能,就是透明的部分会有鼠标穿透效果,导致点击到透明部分时无法拖动子窗口。请问这个效果该如何修改?
C# code?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
//实现透明
label_U.BackColor = Color.White
this.TransparencyKey = Color.White
//移动窗口
private static void labelU_MouseDown(object sender, MouseEventArgs e)
{
FxWinApis.ReleaseCapture()
Control g = sender as Control
if (g != null)
{
FxWinApis.SendMessage(this.Handle, FxWinApis.WM_SYSCOMMAND, FxWinApis.SC_MOVE + FxWinApis.HTCAPTION, 0)
}
}
//缩放窗口
private void FormShowVoltage_MouseDown(object sender, MouseEventArgs e)
{
this.Capture = false
Form ff = new Form()
ff.StartPosition = FormStartPosition.Manual
ff.Size = this.Size
ff.Location = this.Location
ff.SizeChanged += new EventHandler(ff_SizeChanged)
ff.LocationChanged += new EventHandler(ff_LocationChanged)
int pos = GetPostionAt(e.Location)
if (pos >0)
{
SendMessage(ff.Handle, WM_SYSCOMMAND, SC_Size + pos, 0)//发送缩放消息
}
ff.Dispose()
}
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)