如何在C#中创建自定义滚动条? [英] How to Create Custom Scroll bars in C#?

查看:103
本文介绍了如何在C#中创建自定义滚动条?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要像这样滚动吧

[<] [P1,P2,P3,P4,P5,P6,P7] [>]

按钮,面板,按钮



当我点击左键时,面板上的项目必须向左移动,当点击右键时,它必须向右移动。



请帮帮我..先谢谢。

解决方案

好像你必须写一个自定义绘制的滚动条。此页面可能会对您有所帮助:

自定义绘制滚动条 [ ^ ]


< blockquote> HScrollBar控件是一个支持控件,用于向没有内置滚动的控件(如容器控件)添加水平滚动功能。对于已经内置滚动的控件,您不需要此控件。



在本文中,我将讨论如何创建HScrollBar控件并在没有内置滚动功能的Windows窗体控件上应用水平滚动。之后,我将讨论HScrollBar控件可用的各种属性和方法。





创建HScrollBar



我们可以使用创建HScrollBar控件表单设计者在设计时或在运行时使用代码中的HScrollBar类(也称为动态)。



在设计时创建HScrollBar控件,您只需将HScrollBar控件从Toolbox拖放到Visual Studio中的Form即可。在Form上拖放HScrollBar后,HScrollBar如图1所示。一旦HScrollBar在Form上,您可以移动它并使用鼠标调整其大小并设置其属性和事件。





HSCBarImg1.jpg

图1



创建HScrollBar控件在运行时只是创建HScrollBar类的实例,设置其属性并将HScrollBar类添加到Form控件的工作。



创建动态的第一步HScrollBar用于创建HScrollBar类的实例。以下代码片段创建一个HScrollBar控件对象。



HScrollBar hScroller = new HScrollBar();



在下一步中,您可以设置HScrollBar控件的属性。以下代码片段设置HScrollBar的高度和宽度属性。



hScroller.Height = 40;

hScroller.Width = 300;



一旦HScrollBar控件准备好了它的属性,下一步就是将HScrollBar控件添加到Form中。为此,我们使用Form.Controls.Add方法。以下代码片段将HScrollBar控件添加到当前表单。



this.Controls.Add(hScroller);



设置HScrollBar属性



在表单上放置HScrollBar控件后,下一步是设置属性。



设置属性的最简单方法是从属性窗口。您可以通过按F4打开属性窗口,或者右键单击控件并选择属性菜单项。 Properties窗口如图2所示。





HSCBarImg2.jpg

图2



位置,高度,宽度和大小



Location属性采用一个Point来指定HScrollBar的起始位置表单。 Size属性指定控件的大小。我们也可以使用Width和Height属性而不是Size属性。 Dock属性用于停靠控件。以下代码片段设置HScrollBar控件的Dock,Width和Height属性。



hScroller.Dock = DockStyle.Bottom;

hScroller .Width = 250;

hScroller.Height = 200;

名称



名称属性表示唯一名称一个HScrollBar控件。它用于访问代码中的控件。以下代码片段设置并获取HScrollBar控件的名称和文本。



authorGroup.Name =HScrollBar1;

最大值,最小值和值



HScrollBar的Value属性表示HScrollBar控件的当前值。 Minimum和Maximum属性用于设置HScrollBar的最小和最大限制。以下代码段设置HScrollBar控件的Minimum,Maximum和Value属性。



hScroller.Minimum = 0;

hScroller。最大值= 100;

hScroller.Value = 40;

将HScrollBar附加到控件



水平滚动条控件通过其滚动事件附加到控件。在滚动事件处理程序中,我们通常会读取HScrollBar的当前值,并根据此值,我们应用于其他控件。例如,我们可以通过在滚动事件栏上再次在PictureBox中显示图像来实现PictureBox控件中的滚动。



以下代码片段为其添加了一个事件处理程序滚动事件。



hScroller.Scroll + = new System.Windows.Forms.ScrollEventHandler(hScroller_Scroll);





以下代码片段用于根据HScrollBar的值重绘PictureBox上的图像。



private void hScroller_Scroll (对象发送者,ScrollEventArgs e)

{

图形g = pictureBox1.CreateGraphics();

g.DrawImage(pictureBox1.Image,

新的Rectangle(0,0,pictureBox1.Height,hScroller.Value));

}



或点击此处: http://www.c-sharpcorner.com/uploadfile/ mahesh / horizo​​ntal-scrollbar-in-C-Sharp / [ ^ ]


< blockquote>点击此处: http://www.c-sharpcorner.com/ uploadfile / mahesh / horizo​​ntal-scrollbar-in-C-Sharp / [ ^ ]


I need to scroll bar like this
[<] [P1,P2,P3,P4,P5,P6,P7] [>]
button , panel, button

when i click on left button, items on the panel have to move left , when click right button , it have to move right.

Please Help Me.. Thanks in Advance.

解决方案

Seems like you have to write a custom drawn scroll bar. This page may be helpful:
Custom Drawn Scrollbar[^]


An HScrollBar control is a supporting control that is used to add horizontal scrolling capability to a control that does not have built-in scrolling such as a container control. You do not need this control for the controls that already have built-in scrolling.

In this article, I will discuss how to create an HScrollBar control and apply horizontal scrolling on Windows Forms controls that do not have built-in scrolling feature. After that, I will discuss various properties and methods available for the HScrollBar control.


Creating an HScrollBar

We can create an HScrollBar control using a Forms designer at design-time or using the HScrollBar class in code at run-time (also known as dynamically).

To create an HScrollBar control at design-time, you simply drag and drop an HScrollBar control from Toolbox to a Form in Visual Studio. After you drag and drop an HScrollBar on a Form, the HScrollBar looks like Figure 1. Once an HScrollBar is on the Form, you can move it around and resize it using mouse and set its properties and events.


HSCBarImg1.jpg
Figure 1

Creating an HScrollBar control at run-time is merely a work of creating an instance of HScrollBar class, set its properties and add HScrollBar class to the Form controls.

First step to create a dynamic HScrollBar is to create an instance of HScrollBar class. The following code snippet creates an HScrollBar control object.

HScrollBar hScroller = new HScrollBar();

In the next step, you may set properties of an HScrollBar control. The following code snippet sets the height and width properties of an HScrollBar.

hScroller.Height = 40;
hScroller.Width = 300;

Once an HScrollBar control is ready with its properties, next step is to add the HScrollBar control to the Form. To do so, we use Form.Controls.Add method. The following code snippet adds an HScrollBar control to the current Form.

this.Controls.Add(hScroller);

Setting HScrollBar Properties

After you place an HScrollBar control on a Form, the next step is to set properties.

The easiest way to set properties is from the Properties Window. You can open Properties window by pressing F4 or right click on a control and select Properties menu item. The Properties window looks like Figure 2.


HSCBarImg2.jpg
Figure 2

Location, Height, Width, and Size

The Location property takes a Point that specifies the starting position of the HScrollBar on a Form. The Size property specifies the size of the control. We can also use Width and Height property instead of Size property. The Dock property is used to dock a control. The following code snippet sets Dock, Width, and Height properties of an HScrollBar control.

hScroller.Dock = DockStyle.Bottom;
hScroller.Width = 250;
hScroller.Height = 200;
Name

Name property represents a unique name of an HScrollBar control. It is used to access the control in the code. The following code snippet sets and gets the name and text of an HScrollBar control.

authorGroup.Name = "HScrollBar1";
Maximum, Minimum, and Value

Value property of an HScrollBar represents the current value of an HScrollBar control. The Minimum and Maximum properties are used to set minimum and maximum limit of an HScrollBar. The following code snippet sets the Minimum, Maximum, and Value properties of an HScrollBar control.

hScroller.Minimum = 0;
hScroller.Maximum = 100;
hScroller.Value = 40;
Attaching HScrollBar to a Control

Horizontal scroll bar control is attached to a control by its scroll event. On the scroll event hander, we usually read the current value of an HScrollBar and based on this value, we apply on other controls. For example, we can implement scrolling in a PictureBox control by displaying image in a PictureBox again on the scroll event bar.

The following code snippet adds an event handler for the Scroll event.

hScroller.Scroll += new System.Windows.Forms.ScrollEventHandler(hScroller_Scroll);


The following code snippet is used to redraw the image on a PictureBox based on the value of the HScrollBar.

private void hScroller_Scroll(object sender, ScrollEventArgs e)
{
Graphics g = pictureBox1.CreateGraphics();
g.DrawImage(pictureBox1.Image,
new Rectangle(0, 0, pictureBox1.Height, hScroller.Value));
}

Or Click here :http://www.c-sharpcorner.com/uploadfile/mahesh/horizontal-scrollbar-in-C-Sharp/[^]


Click here : http://www.c-sharpcorner.com/uploadfile/mahesh/horizontal-scrollbar-in-C-Sharp/[^]


这篇关于如何在C#中创建自定义滚动条?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆