내 앱의 다른 모든 창 위에 WPF 창을 만드는 방법 (시스템 전체가 아님)?
내 응용 프로그램의 다른 모든 창 위에 내 창이 표시되기를 원합니다 . 창의 TopMost 속성을 설정하면 모든 응용 프로그램의 모든 창 위에 표시되므로 원하지 않습니다.
창의 소유자 속성을 설정해야합니다.
메인 창을 차단하기 위해 showdialog를 통해 창을 표시하거나, 소유자를 차단하지 않고 정상적으로 표시하고 소유자 위에 표시 할 수 있습니다.
다음은 코드 숨김 부분의 코드 예제입니다. 모든 명백한 내용을 생략했습니다.
namespace StackoverflowExample
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
void NewWindowAsDialog(object sender, RoutedEventArgs e)
{
Window myOwnedDialog = new Window();
myOwnedDialog.Owner = this;
myOwnedDialog.ShowDialog();
}
void NormalNewWindow(object sender, RoutedEventArgs e)
{
Window myOwnedWindow = new Window();
myOwnedWindow.Owner = this;
myOwnedWindow.Show();
}
}
}
대신 가장 항상 Top이 될 Popup을 사용하여 Window와 비슷하게 꾸미고 Application과 함께 완전히 연결하여 Main Window의 LocationChanged 이벤트를 처리하고 Popup의 IsOpen 속성을 false로 설정할 수 있습니다.
편집하다:
나는 당신이 다음과 같은 것을 원하기를 바랍니다.
Window1 window;
private void Button_Click(object sender, RoutedEventArgs e)
{
window = new Window1();
window.WindowStartupLocation = WindowStartupLocation.CenterScreen;
window.Topmost = true;
this.LocationChanged+=OnLocationchanged;
window.Show();
}
private void OnLocationchanged(object sender, EventArgs e)
{
if(window!=null)
window.Close();
}
도움이 되었기를 바랍니다 !!!
CustomWindow cw = new CustomWindow();
cw.Owner = Application.Current.MainWindow;
cw.ShowInTaskbar = false;
cw.ShowDialog() ;
XAML 에서 간단하게 할 수 있으며 아직 아무도이 답변을 게시하지 않았다는 사실에 놀랐습니다. 다음 예제에서는에 Window
정의되어 있지만 ResourceLibrary
(참고 x:Key
) 독립 실행 형 Page
스타일 WPF 리소스 에서이 XAML 바인딩을 사용할 수도 있습니다 .
<Window x:Key="other_window"
Topmost="{Binding Source={x:Static Application.Current},Path=MainWindow.IsActive,Mode=OneWay}">
<TextBlock Text="OTHER WINDOW" />
</Window>
Activate () 메서드를 사용합니다. 이것은 창을 전경으로 가져와 활성화하려고 시도합니다. 예 : Window wnd = new xyz (); wnd.Activate ();
가장 좋은 방법은이 두 이벤트를 앱의 모든 창에 설정하는 것입니다.
GotKeyboardFocus
LostKeyboardFocus
이런 식으로:
WiondowOfMyApp_GotKeyboardFocus(object sender, System.Windows.Input.KeyboardFocusChangedEventArgs e)
{
windowThatShouldBeTopMost.TopMost = true;
}
WiondowOfMyApp_LostKeyboardFocus(object sender, System.Windows.Input.KeyboardFocusChangedEventArgs e)
{
windowThatShouldBeTopMost.TopMost = false;
}
- 그리고 확실히 당신이 원하는 모든 창은 당신의 앱의 다른 창에서 접근 할 수 있어야합니다. 제 경우에는 기본 창과 기본 창 위에 있어야하는 또 다른 창이 있으므로 기본 창에 서로 다른 창이있는 것이 나쁘지 않았습니다.
나는 당신과 매우 비슷한 상황에 직면했습니다. 내가 찾은 대부분의 검색 은 내가해야 할 일은 내가 Topmost가되기를 원하는 창 소유자를 주 창이나 Show라는 창으로 설정하는 것 뿐이라고 말했습니다.
어쨌든 저에게 잘 맞는 솔루션을 게시하겠습니다.
내 응용 프로그램과 관련하여 Topmost로 예상되는 창에서 Window.Activated 및 Window.Deactived에 대한 이벤트 처리기를 만들었습니다.
private void Window_Activated(object sender, EventArgs e)
{
Topmost = true;
}
private void Window_Deactived(object sender, EventArgs e)
{
if(Owner == null || Owner.IsActive)
return;
bool hasActiveWindow = false;
foreach(Window ownedWindow in Owner.OwnedWindows)
{
if(ownedWindow.IsActive)
hasActiveWindow = true;
}
if(!hasActiveWindow)
Topmost = false;
}
그것은 나를 위해 잘 작동합니다. 바라건대 이것은 다른 누군가에게 유용합니다. :영형)
팝업 창에서 Show () 메서드를 매개 변수로 오버로드합니다.
Public Overloads Sub Show(Caller As Window)
Me.Owner = Caller
MyBase.Show()
End Sub
그런 다음 Main 창에서 오버로드 된 메서드 Show ()를 호출합니다.
Dim Popup As PopupWindow
Popup = New PopupWindow
Popup.Show(Me)
여러 개의 스레드가 있으며 "최상위"태그도 있습니다. 그것에 대해 검색하거나 좋아 보이는이 게시물로 직접 이동하십시오.
내 응용 프로그램에서만 다른 모든 창 위에 창을 유지하는 방법은 무엇입니까?
나는 OP입니다. 몇 가지 조사와 테스트를 거친 후 대답은 다음과 같습니다.
아니, 정확히 할 방법이 없습니다 .
이를 수행하는 방법은 다음과 같습니다. "최상위"창을 다른 창 GotFocus 및 LostFocus 이벤트를 구독하고 다음을 이벤트 처리기로 사용합니다.
class TopMostWindow
{
void OtherWindow_LostFocus(object sender, EventArgs e)
{
this.Topmost = false;
}
void OtherWindow_GotFocus(object sender, EventArgs e)
{
this.Topmost = true;
}
}
이것을 Windows 태그에 추가 할 수 있습니다.
WindowStartupLocation="CenterScreen"
그런 다음 사용자가 계속 진행하기 위해 확인하도록하려면 표시 할 수도 있습니다.
YourWindow.ShowDialog();
먼저 TopMost 매개 변수없이 시도하고 결과를 확인하십시오.
이 시도:
Popup.PlacementTarget = sender as UIElement;
htis는 어떻습니까?
Private Sub ArrangeWindows(Order As Window())
For I As Integer = 1 To Order.Length -1
Order(I).Owner = Order(I - 1)
Next
End Sub
나도 같은 문제에 직면했고이 질문에 구글을 따랐다. 최근에 다음이 저에게 효과적이라는 것을 알았습니다.
CustomWindow cw = new CustomWindow();
cw.Owner = this;
cw.ShowDialog();
I just ran into this same issue. I have a desktop app that has multiple WPF windows, and I needed my custom splash screen to be on top of the other windows in my app only. No other windows are open when my splash screen comes up, but I do open the MainWindow from my splash screen after some authentication. So I just did something similar to what @GlenSlayden did but in code behind since, like I said, the MainWindow isn't up for me to bind to:
private void SplashScreen_ContentRendered(object sender, EventArgs e)
{
// User authentication...
// ...
MainWindow mainWindow = new MainWindow();
SetBinding(SplashScreen.TopmostProperty, new Binding("IsVisible"))
{
Source = mainWindow,
Mode = BindingMode.OneWay,
UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged
};
mainWindow.Show();
}
Now while my program is loading all the other windows from the MainWindow, the splash screen is on top, but while the program is authenticating the user, it's not topmost, so you can click away on some other program and it will hide behind it. It's the closest thing I could find to a solution for this problem. It's not perfect because it still goes over top of all other applications while my program is loading after authentication, but that's not for very long in my case.
Just learning C# and ran across similar situation. but found a solution that I think may help. You may have figured this a long time ago. this will be from starting a new project but you can use it in any.
1) Start new project.
2) go to Project, then New Windows form, then select Windows Form and name Splash.
3) set size, background, text, etc as desired.
4) Under Properties of the Splash.cs form set Start Position: CenterScreen and TopMost: true
5) form1 add "using System.Threading;"
6) form1 under class add "Splash splashscreen = new Splash();"
7) form1 add "splashscreen.Show();" and "Application.DoEvents();"
8) form1 Under Events>>Focus>>Activated add "Thread.Sleep(4000); splashscreen.Close();"
9) Splash.cs add under "Public Splash" add "this.BackColor = Color.Aqua;" /can use any color
10) This is the code for Form1.cs
public partial class Form1 : Form
{
Splash splashscreen = new Splash();
public Form1()
{
InitializeComponent();
splashscreen.Show();
Application.DoEvents();
}
private void Form1_Activated(object sender, EventArgs e)
{
Thread.Sleep(4000);
splashscreen.Close();
}
}
11) this is the code on Splash.cs
public partial class Splash : Form
{
public Splash()
{
InitializeComponent();
this.BackColor = Color.Aqua;
}
}
12) I found that if you do NOT do something in the splash then the screen will not stay on the top for the time the first form needs to activate. The Thread count will disappear the splash after x seconds, so your program is normal.
I just ran into the same problem and found trouble setting the owner using MVVM without causing the app to crash in production. I have a Window Manager View Model that includes a command to open a window using the uri of the window - and I wasn't able to set the owner to App.MainWindow without the app crashing.
So - Instead of setting the owner, I bound the TopMost property of the window to a property in my Window Manager View model which indicates whether the application is currently active. If the application is active, the window is on top as I would like. If it is not active, other windows can cover it.
Here is what I added to my View Model:
public class WindowManagerVM : GalaSoft.MvvmLight.ViewModelBase
{
public WindowManagerVM()
{
App.Current.Activated += (s, e) => IsAppActive = true;
App.Current.Deactivated += (s, e) => IsAppActive = false;
}
private bool _isAppActive = true;
public bool IsAppActive
{
get => _isAppActive;
set
{
if (_isAppActive != value)
{
_isAppActive = value;
RaisePropertyChanged(() => IsAppActive);
}
}
}
}
Here is the XAML that implements it (I use MVVM light with a ViewModelLocator as a static resource in my app called Locator):
<Window Topmost="{Binding WindowManager.IsAppActive, Source={StaticResource Locator}}"/>
ReferenceURL : https://stackoverflow.com/questions/2546566/how-to-make-a-wpf-window-be-on-top-of-all-other-windows-of-my-app-not-system-wi
'programing tip' 카테고리의 다른 글
localhost 용 스니퍼 (Windows OS) (0) | 2020.12.25 |
---|---|
공유 메모리 대 메시지 전달은 대용량 데이터 구조를 어떻게 처리합니까? (0) | 2020.12.25 |
명령 줄에서 Javadoc을 생성하는 방법 (0) | 2020.12.25 |
호버시 연속 CSS 회전 애니메이션, 호버 아웃시 0deg로 다시 애니메이션 (0) | 2020.12.25 |
rabbitmq에서 풀링 연결 또는 채널 사이에 성능 차이가 있습니까? (0) | 2020.12.25 |