admin管理员组

文章数量:1389772

what i been trying to do now for a couple of days is create a cool extruding image from my app in WPF however was not able to get the floating image to dissaper if I open a app or move a app over my app. i not the best at explaining it but this is what I mean its easier to show it.

but when I first load it up it will show properly,

so how I acchived this is by creating a floating transpatant window then called it from the processing window with anchoring width and height

the issue I have is when I set this up I set it up so if a app goes over the screen it will get rid of the image so it does not appear infrount of the app was the issue I was having

so what I did was create the following.

using System;

using System.Windows; using System.Windows.Input;

namespace Dark_Admin_Panel.View { public partial class FloatingImageWindow : Window { private readonly ProcessingWindow _parentWindow; private bool _isDragging = false;

    public FloatingImageWindow(ProcessingWindow parentWindow)
    {
        InitializeComponent();
        _parentWindow = parentWindow;
        PositionWindow(); // Align on startup

        // Update position when ProcessingWindow moves or resizes
        _parentWindow.LocationChanged += ParentWindow_LocationChanged;
        _parentWindow.SizeChanged += ParentWindow_LocationChanged;
    }

    private void PositionWindow()
    {
        this.UpdateLayout(); // Ensure layout updates correctly

        // Move the floating window to be exactly at the bottom-right corner of ProcessingWindow
        this.Left = _parentWindow.Left + _parentWindow.Width - this.Width + 40; // Small right offset
        this.Top = _parentWindow.Top + _parentWindow.Height - this.Height; // Align to bottom
    }

    private void ParentWindow_LocationChanged(object sender, EventArgs e)
    {

       ////PositionWindow(); // Adjust position if parent moves
        
    }

    private void FloatingWindow_MouseDown(object sender, MouseButtonEventArgs e)
    {
        if (e.LeftButton == MouseButtonState.Pressed)
        {
            this.DragMove();
            _isDragging = true;
        }
    }

    private void FloatingWindow_MouseUp(object sender, MouseButtonEventArgs e)
    {
        _isDragging = false;
    }
}

}

then in the ProcessingWindow

        private FloatingImageWindow _floatingWindow;
    private System.Windows.Threading.DispatcherTimer _visibilityTimer;
    private bool _isFloatingWindowVisible = true; // Track current visibility state

    // Windows API for detecting focus
    private const int WM_ACTIVATEAPP = 0x001C;
    [DllImport("user32.dll")]
    private static extern IntPtr GetForegroundWindow();

    [DllImport("user32.dll")]
    private static extern bool IsWindowVisible(IntPtr hWnd);

    // Import Windows API

    [DllImport("user32.dll")]
    private static extern IntPtr GetActiveWindow();

    [DllImport("user32.dll")]
    private static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect);


    [StructLayout(LayoutKind.Sequential)]
    private struct RECT
    {
        public int Left;
        public int Top;
        public int Right;
        public int Bottom;
    }
        private void ProcessingWindow_Loaded(object sender, RoutedEventArgs e)
    {
        ShowFloatingWindow();
        HookWindowFocusEvents();
        CheckIfButtonShouldBeRemoved();
    }

        private void HookWindowFocusEvents()
    {
        IntPtr hwnd = new WindowInteropHelper(this).Handle;
        HwndSource source = HwndSource.FromHwnd(hwnd);
        if (source != null)
        {
            source.AddHook(WndProc);
        }
    }

        private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
    {
        if (msg == WM_ACTIVATEAPP)
        {
            bool isActivated = wParam.ToInt32() != 0;

            if (isActivated)
            {
                ShowFloatingWindowSmoothly();
            }
            else
            {
                HideFloatingWindowSmoothly();
            }
        }
        return IntPtr.Zero;
    }

    private void ShowFloatingWindowSmoothly()
    {
        if (!_isFloatingWindowVisible)
        {
            _floatingWindow?.Show();
            _isFloatingWindowVisible = true;
        }
    }
    private void HideFloatingWindowSmoothly()
    {
        if (_isFloatingWindowVisible)
        {
            _floatingWindow?.Hide();
            _isFloatingWindowVisible = false;
        }
    }

    private void ShowFloatingWindow()
    {
        if (_floatingWindow == null)
        {
            _floatingWindow = new FloatingImageWindow(this)
            {
                WindowStartupLocation = WindowStartupLocation.Manual,
                AllowsTransparency = true,
                ShowInTaskbar = false // Prevents showing separately in the taskbar
                
            };

            _floatingWindow.Show();
        }
    }

    private void CloseButton_Click(object sender, RoutedEventArgs e)
    {
        _floatingWindow?.Close(); // Close Image Before closing process window. 
        _floatingWindow = null;
        Close();
    }

so basically the issue I am having is this does work for the most part but I have 3 monitors and if I click a app on say second or third monitor the image will dissapier from the process window as well I only want the image to dissapier if a app overlays the program but for the life of me cant get this to work.

and if anyone knows a simple way of getting a image to go outside the box of a window like in image 2 that is better then please do let me know because I searched all over so had to come up with this method its somewhat if a hacky one I understand.

thank you in advance elfenliedtopfan5.

本文标签: cFloating Image when another app goes over the topStack Overflow