建议用: int left = this.splitContainer1.PointToScreen(new Point(this.splitContainer1.Panel2.Left, this.splitContainer1.Panel2.Top)).X;也可以用(这种方法可能有些误差,因为控件边界宽度问题): int left = 0; Control parentControl = this.splitContainer1.Panel2; while (parentControl != null) { left += parentControl.Left; parentControl = parentControl.Parent; }
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace WindowsApplication22
{
public partial class Form1 : Form
{
Point P1, P2; public Form1()
{
InitializeComponent();
} private void splitContainer1_Panel1_Resize(object sender, EventArgs e)
{
P1 = splitContainer1.Panel1.PointToScreen(new Point(0, 0));
this.Text = "panel1 - (x:" + P1.X + ",y:" + P1.Y + ") panel2 - (x:" + P2.X + ",y:" + P2.Y+")";
} private void splitContainer1_Panel2_Resize(object sender, EventArgs e)
{
P2 = splitContainer1.Panel2.PointToScreen(new Point(0, 0));
this.Text = "panel1 - (x:" + P1.X + ",y:" + P1.Y + ") panel2 - (x:" + P2.X + ",y:" + P2.Y + ")";
} private void Form1_Move(object sender, EventArgs e)
{
P1 = splitContainer1.Panel1.PointToScreen(new Point(0, 0));
P2 = splitContainer1.Panel2.PointToScreen(new Point(0, 0));
this.Text = "panel1 - (x:" + P1.X + ",y:" + P1.Y + ") panel2 - (x:" + P2.X + ",y:" + P2.Y + ")";
}
}
}
int left = this.splitContainer1.PointToScreen(new Point(this.splitContainer1.Panel2.Left, this.splitContainer1.Panel2.Top)).X;也可以用(这种方法可能有些误差,因为控件边界宽度问题):
int left = 0;
Control parentControl = this.splitContainer1.Panel2;
while (parentControl != null)
{
left += parentControl.Left;
parentControl = parentControl.Parent;
}