C# Powershell:同步滚动两个RichTextBox
我想同步两个C# Powershell:同步滚动两个RichTextBox,c#,winforms,powershell,event-handling,richtextbox,C#,Winforms,Powershell,Event Handling,Richtextbox,我想同步两个Winformsrichtextbox的滚动。当RTB2被对齐时,RTB1需要始终精确对齐。 我试图在这里转换这个c代码,但到目前为止失败了。所以我需要帮助。 现在它会产生多个错误: Type [ScrollBarCommands] was not found... Type [ScrollBarType] was not found.... Type [Message] was not found... Also "illegal conversions" and so on.
Winforms
richtextbox的滚动。当RTB2被对齐时,RTB1需要始终精确对齐。
我试图在这里转换这个c代码,但到目前为止失败了。所以我需要帮助。
现在它会产生多个错误:
Type [ScrollBarCommands] was not found...
Type [ScrollBarType] was not found....
Type [Message] was not found...
Also "illegal conversions" and so on.
这是一个示例脚本:
[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()
编辑,请注意:这不是一个副本,这是关于**Powershell:)**
滚动条类型
、滚动条命令
和消息
是User32.WinUtils
命名空间的成员。另外,ptr
应该是System.IntPtr
编辑:
要在拖动滚动条的同时滚动$rtb2
(不仅仅是松开鼠标时),必须使用GetScrollInfo
编辑2:
固定箭头滚动按钮不工作
编辑3:
修复了其他错误。重组代码
$typeDef = @"
using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;
public enum ScrollBarType : uint {
SbHorz = 0,
SbVert = 1,
SbCtl = 2,
SbBoth = 3
}
public enum Message : uint {
WmVScroll = 0x0115
}
public enum ScrollBarCommands : uint {
ThumbPosition = 4,
ThumbTrack = 5
}
[Flags()]
public enum ScrollBarInfo : uint {
Range = 0x0001,
Page = 0x0002,
Pos = 0x0004,
DisableNoScroll = 0x0008,
TrackPos = 0x0010,
All = ( Range | Page | Pos | TrackPos )
}
public class CustomRichTextBox : RichTextBox {
public Control Buddy { get; set; }
public bool ThumbTrack = false;
[StructLayout( LayoutKind.Sequential )]
public struct ScrollInfo {
public uint cbSize;
public uint fMask;
public int nMin;
public int nMax;
public uint nPage;
public int nPos;
public int nTrackPos;
};
[DllImport( "User32.dll" )]
public extern static int SendMessage( IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam );
[DllImport( "User32.dll" )]
public extern static int GetScrollInfo( IntPtr hWnd, int fnBar, ref ScrollInfo lpsi );
public void CustomVScroll() {
int nPos;
ScrollInfo scrollInfo = new ScrollInfo();
scrollInfo.cbSize = (uint)Marshal.SizeOf( scrollInfo );
if (ThumbTrack) {
scrollInfo.fMask = (uint)ScrollBarInfo.TrackPos;
GetScrollInfo( this.Handle, (int)ScrollBarType.SbVert, ref scrollInfo );
nPos = scrollInfo.nTrackPos;
} else {
scrollInfo.fMask = (uint)ScrollBarInfo.Pos;
GetScrollInfo( this.Handle, (int)ScrollBarType.SbVert, ref scrollInfo );
nPos = scrollInfo.nPos;
}
nPos <<= 16;
uint wParam = (uint)ScrollBarCommands.ThumbPosition | (uint)nPos;
SendMessage( Buddy.Handle, (int)Message.WmVScroll, new IntPtr( wParam ), new IntPtr( 0 ) );
}
protected override void WndProc( ref System.Windows.Forms.Message m ) {
if ( m.Msg == (int)Message.WmVScroll ) {
if ( ( m.WParam.ToInt32() & 0xFF ) == (int)ScrollBarCommands.ThumbTrack ) {
ThumbTrack = true;
} else {
ThumbTrack = false;
}
}
base.WndProc( ref m );
}
}
"@
$assemblies = ("System.Windows.Forms", "System.Runtime.InteropServices")
Add-Type -ReferencedAssemblies $assemblies -TypeDefinition $typeDef -Language CSharp
### Form
$form = New-Object System.Windows.Forms.Form
$form.Size = "400,400"
### Rich text box 1 (Synchronized - master)
$rtb1 = New-Object CustomRichTextBox
$rtb1.Size = "190,350"
$rtb1.Location = "200,1"
$rtb1.Text = (1..300 | Out-String)
$form.Controls.Add($rtb1)
### Rich text box 2 (Synchronized - slave)
$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)
### Synchronization setup
$rtb1.Buddy = $rtb2
$rtb1.Add_VScroll({
$this.CustomVScroll()
})
### Run
$form.ShowDialog()
$typeDef=@”
使用制度;
使用System.Windows.Forms;
使用System.Runtime.InteropServices;
公共枚举滚动条类型:uint{
SbHorz=0,
SbVert=1,
SbCtl=2,
sb2=3
}
公共枚举消息:uint{
WMVSCROL=0x0115
}
公共枚举滚动条命令:uint{
拇指位置=4,
拇指轨迹=5
}
[标志()]
公共枚举ScrollBarInfo:uint{
范围=0x0001,
第页=0x0002,
位置=0x0004,
DisableNoScroll=0x0008,
TrackPos=0x0010,
全部=(范围|页面|位置|轨迹位置)
}
公共类CustomRichTextBox:RichTextBox{
公共控制伙伴{get;set;}
公共bool ThumbTrack=false;
[StructLayout(LayoutKind.Sequential)]
公共结构滚动信息{
公共单位cbSize;
公共单位任务;
公共国际信息网;
公共int nMax;
公共单位网页;
公共非营利组织;
公共int nTrackPos;
};
[DllImport(“User32.dll”)]
公共外部静态int SendMessage(IntPtr hWnd、uint msg、IntPtr wParam、IntPtr lParam);
[DllImport(“User32.dll”)]
公共外部静态int GetScrollInfo(IntPtr hWnd、int fnBar、ref ScrollInfo lpsi);
public void CustomVScroll(){
国际非营利组织;
ScrollInfo ScrollInfo=新的ScrollInfo();
scrollInfo.cbSize=(uint)Marshal.SizeOf(scrollInfo);
如果(拇指轨迹){
scrollInfo.fMask=(uint)ScrollBarInfo.TrackPos;
GetScrollInfo(this.Handle,(int)ScrollBarType.SbVert,ref scrollInfo);
nPos=scrollInfo.nTrackPos;
}否则{
scrollInfo.fMask=(uint)ScrollBarInfo.Pos;
GetScrollInfo(this.Handle,(int)ScrollBarType.SbVert,ref scrollInfo);
nPos=scrollInfo.nPos;
}
nPos非常好,谢谢!还有一个问题:现在RTB1只在用户完成滚动(鼠标按钮释放)时才会被嘲笑,有没有办法调整它,让RTB1在用户仍在滚动时被滚动?不确定你的意思是什么?试试看,你就会明白我的意思:)当你滚动右边的框时,左边的框只有在你释放滚动条并停止滚动时才会被滚动。这很好,但理想情况下两者都会同时滚动。我只看到:t只有当你用鼠标和滚动条滚动时,他才会出现。哦,我没有注意到,因为我正在使用滚动轮,它工作正常。让我看看我是否也能工作。出于某种原因,[User32.WinUtils]::GetScrollPos($rtb1.Handle,[User32.WinUtils+ScrollBarType]::SbVert)
在拖动滚动条时没有更新。