我需要使列表框透明。我发现VB.NET中的代码可以解决问题。但是,我怎么能在C#中实现它?
这是VB代码:
Option Explicit
Private Const WS_EX_TRANSPARENT = &H20&
Private Const GWL_EXSTYLE = (-20)
Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Public Function MakeListTransparent(ListCtl As Object) As Boolean
On Error Resume Next
ListCtl.BackColor = ListCtl.Parent.BackColor
SetWindowLong ListCtl.hwnd, GWL_EXSTYLE, WS_EX_TRANSPARENT
MakeListTransparent = Err.LastDllError = 0
End Function
这里是我的C#ATTEMPT:
public const int WS_EX_TRANSPARENT = 0x20;
public const int GWL_EXSTYLE = (-20);
[DllImport("user32.dll")]
static extern int SetWindowLong(IntPtr hWnd, int nIndex, UInt32 dwNewLong);
//somewhere in my form:
listBox1.BackColor = Color.AliceBlue; // <-- to see an INITIAL color
SetWindowLong(listBox1.Handle, GWL_EXSTYLE, WS_EX_TRANSPARENT); // <-- to see if it turns TRANSPARENT
我没有正确地将这个从VB转换为C#吗?有更好的方法吗?我迫切需要让列表框变得透明......
答案 0 :(得分:0)
我需要使列表框透明。有更好的方法吗?
我想知道你是否让它变得比它需要的更复杂。将BackColor设置为listBox1.Parent.BackColor并将borderstyle设置为None,所有可见的是项列表。当然,如果你想暴露一个被列表框隐藏的控件,只需将Visible属性设置为false。