C# splitContainer 控件的简单问题

低调的感觉 2008-12-03 04:23:23
我现在把 splitContainer1 控件分成了
1 2 两个区(一行两列)
由于在程序运行的时候
1 2 这两个区是可以随意改变的
我怎么得到 2 这个区左上角相对于显示器左上角的左边
...全文
62 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
net5i 2008-12-03
  • 打赏
  • 举报
回复
建议用:
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;
}
wartim 2008-12-03
  • 打赏
  • 举报
回复

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 + ")";
}
}
}

111,130

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧