2

我需要使列表框透明。我在 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# 尝试:

    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# 吗?有没有更好的方法?我迫切需要使列表框透明...

4

1 回答 1

0

I need to make a listbox transparent. Is there a better method?

I'm wondering if you're making it more complicated than it needs to be. Setting the BackColor to the listBox1.Parent.BackColor and setting the borderstyle to None, all that is visible is the list of items. Of course if you want to expose a control that's hidden by the listbox, just set the Visible property to false.

于 2013-08-28T17:35:09.997 回答