애플리케이션 중앙에 표시할 대화 상자 위치를 설정하는 방법은 무엇입니까?
대화 상자의 위치를 설정하는 방법.ShowDialog();
주 창 중앙에 표시하시겠습니까?
이것이 제가 포지션을 설정하는 방법입니다.
private void Window_Loaded(object sender, RoutedEventArgs e)
{
PresentationSource source = PresentationSource.FromVisual(this);
if (source != null)
{
Left = ??
Top = ??
}
}
Dialog(대화 상자)에 속한 XAML에서 다음을 수행합니다.
<Window ... WindowStartupLocation="CenterOwner">
C#에서 Dialog를 인스턴스화할 경우:
MyDlg dlg = new MyDlg();
dlg.Owner = this;
if (dlg.ShowDialog() == true)
{
...
나는 xaml 마크업을 사용하는 것이 더 쉽다고 생각합니다.
<Window WindowStartupLocation="CenterOwner">
다음과 같이 로드된 이벤트에서 메인 창을 잡으려고 시도할 수 있습니다.
private void Window_Loaded(object sender, RoutedEventArgs e)
{
Application curApp = Application.Current;
Window mainWindow = curApp.MainWindow;
this.Left = mainWindow.Left + (mainWindow.Width - this.ActualWidth) / 2;
this.Top = mainWindow.Top + (mainWindow.Height - this.ActualHeight) / 2;
}
뒤에 코드가 있습니다.
public partial class CenteredWindow:Window
{
public CenteredWindow()
{
InitializeComponent();
WindowStartupLocation = WindowStartupLocation.CenterOwner;
Owner = Application.Current.MainWindow;
}
}
저는 이 질문에 대한 모든 사람들의 대답이 답이 되어야 하는 부분이라고 생각합니다.저는 이 문제에 대한 가장 쉽고 우아한 접근법이라고 생각하는 것들을 간단히 정리할 것입니다.
창을 배치할 첫 번째 설정입니다.여기 주인이 있습니다.
<Window WindowStartupLocation="CenterOwner">
창을 열기 전에 소유자를 지정해야 하며 다른 게시물에서 현재 응용 프로그램의 주 창에 대한 정적 게터를 사용하여 주 창에 액세스할 수 있습니다.
Window window = new Window();
window.Owner = Application.Current.MainWindow;
window.Show();
바로 그겁니다.
나는 이것이 최고라고 생각합니다.
frmSample fs = new frmSample();
fs.Owner = this; // <-----
fs.WindowStartupLocation = WindowStartupLocation.CenterOwner;
var result = fs.ShowDialog();
표시해야 할 창을 거의 제어할 수 없는 경우 다음 스니펫이 유용할 수 있습니다.
public void ShowDialog(Window window)
{
Dispatcher.BeginInvoke(
new Func<bool?>(() =>
{
window.Owner = Application.Current.MainWindow;
window.WindowStartupLocation = WindowStartupLocation.CenterOwner;
return window.ShowDialog();
}));
}
WPF 대화 상자를 Windows Forms 상위 양식의 중앙에 배치하기 위해 Application 이후 대화 상자에 상위 양식을 전달했습니다.Current가 Windows Form 부모를 반환하지 않았습니다(부모 앱이 WPF인 경우에만 작동하는 것으로 가정합니다).
public partial class DialogView : Window
{
private readonly System.Windows.Forms.Form _parent;
public DialogView(System.Windows.Forms.Form parent)
{
InitializeComponent();
_parent = parent;
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
this.Left = _parent.Left + (_parent.Width - this.ActualWidth) / 2;
this.Top = _parent.Top + (_parent.Height - this.ActualHeight) / 2;
}
}
WPF 대화 상자에서 Window StartupLocation을 설정합니다.
<Window WindowStartupLocation="CenterParent">
Windows Form에서 WPF 대화 상자를 로드하는 방법은 다음과 같습니다.
DialogView dlg = new DialogView();
dlg.Owner = this;
if (dlg.ShowDialog() == true)
{
...
상위 창을 창(Owner)으로 설정한 다음 WindowStartupLocation 속성을 "CenterParent"로 설정해야 합니다.
Fredrik Hedblad의 답변에 메인 윈도우의 크기를 조정하거나 최대화했다면 결과가 잘못된 것입니다. 메인 윈도우이기 때문입니다.너비 및 주 창입니다.높이는 XAML에 설정된 값을 반영합니다.
실제 값을 원하는 경우 주 창을 사용할 수 있습니다.실제 너비 및 주 창입니다.실제 높이:
private void Window_Loaded(object sender, RoutedEventArgs e)
{
Application curApp = Application.Current;
Window mainWindow = curApp.MainWindow;
this.Left = mainWindow.Left + (mainWindow.ActualWidth - this.ActualWidth) / 2;
this.Top = mainWindow.Top + (mainWindow.ActualHeight - this.ActualHeight) / 2;
}
문서화를 위해, 저는 제가 어떻게 비슷한 것을 성취했는지에 대한 예를 여기에 추가할 것입니다.제가 필요로 하는 것은 상위 창 콘텐츠 영역 전체(제목 표시줄 제외)를 포함하는 팝업이었지만 대화 상자의 중심을 맞추고 내용을 늘리면 대화 상자가 항상 아래에서 약간 오프셋되어 있기 때문에 작동하지 않았습니다.
사용자 환경에 대한 참고:테두리 없는 대화 상자가 표시될 때 부모 창을 끌거나 닫을 수 없는 것은 좋지 않으므로 다시 사용하는 것을 고려해 보겠습니다.저도 이 답변을 올린 후에 이를 하지 않기로 결정했고, 다른 사람들이 볼 수 있도록 남겨둘 것입니다.
몇 번의 검색과 테스트 끝에, 저는 마침내 이렇게 할 수 있었습니다.
var dialog = new DialogWindow
{
//this = MainWindow
Owner = this
};
dialog.WindowStartupLocation = WindowStartupLocation.Manual;
dialog.WindowStyle = WindowStyle.None;
dialog.ShowInTaskbar = false;
dialog.ResizeMode = ResizeMode.NoResize;
dialog.AllowsTransparency = true;
var ownerContent = (FrameworkElement) Content;
dialog.MaxWidth = ownerContent.ActualWidth;
dialog.Width = ownerContent.ActualWidth;
dialog.MaxHeight = ownerContent.ActualHeight;
dialog.Height = ownerContent.ActualHeight;
var contentPoints = ownerContent.PointToScreen(new Point(0, 0));
dialog.Left = contentPoints.X;
dialog.Top = contentPoints.Y;
dialog.ShowDialog();
그DialogWindow
는 Window이며 소유자는 기본 응용 프로그램 Window로 설정되어 있습니다. 그WindowStartupLocation
로 설정해야 합니다.Manual
수동 위치 설정이 가능합니다.
결과:
더 쉬운 방법이 있을지는 모르겠지만, 다른 방법은 저에게 효과가 없을 것 같았습니다.
XAML:
<Window WindowStartupLocation="CenterScreen">
이 코드는 xaml에서 WindowStartupLocation 속성을 사용하지 않으려는 경우에 작동합니다.
private void CenterWindowOnApplication()
{
System.Windows.Application curApp = System.Windows.Application.Current;
Window mainWindow = curApp.MainWindow;
if (mainWindow.WindowState == WindowState.Maximized)
{
// Get the mainWindow's screen:
var screen = System.Windows.Forms.Screen.FromRectangle(new System.Drawing.Rectangle((int)mainWindow.Left, (int)mainWindow.Top, (int)mainWindow.Width, (int)mainWindow.Height));
double screenWidth = screen.WorkingArea.Width;
double screenHeight = screen.WorkingArea.Height;
double popupwindowWidth = this.Width;
double popupwindowHeight = this.Height;
this.Left = (screenWidth / 2) - (popupwindowWidth / 2);
this.Top = (screenHeight / 2) - (popupwindowHeight / 2);
}
else
{
this.Left = mainWindow.Left + ((mainWindow.ActualWidth - this.ActualWidth) / 2;
this.Top = mainWindow.Top + ((mainWindow.ActualHeight - this.ActualHeight) / 2);
}
}
저는 "스크린"을 사용하고 있습니다.작업 표시줄이 기본 창을 더 작게 만들기 때문에 "작업 영역"입니다.창을 화면 중앙에 배치하려면 "화면"을 사용하면 됩니다.대신 "한계".
하위 창의 경우 XAML로 설정합니다.
WindowStartupLocation="CenterOwner"
자녀 창을 부모의 대화상자 및 중심으로 호출하려면 부모 창에서 호출합니다. 예:
private void ConfigButton_OnClick(object sender, RoutedEventArgs e)
{
var window = new ConfigurationWindow
{
Owner = this
};
window.ShowDialog();
}
언급URL : https://stackoverflow.com/questions/4306593/how-to-set-a-dialog-position-to-show-at-the-center-of-the-application
'programing' 카테고리의 다른 글
월 번호에서 월 이름 가져오기 (0) | 2023.05.05 |
---|---|
Azure 함수 - appsettings.json 사용 (0) | 2023.05.05 |
원격 태그를 삭제하려면 어떻게 해야 합니까? (0) | 2023.05.05 |
Angular - ng: 명령을 찾을 수 없습니다. (0) | 2023.05.05 |
복수의 값을 한 번에 포스트그레스 테이블에 삽입하려면 어떻게 해야 합니까? (0) | 2023.05.05 |