magdlg.cpp
Upload User: xhy777
Upload Date: 2007-02-14
Package Size: 24088k
Code Size: 14k
Category:

Windows Kernel

Development Platform:

Visual C++

  1. // MagDlg.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "Magnify.h"
  5. #include "MagBar.h"
  6. #include "MagDlg.h"
  7. #include "Registry.h"
  8. #include "..Mag_HookMag_Hook.h" // for WM_EVENT_MOUSEMOVE
  9. #ifdef _DEBUG
  10. #define new DEBUG_NEW
  11. #undef THIS_FILE
  12. static char THIS_FILE[] = __FILE__;
  13. #endif
  14. // JMC: HACK - REMOVE
  15. const TCHAR m_szRegSubkey[] = __TEXT("Software\Microsoft\Magnify");
  16. // Hotkey identifiers for RegisterHotKey
  17. #define HKID_ZOOMUP               1
  18. #define HKID_ZOOMDOWN             2
  19. #define HKID_TOGGLEMOUSETRACKING  3
  20. #define HKID_COPYTOCLIPBOARD      4
  21. /////////////////////////////////////////////////////////////////////////////
  22. // CAboutDlg dialog used for App About
  23. class CAboutDlg : public CDialog
  24. {
  25. public:
  26. CAboutDlg();
  27. // Dialog Data
  28. //{{AFX_DATA(CAboutDlg)
  29. enum { IDD = IDD_ABOUTBOX };
  30. //}}AFX_DATA
  31. // ClassWizard generated virtual function overrides
  32. //{{AFX_VIRTUAL(CAboutDlg)
  33. protected:
  34. virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
  35. //}}AFX_VIRTUAL
  36. // Implementation
  37. protected:
  38. //{{AFX_MSG(CAboutDlg)
  39. //}}AFX_MSG
  40. DECLARE_MESSAGE_MAP()
  41. };
  42. CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
  43. {
  44. //{{AFX_DATA_INIT(CAboutDlg)
  45. //}}AFX_DATA_INIT
  46. }
  47. void CAboutDlg::DoDataExchange(CDataExchange* pDX)
  48. {
  49. CDialog::DoDataExchange(pDX);
  50. //{{AFX_DATA_MAP(CAboutDlg)
  51. //}}AFX_DATA_MAP
  52. }
  53. BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
  54. //{{AFX_MSG_MAP(CAboutDlg)
  55. // No message handlers
  56. //}}AFX_MSG_MAP
  57. END_MESSAGE_MAP()
  58. /////////////////////////////////////////////////////////////////////////////
  59. // CMagnifyDlg dialog
  60. #pragma warning(disable:4355)  // Disable warning C4355: 'this' : used in base member initializer list
  61. CMagnifyDlg::CMagnifyDlg(CWnd* pParent /*=NULL*/)
  62. : CDialog(CMagnifyDlg::IDD, pParent), m_wndStationary(this)
  63. {
  64. m_fOnInitDialogCalledYet = FALSE;
  65. //{{AFX_DATA_INIT(CMagnifyDlg)
  66. m_fStationaryTrackText = TRUE;
  67. m_fStationaryTrackSecondaryFocus = TRUE;
  68. m_fStationaryTrackCursor = TRUE;
  69. m_fStationaryTrackFocus = TRUE;
  70. m_nStationaryMagLevel = 2;
  71. m_fStationaryInvertColors = FALSE;
  72. m_fStationaryUseHighContrast = FALSE;
  73. //}}AFX_DATA_INIT
  74. // Set default hotkey for toggling mouse tracking to Ctrl+PageDown
  75. fuModifiersToggleMouseTracking = MOD_CONTROL;
  76. vkToggleMouseTracking = VK_NEXT;
  77. // Set default hotkey for copy to Clipboard to Ctrl+PrintScreen
  78. fuModifiersCopyToClipboard = MOD_ALT | MOD_SHIFT;// | MOD_CONTROL;
  79. vkCopyToClipboard = VK_SNAPSHOT;
  80. // Check the registry to see if we have been used before and if so,
  81. // reload our persistent settings.
  82. CRegSettings reg;
  83. if (reg.OpenSubkey(TRUE, HKEY_CURRENT_USER, m_szRegSubkey) == ERROR_SUCCESS) {
  84. reg.GetBOOL(__TEXT("StationaryTrackText"), &m_fStationaryTrackText);
  85. reg.GetBOOL(__TEXT("StationaryTrackSecondaryFocus"), &m_fStationaryTrackSecondaryFocus);
  86. reg.GetBOOL(__TEXT("StationaryTrackCursor"), &m_fStationaryTrackCursor);
  87. reg.GetBOOL(__TEXT("StationaryTrackFocus"), &m_fStationaryTrackFocus);
  88. reg.GetBOOL(__TEXT("StationaryInvertColors"), &m_fStationaryInvertColors);
  89. reg.GetDWORD(__TEXT("HotKeyModifiersToggleMouseTracking"), (PDWORD) &fuModifiersToggleMouseTracking);
  90. reg.GetDWORD(__TEXT("HotKeyVirtKeyToggleMouseTracking"), (PDWORD) &vkToggleMouseTracking);
  91. reg.GetDWORD(__TEXT("HotKeyModifiersCopyToClipboard"), (PDWORD) &fuModifiersCopyToClipboard);
  92. reg.GetDWORD(__TEXT("HotKeyVirtKeyCopyToClipboard"), (PDWORD) &vkCopyToClipboard);
  93. reg.GetDWORD(__TEXT("StationaryMagLevel"), (PDWORD) &m_nStationaryMagLevel);
  94. }
  95. // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
  96. m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  97. }
  98. CMagnifyDlg::~CMagnifyDlg()
  99. {
  100. // Save the current state of in the registry so that we'll
  101. // come up in the same state the next time the user runs us.
  102. CRegSettings reg;
  103. if (reg.OpenSubkey(FALSE, HKEY_CURRENT_USER, m_szRegSubkey) == ERROR_SUCCESS) {
  104. reg.PutBOOL(__TEXT("StationaryTrackText"), m_fStationaryTrackText);
  105. reg.PutBOOL(__TEXT("StationaryTrackSecondaryFocus"), m_fStationaryTrackSecondaryFocus);
  106. reg.PutBOOL(__TEXT("StationaryTrackCursor"), m_fStationaryTrackCursor);
  107. reg.PutBOOL(__TEXT("StationaryTrackFocus"), m_fStationaryTrackFocus);
  108. reg.PutBOOL(__TEXT("StationaryInvertColors"), m_fStationaryInvertColors);
  109. reg.PutDWORD(__TEXT("HotKeyModifiersToggleMouseTracking"), fuModifiersToggleMouseTracking);
  110. reg.PutDWORD(__TEXT("HotKeyVirtKeyToggleMouseTracking"), vkToggleMouseTracking);
  111. reg.PutDWORD(__TEXT("HotKeyModifiersCopyToClipboard"), fuModifiersCopyToClipboard);
  112. reg.PutDWORD(__TEXT("HotKeyVirtKeyCopyToClipboard"), vkCopyToClipboard);
  113. reg.PutDWORD(__TEXT("StationaryMagLevel"), m_nStationaryMagLevel);
  114. }
  115. }
  116. void CMagnifyDlg::DoDataExchange(CDataExchange* pDX)
  117. {
  118. CDialog::DoDataExchange(pDX);
  119. //{{AFX_DATA_MAP(CMagnifyDlg)
  120. DDX_Control(pDX, IDC_STATIONARYMAGSPIN, m_wndMagLevSpin);
  121. DDX_Check(pDX, IDC_STATIONARYTRACKTEXT, m_fStationaryTrackText);
  122. DDX_Check(pDX, IDC_STATIONARYTRACKSECONDARYFOCUS, m_fStationaryTrackSecondaryFocus);
  123. DDX_Check(pDX, IDC_STATIONARYTRACKMOUSECURSOR, m_fStationaryTrackCursor);
  124. DDX_Check(pDX, IDC_STATIONARYTRACKKYBDFOCUS, m_fStationaryTrackFocus);
  125. DDX_Text(pDX, IDC_STATIONARYMAGLEVEL, m_nStationaryMagLevel);
  126. DDV_MinMaxInt(pDX, m_nStationaryMagLevel, 1, 9);
  127. DDX_Check(pDX, IDC_STATIONARYINVERTCOLORS, m_fStationaryInvertColors);
  128. DDX_Check(pDX, IDC_STATIONARYHIGHCONTRAST, m_fStationaryUseHighContrast);
  129. //}}AFX_DATA_MAP
  130. }
  131. BEGIN_MESSAGE_MAP(CMagnifyDlg, CDialog)
  132. //{{AFX_MSG_MAP(CMagnifyDlg)
  133. ON_WM_SYSCOMMAND()
  134. ON_WM_PAINT()
  135. ON_WM_QUERYDRAGICON()
  136. ON_WM_DESTROY()
  137. ON_WM_CLOSE()
  138. ON_EN_CHANGE(IDC_STATIONARYMAGLEVEL, OnChangeStationarymaglevel)
  139. ON_BN_CLICKED(IDC_STATIONARYTRACKMOUSECURSOR, OnStationarytrackmousecursor)
  140. ON_BN_CLICKED(IDC_STATIONARYTRACKKYBDFOCUS, OnStationarytrackkybdfocus)
  141. ON_BN_CLICKED(IDC_STATIONARYTRACKSECONDARYFOCUS, OnStationarytracksecondaryfocus)
  142. ON_BN_CLICKED(IDC_STATIONARYTRACKTEXT, OnStationarytracktext)
  143. ON_BN_CLICKED(IDC_STATIONARYINVERTCOLORS, OnStationaryinvertcolors)
  144. ON_BN_CLICKED(IDC_STATIONARYHIGHCONTRAST, OnStationaryhighcontrast)
  145. //}}AFX_MSG_MAP
  146. ON_MESSAGE(WM_HOTKEY, OnHotKey)
  147. END_MESSAGE_MAP()
  148. /////////////////////////////////////////////////////////////////////////////
  149. // CMagnifyDlg message handlers
  150. BOOL CMagnifyDlg::OnInitDialog()
  151. {
  152. CDialog::OnInitDialog();
  153. // Add "About..." menu item to system menu.
  154. // IDM_ABOUTBOX must be in the system command range.
  155. ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
  156. ASSERT(IDM_ABOUTBOX < 0xF000);
  157. CMenu* pSysMenu = GetSystemMenu(FALSE);
  158. if (pSysMenu != NULL)
  159. {
  160. CString strAboutMenu;
  161. strAboutMenu.LoadString(IDS_ABOUTBOX);
  162. if (!strAboutMenu.IsEmpty())
  163. {
  164. pSysMenu->AppendMenu(MF_SEPARATOR);
  165. pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
  166. }
  167. }
  168. // Set the icon for this dialog.  The framework does this automatically
  169. //  when the application's main window is not a dialog
  170. SetIcon(m_hIcon, TRUE); // Set big icon
  171. SetIcon(m_hIcon, FALSE); // Set small icon
  172. m_hEventAlreadyRunning = CreateEvent(NULL, FALSE, FALSE, __TEXT("JMR\MSMagnifierAlreadyRunning"));
  173. if (GetLastError() == ERROR_ALREADY_EXISTS) {
  174. // Prevent multiple instances from running
  175. EndDialog(1);
  176. return(FALSE);
  177. }
  178. m_fOnInitDialogCalledYet = TRUE;
  179. // Create the Stationary and Roaming windows
  180. m_wndStationary.Create(IDD_STATIONARY, NULL);
  181. //m_wndRoaming.Create;
  182. // Register the hotkeys
  183. ::RegisterHotKey(m_hWnd, HKID_ZOOMUP,   MOD_WIN, VK_UP);
  184. ::RegisterHotKey(m_hWnd, HKID_ZOOMDOWN, MOD_WIN, VK_DOWN);
  185. ::RegisterHotKey(m_hWnd, HKID_TOGGLEMOUSETRACKING, fuModifiersToggleMouseTracking, vkToggleMouseTracking);
  186. ::RegisterHotKey(m_hWnd, HKID_COPYTOCLIPBOARD,     fuModifiersCopyToClipboard,     vkCopyToClipboard);
  187. // Set the magnification spinner's range
  188. m_wndMagLevSpin.SetRange(nMinZoom, nMaxZoom);
  189. // Make this dialog box a topmost window too.
  190. // SetWindowPos(&wndTopMost, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
  191. return TRUE;  // return TRUE  unless you set the focus to a control
  192. }
  193. void CMagnifyDlg::OnSysCommand(UINT nID, LPARAM lParam)
  194. {
  195. if ((nID & 0xFFF0) == IDM_ABOUTBOX)
  196. {
  197. CAboutDlg dlgAbout;
  198. dlgAbout.DoModal();
  199. }
  200. else
  201. {
  202. CDialog::OnSysCommand(nID, lParam);
  203. }
  204. }
  205. // If you add a minimize button to your dialog, you will need the code below
  206. //  to draw the icon.  For MFC applications using the document/view model,
  207. //  this is automatically done for you by the framework.
  208. void CMagnifyDlg::OnPaint() 
  209. {
  210. if (IsIconic())
  211. {
  212. CPaintDC dc(this); // device context for painting
  213. SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
  214. // Center icon in client rectangle
  215. int cxIcon = GetSystemMetrics(SM_CXICON);
  216. int cyIcon = GetSystemMetrics(SM_CYICON);
  217. CRect rect;
  218. GetClientRect(&rect);
  219. int x = (rect.Width() - cxIcon + 1) / 2;
  220. int y = (rect.Height() - cyIcon + 1) / 2;
  221. // Draw the icon
  222. dc.DrawIcon(x, y, m_hIcon);
  223. }
  224. else
  225. {
  226. CDialog::OnPaint();
  227. }
  228. }
  229. // The system calls this to obtain the cursor to display while the user drags
  230. //  the minimized window.
  231. HCURSOR CMagnifyDlg::OnQueryDragIcon()
  232. {
  233. return (HCURSOR) m_hIcon;
  234. }
  235. void CMagnifyDlg::OnDestroy() 
  236. {
  237. ::UnregisterHotKey(m_hWnd, HKID_ZOOMUP);
  238. ::UnregisterHotKey(m_hWnd, HKID_ZOOMDOWN);
  239. ::UnregisterHotKey(m_hWnd, HKID_TOGGLEMOUSETRACKING);
  240. ::UnregisterHotKey(m_hWnd, HKID_COPYTOCLIPBOARD);
  241. CDialog::OnDestroy();
  242. }
  243. void CMagnifyDlg::OnClose() 
  244. {
  245. if (CanExit()) {
  246. // Destroy the Stationary and Roaming windows
  247. m_wndStationary.SendMessage(WM_CLOSE);
  248. CDialog::OnClose();
  249. }
  250. }
  251. void CMagnifyDlg::OnOK() 
  252. {
  253. // Load the values from the controls into the member variables
  254. if (!UpdateData(TRUE)) {
  255. TRACE0("UpdateData failed during dialog termination.n");
  256. // the UpdateData routine will set focus to correct item
  257. return;
  258. } else {
  259. // Data in member variables OK
  260. // For stationary window to refresh using the new Zoom level
  261. m_wndStationary.InvalidateRect(NULL, FALSE);
  262. }
  263. // NOTE: Don't call CDialog::OnOK or the dialog box will be 
  264. // destroyed and the process will terminate.  The process should 
  265. // die when the user presses the Exit button (IDCANCEL)
  266. ShowWindow(SW_MINIMIZE);
  267. // Original MFC-produced code: if (CanExit()) CDialog::OnOK();
  268. }
  269. void CMagnifyDlg::OnCancel() 
  270. {
  271. if (CanExit()) {
  272. // Destroy the Stationary and Roaming windows
  273. m_wndStationary.SendMessage(WM_CLOSE);
  274. CDialog::OnCancel();
  275. }
  276. }
  277. BOOL CMagnifyDlg::CanExit() {
  278. return TRUE;
  279. }
  280. void CMagnifyDlg::UpdateState() {
  281. if (m_fOnInitDialogCalledYet) {
  282. // Load the values from the controls into the member variables
  283. if (!UpdateData(TRUE)) {
  284. TRACE0("UpdateData failed during dialog termination.n");
  285. // the UpdateData routine will set focus to correct item
  286. return;
  287. } else {
  288. // Data in member variables OK
  289. // For stationary window to refresh using the new Zoom level
  290. m_wndStationary.ZoomChanged();
  291. m_wndStationary.InvalidateRect(NULL, FALSE);
  292. }
  293. }
  294. }
  295. void CMagnifyDlg::OnChangeStationarymaglevel() 
  296. {
  297. // TODO: If this is a RICHEDIT control, the control will not
  298. // send this notification unless you override the CDialog::OnInitDialog()
  299. // function to send the EM_SETEVENTMASK message to the control
  300. // with the ENM_CHANGE flag ORed into the lParam mask.
  301. // TODO: Add your control notification handler code here
  302. UpdateState();
  303. }
  304. void CMagnifyDlg::OnStationarytrackmousecursor() 
  305. {
  306. // TODO: Add your control notification handler code here
  307. UpdateState();
  308. }
  309. void CMagnifyDlg::OnStationarytrackkybdfocus() 
  310. {
  311. // TODO: Add your control notification handler code here
  312. UpdateState();
  313. }
  314. void CMagnifyDlg::OnStationarytracksecondaryfocus() 
  315. {
  316. // TODO: Add your control notification handler code here
  317. UpdateState();
  318. }
  319. void CMagnifyDlg::OnStationarytracktext() 
  320. {
  321. // TODO: Add your control notification handler code here
  322. UpdateState();
  323. }
  324. void CMagnifyDlg::OnStationaryinvertcolors() 
  325. {
  326. // TODO: Add your control notification handler code here
  327. UpdateState();
  328. }
  329. LRESULT CMagnifyDlg::OnHotKey(WPARAM wParam, LPARAM lParam) {
  330. int idHotKey = wParam;
  331. UpdateData();
  332. switch (idHotKey) {
  333. case HKID_ZOOMUP:
  334. case HKID_ZOOMDOWN:
  335. if (idHotKey == HKID_ZOOMUP) {
  336. if (m_nStationaryMagLevel < nMaxZoom) m_nStationaryMagLevel++;
  337. } else {
  338. if (m_nStationaryMagLevel > nMinZoom) m_nStationaryMagLevel--;
  339. }
  340. UpdateData(FALSE);
  341. m_wndStationary.ZoomChanged();
  342. m_wndStationary.InvalidateRect(NULL, FALSE);
  343. break;
  344. case HKID_TOGGLEMOUSETRACKING:
  345. m_fStationaryTrackCursor ^= TRUE;
  346. UpdateData(FALSE);   // Make sure the dialog box reflect the change.
  347. break;
  348. case HKID_COPYTOCLIPBOARD:
  349. m_wndStationary.CopyToClipboard();
  350. break;
  351. }
  352. return(0);
  353. }
  354. void CMagnifyDlg::OnStationaryhighcontrast() 
  355. {
  356. UpdateData();
  357. HIGHCONTRAST hc;
  358. hc.cbSize = sizeof(hc);
  359. if( SystemParametersInfo(SPI_GETHIGHCONTRAST, sizeof(hc), &hc, FALSE)
  360. && (hc.dwFlags & HCF_AVAILABLE))
  361. {
  362. if(m_fStationaryUseHighContrast)
  363. hc.dwFlags |= HCF_HIGHCONTRASTON;
  364. else
  365. hc.dwFlags &= ~HCF_HIGHCONTRASTON;
  366. SystemParametersInfo(SPI_SETHIGHCONTRAST, sizeof(hc), &hc, TRUE);
  367. }
  368. #pragma message("FINISHFINISHFINISHFINISHFINISHFINISHFINISHFINISH - CHANGE FOR NT4")
  369. }
  370. BOOL CMagnifyDlg::PreTranslateMessage(MSG* pMsg) 
  371. {
  372. // JMC: HACK TO GET THIS TO WORK WITH MouseHook
  373. switch(pMsg->message)
  374. {
  375. case WM_MOUSEMOVE:
  376. case WM_NCMOUSEMOVE:
  377. FakeCursorMove(pMsg->pt);
  378. break;
  379. default:
  380. break;
  381. }
  382. return CDialog::PreTranslateMessage(pMsg);
  383. }