2 * PROJECT: ReactOS Magnify
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: base/applications/magnify/magnifier.c
5 * PURPOSE: Magnification of parts of the screen.
6 * COPYRIGHT: Copyright 2007 Marc Piulachs <marc.piulachs@codexchange.net>
10 #include "magnifier.h"
12 const TCHAR szWindowClass
[] = TEXT("MAGNIFIER");
14 #define MAX_LOADSTRING 100
17 HINSTANCE hInst
; // current instance
20 TCHAR szTitle
[MAX_LOADSTRING
]; // The title bar text
22 #define REPAINT_SPEED 100
24 HWND hDesktopWindow
= NULL
;
26 //Current magnified area
34 // Forward declarations of functions included in this code module:
35 ATOM
MyRegisterClass(HINSTANCE hInstance
);
36 BOOL
InitInstance(HINSTANCE
, int);
37 LRESULT CALLBACK
WndProc(HWND
, UINT
, WPARAM
, LPARAM
);
38 INT_PTR CALLBACK
AboutProc(HWND
, UINT
, WPARAM
, LPARAM
);
39 INT_PTR CALLBACK
OptionsProc(HWND
, UINT
, WPARAM
, LPARAM
);
40 INT_PTR CALLBACK
WarningProc(HWND
, UINT
, WPARAM
, LPARAM
);
42 int WINAPI
WinMain( HINSTANCE hInstance
, HINSTANCE hPrevInstance
, LPSTR lpCmdLine
, int nCmdShow
)
44 // TODO: Place code here.
48 UNREFERENCED_PARAMETER(hPrevInstance
);
49 UNREFERENCED_PARAMETER(lpCmdLine
);
51 // Initialize global strings
52 LoadString(hInstance
, IDS_APP_TITLE
, szTitle
, MAX_LOADSTRING
);
53 MyRegisterClass(hInstance
);
55 // Perform application initialization:
56 if (!InitInstance (hInstance
, nCmdShow
))
61 hAccelTable
= LoadAccelerators(hInstance
, MAKEINTRESOURCE(IDC_MAGNIFIER
));
64 while (GetMessage(&msg
, NULL
, 0, 0))
66 if (!TranslateAccelerator(msg
.hwnd
, hAccelTable
, &msg
))
68 TranslateMessage(&msg
);
69 DispatchMessage(&msg
);
73 return (int) msg
.wParam
;
79 * FUNCTION: MyRegisterClass()
81 * PURPOSE: Registers the window class.
85 * This function and its usage are only necessary if you want this code
86 * to be compatible with Win32 systems prior to the 'RegisterClassEx'
87 * function that was added to Windows 95. It is important to call this function
88 * so that the application will get 'well formed' small icons associated
91 ATOM
MyRegisterClass(HINSTANCE hInstance
)
95 wc
.style
= CS_HREDRAW
| CS_VREDRAW
;
96 wc
.lpfnWndProc
= WndProc
;
99 wc
.hInstance
= hInstance
;
100 wc
.hIcon
= LoadIcon(hInstance
, MAKEINTRESOURCE(IDI_ICON
));
101 wc
.hCursor
= LoadCursor(NULL
, IDC_ARROW
);
102 wc
.hbrBackground
= (HBRUSH
)(COLOR_WINDOW
+1);
103 wc
.lpszMenuName
= MAKEINTRESOURCE(IDC_MAGNIFIER
);
104 wc
.lpszClassName
= szWindowClass
;
106 return RegisterClass(&wc
);
110 * FUNCTION: InitInstance(HINSTANCE, int)
112 * PURPOSE: Saves instance handle and creates main window
116 * In this function, we save the instance handle in a global variable and
117 * create and display the main program window.
119 BOOL
InitInstance(HINSTANCE hInstance
, int nCmdShow
)
121 hInst
= hInstance
; // Store instance handle in our global variable
124 hMainWnd
= CreateWindowEx(
143 ShowWindow(hMainWnd
, (bStartMinimized
) ? SW_MINIMIZE
: nCmdShow
);
144 UpdateWindow(hMainWnd
);
148 DialogBox (hInstance
, MAKEINTRESOURCE(IDD_WARNINGDIALOG
), hMainWnd
, (DLGPROC
)WarningProc
);
156 if (!IsIconic(hMainWnd
))
158 // Invalidate the client area forcing a WM_PAINT message
159 InvalidateRgn(hMainWnd
, NULL
, TRUE
);
165 HDC desktopHdc
= NULL
;
176 int Width
, Height
, AppWidth
, AppHeight
;
177 LONG blitAreaWidth
, blitAreaHeight
, blitAreaX
, blitAreaY
;
179 desktopHdc
= GetWindowDC (hDesktopWindow
);
181 GetClientRect(hMainWnd
, &appRect
);
182 GetWindowRect(hDesktopWindow
, &R
);
184 ZeroMemory(&cinfo
, sizeof(CURSORINFO
));
185 ZeroMemory(&iinfo
, sizeof(ICONINFO
));
186 cinfo
.cbSize
= sizeof(cinfo
);
187 GetCursorInfo(&cinfo
);
188 GetIconInfo(cinfo
.hCursor
, &iinfo
);
190 /* Create a memory DC compatible with client area DC.*/
191 HdcStrech
= CreateCompatibleDC(desktopHdc
);
193 /* Create a bitmap compatible with the client area DC.*/
194 HbmpStrech
= CreateCompatibleBitmap(
199 /* Select our bitmap in memory DC and save the old one.*/
200 hOld
= SelectObject (HdcStrech
, HbmpStrech
);
202 /* Paint the screen bitmap to our in memory DC */
214 /* Draw the mouse pointer in the right position */
217 pMouse
.x
- iinfo
.xHotspot
, // - 10,
218 pMouse
.y
- iinfo
.yHotspot
, // - 10,
221 Width
= (R
.right
- R
.left
);
222 Height
= (R
.bottom
- R
.top
);
224 AppWidth
= (appRect
.right
- appRect
.left
);
225 AppHeight
= (appRect
.bottom
- appRect
.top
);
227 blitAreaWidth
= AppWidth
/ iZoom
;
228 blitAreaHeight
= AppHeight
/ iZoom
;
230 blitAreaX
= (cp
.x
) - (blitAreaWidth
/2);
231 blitAreaY
= (cp
.y
) - (blitAreaHeight
/2);
243 if (blitAreaX
> (Width
- blitAreaWidth
))
245 blitAreaX
= (Width
- blitAreaWidth
);
248 if (blitAreaY
> (Height
- blitAreaHeight
))
250 blitAreaY
= (Height
- blitAreaHeight
);
258 /* Blast the stretched image from memory DC to window DC.*/
274 DeleteObject(iinfo
.hbmMask
);
276 DeleteObject(iinfo
.hbmColor
);
277 SelectObject (HdcStrech
, hOld
);
278 DeleteObject (HbmpStrech
);
279 DeleteDC (HdcStrech
);
280 ReleaseDC(hDesktopWindow
, desktopHdc
);
284 * FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
286 * PURPOSE: Processes messages for the main window.
288 * WM_COMMAND - process the application menu
289 * WM_PAINT - Paint the main window
290 * WM_DESTROY - post a quit message and return
293 LRESULT CALLBACK
WndProc(HWND hWnd
, UINT message
, WPARAM wParam
, LPARAM lParam
)
304 HWND hwnd1
, hwnd2
, hwnd3
;
308 //Get current mouse position
309 GetCursorPos (&pNewMouse
);
312 hwnd1
= GetForegroundWindow ();
313 a
= GetWindowThreadProcessId(hwnd1
, NULL
);
314 b
= GetCurrentThreadId();
315 AttachThreadInput (a
, b
, TRUE
);
318 GetCaretPos( &pNewCaret
);
319 ClientToScreen (hwnd2
, (LPPOINT
) &pNewCaret
);
320 AttachThreadInput (a
, b
, FALSE
);
322 //Get current control focus
324 GetWindowRect (hwnd3
, &controlRect
);
325 pNewFocus
.x
= controlRect
.left
;
326 pNewFocus
.y
= controlRect
.top
;
328 //If mouse has moved ....
329 if (((pMouse
.x
!= pNewMouse
.x
) || (pMouse
.y
!= pNewMouse
.y
)) && bFollowMouse
)
331 //Update to new position
336 else if (((pCaret
.x
!= pNewCaret
.x
) || (pCaret
.y
!= pNewCaret
.y
)) && bFollowCaret
)
338 //Update to new position
343 else if (((pFocus
.x
!= pNewFocus
.x
) || (pFocus
.y
!= pNewFocus
.y
)) && bFollowFocus
)
345 //Update to new position
353 wmId
= LOWORD(wParam
);
354 // Parse the menu selections:
358 DialogBox(hInst
, MAKEINTRESOURCE(IDD_DIALOGOPTIONS
), hWnd
, (DLGPROC
)OptionsProc
);
361 DialogBox(hInst
, MAKEINTRESOURCE(IDD_ABOUTBOX
), hWnd
, (DLGPROC
)AboutProc
);
367 return DefWindowProc(hWnd
, message
, wParam
, lParam
);
372 PAINTSTRUCT PaintStruct
;
374 dc
= BeginPaint(hWnd
, &PaintStruct
);
376 EndPaint(hWnd
, &PaintStruct
);
380 //handle WM_ERASEBKGND by simply returning non-zero because we did all the drawing in WM_PAINT.
383 //Save settings to registry
385 KillTimer (hWnd
, 1);
389 //Load settings from registry
392 //Get the desktop window
393 hDesktopWindow
= GetDesktopWindow();
396 SetTimer (hWnd
, 1, REPAINT_SPEED
, NULL
);
399 return DefWindowProc(hWnd
, message
, wParam
, lParam
);
404 // Message handler for about box.
405 INT_PTR CALLBACK
AboutProc(HWND hDlg
, UINT message
, WPARAM wParam
, LPARAM lParam
)
407 UNREFERENCED_PARAMETER(lParam
);
411 return (INT_PTR
)TRUE
;
414 if (LOWORD(wParam
) == IDOK
|| LOWORD(wParam
) == IDCANCEL
)
416 EndDialog(hDlg
, LOWORD(wParam
));
417 return (INT_PTR
)TRUE
;
421 return (INT_PTR
)FALSE
;
424 // Message handler for options box.
425 INT_PTR CALLBACK
OptionsProc(HWND hDlg
, UINT message
, WPARAM wParam
, LPARAM lParam
)
427 UNREFERENCED_PARAMETER(lParam
);
432 //Add the zoom items....
433 SendDlgItemMessage(hDlg
, IDC_ZOOM
, CB_ADDSTRING
, 0, (LPARAM
)("1"));
434 SendDlgItemMessage(hDlg
, IDC_ZOOM
, CB_ADDSTRING
, 0, (LPARAM
)("2"));
435 SendDlgItemMessage(hDlg
, IDC_ZOOM
, CB_ADDSTRING
, 0, (LPARAM
)("3"));
436 SendDlgItemMessage(hDlg
, IDC_ZOOM
, CB_ADDSTRING
, 0, (LPARAM
)("4"));
437 SendDlgItemMessage(hDlg
, IDC_ZOOM
, CB_ADDSTRING
, 0, (LPARAM
)("5"));
438 SendDlgItemMessage(hDlg
, IDC_ZOOM
, CB_ADDSTRING
, 0, (LPARAM
)("6"));
439 SendDlgItemMessage(hDlg
, IDC_ZOOM
, CB_ADDSTRING
, 0, (LPARAM
)("7"));
440 SendDlgItemMessage(hDlg
, IDC_ZOOM
, CB_ADDSTRING
, 0, (LPARAM
)("8"));
443 SendDlgItemMessage(hDlg
, IDC_ZOOM
, CB_SETCURSEL
, iZoom
- 1, 0);
446 SendDlgItemMessage(hDlg
,IDC_FOLLOWMOUSECHECK
,BM_SETCHECK
, wParam
,0);
449 SendDlgItemMessage(hDlg
,IDC_FOLLOWKEYBOARDCHECK
,BM_SETCHECK
, wParam
,0);
452 SendDlgItemMessage(hDlg
,IDC_FOLLOWTEXTEDITINGCHECK
,BM_SETCHECK
, wParam
,0);
455 SendDlgItemMessage(hDlg
,IDC_INVERTCOLORSCHECK
,BM_SETCHECK
, wParam
,0);
458 SendDlgItemMessage(hDlg
,IDC_STARTMINIMIZEDCHECK
,BM_SETCHECK
, wParam
,0);
461 SendDlgItemMessage(hDlg
,IDC_SHOWMAGNIFIERCHECK
,BM_SETCHECK
, wParam
,0);
463 return (INT_PTR
)TRUE
;
466 switch(LOWORD(wParam
))
470 EndDialog(hDlg
, LOWORD(wParam
));
471 return (INT_PTR
)TRUE
;
473 case IDC_BUTTON_HELP
:
475 MessageBox(hDlg
, TEXT("Magnifier help not available yet!") , TEXT("Help") , MB_OK
);
478 if(HIWORD(wParam
) == CBN_SELCHANGE
)
480 HWND hCombo
= GetDlgItem(hDlg
,IDC_ZOOM
);
482 /* Get index of current selection and the text of that selection. */
483 iZoom
= SendMessage( hCombo
, CB_GETCURSEL
, (WPARAM
) wParam
, (LPARAM
) lParam
) + 1;
485 //Update the magnigier UI
489 case IDC_INVERTCOLORSCHECK
:
490 bInvertColors
= IsDlgButtonChecked (hDlg
, IDC_INVERTCOLORSCHECK
);
493 case IDC_FOLLOWMOUSECHECK
:
494 bFollowMouse
= IsDlgButtonChecked (hDlg
, IDC_FOLLOWMOUSECHECK
);
496 case IDC_FOLLOWKEYBOARDCHECK
:
497 bFollowFocus
= IsDlgButtonChecked (hDlg
, IDC_FOLLOWKEYBOARDCHECK
);
499 case IDC_FOLLOWTEXTEDITINGCHECK
:
500 bFollowCaret
= IsDlgButtonChecked (hDlg
, IDC_FOLLOWTEXTEDITINGCHECK
);
502 case IDC_STARTMINIMIZEDCHECK
:
503 bStartMinimized
= IsDlgButtonChecked (hDlg
, IDC_STARTMINIMIZEDCHECK
);
505 case IDC_SHOWMAGNIFIER
:
506 bShowMagnifier
= IsDlgButtonChecked (hDlg
, IDC_SHOWMAGNIFIERCHECK
);
508 ShowWindow (hMainWnd
, SW_SHOW
);
510 ShowWindow (hMainWnd
, SW_HIDE
);
515 return (INT_PTR
)FALSE
;
518 // Message handler for warning box.
519 INT_PTR CALLBACK
WarningProc(HWND hDlg
, UINT message
, WPARAM wParam
, LPARAM lParam
)
521 UNREFERENCED_PARAMETER(lParam
);
526 return (INT_PTR
)TRUE
;
528 switch(LOWORD(wParam
))
530 case IDC_SHOWWARNINGCHECK
:
531 bShowWarning
= !IsDlgButtonChecked (hDlg
, IDC_SHOWWARNINGCHECK
);
534 if (LOWORD(wParam
) == IDOK
|| LOWORD(wParam
) == IDCANCEL
)
536 EndDialog(hDlg
, LOWORD(wParam
));
537 return (INT_PTR
)TRUE
;
541 return (INT_PTR
)FALSE
;