具体实现:当出现滚动条时,定位到中心位置
const int EM_LINESCROLL = 0x00B6;
[DllImport("user32.dll")]
static extern int SetScrollPos(IntPtr hWnd, int nBar, int nPos, bool bRedraw);
[DllImport("user32.dll")]
static extern int SendMessage(IntPtr hWnd, int wMsg, int wParam, int lParam);
[DllImport("user32.dll")]
static extern int GetScrollPos(IntPtr hwnd, int nbar);
[DllImport("user32", CharSet = CharSet.Auto)]
private static extern bool PostMessage(IntPtr hWnd, int nBar, int wParam, int lParam);
[DllImport("user32", CharSet = CharSet.Auto)]
private static extern bool GetScrollRange(IntPtr hWnd, int nBar, out int lpMinPos, out int lpMaxPos);
[DllImport("user32.dll")]
static extern bool SetScrollRange (IntPtr hwnd, int iBar, int iMin,int iMax,bool bRedraw) ;
// [DllImport("user32.dll")]
//static extern bool GetScrollRange(IntPtr hwnd,int ibar,out int X ,out int y);
private const int SB_HORZ = 0x0;
private const int SB_VERT = 0x1;
private const int WM_HSCROLL = 0x114;
private const int WM_VSCROLL = 0x115;
private const int SB_THUMBPOSITION = 4;
public int HScrollPos
{
get { return GetScrollPos((IntPtr)this.scrollpanel.Handle, SB_HORZ); }
set { SetScrollPos((IntPtr)this.scrollpanel.Handle, SB_HORZ, value, true); }
}
/// <summary>
/// Gets and Sets the Vertical Scroll position of the control.
/// </summary>
public int VScrollPos
{
get { return GetScrollPos((IntPtr)this.scrollpanel.Handle, SB_VERT); }
set { SetScrollPos((IntPtr)this.scrollpanel.Handle, SB_VERT, value, false); }
}
private void 放大_Click_1(object sender, EventArgs e)
{
btnSmaller.Enabled = true;
int CtlWidth = axPhRemoteCtrl.Size.Width;
int CtlHeight = axPhRemoteCtrl.Size.Height;
int HeightConstX = CtlWidth / 10, HeightConstY = CtlHeight / 10;
int CenterPointX = CtlWidth / 20, CenterPointY = CtlHeight / 20;
this.axPhRemoteCtrl.Dock = DockStyle.None;
this.axPhRemoteCtrl.Size = new Size(CtlWidth + HeightConstX, CtlHeight + HeightConstY);
VScrollPos += CenterPointY;
SendMessage(this.scrollpanel.Handle, WM_VSCROLL, SB_THUMBPOSITION + 0x10000 * CenterPointY, 0);
HScrollPos += CenterPointX;
SendMessage(this.scrollpanel.Handle, WM_HSCROLL, SB_THUMBPOSITION + 0x10000 * CenterPointX, 0);
this.scrollpanel.Invalidate();
}
private void 缩小_Click(object sender, EventArgs e)
{
GetScrollRange(this.scrollpanel.Handle, SB_HORZ, out hx, out hy);
GetScrollRange(this.scrollpanel.Handle, SB_VERT, out vx, out vy);
MessageBox.Show(hx+ " "+ hy + " "+vx + " "+ vy );
this.btnLarger.Enabled = true;
int CtlWidth = axPhRemoteCtrl.Size.Width;
int CtlHeight = axPhRemoteCtrl.Size.Height;
int HeightConstX = -CtlWidth / 10, HeightConstY = -CtlHeight / 10;
int CenterPointX = -CtlWidth / 20, CenterPointY = -CtlHeight / 20;
if (this.scrollpanel.Width >= (CtlWidth + 2*HeightConstX) || scrollpanel.Height >= (CtlHeight + 2*HeightConstY))
{
//MessageBox.Show(this.scrollpanel.Width + " >= " + (CtlWidth + HeightConstX) + "||" + scrollpanel.Height + ">=" + (CtlHeight + HeightConstY));
this.axPhRemoteCtrl.Dock = DockStyle.Fill;
this.btnSmaller.Enabled = false;
return;
}
this.axPhRemoteCtrl.Dock = DockStyle.None;
this.axPhRemoteCtrl.Size = new Size(CtlWidth + HeightConstX, CtlHeight + HeightConstY);
VScrollPos += CenterPointY;
SendMessage(this.scrollpanel.Handle, WM_VSCROLL, SB_THUMBPOSITION + 0x10000 * CenterPointY, 0);
HScrollPos += CenterPointX;
SendMessage(this.scrollpanel.Handle, WM_HSCROLL, SB_THUMBPOSITION + 0x10000 * CenterPointX, 0);
this.scrollpanel.Invalidate();
}