垂直滚动条代码

代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Windows.Forms.Design;
using System.Diagnostics;
namespace CustomControls {

[Designer(typeof(ScrollbarControlDesigner))]
public class CustomScrollbar : UserControl {

protected Color moChannelColor = Color.Empty;
protected Image moUpArrowImage = null;
//protected Image moUpArrowImage_Over = null;
//protected Image moUpArrowImage_Down = null;
protected Image moDownArrowImage = null;
//protected Image moDownArrowImage_Over = null;
//protected Image moDownArrowImage_Down = null;
protected Image moThumbArrowImage = null;

protected Image moThumbTopImage = null;
protected Image moThumbTopSpanImage = null;
protected Image moThumbBottomImage = null;
protected Image moThumbBottomSpanImage = null;
protected Image moThumbMiddleImage = null;

protected int moLargeChange = 10;
protected int moSmallChange = 1;
protected int moMinimum = 0;
protected int moMaximum = 100;
protected int moValue = 0;
private int nClickPoint;

protected int moThumbTop = 0;

protected bool moAutoSize = false;

private bool moThumbDown = false;
private bool moThumbDragging = false;

public new event EventHandler Scroll = null;
public event EventHandler ValueChanged = null;

private int GetThumbHeight()
{
int nTrackHeight = (this.Height - (UpArrowImage.Height + DownArrowImage.Height));
float fThumbHeight = ((float)LargeChange / (float)Maximum) * nTrackHeight;
int nThumbHeight = (int)fThumbHeight;

if (nThumbHeight > nTrackHeight)
{
nThumbHeight = nTrackHeight;
fThumbHeight = nTrackHeight;
}
if (nThumbHeight < 56)
{
nThumbHeight = 56;
fThumbHeight = 56;
}

return nThumbHeight;
}

public CustomScrollbar() {

InitializeComponent();
SetStyle(ControlStyles.ResizeRedraw, true);
SetStyle(ControlStyles.AllPaintingInWmPaint, true);
SetStyle(ControlStyles.DoubleBuffer, true);

moChannelColor = Color.FromArgb(51, 166, 3);
UpArrowImage = Resource.uparrow;
DownArrowImage = Resource.downarrow;

ThumbBottomImage = Resource.ThumbBottom;
ThumbBottomSpanImage = Resource.ThumbSpanBottom;
ThumbTopImage = Resource.ThumbTop;
ThumbTopSpanImage = Resource.ThumbSpanTop;
ThumbMiddleImage = Resource.ThumbMiddle;

this.Width = UpArrowImage.Width;
base.MinimumSize = new Size(UpArrowImage.Width, UpArrowImage.Height + DownArrowImage.Height + GetThumbHeight());
}

[EditorBrowsable(EditorBrowsableState.Always), Browsable(true), DefaultValue(false), Category("Behavior"), Description("LargeChange")]
public int LargeChange {
get { return moLargeChange; }
set { moLargeChange = value;
Invalidate();
}
}

[EditorBrowsable(EditorBrowsableState.Always), Browsable(true), DefaultValue(false), Category("Behavior"), Description("SmallChange")]
public int SmallChange {
get { return moSmallChange; }
set { moSmallChange = value;
Invalidate();
}
}

[EditorBrowsable(EditorBrowsableState.Always), Browsable(true), DefaultValue(false), Category("Behavior"), Description("Minimum")]
public int Minimum {
get { return moMinimum; }
set { moMinimum = value;
Invalidate();
}
}

[EditorBrowsable(EditorBrowsableState.Always), Browsable(true), DefaultValue(false), Category("Behavior"), Description("Maximum")]
public int Maximum {
get { return moMaximum; }
set { moMaximum = value;
Invalidate();
}
}

[EditorBrowsable(EditorBrowsableState.Always), Browsable(true), DefaultValue(false), Category("Behavior"), Description("Value")]
public int Value {
get { return moValue; }
set { moValue = value;

int nTrackHeight = (this.Height - (UpArrowImage.Height + DownArrowImage.Height));
float fThumbHeight = ((float)LargeChange / (float)Maximum) * nTrackHeight;
int nThumbHeight = (int)fThumbHeight;

if (nThumbHeight > nTrackHeight)
{
nThumbHeight = nTrackHeight;
fThumbHeight = nTrackHeight;
}
if (nThumbHeight < 56)
{
nThumbHeight = 56;
fThumbHeight = 56;
}

//figure out value
int nPixelRange = nTrackHeight - nThumbHeight;
int nRealRange = (Maximum - Minimum)-LargeChange;
float fPerc = 0.0f;
if (nRealRange != 0)
{
fPerc = (float)moValue / (float)nRealRange;

}

float fTop = fPerc * nPixelRange;
moThumbTop = (int)fTop;

Invalidate();
}
}

[EditorBrowsable(EditorBrowsableState.Always), Browsable(true), DefaultValue(false), Category("Skin"), Description("Channel Color")]
public Color ChannelColor
{
get { return moChannelColor; }
set { moChannelColor = value; }
}

[EditorBrowsable(EditorBrowsableState.Always), Browsable(true), DefaultValue(false), Category("Skin"), Description("Up Arrow Graphic")]
public Image UpArrowImage {
get { return moUpArrowImage; }
set { moUpArrowImage = value; }
}

[EditorBrowsable(EditorBrowsableState.Always), Browsable(true), DefaultValue(false), Category("Skin"), Description("Up Arrow Graphic")]
public Image DownArrowImage {
get { return moDownArrowImage; }
set { moDownArrowImage = value; }
}

[EditorBrowsable(EditorBrowsableState.Always), Browsable(true), DefaultValue(false), Category("Skin"), Description("Up Arrow Graphic")]
public Image ThumbTopImage {
get { return moThumbTopImage; }
set { moThumbTopImage = value; }
}

[EditorBrowsable(EditorBrowsableState.Always), Browsable(true), DefaultValue(false), Category("Skin"), Description("Up Arrow Graphic")]
public Image ThumbTopSpanImage {
get { return moThumbTopSpanImage; }
set { moThumbTopSpanImage = value; }
}

[EditorBrowsable(EditorBrowsableState.Always), Browsable(true), DefaultValue(false), Category("Skin"), Description("Up Arrow Graphic")]
public Image ThumbBottomImage {
get { return moThumbBottomImage; }
set { moThumbBottomImage = value; }
}

[EditorBrowsable(EditorBrowsableState.Always), Browsable(true), DefaultValue(false), Category("Skin"), Description("Up Arrow Graphic")]
public Image ThumbBottomSpanImage {
get { return moThumbBottomSpanImage; }
set { moThumbBottomSpanImage = value; }
}

[EditorBrowsable(EditorBrowsableState.Always), Browsable(true), DefaultValue(false), Category("Skin"), Description("Up Arrow Graphic")]
public Image ThumbMiddleImage {
get { return moThumbMiddleImage; }
set { moThumbMiddleImage = value; }
}

protected override void OnPaint(PaintEventArgs e) {

e.Graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor;

if (UpArrowImage != null) {
e.Graphics.DrawImage(UpArrowImage, new Rectangle(new Point(0,0), new Size(this.Width, UpArrowImage.Height)));
}

Brush oBrush = new SolidBrush(moChannelColor);
Brush oWhiteBrush = new SolidBrush(Color.FromArgb(255,255,255));

//draw channel left and right border colors
e.Graphics.FillRectangle(oWhiteBrush, new Rectangle(0,UpArrowImage.Height, 1, (this.Height-DownArrowImage.Height)));
e.Graphics.FillRectangle(oWhiteBrush, new Rectangle(this.Width-1, UpArrowImage.Height, 1, (this.Height - DownArrowImage.Height)));

//draw channel
e.Graphics.FillRectangle(oBrush, new Rectangle(1, UpArrowImage.Height, this.Width-2, (this.Height-DownArrowImage.Height)));

//draw thumb
int nTrackHeight = (this.Height - (UpArrowImage.Height + DownArrowImage.Height));
float fThumbHeight = ((float)LargeChange / (float)Maximum) * nTrackHeight;
int nThumbHeight = (int)fThumbHeight;

if (nThumbHeight > nTrackHeight) {
nThumbHeight = nTrackHeight;
fThumbHeight = nTrackHeight;
}
if (nThumbHeight < 56) {
nThumbHeight = 56;
fThumbHeight = 56;
}

//Debug.WriteLine(nThumbHeight.ToString());

float fSpanHeight = (fThumbHeight - (ThumbMiddleImage.Height + ThumbTopImage.Height + ThumbBottomImage.Height)) / 2.0f;
int nSpanHeight = (int)fSpanHeight;

int nTop = moThumbTop;
nTop += UpArrowImage.Height;

//draw top
e.Graphics.DrawImage(ThumbTopImage, new Rectangle(1, nTop, this.Width - 2, ThumbTopImage.Height));

nTop += ThumbTopImage.Height;
//draw top span
Rectangle rect = new Rectangle(1, nTop, this.Width - 2, nSpanHeight);

e.Graphics.DrawImage(ThumbTopSpanImage, 1.0f,(float)nTop, (float)this.Width-2.0f, (float) fSpanHeight*2);

nTop += nSpanHeight;
//draw middle
e.Graphics.DrawImage(ThumbMiddleImage, new Rectangle(1, nTop, this.Width - 2, ThumbMiddleImage.Height));

nTop += ThumbMiddleImage.Height;
//draw top span
rect = new Rectangle(1, nTop, this.Width - 2, nSpanHeight*2);
e.Graphics.DrawImage(ThumbBottomSpanImage, rect);

nTop += nSpanHeight;
//draw bottom
e.Graphics.DrawImage(ThumbBottomImage, new Rectangle(1, nTop, this.Width - 2, nSpanHeight));

if (DownArrowImage != null) {
e.Graphics.DrawImage(DownArrowImage, new Rectangle(new Point(0, (this.Height-DownArrowImage.Height)), new Size(this.Width, DownArrowImage.Height)));
}

}
public override bool AutoSize {
get {
return base.AutoSize;
}
set {
base.AutoSize = value;
if (base.AutoSize) {
this.Width = moUpArrowImage.Width;
}
}
}

private void InitializeComponent() {
this.SuspendLayout();
//
// CustomScrollbar
//
this.Name = "CustomScrollbar";
this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.CustomScrollbar_MouseDown);
this.MouseMove += new System.Windows.Forms.MouseEventHandler(this.CustomScrollbar_MouseMove);
this.MouseUp += new System.Windows.Forms.MouseEventHandler(this.CustomScrollbar_MouseUp);
this.ResumeLayout(false);

}

private void CustomScrollbar_MouseDown(object sender, MouseEventArgs e) {
Point ptPoint = this.PointToClient(Cursor.Position);
int nTrackHeight = (this.Height - (UpArrowImage.Height + DownArrowImage.Height));
float fThumbHeight = ((float)LargeChange / (float)Maximum) * nTrackHeight;
int nThumbHeight = (int)fThumbHeight;

if (nThumbHeight > nTrackHeight) {
nThumbHeight = nTrackHeight;
fThumbHeight = nTrackHeight;
}
if (nThumbHeight < 56) {
nThumbHeight = 56;
fThumbHeight = 56;
}

int nTop = moThumbTop;
nTop += UpArrowImage.Height;

Rectangle thumbrect = new Rectangle(new Point(1, nTop), new Size(ThumbMiddleImage.Width, nThumbHeight));
if (thumbrect.Contains(ptPoint))
{

//hit the thumb
nClickPoint = (ptPoint.Y - nTop);
//MessageBox.Show(Convert.ToString((ptPoint.Y - nTop)));
this.moThumbDown = true;
}

Rectangle uparrowrect = new Rectangle(new Point(1, 0), new Size(UpArrowImage.Width, UpArrowImage.Height));
if (uparrowrect.Contains(ptPoint))
{

int nRealRange = (Maximum - Minimum)-LargeChange;
int nPixelRange = (nTrackHeight - nThumbHeight);
if (nRealRange > 0)
{
if (nPixelRange > 0)
{
if ((moThumbTop - SmallChange) < 0)
moThumbTop = 0;
else
moThumbTop -= SmallChange;

//figure out value
float fPerc = (float)moThumbTop / (float)nPixelRange;
float fValue = fPerc * (Maximum - LargeChange);

moValue = (int)fValue;
Debug.WriteLine(moValue.ToString());

if (ValueChanged != null)
ValueChanged(this, new EventArgs());

if (Scroll != null)
Scroll(this, new EventArgs());

Invalidate();
}
}
}

Rectangle downarrowrect = new Rectangle(new Point(1, UpArrowImage.Height+nTrackHeight), new Size(UpArrowImage.Width, UpArrowImage.Height));
if (downarrowrect.Contains(ptPoint))
{
int nRealRange = (Maximum - Minimum) - LargeChange;
int nPixelRange = (nTrackHeight - nThumbHeight);
if (nRealRange > 0)
{
if (nPixelRange > 0)
{
if ((moThumbTop + SmallChange) > nPixelRange)
moThumbTop = nPixelRange;
else
moThumbTop += SmallChange;

//figure out value
float fPerc = (float)moThumbTop / (float)nPixelRange;
float fValue = fPerc * (Maximum-LargeChange);

moValue = (int)fValue;
Debug.WriteLine(moValue.ToString());

if (ValueChanged != null)
ValueChanged(this, new EventArgs());

if (Scroll != null)
Scroll(this, new EventArgs());

Invalidate();
}
}
}
}

private void CustomScrollbar_MouseUp(object sender, MouseEventArgs e) {
this.moThumbDown = false;
this.moThumbDragging = false;
}

private void MoveThumb(int y) {
int nRealRange = Maximum - Minimum;
int nTrackHeight = (this.Height - (UpArrowImage.Height + DownArrowImage.Height));
float fThumbHeight = ((float)LargeChange / (float)Maximum) * nTrackHeight;
int nThumbHeight = (int)fThumbHeight;

if (nThumbHeight > nTrackHeight) {
nThumbHeight = nTrackHeight;
fThumbHeight = nTrackHeight;
}
if (nThumbHeight < 56) {
nThumbHeight = 56;
fThumbHeight = 56;
}

int nSpot = nClickPoint;

int nPixelRange = (nTrackHeight - nThumbHeight);
if (moThumbDown && nRealRange > 0) {
if (nPixelRange > 0) {
int nNewThumbTop = y - (UpArrowImage.Height+nSpot);

if(nNewThumbTop<0)
{
moThumbTop = nNewThumbTop = 0;
}
else if(nNewThumbTop > nPixelRange)
{
moThumbTop = nNewThumbTop = nPixelRange;
}
else {
moThumbTop = y - (UpArrowImage.Height + nSpot);
}

//figure out value
float fPerc = (float)moThumbTop / (float)nPixelRange;
float fValue = fPerc * (Maximum-LargeChange);
moValue = (int)fValue;
Debug.WriteLine(moValue.ToString());

Application.DoEvents();

Invalidate();
}
}
}

private void CustomScrollbar_MouseMove(object sender, MouseEventArgs e) {
if(moThumbDown == true)
{
this.moThumbDragging = true;
}

if (this.moThumbDragging) {

MoveThumb(e.Y);
}

if(ValueChanged != null)
ValueChanged(this, new EventArgs());

if(Scroll != null)
Scroll(this, new EventArgs());
}

}

internal class ScrollbarControlDesigner : System.Windows.Forms.Design.ControlDesigner {

public override SelectionRules SelectionRules {
get {
SelectionRules selectionRules = base.SelectionRules;
PropertyDescriptor propDescriptor = TypeDescriptor.GetProperties(this.Component)["AutoSize"];
if (propDescriptor != null) {
bool autoSize = (bool)propDescriptor.GetValue(this.Component);
if (autoSize) {
selectionRules = SelectionRules.Visible | SelectionRules.Moveable | SelectionRules.BottomSizeable | SelectionRules.TopSizeable;
}
else {
selectionRules = SelectionRules.Visible | SelectionRules.AllSizeable | SelectionRules.Moveable;
}
}
return selectionRules;
}
}
}
}

时间: 2024-08-09 21:51:28

垂直滚动条代码的相关文章

如何让VB6代码编辑器垂直滚动条随鼠标滚轮滚动

VB6毕竟是很老的产品了,它的代码编辑器垂直滚动条并不能随鼠标的滚轮而滚动,这个问题会让我们在编写代码的时候觉得很不方便,不过还是有一种方法可以解决这个问题的.    先下载一个微软发布的"VB6IDEMouseWheelAddin.dll"文件(此文件已经上传到百度网盘,网址:http://pan.baidu.com/s/1c06KY7e,或者也可以自己百度一下该文件名下载),然后按照以下的方法进行(注意:此处介绍的是一种通用的方法,适合是32位和64位的系统上使用): 一.  将下

jQuery 的 Div 标签滚动条属性及判断垂直滚动条是否到达底部

转自:http://blog.163.com/[email protected]/blog/static/1284651702010894509982/ 关于 jQuery 的 Div 标签的滚动条的概念,没有几个人能够完全.正确搞明白的. 我知道很多人不同意我的观点.但是如果去百度上搜素,与 jQuery 滚动条有关的信息,都是关于滚动条外观和滚动条插件的.我最近在制作一个滚动条相关的页面效果,去 CSDN 论坛里提问,得到了一段代码,还是错误的. jQuery 里和滚动条有关的概念很多,但是

unity3d v5.1.1 ugui 带垂直滚动条的文本框

http://www.cnblogs.com/zhaoqingqing/p/3973167.html?utm_source=tuicool http://blog.csdn.net/rcfalcon/article/details/43459387 看了这两篇帖子,还是他妈的不知道怎么做,但第一个帖子的作法我已经实现了帖子内容,第二个帖子完全实现不了,于是把第一帖和第二帖的内容结合起来搞定了,我就操了,一个带垂直滚动条的文本框都这么难做,能不能提供现成的? 一.在Canvas新建一个Panel,

自己写一个jQuery垂直滚动条插件(panel)

html中原生的滚动条比较难看,所以有些网站,会自己实现滚动条,导航网站hao123在一个侧栏中,就自定义了垂直滚动条,效果比较好看,截图如下: 这个滚动条,只有在鼠标悬停在这个区域内时才显示,半透明效果,很节省空间的说~~,说实话,这个效果我非常喜欢. 垂直滚动条的原理,简单来说: 先起个名字,外层的叫wrapper,内层的叫content,wrapper需要有非static的定位,content需要绝对定位,这样就可以通过调节top值来模拟内容滚动. 具体说一下: 1.wrapper的ove

【转】MFC编辑框自动换行,垂直滚动条自动下移

1.新建一个编辑框控件(Edit Control),将其多行(Multiline)前面打勾(属性设置为True),Auto HScroll前面的勾去掉(属性设置False),这样就可以实现每一行填满后自动换行了. 2.再将垂直滚动条(Vetrical Scroll)前面打勾(属性设置为True),当输入或显示超过编辑框的大小后就会出现垂直滚动条. 3.如果是输入,滚动条会自动移动跟随到当前输入行,但是如果是设置将很多内容一次性让编辑框显示,滚动条就会一直处于最上方,需要手动拉到最下面才能看见最后

LinearLayout 垂直滚动条

http://blog.csdn.net/yuxiaohui78/article/details/8167571 [java] view plaincopy <?xml version="1.0" encoding="utf-8"?> <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="

jquery easyui datagrid 无滚动条,datagrid 没垂直滚动条

jquery easyui datagrid 无滚动条,datagrid 没垂直滚动条 ============================== 蕃薯耀 2018年2月6日 http://www.cnblogs.com/fanshuyao/ 一.问题描述: 当jquery easyui datagrid中数据量大时,body页面又设置高度超出隐藏时(如:overflow-y:hidden)时,这时datagrid加载满整个页面时也无滚动条. 二.解决方案: 给datagrid绑定onLoad

MFC 控件编程之水平滚动条跟垂直滚动条

MFC 控件编程之水平滚动条跟垂直滚动条 一点水平滚动条的操作 首先在操作滚动条的时候.我们要知道滚动条的一些属性. 比如我们要设置 最大值 最小值. 以及每次递增的值是多少.都要设置. 所有就有一个结构.专门存储了滚动条信息. 而我们在对话框一启动就要进行控件初始化.下方看下结构. typedef struct tagSCROLLINFO { UINT cbSize; 自身大小 UINT fMask; 滚动条的范围.左边->右边方式. int nMin; 滚动条最小值 int nMax; 滚动

java中如何将JScrollPane的垂直滚动条自动移动到最下端

JPanel QQP = new JPanel(); JScrollPane jsp = new JScrollPane(QQP); JScrollBar jsb = jsp.getVerticalScrollBar(); QQP.updateUI();//利用当前外观的值重置 UI 属性. 也可以保证滚动条随时的更新 //终于搞好了,将垂直滚动条自动的移动到最低端 //setViewPosition:设置显示在视口左上角的视图坐标 // jsp.getVerticalScrollBar().g