Show form(Winform) after hiding window(WPF) that will cause mouse cannot move normally

cooper29 0 Reputation points
2023-08-03T02:46:38.8133333+00:00

I expect to show my Form2 when dragging any of window on the desktop after click 'Hide this window', but the dragging window always be hanging when the form was showing, I wish to be able to drag the window and show form smoothly at meanwhile.

User's image

Here are my steps:

1.Click 'Hook' button to call InitHooks().

2.Click 'Hide this window' to call ButtonHide_Click().

3.Dragging window and show form at same time.

public void InitHooks()
        {
            #region Mouse
            m_hhook1 = IntPtr.Zero;
            m_hhook2 = IntPtr.Zero;
            m_hhook3 = IntPtr.Zero;
            dele = new WinEventDelegate(WinEventProc);
            m_hhook1 = SetWinEventHook(EVENT_SYSTEM_FOREGROUND, EVENT_SYSTEM_FOREGROUND, IntPtr.Zero, dele, 0, 0, WINEVENT_OUTOFCONTEXT | WINEVENT_SKIPOWNPROCESS);
            m_hhook2 = SetWinEventHook(EVENT_SYSTEM_MOVESIZESTART, EVENT_SYSTEM_MOVESIZESTART, IntPtr.Zero, dele, 0, 0, WINEVENT_OUTOFCONTEXT | WINEVENT_SKIPOWNPROCESS);
            m_hhook3 = SetWinEventHook(EVENT_SYSTEM_MOVESIZEEND, EVENT_SYSTEM_MOVESIZEEND, IntPtr.Zero, dele, 0, 0, WINEVENT_OUTOFCONTEXT | WINEVENT_SKIPOWNPROCESS);
            #endregion
        }
public static void WinEventProc(IntPtr hWinEventHook, uint eventType, IntPtr hwnd, int idObject, int idChild, uint dwEventThread, uint dwmsEventTime)
        {

            switch (eventType)
            {
                case 0x0003: //EVENT_SYSTEM_FOREGROUND:
                    break;
                case 0x000A: //EVENT_SYSTEM_MOVESIZESTART:
                    mLOCATIONCHANGE = SetWinEventHook(EVENT_OBJECT_LOCATIONCHANGE, EVENT_OBJECT_LOCATIONCHANGE, IntPtr.Zero, dele, 0, 0, WINEVENT_OUTOFCONTEXT);
                    break;
                case 0x000B: //EVENT_SYSTEM_MOVESIZEEND:
                    if (UnhookWinEvent(mLOCATIONCHANGE))
                        mLOCATIONCHANGE = IntPtr.Zero;
                    _form2.Hide();
                    break;
                case 0x800B: //EVENT_OBJECT_LOCATIONCHANGE
                    _form2.Show();
                    break;

                default:
                    break;
            }
        }

Form2 settings:

public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
            SetFormSize();
            Button button = new Button();
            panel1.BackColor = Color.AliceBlue;
            panel1.Controls.Add(button);
        }

        private void SetFormSize()
        {
            Screen[] screen = Screen.AllScreens;
            this.Width = screen[1].WorkingArea.Width;
            this.Height = screen[1].WorkingArea.Height;
            this.Location = new Point(screen[1].WorkingArea.X, screen[1].WorkingArea.Y);
        }
    }
private void InitializeComponent()
        {
            this.panel1 = new System.Windows.Forms.Panel();
            this.SuspendLayout();
            // 
            // panel1
            // 
            this.panel1.Location = new System.Drawing.Point(46, 61);
            this.panel1.Name = "panel1";
            this.panel1.Size = new System.Drawing.Size(318, 274);
            this.panel1.TabIndex = 0;
            // 
            // Form2
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(9F, 18F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(800, 450);
            this.Controls.Add(this.panel1);
            this.Name = "Form2";
            this.Text = "Form2";
            this.TopMost = false;
            this.AllowDrop = true;
            this.ResumeLayout(false);
        }

MainWindow settings:

<Window x:Class="WpfApp1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:ui="http://schemas.modernwpf.com/2019" 
        xmlns:wpfapp1="clr-namespace:WpfApp1"
        ui:WindowHelper.UseModernWindowStyle="True"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <wpfapp1:UserControl1 Width="50" Height="50" Margin="725,30,25,355"/>
        <Button Content="Hook" HorizontalAlignment="Left" Margin="195,180,0,0" VerticalAlignment="Top" Click="ButtonHook_Click"/>
        <Button Content="UnHook" HorizontalAlignment="Left" Margin="195,226,0,0" VerticalAlignment="Top" Click="ButtonUnHook_Click"/>
        <Button Content="Hide this window" HorizontalAlignment="Left" Margin="195,275,0,0" VerticalAlignment="Top" Click="ButtonHide_Click"/>

    </Grid>
</Window>
public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            this.Deactivated += MainWindow_Deactivated;
        }

        private void MainWindow_Deactivated(object? sender, EventArgs e)
        {
            Debug.WriteLine("MainWindow_Deactivated");
        }

        private void ButtonHook_Click(object sender, RoutedEventArgs e)
        {
            Class1.InitHook();
        }
        private void ButtonUnHook_Click(object sender, RoutedEventArgs e)
        {
            Class1.UnHook();
        }

        private void btnUnknow_Click(object sender, RoutedEventArgs e)
        {
            
        }

        private void ButtonHide_Click(object sender, RoutedEventArgs e)
        {
            this.Hide();
        }
    }

Thanks.

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,804 questions
Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,635 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
9,978 questions
{count} votes