您可以将所有控件放在一个透明的面板中,并溢出表单的边界(例如,如果表单的宽度为 500 像素,高度为 600 像素,您可以使面板比表单更宽,并将控件放置在其中)。在面板下方留出一些空间并在那里添加您的按钮。
在 LeftScrlBtn_Click 代码块中添加以下内容:this.panel1.Left--;
将此添加到 RightScrlBtn_Click:this.panel1.Left++;
当然,您可以更改面板的增量值,使其滚动得更快。例如,只需将 ++ 替换为 +=5 或 -- 替换为 -=5 即可在点击时滚动 5 个像素。
这是我做的一个代码示例(进入 Form1.cs):
using System.Collections.Generic;
using System.Windows.Forms;
namespace StackOfAnswer
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//Scrolling left
this.panel1.Left--;
}
private void button2_Click(object sender, EventArgs e)
{
//Scrolling right
this.panel1.Left++;
}
}
}
这是进入 Form1.Designer.cs 的代码:
namespace StackOfAnswer
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1));
this.panel1 = new System.Windows.Forms.Panel();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
this.panel1.SuspendLayout();
this.SuspendLayout();
//
// panel1
//
this.panel1.BackColor = System.Drawing.Color.Black;
this.panel1.Controls.Add(this.label1);
this.panel1.Location = new System.Drawing.Point(-3, -1);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(904, 422);
this.panel1.TabIndex = 0;
//
// button1
//
this.button1.Location = new System.Drawing.Point(24, 427);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 1;
this.button1.Text = "scroll left";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(713, 427);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(75, 23);
this.button2.TabIndex = 1;
this.button2.Text = "scroll right";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// label1
//
this.label1.AutoSize = true;
this.label1.ForeColor = System.Drawing.Color.White;
this.label1.Location = new System.Drawing.Point(24, 136);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(1561, 13);
this.label1.TabIndex = 0;
this.label1.Text = " example example example example example example example example example example example example example example example example example example example example example example example ";
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(800, 450);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Controls.Add(this.panel1);
this.Name = "Form1";
this.Text = "Form1";
this.panel1.ResumeLayout(false);
this.panel1.PerformLayout();
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.Panel panel1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
}
}
应该这样做。