• c# WPF 设置窗口一直在其中窗口后面/底层窗口


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Shapes;
    using System.Runtime.InteropServices;
    using System.Windows.Interop;
    
    namespace MMDesktop
    {
        /// <summary>
        /// Interaction logic for Window1.xaml
        /// </summary>
        public partial class Window1 : Window
        {
    
            const UInt32 SWP_NOSIZE = 0x0001;
            const UInt32 SWP_NOMOVE = 0x0002;
            const UInt32 SWP_NOACTIVATE = 0x0010;
            const UInt32 SWP_NOZORDER = 0x0004;
            const int WM_ACTIVATEAPP = 0x001C;
            const int WM_ACTIVATE = 0x0006;
            const int WM_SETFOCUS = 0x0007;
            static readonly IntPtr HWND_BOTTOM = new IntPtr(1);
            const int WM_WINDOWPOSCHANGING = 0x0046;
    
            [DllImport("user32.dll")]
            static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X,
               int Y, int cx, int cy, uint uFlags);
            [DllImport("user32.dll")]
            static extern IntPtr DeferWindowPos(IntPtr hWinPosInfo, IntPtr hWnd,
               IntPtr hWndInsertAfter, int x, int y, int cx, int cy, uint uFlags);
            [DllImport("user32.dll")]
            static extern IntPtr BeginDeferWindowPos(int nNumWindows);
            [DllImport("user32.dll")]
            static extern bool EndDeferWindowPos(IntPtr hWinPosInfo);
    
    
    
            public Window1()
            {
                InitializeComponent();
            }
    
            private void Window_Loaded(object sender, RoutedEventArgs e)
            {
                IntPtr hWnd = new WindowInteropHelper(this).Handle;
                SetWindowPos(hWnd, HWND_BOTTOM, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE);
    
                IntPtr windowHandle = (new WindowInteropHelper(this)).Handle;
                HwndSource src = HwndSource.FromHwnd(windowHandle);
                src.AddHook(new HwndSourceHook(WndProc));
            }
    
            private IntPtr WndProc(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
            {
                if (msg == WM_SETFOCUS)
                {
                    IntPtr hWnd2 = new WindowInteropHelper(this).Handle;
                    SetWindowPos(hWnd2, HWND_BOTTOM, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE);
                    handled = true;
                }
                return IntPtr.Zero;
            }
    
            private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
            {
                IntPtr windowHandle = (new WindowInteropHelper(this)).Handle;
                HwndSource src = HwndSource.FromHwnd(windowHandle);
                src.RemoveHook(new HwndSourceHook(this.WndProc));
            }
    
        }
    }
    

      

    UI:

    <Window x:Class="MMDesktop.Window1"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="Window1" Height="300" Width="300"
            Closing="Window_Closing" Loaded="Window_Loaded"
            >
        <Grid></Grid>
    </Window>
    

      

  • 相关阅读:
    简单的jquery左侧导航栏和页面选中
    SQL 无限级分类语句
    创建第一个MVC专案--初识MVC
    powershell读写磁盘变量(对象序列化)
    powershell字符界面的,powershell加WPF界面的,2048游戏
    Invoke-WebRequest Invoke-RestMethod 乱码研究
    powershell中的两只爬虫
    powershell脚本闪电输入神器
    请把不听话的【return】关进【class】这个笼子
    当powershell遇上mysql引发的血案
  • 原文地址:https://www.cnblogs.com/wgscd/p/10518066.html
Copyright © 2020-2023  润新知