두 가지 사용자 컨트롤이 있습니다.
편의상 검은 색은 부모이고 빨간색은 자식입니다.
다음 그림과 같이 부모의 창 위치를 가져와 자식 창을 배치하려고합니다.
나는 많은 검색을하고 위치를 테스트했지만 부모의 위치는 null이었습니다.
빨간색과 같은 자식 창을 배치하고 싶습니다.
다음은 내 코드의 일부입니다.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace Copsys.Comm
{
public partial class dlgNoticeInputControl : Window
{
public dlgNoticeInputControl()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
CommControl com = new CommControl();
//not parent location data...
double parentWidth = com.Width; //parent(MainForm) Width
double parentHeight = com.Height; //parent(MainForm) Height
double childWidth = this.Width; //child(Form1) Width
double childHeight = this.Height; //child(Form1) Height
Point positions = this.PointToScreen(new Point(parentWidth - childWidth - 10, parentHeight - childHeight - 10));
}
}
}
내가 작성한 코드보다 더 좋은 방법이 있다면 가르쳐주세요.
수동으로 찾는 방법을 고려 중입니다.
수동으로 위치 값을 조회합니다.
위치 값을 직접 제공하여 창을 이동해보십시오.
현재 코드는 버튼의 x 및 y 축을 계산하고 +-를 제공하여 위치 값을 고정합니다.
private void button1_Click(object sender, RoutedEventArgs e)
{
var dlg = new dlgNoticeInputControl();
Console.WriteLine(((Button)sender).Margin);
//MessageBox.Show(""+((Button)sender).PointToScreen(new Point(0, 0)));
dlg.Left = ((Button)sender).PointToScreen(new Point(0, 0)).X - 580;
dlg.Top = ((Button)sender).PointToScreen(new Point(0, 0)).Y - 500;
dlg.Show();
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다