トップ回答者
WPFアプリケーション起動時にウィンドウをアクティブにしたくない

質問
回答
-
他にも方法があるかもしれませんが、OnSourceInitialized のタイミングで WS_EX_NOACTIVATE を付けて、Window_Loaded のタイミングで WS_EX_NOACTIVATE を解除するのはどうでしょうか?
Imports System.Windows.Interop Imports System.Runtime.InteropServices Class MainWindow <DllImport("user32.dll", EntryPoint:="GetWindowLong")> Private Shared Function GetWindowLong(ByVal hWnd As IntPtr, ByVal nIndex As Integer) As Integer End Function <DllImport("user32.dll", EntryPoint:="SetWindowLong")> Private Shared Function SetWindowLong(ByVal hWnd As IntPtr, ByVal nIndex As Integer, ByVal dwNewLong As Integer) As Integer End Function Private Const GWL_EXSTYLE As Integer = -20 Private Const WS_EX_NOACTIVATE As Integer = &H8000000 Protected Overrides Sub OnSourceInitialized(ByVal e As System.EventArgs) MyBase.OnSourceInitialized(e) Dim helper As New WindowInteropHelper(Me) Dim nStyle As Integer = GetWindowLong(helper.Handle, GWL_EXSTYLE) Call SetWindowLong(helper.Handle, GWL_EXSTYLE, nStyle Or WS_EX_NOACTIVATE) End Sub Private Sub Window_Loaded(sender As Object, e As RoutedEventArgs) Dim source As HwndSource = CType(HwndSource.FromVisual(Me), HwndSource) Dim nStyle As Integer = GetWindowLong(source.Handle, GWL_EXSTYLE) Call SetWindowLong(source.Handle, GWL_EXSTYLE, nStyle And Not WS_EX_NOACTIVATE) End Sub End Class
追伸:
上記のようなコードを書かなくても、Window のプロパティで、「ShowActivate」のチェックを外すと起動時にウィンドウをアクティブにしないようにできるみたいですね。- 編集済み kenjinoteMVP 2017年9月16日 6:20
- 回答としてマーク VB User1 2017年9月16日 7:00
すべての返信
-
他にも方法があるかもしれませんが、OnSourceInitialized のタイミングで WS_EX_NOACTIVATE を付けて、Window_Loaded のタイミングで WS_EX_NOACTIVATE を解除するのはどうでしょうか?
Imports System.Windows.Interop Imports System.Runtime.InteropServices Class MainWindow <DllImport("user32.dll", EntryPoint:="GetWindowLong")> Private Shared Function GetWindowLong(ByVal hWnd As IntPtr, ByVal nIndex As Integer) As Integer End Function <DllImport("user32.dll", EntryPoint:="SetWindowLong")> Private Shared Function SetWindowLong(ByVal hWnd As IntPtr, ByVal nIndex As Integer, ByVal dwNewLong As Integer) As Integer End Function Private Const GWL_EXSTYLE As Integer = -20 Private Const WS_EX_NOACTIVATE As Integer = &H8000000 Protected Overrides Sub OnSourceInitialized(ByVal e As System.EventArgs) MyBase.OnSourceInitialized(e) Dim helper As New WindowInteropHelper(Me) Dim nStyle As Integer = GetWindowLong(helper.Handle, GWL_EXSTYLE) Call SetWindowLong(helper.Handle, GWL_EXSTYLE, nStyle Or WS_EX_NOACTIVATE) End Sub Private Sub Window_Loaded(sender As Object, e As RoutedEventArgs) Dim source As HwndSource = CType(HwndSource.FromVisual(Me), HwndSource) Dim nStyle As Integer = GetWindowLong(source.Handle, GWL_EXSTYLE) Call SetWindowLong(source.Handle, GWL_EXSTYLE, nStyle And Not WS_EX_NOACTIVATE) End Sub End Class
追伸:
上記のようなコードを書かなくても、Window のプロパティで、「ShowActivate」のチェックを外すと起動時にウィンドウをアクティブにしないようにできるみたいですね。- 編集済み kenjinoteMVP 2017年9月16日 6:20
- 回答としてマーク VB User1 2017年9月16日 7:00