카테고리 없음

[WPF] 다른 프로그램 클릭 시 Popup 창이 클릭 되지 않는 문제

Chanhongv 2024. 8. 5. 13:10

WPF 에서 Popup 을 StayOpen=True 로 띄운 후

다른 프로그램을 사용하다가 (외부 프로그램)

다시 Popup 창을 클릭하였을때 Poup 안의 TextBox 라던지 다른 컨트롤들이 선택되지 않는 문제가 발생한다.

 

윈도우 상 MainWindow 가 활성화되지 않고 다른 프로그램이 활성화 되어 있기 때문인데

해결 방법은 Popup 을 클릭했을때 우리 프로그램을 활성화 하면 된다.

 

        [DllImport("user32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        private static extern bool SetForegroundWindow(IntPtr hWnd);

    private void popup_PreviewMouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
    {
        // 실행 중인 애플리케이션의 메인 윈도우를 포그라운드로 가져옵니다.
        SetForegroundWindow(new System.Windows.Interop.WindowInteropHelper(Application.Current.MainWindow).Handle);
    }

 

위 코드를 사용해보자.