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>
11 #include "magnifier.h"
13 const TCHAR szWindowClass
[] = TEXT("MAGNIFIER");
15 #define MAX_LOADSTRING 100
18 HINSTANCE hInst
; // current instance
21 TCHAR szTitle
[MAX_LOADSTRING
]; // The title bar text
23 #define REPAINT_SPEED 100
25 HWND hDesktopWindow
= NULL
;
27 //Current magnified area
35 // Forward declarations of functions included in this code module:
36 ATOM
MyRegisterClass(HINSTANCE hInstance
);
37 BOOL
InitInstance(HINSTANCE
, int);
38 LRESULT CALLBACK
WndProc(HWND
, UINT
, WPARAM
, LPARAM
);
39 INT_PTR CALLBACK
AboutProc(HWND
, UINT
, WPARAM
, LPARAM
);
40 INT_PTR CALLBACK
OptionsProc(HWND
, UINT
, WPARAM
, LPARAM
);
41 INT_PTR CALLBACK
WarningProc(HWND
, UINT
, WPARAM
, LPARAM
);
43 int WINAPI
WinMain( HINSTANCE hInstance
, HINSTANCE hPrevInstance
, LPSTR lpCmdLine
, int nCmdShow
)
45 // TODO: Place code here.
49 UNREFERENCED_PARAMETER(hPrevInstance
);
50 UNREFERENCED_PARAMETER(lpCmdLine
);
52 switch (GetUserDefaultUILanguage())
54 case MAKELANGID(LANG_HEBREW
, SUBLANG_DEFAULT
):
55 SetProcessDefaultLayout(LAYOUT_RTL
);
62 // Initialize global strings
63 LoadString(hInstance
, IDS_APP_TITLE
, szTitle
, MAX_LOADSTRING
);
64 MyRegisterClass(hInstance
);
66 // Perform application initialization:
67 if (!InitInstance (hInstance
, nCmdShow
))
72 hAccelTable
= LoadAccelerators(hInstance
, MAKEINTRESOURCE(IDC_MAGNIFIER
));
75 while (GetMessage(&msg
, NULL
, 0, 0))
77 if (!TranslateAccelerator(msg
.hwnd
, hAccelTable
, &msg
))
79 TranslateMessage(&msg
);
80 DispatchMessage(&msg
);
84 return (int) msg
.wParam
;
90 * FUNCTION: MyRegisterClass()
92 * PURPOSE: Registers the window class.
96 * This function and its usage are only necessary if you want this code
97 * to be compatible with Win32 systems prior to the 'RegisterClassEx'
98 * function that was added to Windows 95. It is important to call this function
99 * so that the application will get 'well formed' small icons associated
102 ATOM
MyRegisterClass(HINSTANCE hInstance
)
106 wc
.style
= CS_HREDRAW
| CS_VREDRAW
;
107 wc
.lpfnWndProc
= WndProc
;
110 wc
.hInstance
= hInstance
;
111 wc
.hIcon
= LoadIcon(hInstance
, MAKEINTRESOURCE(IDI_ICON
));
112 wc
.hCursor
= LoadCursor(NULL
, IDC_ARROW
);
113 wc
.hbrBackground
= (HBRUSH
)(COLOR_WINDOW
+1);
114 wc
.lpszMenuName
= MAKEINTRESOURCE(IDC_MAGNIFIER
);
115 wc
.lpszClassName
= szWindowClass
;
117 return RegisterClass(&wc
);
121 * FUNCTION: InitInstance(HINSTANCE, int)
123 * PURPOSE: Saves instance handle and creates main window
127 * In this function, we save the instance handle in a global variable and
128 * create and display the main program window.
130 BOOL
InitInstance(HINSTANCE hInstance
, int nCmdShow
)
133 hInst
= hInstance
; // Store instance handle in our global variable
135 SystemParametersInfo(SPI_GETWORKAREA
, 0, &rcWorkArea
, 0);
137 /* Create the Window */
138 hMainWnd
= CreateWindowEx(
139 WS_EX_TOPMOST
| WS_EX_PALETTEWINDOW
,
145 (rcWorkArea
.right
- rcWorkArea
.left
) * 2 / 3,
157 ShowWindow(hMainWnd
, (bStartMinimized
) ? SW_MINIMIZE
: nCmdShow
);
158 UpdateWindow(hMainWnd
);
162 DialogBox (hInstance
, MAKEINTRESOURCE(IDD_WARNINGDIALOG
), hMainWnd
, (DLGPROC
)WarningProc
);
170 if (!IsIconic(hMainWnd
))
172 // Invalidate the client area forcing a WM_PAINT message
173 InvalidateRgn(hMainWnd
, NULL
, TRUE
);
179 HDC desktopHdc
= NULL
;
190 int Width
, Height
, AppWidth
, AppHeight
;
191 LONG blitAreaWidth
, blitAreaHeight
, blitAreaX
, blitAreaY
;
193 desktopHdc
= GetWindowDC (hDesktopWindow
);
195 GetClientRect(hMainWnd
, &appRect
);
196 GetWindowRect(hDesktopWindow
, &R
);
198 ZeroMemory(&cinfo
, sizeof(CURSORINFO
));
199 ZeroMemory(&iinfo
, sizeof(ICONINFO
));
200 cinfo
.cbSize
= sizeof(cinfo
);
201 GetCursorInfo(&cinfo
);
202 GetIconInfo(cinfo
.hCursor
, &iinfo
);
204 /* Create a memory DC compatible with client area DC.*/
205 HdcStrech
= CreateCompatibleDC(desktopHdc
);
207 /* Create a bitmap compatible with the client area DC.*/
208 HbmpStrech
= CreateCompatibleBitmap(
213 /* Select our bitmap in memory DC and save the old one.*/
214 hOld
= SelectObject (HdcStrech
, HbmpStrech
);
216 /* Paint the screen bitmap to our in memory DC */
228 /* Draw the mouse pointer in the right position */
231 pMouse
.x
- iinfo
.xHotspot
, // - 10,
232 pMouse
.y
- iinfo
.yHotspot
, // - 10,
235 Width
= (R
.right
- R
.left
);
236 Height
= (R
.bottom
- R
.top
);
238 AppWidth
= (appRect
.right
- appRect
.left
);
239 AppHeight
= (appRect
.bottom
- appRect
.top
);
241 blitAreaWidth
= AppWidth
/ iZoom
;
242 blitAreaHeight
= AppHeight
/ iZoom
;
244 blitAreaX
= (cp
.x
) - (blitAreaWidth
/2);
245 blitAreaY
= (cp
.y
) - (blitAreaHeight
/2);
257 if (blitAreaX
> (Width
- blitAreaWidth
))
259 blitAreaX
= (Width
- blitAreaWidth
);
262 if (blitAreaY
> (Height
- blitAreaHeight
))
264 blitAreaY
= (Height
- blitAreaHeight
);
272 /* Blast the stretched image from memory DC to window DC.*/
284 rop
| NOMIRRORBITMAP
);
289 DeleteObject(iinfo
.hbmMask
);
291 DeleteObject(iinfo
.hbmColor
);
292 SelectObject (HdcStrech
, hOld
);
293 DeleteObject (HbmpStrech
);
294 DeleteDC (HdcStrech
);
295 ReleaseDC(hDesktopWindow
, desktopHdc
);
299 * FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
301 * PURPOSE: Processes messages for the main window.
303 * WM_COMMAND - process the application menu
304 * WM_PAINT - Paint the main window
305 * WM_DESTROY - post a quit message and return
308 LRESULT CALLBACK
WndProc(HWND hWnd
, UINT message
, WPARAM wParam
, LPARAM lParam
)
319 HWND hwnd1
, hwnd2
, hwnd3
;
323 //Get current mouse position
324 GetCursorPos (&pNewMouse
);
327 hwnd1
= GetForegroundWindow ();
328 a
= GetWindowThreadProcessId(hwnd1
, NULL
);
329 b
= GetCurrentThreadId();
330 AttachThreadInput (a
, b
, TRUE
);
333 GetCaretPos( &pNewCaret
);
334 ClientToScreen (hwnd2
, (LPPOINT
) &pNewCaret
);
335 AttachThreadInput (a
, b
, FALSE
);
337 //Get current control focus
339 GetWindowRect (hwnd3
, &controlRect
);
340 pNewFocus
.x
= controlRect
.left
;
341 pNewFocus
.y
= controlRect
.top
;
343 //If mouse has moved ....
344 if (((pMouse
.x
!= pNewMouse
.x
) || (pMouse
.y
!= pNewMouse
.y
)) && bFollowMouse
)
346 //Update to new position
351 else if (((pCaret
.x
!= pNewCaret
.x
) || (pCaret
.y
!= pNewCaret
.y
)) && bFollowCaret
)
353 //Update to new position
358 else if (((pFocus
.x
!= pNewFocus
.x
) || (pFocus
.y
!= pNewFocus
.y
)) && bFollowFocus
)
360 //Update to new position
368 wmId
= LOWORD(wParam
);
369 // Parse the menu selections:
373 DialogBox(hInst
, MAKEINTRESOURCE(IDD_DIALOGOPTIONS
), hWnd
, (DLGPROC
)OptionsProc
);
376 DialogBox(hInst
, MAKEINTRESOURCE(IDD_ABOUTBOX
), hWnd
, (DLGPROC
)AboutProc
);
382 return DefWindowProc(hWnd
, message
, wParam
, lParam
);
387 PAINTSTRUCT PaintStruct
;
389 dc
= BeginPaint(hWnd
, &PaintStruct
);
391 EndPaint(hWnd
, &PaintStruct
);
395 //handle WM_ERASEBKGND by simply returning non-zero because we did all the drawing in WM_PAINT.
398 //Save settings to registry
400 KillTimer (hWnd
, 1);
404 //Load settings from registry
407 //Get the desktop window
408 hDesktopWindow
= GetDesktopWindow();
411 SetTimer (hWnd
, 1, REPAINT_SPEED
, NULL
);
414 return DefWindowProc(hWnd
, message
, wParam
, lParam
);
419 // Message handler for about box.
420 INT_PTR CALLBACK
AboutProc(HWND hDlg
, UINT message
, WPARAM wParam
, LPARAM lParam
)
422 UNREFERENCED_PARAMETER(lParam
);
426 return (INT_PTR
)TRUE
;
429 if (LOWORD(wParam
) == IDOK
|| LOWORD(wParam
) == IDCANCEL
)
431 EndDialog(hDlg
, LOWORD(wParam
));
432 return (INT_PTR
)TRUE
;
436 return (INT_PTR
)FALSE
;
439 // Message handler for options box.
440 INT_PTR CALLBACK
OptionsProc(HWND hDlg
, UINT message
, WPARAM wParam
, LPARAM lParam
)
442 UNREFERENCED_PARAMETER(lParam
);
447 //Add the zoom items....
448 SendDlgItemMessage(hDlg
, IDC_ZOOM
, CB_ADDSTRING
, 0, (LPARAM
)("1"));
449 SendDlgItemMessage(hDlg
, IDC_ZOOM
, CB_ADDSTRING
, 0, (LPARAM
)("2"));
450 SendDlgItemMessage(hDlg
, IDC_ZOOM
, CB_ADDSTRING
, 0, (LPARAM
)("3"));
451 SendDlgItemMessage(hDlg
, IDC_ZOOM
, CB_ADDSTRING
, 0, (LPARAM
)("4"));
452 SendDlgItemMessage(hDlg
, IDC_ZOOM
, CB_ADDSTRING
, 0, (LPARAM
)("5"));
453 SendDlgItemMessage(hDlg
, IDC_ZOOM
, CB_ADDSTRING
, 0, (LPARAM
)("6"));
454 SendDlgItemMessage(hDlg
, IDC_ZOOM
, CB_ADDSTRING
, 0, (LPARAM
)("7"));
455 SendDlgItemMessage(hDlg
, IDC_ZOOM
, CB_ADDSTRING
, 0, (LPARAM
)("8"));
458 SendDlgItemMessage(hDlg
, IDC_ZOOM
, CB_SETCURSEL
, iZoom
- 1, 0);
461 SendDlgItemMessage(hDlg
,IDC_FOLLOWMOUSECHECK
,BM_SETCHECK
, wParam
,0);
464 SendDlgItemMessage(hDlg
,IDC_FOLLOWKEYBOARDCHECK
,BM_SETCHECK
, wParam
,0);
467 SendDlgItemMessage(hDlg
,IDC_FOLLOWTEXTEDITINGCHECK
,BM_SETCHECK
, wParam
,0);
470 SendDlgItemMessage(hDlg
,IDC_INVERTCOLORSCHECK
,BM_SETCHECK
, wParam
,0);
473 SendDlgItemMessage(hDlg
,IDC_STARTMINIMIZEDCHECK
,BM_SETCHECK
, wParam
,0);
476 SendDlgItemMessage(hDlg
,IDC_SHOWMAGNIFIERCHECK
,BM_SETCHECK
, wParam
,0);
478 return (INT_PTR
)TRUE
;
481 switch(LOWORD(wParam
))
485 EndDialog(hDlg
, LOWORD(wParam
));
486 return (INT_PTR
)TRUE
;
488 case IDC_BUTTON_HELP
:
490 MessageBox(hDlg
, TEXT("Magnifier help not available yet!") , TEXT("Help") , MB_OK
);
493 if(HIWORD(wParam
) == CBN_SELCHANGE
)
495 HWND hCombo
= GetDlgItem(hDlg
,IDC_ZOOM
);
497 /* Get index of current selection and the text of that selection. */
498 iZoom
= SendMessage( hCombo
, CB_GETCURSEL
, (WPARAM
) wParam
, (LPARAM
) lParam
) + 1;
500 //Update the magnigier UI
504 case IDC_INVERTCOLORSCHECK
:
505 bInvertColors
= IsDlgButtonChecked (hDlg
, IDC_INVERTCOLORSCHECK
);
508 case IDC_FOLLOWMOUSECHECK
:
509 bFollowMouse
= IsDlgButtonChecked (hDlg
, IDC_FOLLOWMOUSECHECK
);
511 case IDC_FOLLOWKEYBOARDCHECK
:
512 bFollowFocus
= IsDlgButtonChecked (hDlg
, IDC_FOLLOWKEYBOARDCHECK
);
514 case IDC_FOLLOWTEXTEDITINGCHECK
:
515 bFollowCaret
= IsDlgButtonChecked (hDlg
, IDC_FOLLOWTEXTEDITINGCHECK
);
517 case IDC_STARTMINIMIZEDCHECK
:
518 bStartMinimized
= IsDlgButtonChecked (hDlg
, IDC_STARTMINIMIZEDCHECK
);
520 case IDC_SHOWMAGNIFIER
:
521 bShowMagnifier
= IsDlgButtonChecked (hDlg
, IDC_SHOWMAGNIFIERCHECK
);
523 ShowWindow (hMainWnd
, SW_SHOW
);
525 ShowWindow (hMainWnd
, SW_HIDE
);
530 return (INT_PTR
)FALSE
;
533 // Message handler for warning box.
534 INT_PTR CALLBACK
WarningProc(HWND hDlg
, UINT message
, WPARAM wParam
, LPARAM lParam
)
536 UNREFERENCED_PARAMETER(lParam
);
541 return (INT_PTR
)TRUE
;
543 switch(LOWORD(wParam
))
545 case IDC_SHOWWARNINGCHECK
:
546 bShowWarning
= !IsDlgButtonChecked (hDlg
, IDC_SHOWWARNINGCHECK
);
549 if (LOWORD(wParam
) == IDOK
|| LOWORD(wParam
) == IDCANCEL
)
551 EndDialog(hDlg
, LOWORD(wParam
));
552 return (INT_PTR
)TRUE
;
556 return (INT_PTR
)FALSE
;