WPFで常時非アクティブになるウィンドウを作成したいです。
調べたところWindowsAPIで、ウィンドウスタイルを設定する方法があることが分かりました。
試してみましたが、呼び出し元のWindowも同じスタイル(常時非アクティブ)にしないと
呼び出し先のウィンドウがアクティブになってしまう状態です。
呼び出し先のウィンドウだけ常時非アクティブにすることは可能でしょうか?
以下のサンプルだと、MainWindowがアクティブにできるウィンドウで、NonActiveWindowが
スタイルを設定して常時非アクティブにしている状態です。
※説明に不足があった為補足です。
例えば、ブラウザなどを起動していてそちらがアクティブの状態であれば、
NonActiveWindowをクリックしても非アクティブ状態のままですが、
MainWindowがアクティブの状態でNonActiveWindowをクリックすると
NonActiveWindowがアクティブになってしまうので、それを防ぎたいです。
VB.net
1Class MainWindow 2 Private Sub Button_Click(sender As Object, e As RoutedEventArgs) 3 Dim w As New NonActiveWindow 4 w.Show() ’ここで表示したウィンドウだけ常時非アクティブにしたい。 5 End Sub 6End Class
WPF
1<Window x:Class="MainWindow" 2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 4 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 5 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 6 xmlns:local="clr-namespace:WpfApp8" 7 mc:Ignorable="d" 8 Title="MainWindow" Height="450" Width="800" ShowActivated="False" Topmost="True"> 9 <Grid> 10 <Button Click="Button_Click"/> 11 </Grid> 12</Window>
VB.net
1Imports System.Runtime.InteropServices 2 3Public Class NonActiveWindow 4 <DllImport("user32.dll")> 5 Private Shared Function GetWindowLong(hWnd As IntPtr, index As Integer) As IntPtr 6 End Function 7 <DllImport("user32.dll")> 8 Private Shared Function SetWindowLong(hWnd As IntPtr, index As Integer, newValue As IntPtr) As IntPtr 9 End Function 10 11 Private Const GWL_EXSTYLE = -20 12 Private Enum WS_EX As Long 13 TOOLWINDOW = &H80 14 NOACTIVATE = &H8000000 15 End Enum 16 17 Private Sub NonActiveWindow_Loaded(sender As Object, e As RoutedEventArgs) Handles Me.Loaded 18 Dim helper = New System.Windows.Interop.WindowInteropHelper(Me) 19 Dim oldStyle = GetWindowLong(helper.Handle, GWL_EXSTYLE) 20 Dim newStyle = New IntPtr(oldStyle.ToInt64() Or WS_EX.NOACTIVATE Or WS_EX.TOOLWINDOW) 21 SetWindowLong(helper.Handle, GWL_EXSTYLE, newStyle) 22 End Sub 23End Class
WPF
1<Window x:Class="NonActiveWindow" 2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 4 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 5 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 6 xmlns:local="clr-namespace:WpfApp8" 7 mc:Ignorable="d" 8 Title="NonActiveWindow" Height="450" Width="800" ShowActivated="False"> 9 <Grid> 10 </Grid> 11</Window> 12
回答1件
あなたの回答
tips
プレビュー