2 * PROJECT: ReactOS Magnify
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: base/applications/magnify/magnifier.c
6 * COPYRIGHT: Copyright 2007 Marc Piulachs <marc.piulachs@codexchange.net>
12 #include "magnifier.h"
15 const TCHAR szWindowClass
[] = TEXT("MAGNIFIER");
17 #define MAX_LOADSTRING 100
20 HINSTANCE hInst
; // current instance
23 TCHAR szTitle
[MAX_LOADSTRING
]; // The title bar text
25 #define REPAINT_SPEED 100
27 HWND hDesktopWindow
= NULL
;
29 //Current magnified area
37 // Forward declarations of functions included in this code module:
38 ATOM
MyRegisterClass(HINSTANCE hInstance
);
39 BOOL
InitInstance(HINSTANCE
, int);
40 LRESULT CALLBACK
WndProc(HWND
, UINT
, WPARAM
, LPARAM
);
41 INT_PTR CALLBACK
AboutProc(HWND
, UINT
, WPARAM
, LPARAM
);
42 INT_PTR CALLBACK
OptionsProc(HWND
, UINT
, WPARAM
, LPARAM
);
43 INT_PTR CALLBACK
WarningProc(HWND
, UINT
, WPARAM
, LPARAM
);
45 int WINAPI
WinMain( HINSTANCE hInstance
, HINSTANCE hPrevInstance
, LPSTR lpCmdLine
, int nCmdShow
)
47 // TODO: Place code here.
51 UNREFERENCED_PARAMETER(hPrevInstance
);
52 UNREFERENCED_PARAMETER(lpCmdLine
);
54 // Initialize global strings
55 LoadString(hInstance
, IDS_APP_TITLE
, szTitle
, MAX_LOADSTRING
);
56 MyRegisterClass(hInstance
);
58 // Perform application initialization:
59 if (!InitInstance (hInstance
, nCmdShow
))
64 hAccelTable
= LoadAccelerators(hInstance
, MAKEINTRESOURCE(IDC_MAGNIFIER
));
67 while (GetMessage(&msg
, NULL
, 0, 0))
69 if (!TranslateAccelerator(msg
.hwnd
, hAccelTable
, &msg
))
71 TranslateMessage(&msg
);
72 DispatchMessage(&msg
);
76 return (int) msg
.wParam
;
82 // FUNCTION: MyRegisterClass()
84 // PURPOSE: Registers the window class.
88 // This function and its usage are only necessary if you want this code
89 // to be compatible with Win32 systems prior to the 'RegisterClassEx'
90 // function that was added to Windows 95. It is important to call this function
91 // so that the application will get 'well formed' small icons associated
94 ATOM
MyRegisterClass(HINSTANCE hInstance
)
98 wc
.style
= CS_HREDRAW
| CS_VREDRAW
;
99 wc
.lpfnWndProc
= WndProc
;
102 wc
.hInstance
= hInstance
;
103 wc
.hIcon
= LoadIcon(hInstance
, MAKEINTRESOURCE(IDI_ICON
));
104 wc
.hCursor
= LoadCursor(NULL
, IDC_ARROW
);
105 wc
.hbrBackground
= (HBRUSH
)(COLOR_WINDOW
+1);
106 wc
.lpszMenuName
= MAKEINTRESOURCE(IDC_MAGNIFIER
);
107 wc
.lpszClassName
= szWindowClass
;
109 return RegisterClass(&wc
);
113 // FUNCTION: InitInstance(HINSTANCE, int)
115 // PURPOSE: Saves instance handle and creates main window
119 // In this function, we save the instance handle in a global variable and
120 // create and display the main program window.
122 BOOL
InitInstance(HINSTANCE hInstance
, int nCmdShow
)
124 hInst
= hInstance
; // Store instance handle in our global variable
127 hMainWnd
= CreateWindowEx(
146 ShowWindow(hMainWnd
, (bStartMinimized
) ? SW_MINIMIZE
: nCmdShow
);
147 UpdateWindow(hMainWnd
);
151 DialogBox (hInstance
, MAKEINTRESOURCE(IDD_WARNINGDIALOG
), hMainWnd
, (DLGPROC
)WarningProc
);
159 if (!IsIconic(hMainWnd
))
161 // Invalidate the client area forcing a WM_PAINT message
162 InvalidateRgn(hMainWnd
, NULL
, TRUE
);
168 HDC desktopHdc
= NULL
;
179 int Width
, Height
, AppWidth
, AppHeight
;
180 LONG blitAreaWidth
, blitAreaHeight
, blitAreaX
, blitAreaY
;
182 desktopHdc
= GetWindowDC (hDesktopWindow
);
184 GetClientRect(hMainWnd
, &appRect
);
185 GetWindowRect(hDesktopWindow
, &R
);
187 memset(&cinfo
, 0, sizeof(cinfo
));
188 cinfo
.cbSize
= sizeof(cinfo
);
189 GetCursorInfo(&cinfo
);
190 GetIconInfo(cinfo
.hCursor
, &iinfo
);
192 /* Create a memory DC compatible with client area DC.*/
193 HdcStrech
= CreateCompatibleDC(desktopHdc
);
195 /* Create a bitmap compatible with the client area DC.*/
196 HbmpStrech
= CreateCompatibleBitmap(
201 /* Select our bitmap in memory DC and save the old one.*/
202 hOld
= SelectObject (HdcStrech
, HbmpStrech
);
204 /* Paint the screen bitmap to our in memory DC */
216 /* Draw the mouse pointer in the right position */
219 pMouse
.x
- iinfo
.xHotspot
, // - 10,
220 pMouse
.y
- iinfo
.yHotspot
, // - 10,
223 Width
= (R
.right
- R
.left
);
224 Height
= (R
.bottom
- R
.top
);
226 AppWidth
= (appRect
.right
- appRect
.left
);
227 AppHeight
= (appRect
.bottom
- appRect
.top
);
229 blitAreaWidth
= AppWidth
/ iZoom
;
230 blitAreaHeight
= AppHeight
/ iZoom
;
232 blitAreaX
= (cp
.x
) - (blitAreaWidth
/2);
233 blitAreaY
= (cp
.y
) - (blitAreaHeight
/2);
245 if (blitAreaX
> (Width
- blitAreaWidth
))
247 blitAreaX
= (Width
- blitAreaWidth
);
250 if (blitAreaY
> (Height
- blitAreaHeight
))
252 blitAreaY
= (Height
- blitAreaHeight
);
260 /* Blast the stretched image from memory DC to window DC.*/
275 SelectObject (HdcStrech
, hOld
);
276 DeleteObject (HbmpStrech
);
277 DeleteDC (HdcStrech
);
278 ReleaseDC(hDesktopWindow
, desktopHdc
);
282 // FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
284 // PURPOSE: Processes messages for the main window.
286 // WM_COMMAND - process the application menu
287 // WM_PAINT - Paint the main window
288 // WM_DESTROY - post a quit message and return
291 LRESULT CALLBACK
WndProc(HWND hWnd
, UINT message
, WPARAM wParam
, LPARAM lParam
)
302 HWND hwnd1
, hwnd2
, hwnd3
;
306 //Get current mouse position
307 GetCursorPos (&pNewMouse
);
310 hwnd1
= GetForegroundWindow ();
311 a
= GetWindowThreadProcessId(hwnd1
, NULL
);
312 b
= GetCurrentThreadId();
313 AttachThreadInput (a
, b
, TRUE
);
316 GetCaretPos( &pNewCaret
);
317 ClientToScreen (hwnd2
, (LPPOINT
) &pNewCaret
);
318 AttachThreadInput (a
, b
, FALSE
);
320 //Get current control focus
322 GetWindowRect (hwnd3
, &controlRect
);
323 pNewFocus
.x
= controlRect
.left
;
324 pNewFocus
.y
= controlRect
.top
;
326 //If mouse has moved ....
327 if (((pMouse
.x
!= pNewMouse
.x
) || (pMouse
.y
!= pNewMouse
.y
)) && bFollowMouse
)
329 //Update to new position
334 else if (((pCaret
.x
!= pNewCaret
.x
) || (pCaret
.y
!= pNewCaret
.y
)) && bFollowCaret
)
336 //Update to new position
341 else if (((pFocus
.x
!= pNewFocus
.x
) || (pFocus
.y
!= pNewFocus
.y
)) && bFollowFocus
)
343 //Update to new position
351 wmId
= LOWORD(wParam
);
352 wmEvent
= HIWORD(wParam
);
353 // Parse the menu selections:
357 DialogBox(hInst
, MAKEINTRESOURCE(IDD_DIALOGOPTIONS
), hWnd
, (DLGPROC
)OptionsProc
);
360 DialogBox(hInst
, MAKEINTRESOURCE(IDD_ABOUTBOX
), hWnd
, (DLGPROC
)AboutProc
);
366 return DefWindowProc(hWnd
, message
, wParam
, lParam
);
371 PAINTSTRUCT PaintStruct
;
373 dc
= BeginPaint(hWnd
, &PaintStruct
);
375 EndPaint(hWnd
, &PaintStruct
);
379 //handle WM_ERASEBKGND by simply returning non-zero because we did all the drawing in WM_PAINT.
382 //Save settings to registry
384 KillTimer (hWnd
, 1);
388 //Load settings from registry
391 //Get the desktop window
392 hDesktopWindow
= GetDesktopWindow();
395 SetTimer (hWnd
, 1, REPAINT_SPEED
, NULL
);
398 return DefWindowProc(hWnd
, message
, wParam
, lParam
);
403 // Message handler for about box.
404 INT_PTR CALLBACK
AboutProc(HWND hDlg
, UINT message
, WPARAM wParam
, LPARAM lParam
)
406 UNREFERENCED_PARAMETER(lParam
);
410 return (INT_PTR
)TRUE
;
413 if (LOWORD(wParam
) == IDOK
|| LOWORD(wParam
) == IDCANCEL
)
415 EndDialog(hDlg
, LOWORD(wParam
));
416 return (INT_PTR
)TRUE
;
420 return (INT_PTR
)FALSE
;
423 // Message handler for options box.
424 INT_PTR CALLBACK
OptionsProc(HWND hDlg
, UINT message
, WPARAM wParam
, LPARAM lParam
)
426 UNREFERENCED_PARAMETER(lParam
);
431 //Add the zoom items....
432 SendDlgItemMessage(hDlg
, IDC_ZOOM
, CB_ADDSTRING
, 0, (LPARAM
)("1"));
433 SendDlgItemMessage(hDlg
, IDC_ZOOM
, CB_ADDSTRING
, 0, (LPARAM
)("2"));
434 SendDlgItemMessage(hDlg
, IDC_ZOOM
, CB_ADDSTRING
, 0, (LPARAM
)("3"));
435 SendDlgItemMessage(hDlg
, IDC_ZOOM
, CB_ADDSTRING
, 0, (LPARAM
)("4"));
436 SendDlgItemMessage(hDlg
, IDC_ZOOM
, CB_ADDSTRING
, 0, (LPARAM
)("5"));
437 SendDlgItemMessage(hDlg
, IDC_ZOOM
, CB_ADDSTRING
, 0, (LPARAM
)("6"));
438 SendDlgItemMessage(hDlg
, IDC_ZOOM
, CB_ADDSTRING
, 0, (LPARAM
)("7"));
439 SendDlgItemMessage(hDlg
, IDC_ZOOM
, CB_ADDSTRING
, 0, (LPARAM
)("8"));
442 SendDlgItemMessage(hDlg
, IDC_ZOOM
, CB_SETCURSEL
, iZoom
- 1, 0);
445 SendDlgItemMessage(hDlg
,IDC_FOLLOWMOUSECHECK
,BM_SETCHECK
, wParam
,0);
448 SendDlgItemMessage(hDlg
,IDC_FOLLOWKEYBOARDCHECK
,BM_SETCHECK
, wParam
,0);
451 SendDlgItemMessage(hDlg
,IDC_FOLLOWTEXTEDITINGCHECK
,BM_SETCHECK
, wParam
,0);
454 SendDlgItemMessage(hDlg
,IDC_INVERTCOLORSCHECK
,BM_SETCHECK
, wParam
,0);
457 SendDlgItemMessage(hDlg
,IDC_STARTMINIMIZEDCHECK
,BM_SETCHECK
, wParam
,0);
460 SendDlgItemMessage(hDlg
,IDC_SHOWMAGNIFIERCHECK
,BM_SETCHECK
, wParam
,0);
462 return (INT_PTR
)TRUE
;
465 switch(LOWORD(wParam
))
469 EndDialog(hDlg
, LOWORD(wParam
));
470 return (INT_PTR
)TRUE
;
472 case IDC_BUTTON_HELP
:
474 MessageBox(hDlg
, TEXT("Magnifier help not available yet!") , TEXT("Help") , MB_OK
);
477 if(HIWORD(wParam
) == CBN_SELCHANGE
)
479 HWND hCombo
= GetDlgItem(hDlg
,IDC_ZOOM
);
481 /* Get index of current selection and the text of that selection. */
482 iZoom
= SendMessage( hCombo
, CB_GETCURSEL
, (WPARAM
) wParam
, (LPARAM
) lParam
) + 1;
484 //Update the magnigier UI
488 case IDC_INVERTCOLORSCHECK
:
489 bInvertColors
= IsDlgButtonChecked (hDlg
, IDC_INVERTCOLORSCHECK
);
492 case IDC_FOLLOWMOUSECHECK
:
493 bFollowMouse
= IsDlgButtonChecked (hDlg
, IDC_FOLLOWMOUSECHECK
);
495 case IDC_FOLLOWKEYBOARDCHECK
:
496 bFollowFocus
= IsDlgButtonChecked (hDlg
, IDC_FOLLOWKEYBOARDCHECK
);
498 case IDC_FOLLOWTEXTEDITINGCHECK
:
499 bFollowCaret
= IsDlgButtonChecked (hDlg
, IDC_FOLLOWTEXTEDITINGCHECK
);
501 case IDC_STARTMINIMIZEDCHECK
:
502 bStartMinimized
= IsDlgButtonChecked (hDlg
, IDC_STARTMINIMIZEDCHECK
);
504 case IDC_SHOWMAGNIFIER
:
505 bShowMagnifier
= IsDlgButtonChecked (hDlg
, IDC_SHOWMAGNIFIERCHECK
);
507 ShowWindow (hMainWnd
, SW_SHOW
);
509 ShowWindow (hMainWnd
, SW_HIDE
);
514 return (INT_PTR
)FALSE
;
517 // Message handler for warning box.
518 INT_PTR CALLBACK
WarningProc(HWND hDlg
, UINT message
, WPARAM wParam
, LPARAM lParam
)
520 UNREFERENCED_PARAMETER(lParam
);
525 return (INT_PTR
)TRUE
;
527 switch(LOWORD(wParam
))
529 case IDC_SHOWWARNINGCHECK
:
530 bShowWarning
= !IsDlgButtonChecked (hDlg
, IDC_SHOWWARNINGCHECK
);
533 if (LOWORD(wParam
) == IDOK
|| LOWORD(wParam
) == IDCANCEL
)
535 EndDialog(hDlg
, LOWORD(wParam
));
536 return (INT_PTR
)TRUE
;
540 return (INT_PTR
)FALSE
;