没有控件的分层形式 [英] layered form with no controles
本文介绍了没有控件的分层形式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
请帮帮我,我喜欢这段代码使表格没有背景色并显示背景图像,这将使图像看起来与png质量一样好,但是问题是,表格将隐藏所有控件,但是背景图片:(
代码是class:
help me please, i fond this code to make the form without background color and show the background image, and this will make the image look as good as the png quality but the problem is, the form will hide all the controls but the background image :(
the code is class:
Imports System.Runtime.InteropServices
Public Class rtaLayeredForm
Private mParent As Form
Public ReadOnly Property Parent() As Form
Get
Return mParent
End Get
End Property
Public Sub SetImage(ByVal frm As Form, ByVal btm As Bitmap)
Try
mParent = frm
Dim exStyle As Int32 = APIs.GetWindowLong(frm.Handle.ToInt32, APIs.GWL_EXSTYLE)
APIs.SetWindowLong(frm.Handle.ToInt32, APIs.GWL_EXSTYLE, exStyle Or APIs.WS_EX_LAYERED)
If Not (Bitmap.IsCanonicalPixelFormat(btm.PixelFormat)) Or Not (Bitmap.IsAlphaPixelFormat(btm.PixelFormat)) Then
Throw New ApplicationException("The picture must be 32bit picture with alpha channel.")
End If
Dim hDC As Int32 = APIs.GetDC(0)
Dim hBtm As Int32
Dim memDC As Int32 = APIs.CreateCompatibleDC(hDC)
Dim Bits As Int32
Dim dstPos As New APIs.POINTAPI(frm.Left, frm.Top)
Dim srcPos As New APIs.POINTAPI(0, 0)
Dim Siz As New APIs.SizeAPI(btm.Width, btm.Height)
Dim Opt As New APIs.BLEND_OPTIONS
Opt.SourceConstantAlpha = 255
Opt.AlphaFormat = 1
Opt.BlendOp = 0
Opt.BlendFlags = 0
Try
hBtm = btm.GetHbitmap(Color.Black).ToInt32
Bits = APIs.SelectObject(memDC, hBtm)
APIs.UpdateLayeredWindow(frm.Handle, hDC, dstPos, Siz, memDC, srcPos, 0, Opt, 2)
Catch ex As Exception
End Try
If hBtm <> 0 Then
APIs.SelectObject(memDC, Bits)
APIs.DeleteObject(hBtm)
End If
APIs.ReleaseDC(0, hDC)
APIs.DeleteDC(memDC)
Catch ex As Exception
End Try
End Sub
Private Class APIs
Public Declare Function CreateCompatibleDC Lib "gdi32.dll" (ByVal hdc As Int32) As Int32
Public Declare Function GetDC Lib "user32.dll" (ByVal hwnd As Int32) As Int32
Public Declare Function SelectObject Lib "gdi32.dll" (ByVal hdc As Int32, ByVal hObject As Int32) As Int32
Public Declare Function ReleaseDC Lib "user32.dll" (ByVal hwnd As Int32, ByVal hdc As Int32) As Int32
Public Declare Function DeleteDC Lib "gdi32.dll" (ByVal hdc As Int32) As Int32
Public Declare Function DeleteObject Lib "gdi32.dll" (ByVal hObject As Int32) As Int32
Public Declare Function UpdateLayeredWindow Lib "user32.dll" (ByVal hWnd As Int32, ByVal hdcDst As Int32, ByRef pptDst As POINTAPI, ByRef psize As SizeAPI, ByVal hdcSrc As Int32, ByRef pptSrc As POINTAPI, ByVal crKey As Int32, ByRef pblend As BLEND_OPTIONS, ByVal dwFlags As Int32) As Int32
Public Declare Function GetWindowLong Lib "user32.dll" Alias "GetWindowLongW" (ByVal hwnd As Int32, ByVal nIndex As Int32) As Int32
Public Declare Function SetWindowLong Lib "user32.dll" Alias "SetWindowLongW" (ByVal hwnd As Int32, ByVal nIndex As Int32, ByVal dwNewLong As Int32) As Int32
Public Const GWL_EXSTYLE As Int32 = -20
Public Const WS_EX_LAYERED As Int32 = &H80000
<StructLayout(LayoutKind.Sequential, Pack:=1)> _
Public Structure BLEND_OPTIONS
Public BlendOp As Byte
Public BlendFlags As Byte
Public SourceConstantAlpha As Byte
Public AlphaFormat As Byte
End Structure
<StructLayout(LayoutKind.Sequential)> _
Public Structure SizeAPI
Public cx As Int32
Public cy As Int32
Sub New(ByVal cx As Int32, ByVal cy As Int32)
Me.cx = cx
Me.cy = cy
End Sub
End Structure
<StructLayout(LayoutKind.Sequential)> _
Public Structure POINTAPI
Public x As Int32
Public y As Int32
Sub New(ByVal x As Int32, ByVal y As Int32)
Me.x = x
Me.y = y
End Sub
End Structure
Public Declare Function ExtCreateRegion Lib "gdi32.dll" ( _
ByRef lpXform As XFORM, _
ByVal nCount As Int32, _
ByRef lpRgnData As RGNDATA) As Int32
<StructLayout(LayoutKind.Sequential)> _
Public Structure XFORM
Public eM11 As Single
Public eM12 As Single
Public eM21 As Single
Public eM22 As Single
Public eDx As Single
Public eDy As Single
End Structure
<StructLayout(LayoutKind.Sequential)> _
Public Structure RGNDATA
Public rdh As RGNDATAHEADER
Public Buffer As Byte
End Structure
<StructLayout(LayoutKind.Sequential)> _
Public Structure RGNDATAHEADER
Public dwSize As Int32
Public iType As Int32
Public nCount As Int32
Public nRgnSize As Int32
Public rcBound As RECT
End Structure
<StructLayout(LayoutKind.Sequential)> _
Public Structure RECT
Public Left As Int32
Public Top As Int32
Public Right As Int32
Public Bottom As Int32
End Structure
End Class
End Class
并且我必须在load事件中编写以下代码的形式:
and in the form i have to write this code in the load event:
Dim Layer As New rtaLayeredForm()
Layer.SetImage(Me, Me.BackgroundImage)
有人可以教我如何显示控件吗?
can anyone show me how to show the controls ??
推荐答案
不要在网络上复制大量您不了解的代码.处理窗体的paint事件,并在其中绘制所需的背景,然后调用base方法.您的控件将可见,而且不是hack.
Don''t copy huge amounts of code off the web that you don''t understand. Handle your form''s paint event, and, in it, draw the background you want, then call the base method. Your controls will be visible just fine, and it''s not a hack.
这篇关于没有控件的分层形式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文