Reputation: 4777
I have a WPF application that I have removed the border and default controls by doing the following:
WindowStyle="None" AllowsTransparency="True"
Now I added a MouseDown handler MouseDown="Window_MouseDown" and added the following code to allow me to move my Window around:
private void Window_MouseDown(object sender, MouseButtonEventArgs e)
{
try
{
if (e.ChangedButton == MouseButton.Left)
DragMove();
}
catch (Exception ex) { }
}
But now I have a UserControl that I want to resize to the full size of my Application so to achieve this I created a new Window that is the same size as my MainWindow and placed the UserControl onto it. I create this new Window and set its parent to be my main application like so:
public MyFullScreenWindow()
{
InitializeComponent();
this.Owner = App.Current.MainWindow;
}
I launch this window like so:
MyFullScreenWindow fullScreen = new MyFullScreenWindow();
fullScreenVideo.ShowDialog();
My problem is that I want to still move my whole application around whenever the user clicks and moves this new window. To achieve this I have added an Event to MyFullScreenWindow:
public partial class MyFullScreenWindow: Window
{
static public event EventHandler MouseDownEvent;
public MyFullScreenWindow()
{
InitializeComponent();
this.Owner = App.Current.MainWindow;
}
private void Window_MouseDown(object sender, MouseButtonEventArgs e)
{
if (MouseDownEvent != null)
{
MouseDownEvent(sender, e);
}
}
}
And I handle this event in my MainWindow like so:
MyFullScreenWindow.MouseDownEvent += new EventHandler(MyFullScreenMouseDownHandler);
private void MyFullScreenMouseDownHandler(object sender, EventArgs e)
{
DragMove();
}
But when I click and drag I see that the event is fired off but my whole application does not move like it should. Why is this??
Upvotes: 0
Views: 1834
Reputation: 759
Simply add the following function code only
protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
{
base.OnMouseLeftButtonDown(e);
// Begin dragging the window
this.DragMove();
}
move your window where ever you want.
Upvotes: 1
Reputation: 26298
I don't know the context of your problem, but why would you create a new window, "place" it inside the parent one? You can use controls without creating a new window.
It's possible that you are doing this because you need AllowTransparency=false functionality, while showing it in AllowTransparency=true window. (I had this case when working with DirectShow). If you're doing this for custom window chrome, then don't use AllowTransparency - it decreases performance greatly. It's creating picture of every frame and disables GPU acceleration, after all! Instead, you can adapt to CustomChromeWindow project(found on Internets).
Note that you can enable the main window so that it would process messages from underlying window:
public class WindowSecond : Window
{
public WindowSecond()
{
Owner = Application.Current.MainWindow;
MouseDown += delegate
{
// maybe cache it.
IntPtr handle = new WindowInteropHelper(Owner).Handle;
EnableWindow(handle, true);
Application.Current.MainWindow.DragMove();
EnableWindow(handle, false);
};
}
[DllImport("user32")]
internal static extern bool EnableWindow(IntPtr hwnd, bool bEnable);
}
Upvotes: 0