source: trunk/Utilities/StealFocusViaAttachInputThread/MainDialog.h @ 73

Last change on this file since 73 was 73, checked in by roman, 12 years ago
  • Property svn:keywords set to Id
File size: 3.4 KB
Line 
1////////////////////////////////////////////////////////////
2// Copyright (C) Roman Ryltsov, 2008-2012
3// Created by Roman Ryltsov roman@alax.info
4//
5// $Id: MainDialog.h 73 2012-07-09 07:21:42Z roman $
6
7#pragma once
8
9////////////////////////////////////////////////////////////
10// CMainDialog
11
12class CMainDialog : 
13        public CDialogImpl<CMainDialog>
14{
15public:
16        enum { IDD = IDD_MAIN };
17
18BEGIN_MSG_MAP_EX(CMainDialog)
19        //CHAIN_MSG_MAP(CDialogImpl<CMainDialog>)
20        MSG_WM_INITDIALOG(OnInitDialog)
21        MSG_WM_TIMER(OnTimer)
22        COMMAND_ID_HANDLER_EX(IDCANCEL, OnCommand)
23        REFLECT_NOTIFICATIONS()
24END_MSG_MAP()
25
26public:
27
28        ////////////////////////////////////////////////////////
29        // Time Identifiers
30
31        enum
32        {
33                TIMER_FIRST = 0,
34                TIMER_UPDATEFORGEOUND,
35                TIMER_ACTION,
36        };
37
38private:
39        CRoEdit m_ForegroundEdit;
40        CRoEdit m_StateEdit;
41        INT m_nActionCountdown;
42        CString m_sAction;
43
44public:
45// CMainDialog
46        CMainDialog()
47        {
48        }
49
50// Window Message Handelrs
51        LRESULT OnInitDialog(HWND, LPARAM)
52        {
53                SetIcon(AtlLoadIconImage(IDI_MODULE, LR_DEFAULTCOLOR, GetSystemMetrics(SM_CXICON), GetSystemMetrics(SM_CYICON)), TRUE);
54                SetIcon(AtlLoadIconImage(IDI_MODULE, LR_DEFAULTCOLOR, GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON)), FALSE);
55                _W(CenterWindow());
56                m_ForegroundEdit = GetDlgItem(IDC_FOREGROUND);
57                m_StateEdit = GetDlgItem(IDC_STATE);
58                GetDlgItem(IDC_EDIT).SetFocus();
59                m_nActionCountdown = 7;
60                SetTimer(TIMER_UPDATEFORGEOUND, 1000);
61                SetTimer(TIMER_ACTION, 7250);
62                return FALSE;
63        }
64        LRESULT OnTimer(UINT_PTR nEvent)
65        {
66                switch(nEvent)
67                {
68                case TIMER_UPDATEFORGEOUND:
69                        {
70                                CWindow Window = GetForegroundWindow();
71                                CString sWindow;
72                                if(Window)
73                                {
74                                        CString sText;
75                                        Window.GetWindowText(sText);
76                                        if(sText.GetLength() > 32)
77                                                sText = sText.Left(30) + _T("...");
78                                        sWindow = AtlFormatString(_T("0x%08x \"%s\" (%d)"), Window, sText, Window.GetWindowThreadID());
79                                } else
80                                        sWindow = _T("NULL");
81                                m_ForegroundEdit.SetWindowText(sWindow);
82                                m_nActionCountdown--;
83                                if(m_nActionCountdown > 0)
84                                        m_StateEdit.SetWindowText(AtlFormatString(_T("%d seconds before stealing..."), m_nActionCountdown));
85                                else if(m_nActionCountdown == 0)
86                                        m_StateEdit.SetWindowText(_T("About to steal..."));
87                                else
88                                        m_StateEdit.SetWindowText(m_sAction);
89                        }
90                        break;
91                case TIMER_ACTION:
92                        {
93                                KillTimer(TIMER_ACTION);
94                                CWindow Window = GetForegroundWindow();
95                                if(Window)
96                                {
97                                        const DWORD nWindowThreadIdentifier = Window.GetWindowThreadID();
98                                        const DWORD nThreadIdentifier = GetCurrentThreadId();
99                                        if(!nWindowThreadIdentifier || nWindowThreadIdentifier == nThreadIdentifier)
100                                        {
101                                                if(nWindowThreadIdentifier)
102                                                        AtlMessageBoxEx(m_hWnd, _T("The application is supposed to steal focus from another application, so have another application running in foreground to check the stealing out!"), IDS_WARNING, MB_ICONINFORMATION | MB_OK);
103                                                m_nActionCountdown = 7;
104                                                SetTimer(TIMER_ACTION, 7250);
105                                                return 0;
106                                        }
107                                        __E(AttachThreadInput(nThreadIdentifier, nWindowThreadIdentifier, TRUE));
108                                        GetDlgItem(IDC_EDIT).SetFocus();
109                                        _W(AttachThreadInput(nThreadIdentifier, nWindowThreadIdentifier, FALSE));
110                                        m_sAction = _T("Done");
111                                } else
112                                        m_sAction = _T("Nothing to do");
113                        }
114                        break;
115                default:
116                        SetMsgHandled(FALSE);
117                }
118                return 0;
119        }
120        LRESULT OnCommand(UINT, INT nIdentifier, HWND)
121        {
122                _W(EndDialog(nIdentifier));
123                return 0;
124        }
125};
Note: See TracBrowser for help on using the repository browser.