zoukankan      html  css  js  c++  java
  • 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>
    

      

  • 相关阅读:
    python之路---23 模块 os sys pickle json
    Fiddler抓包【7】_次要功能和第三方插件
    Fiddler抓包【6】_Fiddler Script
    Fiddler抓包【5】_Fiddler过滤
    Fiddler抓包【4】_重定向AutoResponder
    Fiddler抓包【3】_设置断点修改
    Fiddler抓包【2】_捕获设置
    Fiddler抓包【1】_介绍及界面概述
    CSS常见兼容问题以及解决办法
    webpack入门文档教程
  • 原文地址:https://www.cnblogs.com/wgscd/p/10518066.html
Copyright © 2011-2022 走看看