Detect Clipboard text change with unicode support in vb.net - vb.net

iam using the below code to check text change in clipboard but its not supported with unicode chracters like ا ب ج is there any way to add support for unicode text or is there any other way to do same work?
#Region " Definitions "
'Constants for API Calls...
Private Const WM_DRAWCLIPBOARD As Integer = &H308
Private Const WM_CHANGECBCHAIN As Integer = &H30D
'Handle for next clipboard viewer...
Private mNextClipBoardViewerHWnd As IntPtr
'API declarations...
Declare Auto Function SetClipboardViewer Lib "user32" (ByVal HWnd As IntPtr) As IntPtr
Declare Auto Function ChangeClipboardChain Lib "user32" (ByVal HWnd As IntPtr, ByVal HWndNext As IntPtr) As Boolean
Declare Auto Function SendMessage Lib "User32" (ByVal HWnd As IntPtr, ByVal Msg As Integer, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As Long
#End Region
#Region " Contructor "
#End Region
#Region " Message Process "
'Override WndProc to get messages...
Protected Overrides Sub WndProc(ByRef m As Message)
Dim iData As IDataObject = New DataObject()
iData = Clipboard.GetDataObject()
Select Case m.Msg
Case Is = WM_DRAWCLIPBOARD 'The clipboard has changed...
'##########################################################################
' Process Clipboard Here :)........................
'##########################################################################
MsgBox(CStr(iData.GetData(DataFormats.Text)))
SendMessage(mNextClipBoardViewerHWnd, m.Msg, m.WParam, m.LParam)
Case Is = WM_CHANGECBCHAIN 'Another clipboard viewer has removed itself...
If m.WParam = CType(mNextClipBoardViewerHWnd, IntPtr) Then
mNextClipBoardViewerHWnd = m.LParam
Else
SendMessage(mNextClipBoardViewerHWnd, m.Msg, m.WParam, m.LParam)
End If
End Select
MyBase.WndProc(m)
End Sub
#End Region
#Region " Dispose "
'Form overrides dispose to clean up...
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
'Set the next clipboard viewer back to the original...
ChangeClipboardChain(Me.Handle, mNextClipBoardViewerHWnd)
MyBase.Dispose(disposing)
End If
End Sub

Your code is not able to detect changes for Unicode text because it is not looking for the Unicode text format. Ansi and Unicode use different clipboard formats, and they can coexist on the clipboard at the same time. DataFormats.Text only supports Ansi text. The documentation even says as much:
Specifies the standard ANSI text format
You need to look for DataFormats.UnicodeText instead of, or in addition to, DataFormats.Text, eg:
Protected Overrides Sub WndProc(ByRef m As Message)
Select Case m.Msg
Case Is = WM_DRAWCLIPBOARD 'The clipboard has changed...
'##########################################################################
' Process Clipboard Here :)........................
'##########################################################################
Dim iData As IDataObject = Clipboard.GetDataObject()
Dim oData as Object = iData.GetData(DataFormats.Text)
If oData IsNot Nothing
MsgBox(CStr(oData), MsgBoxStyle.OKOnly, "Ansi")
End If
oData = iData.GetData(DataFormats.UnicodeText)
If oData IsNot Nothing
MsgBox(CStr(oData), MsgBoxStyle.OKOnly, "Unicode")
End If
Case Is = WM_CHANGECBCHAIN 'Another clipboard viewer has removed itself...
If m.WParam = CType(mNextClipBoardViewerHWnd, IntPtr) Then
mNextClipBoardViewerHWnd = m.LParam
Else
SendMessage(mNextClipBoardViewerHWnd, m.Msg, m.WParam, m.LParam)
End If
End Select
MyBase.WndProc(m)
End Sub

Related

How to capture mouse right click paste function in Winforms

I would like to know how to capture right click and paste option through mouse click.
It is a winforms application. I would be modifying the contents of clipboard before pasting.
I am able to perform this through ctrl+V but not able to find a way to handle mouse right click.
I have tried this so far:
Private Const WM_PASTE As Integer = &H302
Protected Overrides Sub WndProc(ByRef msg As Message)
If msg.Msg = WM_PASTE AndAlso Clipboard.ContainsText() Then
Clipboard.SetText(Clipboard.GetText().Replace(vbCrLf, " "))
End If
MyBase.WndProc(msg)
End Sub
You have to process the WM_PASTE windows message using WndProc (a list of all messages can be found here).
For example, this TextBox will print all text pasted into it (no matter how) to the console instead of displaying it itself:
Class CapturePasteBox
Inherits TextBox
Protected Overrides Sub WndProc(ByRef m As Message)
If m.Msg = &H302 AndAlso Clipboard.ContainsText() Then
Dim text = Clipboard.GetText()
'' do something with text
Console.WriteLine(text)
Return '' return so the text won't be pasted into the TextBox
End If
MyBase.WndProc(m)
End Sub
End Class
In response to your comment:
The ComboBox-control needs some special treatment, since
When sent to a combo box, the WM_PASTE message is handled by its edit control.
So you can use the following function/class using a NativeWindow:
<System.Runtime.InteropServices.DllImport("user32.dll", SetLastError := True)> _
Private Shared Function FindWindowEx(hwndParent As IntPtr, hwndChildAfter As IntPtr, lpszClass As String, lpszWindow As String) As IntPtr
End Function
Public Class PasteHandler
Inherits NativeWindow
Protected Overrides Sub WndProc(ByRef m As Message)
If m.Msg = &H302 Then
Clipboard.SetText(ClipBoard.GetText().Replace("e", "XXX"))
End If
MyBase.WndProc(m)
End Sub
End Class
and use it with your ComboBox:
'' Get the edit control of the combobox
Dim lhWnd As IntPtr = FindWindowEx(yourComboBox.Handle, IntPtr.Zero, "EDIT", Nothing)
'' assign the edit control to the Pastehandler
Dim p As New PasteHandler()
p.AssignHandle(lhWnd)
I found this to be working like awesome:
<System.Runtime.InteropServices.DllImport("user32.dll", SetLastError:=True)> _
Private Shared Function FindWindowEx(hwndParent As IntPtr, hwndChildAfter As IntPtr, lpszClass As String, lpszWindow As String) As IntPtr
End Function
Private Sub SearchCriteria_MouseDown(sender As Object, e As MouseEventArgs) Handles SearchCriteria.MouseDown
Dim lhWnd As IntPtr = FindWindowEx(SearchCriteria.Handle, IntPtr.Zero, "EDIT", Nothing)
If e.Button = Windows.Forms.MouseButtons.Right And lhWnd <> 0 Then
Clipboard.SetText(Clipboard.GetText().Replace(vbCrLf, " "))
End If
End Sub

Get Text From Specific Textboxes From External Application - Visual Basic .Net

I can get text from external application text box but now I want to get text from my desired text box from external application.
My English is not so good that's why see Image Below.
The Below Code Return The First Text Box Value Only.
Imports System.Runtime.InteropServices
Public Class Form1
Private Const WM_GETTEXT As Integer = &HD
Declare Auto Function SendMessage Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal msg As Integer, _
ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr
<DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
Private Shared Function FindWindowEx(ByVal parentHandle As IntPtr, _
ByVal childAfter As IntPtr, _
ByVal lclassName As String, _
ByVal windowTitle As String) As IntPtr
End Function
Declare Auto Function FindWindow Lib "user32.dll" (ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'Find the running notepad window
Dim Hwnd As IntPtr = FindWindow(Nothing, TextBox1.Text)
'Alloc memory for the buffer that recieves the text
Dim Handle As IntPtr = Marshal.AllocHGlobal(100)
'send WM_GWTTEXT message to the notepad window
Dim NumText As Integer = SendMessage(Hwnd, WM_GETTEXT, 50, Handle)
'copy the characters from the unmanaged memory to a managed string
Dim Text As String = Marshal.PtrToStringUni(Handle)
'Display the string using a label
Label1.Text = Text
'Find the Edit control of the Running Notepad
Dim ChildHandle As IntPtr = FindWindowEx(Hwnd, IntPtr.Zero, "Edit", Nothing)
'Alloc memory for the buffer that recieves the text
Dim Hndl As IntPtr = Marshal.AllocHGlobal(200)
'Send The WM_GETTEXT Message
NumText = SendMessage(ChildHandle, WM_GETTEXT, 200, Hndl)
'copy the characters from the unmanaged memory to a managed string
Text = Marshal.PtrToStringUni(Hndl)
'Display the string using a label
Label2.Text = Text
End Sub
End Class
You'll have to loop through children of the main window (External Application) and get their properties.
You'll use the following:
<DllImport("User32.dll")> _
Public Function EnumChildWindows _
(ByVal WindowHandle As IntPtr, ByVal Callback As EnumWindowProcess, _
ByVal lParam As IntPtr) As Boolean
End Function
Public Delegate Function EnumWindowProcess(ByVal Handle As IntPtr, ByVal Parameter As IntPtr) As Boolean
Public Function GetChildWindows(ByVal ParentHandle As IntPtr) As IntPtr()
Dim ChildrenList As New List(Of IntPtr)
Dim ListHandle As GCHandle = GCHandle.Alloc(ChildrenList)
Try
EnumChildWindows(ParentHandle, AddressOf EnumWindow, GCHandle.ToIntPtr(ListHandle))
Finally
If ListHandle.IsAllocated Then ListHandle.Free()
End Try
Return ChildrenList.ToArray
End Function
For for details, check this How can I get properties of controls contained in a popup message box using VB.Net
' try this on excel vbe
External_application_handle=findwindow(vbNullString,"External_application")
textbox_1_handle=findwindowex(External_application_handle,0&,"Edit",vbNullString)
next_handle=textbox_1_handle
textbox_2_handle=findwindowex(External_application_handle,next_handle,"Edit",vbNullString")
Length = SendMessage(textbox_2_handle, WM_GETTEXTLENGTH, 0,0)
buffer$=space(Length)
call sendmessage(textbox_2_handle,Length+1,buffer$)
msgbox buffer

How to trap keypress event in MSword using VSTO?

I am new to VSTO VB.Net programming. and I am developing a word application level Addin and want to trap keypress event. I have tried various codes of hooking but none is working. I want to use application level hook using WH_KEYBOARD instead of WH_KEYBOARD_LL. The following code which I have tried traps just one key stroke after that it stops. Moreover I could not understand where to put trap the keystrokes. How would I use the following for handling key events.
Public Event KeyDown As KeyEventHandler
Public Event KeyPress As KeyPressEventHandler
Public Event KeyUp As KeyEventHandler
The code that I am using is
Imports System.ComponentModel
Imports System.Windows.Forms
Imports System.Runtime.InteropServices
Public Class KeyBoardHook
Inherits Component
Dim PredictString As String
#Region " keyboardHook"
Private Declare Auto Function LoadLibrary Lib "kernel32" (ByVal lpFileName As String) As IntPtr
Private Declare Function UnhookWindowsHookEx Lib "user32" (ByVal hhk As IntPtr) As Boolean
Private Declare Function SetWindowsHookEx Lib "user32" Alias "SetWindowsHookExA" (ByVal idHook As Integer, _
ByVal lpfn As KeyboardProc, ByVal hmod As IntPtr, ByVal dwThreadId As Integer) As IntPtr
Private Delegate Function KeyboardProc(ByVal Code As Integer, ByVal wParam As Integer, ByRef lParam As KBDLLHOOKSTRUCT) As IntPtr
Private Declare Function CallNextHookEx Lib "user32" (ByVal hHook As IntPtr, ByVal nCode As Integer, ByVal wParam As Integer, ByRef lParam As KBDLLHOOKSTRUCT) As IntPtr
Private Structure KBDLLHOOKSTRUCT
Public vkCode As Integer
Public scanCode As Integer
Public flags As Integer
Public time As Integer
Public dwExtraInfo As Integer
End Structure
'Keyboard Constants
Private Const HC_ACTION As Integer = 0
Private Const WM_KEYDOWN As Integer = &H100
Private Const WM_KEYUP As Integer = &H101
Private Const WM_SYSKEYDOWN As Integer = &H104
Private Const WM_SYSKEYUP As Integer = &H105
Private Const WH_KEYBOARD As Integer = 2
Public hKeyboardHook As IntPtr
Public Event KeyDown As KeyEventHandler
Public Event KeyPress As KeyPressEventHandler
Public Event KeyUp As KeyEventHandler
Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Integer) As Integer
Private Declare Function GetKeyState Lib "user32" (ByVal nVirtKey As Integer) As Integer
Private Const VK_ALT As Integer = &H12
Private Const VK_CONTROL As Integer = &H11
Private Const VK_SHIFT As Integer = 16
<MarshalAs(UnmanagedType.FunctionPtr)> Private callback As KeyboardProc
Public Sub HookKeyboard()
callback = New KeyboardProc(AddressOf KeyboardCallback)
Dim hInstance As IntPtr = LoadLibrary("User32")
hKeyboardHook = SetWindowsHookEx(WH_KEYBOARD, callback, hInstance, 0)
CheckHooked()
End Sub
Private Function KeyboardCallback(ByVal Code As Integer, ByVal wParam As Integer, ByRef lParam As KBDLLHOOKSTRUCT) As IntPtr
Dim xy As System.Drawing.Point = Cursor.Position()
Try
If (Code = HC_ACTION Or Code = 3) Then
Dim CapsLock As Boolean = GetKeyState(Keys.CapsLock) = 1
Dim shifting As Boolean = False
Dim modifiers As Keys
If GetAsyncKeyState(VK_CONTROL) <> 0 Then
modifiers = modifiers Or Keys.Control
End If
If GetAsyncKeyState(VK_SHIFT) <> 0 Then
modifiers = modifiers Or Keys.Shift
shifting = True
End If
If GetAsyncKeyState(VK_ALT) <> 0 Then
modifiers = modifiers Or Keys.Alt
End If
Static lastKeys As Keys
Select Case wParam
Case WM_KEYDOWN, WM_SYSKEYDOWN
RaiseEvent KeyDown(Me, New KeyEventArgs(DirectCast(Asc(Chr(lParam.vkCode)), Keys) Or modifiers))
If lastKeys <> (DirectCast(Asc(Chr(lParam.vkCode)), Keys) Or modifiers) Then
lastKeys = (DirectCast(Asc(Chr(lParam.vkCode)), Keys) Or modifiers)
If CapsLock AndAlso shifting Then
RaiseEvent KeyPress(Me, New KeyPressEventArgs(Char.ToLower(Chr(lParam.vkCode))))
ElseIf Not CapsLock AndAlso shifting Then
RaiseEvent KeyPress(Me, New KeyPressEventArgs(Char.ToUpper(Chr(lParam.vkCode))))
ElseIf Not shifting Then
If CapsLock Then
RaiseEvent KeyPress(Me, New KeyPressEventArgs(Char.ToUpper(Chr(lParam.vkCode))))
Else
RaiseEvent KeyPress(Me, New KeyPressEventArgs(Char.ToLower(Chr(lParam.vkCode))))
End If
End If
End If
Case WM_KEYUP, WM_SYSKEYUP
If CapsLock AndAlso shifting Then
RaiseEvent KeyUp(Me, New KeyEventArgs(DirectCast(Asc(Chr(lParam.vkCode)), Keys) Or modifiers))
ElseIf Not CapsLock AndAlso shifting Then
RaiseEvent KeyUp(Me, New KeyEventArgs(DirectCast(Asc(Chr(lParam.vkCode)), Keys) Or modifiers))
ElseIf Not shifting Then
If CapsLock Then
RaiseEvent KeyUp(Me, New KeyEventArgs(DirectCast(Asc(Chr(lParam.vkCode)), Keys) Or modifiers))
Else
RaiseEvent KeyUp(Me, New KeyEventArgs(DirectCast(Asc(Chr(lParam.vkCode)), Keys) Or modifiers))
End If
End If
lastKeys = Nothing
End Select
End If
MsgBox("Keypressed is -> " & lParam.vkCode)
Return CallNextHookEx(hKeyboardHook, Code, wParam, lParam)
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Function
Private Function keyboardHooked() As Boolean
Return hKeyboardHook <> IntPtr.Zero
End Function
Public Sub UnhookKeyboard()
If keyboardHooked() Then
UnhookWindowsHookEx(hKeyboardHook)
End If
End Sub
#End Region
Private Sub CheckHooked()
If keyboardHooked() Then
MsgBox("Keyboard hooked")
Else
MsgBox("Keyboard hook failed: " & Err.LastDllError)
End If
End Sub
End Class
Your question is a possible duplicate of:
How to get the "KeyPress" event from a Word 2010 Addin (developed in C#)?
How to raise an event on MS word Keypress
Capturing keydown event of MS Word using C#
... however, the answer remains the same: you simply can't :)
In my answer to the last of the questions listed above I explain the reason behind this in a bit more detail, and also covers a possible alternative solution involving the WindowSelectionChange event.

How to add more items to the window context menu

I'm making an app for the company I work for and I was wondering how to customise the window's context menu like PuTTY's (aka, it has "New Session..." etc.). I've looked all over Google and can't find the answer I'm looking for.
make a new module and add Imports System.Runtime.InteropServices on top
then declare this
<Flags()> _
Public Enum MenuFlags As Integer
MF_BYPOSITION = 1024
MF_REMOVE = 4096
MF_SEPARATOR = 2048
MF_STRING = 0
End Enum
<DllImport("user32.dll", CallingConvention:=CallingConvention.Cdecl)> _
Public Function GetSystemMenu(ByVal hWnd As IntPtr, Optional ByVal bRevert As Boolean = False) As IntPtr
End Function
<DllImport("user32.dll", CharSet:=CharSet.Auto)> _
Public Function AppendMenu(ByVal hMenu As IntPtr, ByVal uFlags As MenuFlags, ByVal uIDNewItem As Int32, ByVal lpNewItem As String) As Boolean
End Function
then on your form load handler add this code
Dim sysmenu As IntPtr = GetSystemMenu(Me.Handle)
AppendMenu(sysmenu, MenuFlags.MF_STRING, &H1FFF, "Hello")
then, in order to be able to capture the user click on your new menu item, you have to implement this function which will capture all messages, just add it to your form code
Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
If m.Msg = WM_SYSCOMMAND Then
If m.WParam.ToInt32 = &H1FFF Then
' your menu item is clicked, call a function here
End If
End If
MyBase.WndProc(m)
End Sub

TBPF_INDETERMINATE status has no effect in .NET 2.0 on Windows 7 taskbar

I have a very weird problem. I have a VB.NET 2.0 application that takes advantage of Windows 7 taskbar button progress features, i.e. displaying certain progress and application state in the Win7 taskbar button.
Everything works just fine - I can set and update progress, I can set the button to paused or ewrror state, I can set it to No progress. Everything works, except MARQUE (Indeterminate) mode. This is a total mistery, whenever I set state to TBPF_INDETERMINATE (value of 0x1), it simply changes back to NOPROGRESS type, i.e. it removes all progress inidcation from the taskbar button and sets it back to its default state - no animated marque is ever displayed!
I have read documentation on MSDN - http://msdn.microsoft.com/en-us/library/dd391697(v=vs.85).aspx ; tried various combinations like setting progress to 0 and then calling set state to indeterminate; or like setting it to normal first and then to indeterminate - nothing works. It's a total mistery - and there is no clue in the documentation as to why it is failing...
Here's the code:
The API implementation:
<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(left As Integer, top As Integer, right As Integer, bottom As Integer)
Me.left = left
Me.top = top
Me.right = right
Me.bottom = bottom
End Sub
End Structure
Public Enum TBPFLAG
TBPF_NOPROGRESS = 0
TBPF_INDETERMINATE = &H1
TBPF_NORMAL = &H2
TBPF_ERROR = &H4
TBPF_PAUSED = &H8
End Enum
Public Enum TBATFLAG
TBATF_USEMDITHUMBNAIL = &H1
TBATF_USEMDILIVEPREVIEW = &H2
End Enum
Public Enum THBMASK
THB_BITMAP = &H1
THB_ICON = &H2
THB_TOOLTIP = &H4
THB_FLAGS = &H8
End Enum
Public Enum THBFLAGS
THBF_ENABLED = 0
THBF_DISABLED = &H1
THBF_DISMISSONCLICK = &H2
THBF_NOBACKGROUND = &H4
THBF_HIDDEN = &H8
End Enum
<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Auto)> _
Public Structure THUMBBUTTON
<MarshalAs(UnmanagedType.U4)> _
Public dwMask As THBMASK
Public iId As UInteger
Public iBitmap As UInteger
Public hIcon As IntPtr
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=260)> _
Public szTip As String
<MarshalAs(UnmanagedType.U4)> _
Public dwFlags As THBFLAGS
End Structure
<ComImportAttribute()> _
<GuidAttribute("ea1afb91-9e28-4b86-90e9-9e9f8a5eefaf")> _
<InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown)> _
Public Interface ITaskbarList3
' ITaskbarList
<PreserveSig()> _
Sub HrInit()
<PreserveSig()> _
Sub AddTab(hwnd As IntPtr)
<PreserveSig()> _
Sub DeleteTab(hwnd As IntPtr)
<PreserveSig()> _
Sub ActivateTab(hwnd As IntPtr)
<PreserveSig()> _
Sub SetActiveAlt(hwnd As IntPtr)
' ITaskbarList2
<PreserveSig()> _
Sub MarkFullscreenWindow(hwnd As IntPtr, <MarshalAs(UnmanagedType.Bool)> fFullscreen As Boolean)
' ITaskbarList3
Sub SetProgressValue(<[In]()> ByVal hwnd As IntPtr, <[In]()> ByVal ullCompleted As UInt64, <[In]()> ByVal ullTotal As UInt64) 'hwnd As IntPtr, ullCompleted As UInt64, ullTotal As UInt64)
Sub SetProgressState(<[In]()> ByVal hwnd As IntPtr, <[In]()> ByVal tbpFlags As TBPFLAG) 'hwnd As IntPtr, tbpFlags As TBPFLAG) 'As Integer
Sub RegisterTab(hwndTab As IntPtr, hwndMDI As IntPtr)
Sub UnregisterTab(hwndTab As IntPtr)
Sub SetTabOrder(hwndTab As IntPtr, hwndInsertBefore As IntPtr)
Sub SetTabActive(hwndTab As IntPtr, hwndMDI As IntPtr, tbatFlags As TBATFLAG)
Sub ThumbBarAddButtons(hwnd As IntPtr, cButtons As UInteger, <MarshalAs(UnmanagedType.LPArray)> pButtons As THUMBBUTTON())
Sub ThumbBarUpdateButtons(hwnd As IntPtr, cButtons As UInteger, <MarshalAs(UnmanagedType.LPArray)> pButtons As THUMBBUTTON())
Sub ThumbBarSetImageList(hwnd As IntPtr, himl As IntPtr)
Sub SetOverlayIcon(hwnd As IntPtr, hIcon As IntPtr, <MarshalAs(UnmanagedType.LPWStr)> pszDescription As String)
Sub SetThumbnailTooltip(hwnd As IntPtr, <MarshalAs(UnmanagedType.LPWStr)> pszTip As String)
'[MarshalAs(UnmanagedType.LPStruct)]
Sub SetThumbnailClip(hwnd As IntPtr, ByRef prcClip As RECT)
End Interface
<GuidAttribute("56FDF344-FD6D-11d0-958A-006097C9A090")> _
<ClassInterfaceAttribute(ClassInterfaceType.None)> _
<ComImportAttribute()> _
Public Class CTaskbarList
End Class
And here are the actual procedures that use the code:
Friend Sub SetWindows7Progress(ByVal aValue As Integer)
If Not IsWin7orLater Then Exit Sub
If w7tb Is Nothing Then
w7tb = CType(New CTaskbarList, ITaskbarList3)
End If
CType(w7tb, ITaskbarList3).SetProgressValue(My.Forms.Form1.Handle, Math.Min(Math.Max(1, aValue), 1000), 1000)
End Sub
Friend Sub ResetWindows7Progress()
If Not IsWin7orLater Then Exit Sub
If w7tb Is Nothing Then
w7tb = CType(New CTaskbarList, ITaskbarList3)
End If
CType(w7tb, ITaskbarList3).SetProgressState(My.Forms.Form1.Handle, TBPFLAG.TBPF_NOPROGRESS)
End Sub
Friend Sub SetWindows7ProgressMon()
If Not IsWin7orLater Then Exit Sub
If w7tb Is Nothing Then
w7tb = CType(New CTaskbarList, ITaskbarList3)
End If
CType(w7tb, ITaskbarList3).SetProgressState(My.Forms.Form1.Handle, TBPF_INDETERMINATE)
End Sub
I even tried getting the HRESULT code from SetProgressState and checking to make sure no exception is thrown to no avail: SetProgressState always returns 0 (everything is fine); and no exceptions are thrown!
Any help in resolving the matter would be greatly appreciated! I just can't believe that everything works except the MARQUE/INDETERMINATE state!
Thanks.
First, it's very strange that your worker functions are accessing your form's instance using this code:
My.Forms.Form1.Handle
That implies that those functions are not defined in the same class as your form (because if they were, the compiler would prompt you to use Me, instead). And if that's the case, you really should be passing the handle to the form into the function as a parameter.
(The reason for this is so that your functions are reusable. If you hardcode a reference to a particular form, what happens when you rename that form, or display two instances of it on the screen at a time, or just want to show a progress indicator in the taskbar for a different form? Things break. Passing the form instance as a parameter is a much cleaner, more reusable approach.)
Second, there seems like an unnecessary amount of casting going on. Why not just declare the w7tb variable as type ITaskbarList3 in the first place, rather than casting back and forth between that and CTaskbarList?
Third, I'm not sure if this is a typo or the actual problem, but your SetWindows7ProgressMon function does not actually reference the correct value for TBPF_INDETERMINATE. You use an unqualified reference to that identifier, when it's actually defined in the TBPFLAG enumeration.
So, considering all of the above, I would rewrite the second block of code that you posted as follows:
Private w7tb As ITaskbarList3
Friend Sub SetWindows7Progress(ByVal frm As Form, ByVal aValue As Integer)
If (Not IsWin7orLater()) OrElse (frm Is Nothing) Then
Exit Sub
End If
If w7tb Is Nothing Then
w7tb = CType(New CTaskbarList, ITaskbarList3)
End If
w7tb.SetProgressValue(frm.Handle, Math.Min(Math.Max(1, aValue), 1000), 1000)
End Sub
Friend Sub ResetWindows7Progress(ByVal frm As Form)
If (Not IsWin7orLater()) OrElse (frm Is Nothing) Then
Exit Sub
End If
If w7tb Is Nothing Then
w7tb = CType(New CTaskbarList, ITaskbarList3)
End If
w7tb.SetProgressState(frm.Handle, TBPFLAG.TBPF_NOPROGRESS)
End Sub
Friend Sub SetWindows7ProgressMon(ByVal frm As Form)
If (Not IsWin7orLater()) OrElse (frm Is Nothing) Then
Exit Sub
End If
If w7tb Is Nothing Then
w7tb = CType(New CTaskbarList, ITaskbarList3)
End If
w7tb.SetProgressState(frm.Handle, TBPFLAG.TBPF_INDETERMINATE)
End Sub
This is tested to work perfectly on Windows 7 32-bit. Note that you can call the functions from code inside of your form class by simply specifying Me for the frm parameter.