//-------------------------------------------------------------------------------
// 名称:ScrollPanel.cs
// 功能:一个可以自动滚动的Panel(其实Panel也可以AutoScroll,但,当panel中的控件重新得到焦点
// 后,会自动回滚到起始位置)
// 建立人:chinaplate 建立时间:2003-04-15-14
// TODO:
// 1.右下角的button未处理
// 2.panel中元件暂时只能放在(0,0)处
//--------------------------------------------------------------------------------
using System;
using System.Windows.Forms;
namespace Study
{
public class ScrollPanel:Panel
{
#region Data Member
protected const int SCROLL_WIDTH=16;
protected const string OWNER_TAG="_ownered";
protected HScrollBar m_hscrollBar;
protected VScrollBar m_vscrollBar;
protected Button m_scrollBtn;
protected bool m_autoscroll;
#endregion
#region Constructor
public ScrollPanel()
{
initializtion();
//
// TODO: 在此处添加构造函数逻辑
//
scrollPanelConstrutor();
}
#endregion
#region Private Function Member
private void scrollPanelConstrutor()
{
m_autoscroll=false;
}
private void setScrollLoction()
{
m_hscrollBar.Visible=false;
m_vscrollBar.Visible=false;
m_scrollBtn.Visible=false;
m_hscrollBar.Height=SCROLL_WIDTH;
m_hscrollBar.Width=this.Width-SCROLL_WIDTH;
m_hscrollBar.Left=0;
m_hscrollBar.Top=this.Height-SCROLL_WIDTH;
m_vscrollBar.Width=SCROLL_WIDTH;
m_vscrollBar.Height=this.Height-SCROLL_WIDTH;
m_vscrollBar.Top=0;
m_vscrollBar.Left=this.Width-SCROLL_WIDTH;
m_scrollBtn.Height=SCROLL_WIDTH;
m_scrollBtn.Width=SCROLL_WIDTH;
m_scrollBtn.Left=this.Width-SCROLL_WIDTH;
m_scrollBtn.Top=this.Height-SCROLL_WIDTH;
m_hscrollBar.Visible=true;
m_vscrollBar.Visible=true;
m_scrollBtn.Visible=true;
}
private void initializtion()
{
m_hscrollBar=new HScrollBar();
m_vscrollBar=new VScrollBar();
m_scrollBtn=new Button();
m_hscrollBar.Tag=OWNER_TAG;
m_vscrollBar.Tag=OWNER_TAG;
m_scrollBtn.Tag=OWNER_TAG;
m_vscrollBar.Scroll+=new ScrollEventHandler(vscrollBar_Scroll);
m_hscrollBar.Scroll+=new ScrollEventHandler(hscrollBar_Scroll);
m_scrollBtn.Click+=new EventHandler(scrollBtn_Click);
setScrollLoction();
this.Controls.AddRange(new Control[]{
m_hscrollBar,
m_vscrollBar,
m_scrollBtn
});
}
private bool isOwnerdControl(Control con)
{
bool ret=false;
if(con.Tag!=null &&
con.Tag.ToString()==OWNER_TAG) ret=true;
return ret;
}
private void autosizePanel()
{
m_hscrollBar.Enabled=false;
m_vscrollBar.Enabled=false;
//m_scrollBtn.Enabled=false;
if(m_autoscroll)
{
int maxH=0;
int maxV=0;
int curH=0;
int curV=0;
foreach(Control con in this.Controls)
{
if(!isOwnerdControl(con))
{
//TODO:panel中元件暂时只能放在(0,0)处
curH=0+con.Width;
curV=0+con.Height;
if(maxH<curH) maxH=curH;
if(maxV<curV) maxV=curV;
}
}
//curH,V有了新的含义了
curH=(maxH+SCROLL_WIDTH)-this.Width;
curV=(maxV+SCROLL_WIDTH)-this.Height;
if(curH>0)
{
m_hscrollBar.Minimum=0;
m_hscrollBar.Maximum=curH;
m_hscrollBar.Enabled=true;
}
if(curV>0)
{
m_vscrollBar.Minimum=0;
m_vscrollBar.Maximum=curV;
m_vscrollBar.Enabled=true;
}
}
}
#endregion
#region Protected Function Member
protected override void OnSizeChanged(EventArgs e)
{
setScrollLoction();
autosizePanel();
base.OnSizeChanged(e);
}
protected override void OnControlAdded(ControlEventArgs e)
{
e.Control.SizeChanged+=new EventHandler(control_SizeChanged);
base.OnControlAdded(e);
}
#endregion
#region Public Property
public new bool AutoScroll
{
set
{
m_autoscroll=value;
autosizePanel();
}
get
{
return m_autoscroll;
}
}
#endregion
#region Event Declare
public event ScrollEventHandler HScrollScroll;
public event ScrollEventHandler VScrollScroll;
//btn事件的保留
protected virtual void OnHScrollScroll(ScrollEventArgs e)
{
foreach(Control con in this.Controls)
{
if(!isOwnerdControl(con))
{
//TODO:panel中元件暂时只能放在(0,0)处
con.Left=0-e.NewValue;
}
}
if(HScrollScroll!=null)
{
HScrollScroll(this,e);
}
}
protected virtual void OnVScrollScroll(ScrollEventArgs e)
{
foreach(Control con in this.Controls)
{
if(!isOwnerdControl(con))
{
//TODO:panel中元件暂时只能放在(0,0)处
con.Top=0- e.NewValue;
}
}
if(VScrollScroll!=null)
{
VScrollScroll(this,e);
}
}
#endregion
#region Event Member
private void hscrollBar_Scroll(object sender,ScrollEventArgs e)
{
OnHScrollScroll(e);
}
private void vscrollBar_Scroll(object sender,ScrollEventArgs e)
{
OnVScrollScroll(e);
}
private void scrollBtn_Click(object sender,EventArgs e)
{
//保留
}
private void control_SizeChanged(object sender,EventArgs e)
{
autosizePanel();
}
#endregion
}
}