Link to home
Start Free TrialLog in
Avatar of zambuka42
zambuka42

asked on

I need a small C# code snippet converted to VB please

Hi, this is a small code snippet that will send a message to the taskbar notification area to refresh (and remove any icons associated with terminated applications). Any help converting it to VB would be much appreciated. Thanks. -Brian
#define FW(x,y) FindWindowEx(x, NULL, y, L"")
 
void RefreshTaskbarNotificationArea()
{
    HWND hNotificationArea;
    RECT r;
 
    GetClientRect(
        hNotificationArea = FindWindowEx(
            FW(FW(FW(NULL, L"Shell_TrayWnd"), L"TrayNotifyWnd"), L"SysPager"),
            NULL,
            L"ToolbarWindow32",
            L"Notification Area"),
        &r);
    
    for (LONG x = 0; x < r.right; x += 5)
        for (LONG y = 0; y < r.bottom; y += 5)
            SendMessage(
                hNotificationArea,
                WM_MOUSEMOVE,
                0,
                (y << 16) + x);
}

Open in new window

Avatar of abel
abel
Flag of Netherlands image

This is not C# (it is C++) and it is not really trivial to convert that to VB. The declarations for the API functions that you are using can be found on http://www.pinvoke.net but the rest has really to be translated line by line
> I input incorrect information. I am going to remake my question with the correct info.
> THIS WOUOD NOT BE NECESSARY IF THERE WAS AN EDIT BUTTON!!!


there has been an edit button in the past, but it was often abused and caused many problems. Nowadays, you can only edit while there are no comments by experts posted yet.

I'll hit "object" because I think you can simply add the necessary code in the code snippet window as a comment. I will see it and help you with it.
I'll hit "object" because I think you can simply add the necessary code in the code snippet window as a comment. I will see it and help you with it.
Avatar of zambuka42
zambuka42

ASKER

actually, i was going to rename the title to C++ so other's aren't confused with my error
Ah, alright, you mean it is the other way around: you didn't paste the wrong code, but you want the C++ to be translated into VB?
thats correct. I was just trying to fix the typographical mistake you pointed out
Ok, here's a first step towards C# (easier than to VB when you do the first try). I'm only partially aware of what the code might do, so I hope you have an in-depth knowledge of what you are trying to achieve:

private IntPtr FW(IntPtr x, string y) { return FindWindowEx(x, IntPtr.Zero, y, ""); }
void RefreshTaskbarNotificationArea()
{
    IntPtr hNotificationArea;
    RECT r;
 
    GetClientRect(
        hNotificationArea = FindWindowEx(
            FW(FW(FW(IntPtr.Zero, "Shell_TrayWnd"), "TrayNotifyWnd"), "SysPager"),
            IntPtr.Zero,
            "ToolbarWindow32",
            "Notification Area"),
        out r);
 
    for (int x = 0; x < r.right; x += 5)
        for (int y = 0; y < r.bottom; y += 5)
            SendMessage(
                hNotificationArea,
                WM_MOUSEMOVE,
                IntPtr.Zero,
                (y << 16) + x);
}
 
 
// the declarations for PInvoke, taken from pinvoke.net 
// and sometimes altered to fit the usage:
[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr FindWindowEx(IntPtr parentHandle, IntPtr childAfter, string className, string windowTitle);
 
[DllImport("user32.dll")]
static extern bool GetClientRect(IntPtr hWnd, out RECT lpRect);
 
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]
static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, Int32 lParam);
private const UInt32 WM_MOUSEMOVE = 0x0200;
 
[Serializable, StructLayout(LayoutKind.Sequential)]
public struct RECT
{
    public int left;
    public int top;
    public int right;
    public int bottom;
 
    public RECT(int left_, int top_, int right_, int bottom_)
    {
        left = left_;
        top = top_;
        right = right_;
        bottom = bottom_;
    }
 
    public int Height { get { return bottom - top; } }
    public int Width { get { return right - left; } }
    public Size Size { get { return new Size(Width, Height); } }
 
    public Point Location { get { return new Point(left, top); } }
 
    // Handy method for converting to a System.Drawing.Rectangle
    public Rectangle ToRectangle()
    { return Rectangle.FromLTRB(left, top, right, bottom); }
 
    public static RECT FromRectangle(Rectangle rectangle)
    {
        return new RECT(rectangle.Left, rectangle.Top, rectangle.Right, rectangle.Bottom);
    }
 
    public override int GetHashCode()
    {
        return left ^ ((top << 13) | (top >> 0x13))
          ^ ((Width << 0x1a) | (Width >> 6))
          ^ ((Height << 7) | (Height >> 0x19));
    }
 
    #region Operator overloads
 
    public static implicit operator Rectangle(RECT rect)
    {
        return rect.ToRectangle();
    }
 
    public static implicit operator RECT(Rectangle rect)
    {
        return FromRectangle(rect);
    }
 
    #endregion
}

Open in new window

And here's the same code in VB. I tried some online converters, but they made a mess out of it, so I had to do it line by line, step by step, but here it is, for your convenience ;-)

-- Abel --

    Private Sub RefreshTaskbarNotificationArea()
        Dim hNotificationArea As IntPtr
 
        Dim r As RECT
 
        GetClientRect( _
            hNotificationArea = FindWindowEx( _
                FW(FW(FW(IntPtr.Zero, "Shell_TrayWnd"), "TrayNotifyWnd"), "SysPager"), _
                IntPtr.Zero, _
                "ToolbarWindow32", _
                "Notification Area"), _
            r)
 
        For x As Integer = 0 To r.Right Step 5
            For y As Integer = 0 To r.Bottom Step 5
                SendMessage( _
                    hNotificationArea, _
                    WM_MOUSEMOVE, _
                    IntPtr.Zero, _
                    (y << 16) + x)
            Next
        Next
 
    End Sub
 
 
#Region "PInvoke declarations"
 
    ' the declarations for PInvoke, taken from pinvoke.net '
    ' and sometimes altered to fit the usage: '
    <DllImport("user32.dll", SetLastError:=True)> _
    Private Shared Function FindWindowEx(ByVal parentHandle As IntPtr, ByVal childAfter As IntPtr, ByVal className As String, ByVal windowTitle As String) As IntPtr
    End Function
 
    <DllImport("user32.dll")> _
    Private Shared Function GetClientRect(ByVal hWnd As IntPtr, ByVal lpRect As RECT) As Boolean
    End Function
 
    <DllImport("user32.dll", CharSet:=CharSet.Auto, SetLastError:=False)> _
    Private Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal Msg As UInt32, ByVal wParam As IntPtr, ByVal lParam As Int32) As IntPtr
    End Function
 
    Private Const WM_MOUSEMOVE As UInt32 = &H200
 
    <StructLayout(LayoutKind.Sequential)> _
    Public Structure RECT
        Public Left As Integer
        Public Top As Integer
        Public Right As Integer
        Public Bottom As Integer
 
        Public Sub New(ByVal pLeft As Integer, ByVal pTop As Integer, ByVal pRight As Integer, ByVal pBottom As Integer)
            Left = pLeft
            Top = pTop
            Right = pRight
            Bottom = pBottom
        End Sub
 
        Public ReadOnly Property Height() As Integer
            Get
                Return Bottom - Top
            End Get
        End Property
        Public ReadOnly Property Width() As Integer
            Get
                Return Right - Left
            End Get
        End Property
        Public ReadOnly Property Location() As Point
            Get
                Return New Point(Left, Top)
            End Get
        End Property
        Public ReadOnly Property Size() As Size
            Get
                Return New Size(Width, Height)
            End Get
        End Property
 
        Public Function ToRectangle() As Rectangle
            Return Rectangle.FromLTRB(Me.Left, Me.Top, Me.Right, Me.Bottom)
        End Function
 
        Public Shared Function ToRectangle(ByVal sourceRect As RECT) As Rectangle
            Return Rectangle.FromLTRB(sourceRect.Left, sourceRect.Top, sourceRect.Right, sourceRect.Bottom)
        End Function
 
        Public Shared Function FromRectangle(ByVal r As Rectangle) As RECT
            Return New RECT(r.Left, r.Top, r.Right, r.Bottom)
        End Function
    End Structure
 
#End Region

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of abel
abel
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I can't believe you went to all that effort. Thank you so much!!!
You're welcome, glad to be of some help ;-)