0

WinForms を使用して、実際のプログラムで問題が発生した後、最小限のテスト アプリを実行しています。大きなパネル (親) の中に小さなパネル (子) を配置しました。大きなパネルでは AutoScroll が true に設定されています。子パネルには、デフォルトのアンカーが上と左に設定されています。子パネルはドッキングされていません。

私が望む動作は、小さいパネルの位置が上、下、左、右のいずれかにずれすぎたときにスクロールバーが表示されるようにすることです。問題は、右に行き過ぎているか、下に行き過ぎている場合にのみ機能することです。上に行きすぎたり、左に行きすぎたりすると、スクロールバーが表示されなくなります。

2 つの単純なボタンを使用して、子パネルの位置を強制的に左に 200 ピクセル、または右に 200 ピクセルにして、その位置を簡単に変更できるようにします。

ここに私の Form1() コードがあります:

        private void button1_Click(object sender, EventArgs e)
    {
        childPanel.Location = new Point(childPanel.Location.X - 200, childPanel.Location.Y);
        hostPanel.Invalidate();
    }

    private void button2_Click(object sender, EventArgs e)
    {
        childPanel.Location = new Point(childPanel.Location.X + 200, childPanel.Location.Y);
        hostPanel.Invalidate();
    }

デザイナーのコードは次のとおりです。

       private void InitializeComponent()
    {
        this.hostPanel = new System.Windows.Forms.Panel();
        this.childPanel = new System.Windows.Forms.Panel();
        this.moveChildLeft = new System.Windows.Forms.Button();
        this.moveChildRight = new System.Windows.Forms.Button();
        this.hostPanel.SuspendLayout();
        this.SuspendLayout();
        // 
        // hostPanel
        // 
        this.hostPanel.AutoScroll = true;
        this.hostPanel.BackColor = System.Drawing.SystemColors.AppWorkspace;
        this.hostPanel.Controls.Add(this.childPanel);
        this.hostPanel.Location = new System.Drawing.Point(239, 48);
        this.hostPanel.Name = "hostPanel";
        this.hostPanel.Size = new System.Drawing.Size(400, 400);
        this.hostPanel.TabIndex = 0;
        // 
        // childPanel
        // 
        this.childPanel.BackColor = System.Drawing.SystemColors.ButtonHighlight;
        this.childPanel.Location = new System.Drawing.Point(29, 62);
        this.childPanel.Name = "childPanel";
        this.childPanel.Size = new System.Drawing.Size(342, 259);
        this.childPanel.TabIndex = 0;
        // 
        // moveChildLeft
        // 
        this.moveChildLeft.Location = new System.Drawing.Point(61, 81);
        this.moveChildLeft.Name = "moveChildLeft";
        this.moveChildLeft.Size = new System.Drawing.Size(75, 23);
        this.moveChildLeft.TabIndex = 1;
        this.moveChildLeft.Text = "Left 200";
        this.moveChildLeft.UseVisualStyleBackColor = true;
        this.moveChildLeft.Click += new System.EventHandler(this.button1_Click);
        // 
        // moveChildRight
        // 
        this.moveChildRight.Location = new System.Drawing.Point(61, 111);
        this.moveChildRight.Name = "moveChildRight";
        this.moveChildRight.Size = new System.Drawing.Size(75, 23);
        this.moveChildRight.TabIndex = 2;
        this.moveChildRight.Text = "Right 200";
        this.moveChildRight.UseVisualStyleBackColor = true;
        this.moveChildRight.Click += new System.EventHandler(this.button2_Click);
        // 
        // Form1
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(1018, 549);
        this.Controls.Add(this.moveChildRight);
        this.Controls.Add(this.moveChildLeft);
        this.Controls.Add(this.hostPanel);
        this.Name = "Form1";
        this.Text = "Form1";
        this.hostPanel.ResumeLayout(false);
        this.ResumeLayout(false);
    }
4

2 に答える 2

0

最終的に、アプリを WPF に変換することが必要になる場合があります。以来、Winformは小さな死を宣告されています。

于 2013-02-08T18:02:38.703 に答える
0

まだ-WPF によってすぐに解決された別のWinformsの非機能:

XAML:

<Window x:Class="WpfApplication4.Window3"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window3" WindowState="Maximized">
    <DockPanel>
        <StackPanel DockPanel.Dock="Top" Orientation="Horizontal">
            <Button Content="Left" Click="MoveLeft"/>
            <Button Content="Right" Click="MoveRight"/>
        </StackPanel>
        <Border BorderBrush="Blue" BorderThickness="1" Width="300" Height="300">
            <ScrollViewer VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto" x:Name="Scr">
                <Grid Background="Green" Width="100" Height="100" x:Name="Grid"/>
            </ScrollViewer>
        </Border>
    </DockPanel>
</Window>

コードビハインド:

using System.Windows;

namespace WpfApplication4
{
    public partial class Window3 : Window
    {
        public Window3()
        {
            InitializeComponent();
        }

        private void MoveRight(object sender, RoutedEventArgs e)
        {
            if (Grid.Margin.Right <= 0)
            {
                Grid.Margin = new Thickness(Grid.Margin.Left + 100,0,0,0);
            }
            else
            {
                Grid.Margin = new Thickness(0, 0, Grid.Margin.Right - 100, 0);
                Scr.ScrollToHorizontalOffset(Scr.HorizontalOffset - 100);
            }
        }

        private void MoveLeft(object sender, RoutedEventArgs e)
        {
            if (Grid.Margin.Left > 0)
            {
                Grid.Margin = new Thickness(Grid.Margin.Left - 100, 0, 0, 0);
            }
            else
            {
                Grid.Margin = new Thickness(0, 0, Grid.Margin.Right + 100, 0);
                Scr.ScrollToHorizontalOffset(Scr.HorizontalOffset + 100);
            }
        }
    }
}

私のコードをコピーして File -> New -> WPF Application に貼り付けて、自分で結果を確認してください。

于 2013-02-05T21:13:19.860 に答える