I want to sync the scrolling of two Winforms
Richtextboxes. When RTB2 gets scolled, RTB1 needs to be exactly aligned all the time.
I tried to convert this c#-Code here LINK (second answer), but failed so far. So I need help.
Right now it produces multiple errors:
Type [ScrollBarCommands] was not found...
Type [ScrollBarType] was not found....
Type [Message] was not found...
Also "illegal conversions" and so on.
This is an example script:
[void][System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
$form = New-Object system.Windows.Forms.Form
$form.size = "400,400"
$rtb1 = New-Object system.Windows.Forms.RichTextBox
$rtb1.size = "190,350"
$rtb1.location = "200,1"
$rtb1.text = (1..300 | out-string)
$form.controls.add($rtb1)
$rtb2 = New-Object system.Windows.Forms.RichTextBox
$rtb2.size = "190,350"
$rtb2.location = "1,1"
$rtb2.text = (1..300 | out-string)
$rtb2.scrollbars = "none"
$form.controls.add($rtb2)
$code = @'
public enum ScrollBarType : uint {
SbHorz = 0,
SbVert = 1,
SbCtl = 2,
SbBoth = 3
}
public enum Message : uint {
WM_VSCROLL = 0x0115
}
public enum ScrollBarCommands : uint {
SB_THUMBPOSITION = 4
}
[DllImport( "User32.dll" )]
public extern static int GetScrollPos( IntPtr hWnd, int nBar );
[DllImport( "User32.dll" )]
public extern static int SendMessage( IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam );
'@
Add-Type -Name WinUtils -MemberDefinition $code -Namespace User32
$rtb1.add_VScroll({
[uint32]$nPos = [User32.WinUtils]::GetScrollPos( $rtb1.Handle, [ScrollBarType]::SbVert )
[uint32]$npos = $nPos -shl 16
[uint32]$wParam = [ScrollBarCommands]::SB_THUMBPOSITION -bor $nPos
[User32.WinUtils]::SendMessage( $rtb2.Handle, [Message]::WM_VSCROLL, $wParam , [ref]0)
})
$form.showdialog()
Editors, please note: It's not a duplicate, this is about **Powershell. :)**