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 // Initialize global strings
53 LoadString(hInstance
, IDS_APP_TITLE
, szTitle
, MAX_LOADSTRING
);
54 MyRegisterClass(hInstance
);
56 // Perform application initialization:
57 if (!InitInstance (hInstance
, nCmdShow
))
62 hAccelTable
= LoadAccelerators(hInstance
, MAKEINTRESOURCE(IDC_MAGNIFIER
));
65 while (GetMessage(&msg
, NULL
, 0, 0))
67 if (!TranslateAccelerator(msg
.hwnd
, hAccelTable
, &msg
))
69 TranslateMessage(&msg
);
70 DispatchMessage(&msg
);
74 return (int) msg
.wParam
;
80 * FUNCTION: MyRegisterClass()
82 * PURPOSE: Registers the window class.
86 * This function and its usage are only necessary if you want this code
87 * to be compatible with Win32 systems prior to the 'RegisterClassEx'
88 * function that was added to Windows 95. It is important to call this function
89 * so that the application will get 'well formed' small icons associated
92 ATOM
MyRegisterClass(HINSTANCE hInstance
)
96 wc
.style
= CS_HREDRAW
| CS_VREDRAW
;
97 wc
.lpfnWndProc
= WndProc
;
100 wc
.hInstance
= hInstance
;
101 wc
.hIcon
= LoadIcon(hInstance
, MAKEINTRESOURCE(IDI_ICON
));
102 wc
.hCursor
= LoadCursor(NULL
, IDC_ARROW
);
103 wc
.hbrBackground
= (HBRUSH
)(COLOR_WINDOW
+1);
104 wc
.lpszMenuName
= MAKEINTRESOURCE(IDC_MAGNIFIER
);
105 wc
.lpszClassName
= szWindowClass
;
107 return RegisterClass(&wc
);
111 * FUNCTION: InitInstance(HINSTANCE, int)
113 * PURPOSE: Saves instance handle and creates main window
117 * In this function, we save the instance handle in a global variable and
118 * create and display the main program window.
120 BOOL
InitInstance(HINSTANCE hInstance
, int nCmdShow
)
123 hInst
= hInstance
; // Store instance handle in our global variable
125 SystemParametersInfo(SPI_GETWORKAREA
, 0, &rcWorkArea
, 0);
127 /* Create the Window */
128 hMainWnd
= CreateWindowEx(
129 WS_EX_TOPMOST
| WS_EX_PALETTEWINDOW
,
135 (rcWorkArea
.right
- rcWorkArea
.left
) * 2 / 3,
147 ShowWindow(hMainWnd
, (bStartMinimized
) ? SW_MINIMIZE
: nCmdShow
);
148 UpdateWindow(hMainWnd
);
152 DialogBox (hInstance
, MAKEINTRESOURCE(IDD_WARNINGDIALOG
), hMainWnd
, (DLGPROC
)WarningProc
);
160 if (!IsIconic(hMainWnd
))
162 // Invalidate the client area forcing a WM_PAINT message
163 InvalidateRgn(hMainWnd
, NULL
, TRUE
);
169 HDC desktopHdc
= NULL
;
180 int Width
, Height
, AppWidth
, AppHeight
;
181 LONG blitAreaWidth
, blitAreaHeight
, blitAreaX
, blitAreaY
;
183 desktopHdc
= GetWindowDC (hDesktopWindow
);
185 GetClientRect(hMainWnd
, &appRect
);
186 GetWindowRect(hDesktopWindow
, &R
);
188 ZeroMemory(&cinfo
, sizeof(CURSORINFO
));
189 ZeroMemory(&iinfo
, sizeof(ICONINFO
));
190 cinfo
.cbSize
= sizeof(cinfo
);
191 GetCursorInfo(&cinfo
);
192 GetIconInfo(cinfo
.hCursor
, &iinfo
);
194 /* Create a memory DC compatible with client area DC.*/
195 HdcStrech
= CreateCompatibleDC(desktopHdc
);
197 /* Create a bitmap compatible with the client area DC.*/
198 HbmpStrech
= CreateCompatibleBitmap(
203 /* Select our bitmap in memory DC and save the old one.*/
204 hOld
= SelectObject (HdcStrech
, HbmpStrech
);
206 /* Paint the screen bitmap to our in memory DC */
218 /* Draw the mouse pointer in the right position */
221 pMouse
.x
- iinfo
.xHotspot
, // - 10,
222 pMouse
.y
- iinfo
.yHotspot
, // - 10,
225 Width
= (R
.right
- R
.left
);
226 Height
= (R
.bottom
- R
.top
);
228 AppWidth
= (appRect
.right
- appRect
.left
);
229 AppHeight
= (appRect
.bottom
- appRect
.top
);
231 blitAreaWidth
= AppWidth
/ iZoom
;
232 blitAreaHeight
= AppHeight
/ iZoom
;
234 blitAreaX
= (cp
.x
) - (blitAreaWidth
/2);
235 blitAreaY
= (cp
.y
) - (blitAreaHeight
/2);
247 if (blitAreaX
> (Width
- blitAreaWidth
))
249 blitAreaX
= (Width
- blitAreaWidth
);
252 if (blitAreaY
> (Height
- blitAreaHeight
))
254 blitAreaY
= (Height
- blitAreaHeight
);
262 /* Blast the stretched image from memory DC to window DC.*/
278 DeleteObject(iinfo
.hbmMask
);
280 DeleteObject(iinfo
.hbmColor
);
281 SelectObject (HdcStrech
, hOld
);
282 DeleteObject (HbmpStrech
);
283 DeleteDC (HdcStrech
);
284 ReleaseDC(hDesktopWindow
, desktopHdc
);
288 * FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
290 * PURPOSE: Processes messages for the main window.
292 * WM_COMMAND - process the application menu
293 * WM_PAINT - Paint the main window
294 * WM_DESTROY - post a quit message and return
297 LRESULT CALLBACK
WndProc(HWND hWnd
, UINT message
, WPARAM wParam
, LPARAM lParam
)
308 HWND hwnd1
, hwnd2
, hwnd3
;
312 //Get current mouse position
313 GetCursorPos (&pNewMouse
);
316 hwnd1
= GetForegroundWindow ();
317 a
= GetWindowThreadProcessId(hwnd1
, NULL
);
318 b
= GetCurrentThreadId();
319 AttachThreadInput (a
, b
, TRUE
);
322 GetCaretPos( &pNewCaret
);
323 ClientToScreen (hwnd2
, (LPPOINT
) &pNewCaret
);
324 AttachThreadInput (a
, b
, FALSE
);
326 //Get current control focus
328 GetWindowRect (hwnd3
, &controlRect
);
329 pNewFocus
.x
= controlRect
.left
;
330 pNewFocus
.y
= controlRect
.top
;
332 //If mouse has moved ....
333 if (((pMouse
.x
!= pNewMouse
.x
) || (pMouse
.y
!= pNewMouse
.y
)) && bFollowMouse
)
335 //Update to new position
340 else if (((pCaret
.x
!= pNewCaret
.x
) || (pCaret
.y
!= pNewCaret
.y
)) && bFollowCaret
)
342 //Update to new position
347 else if (((pFocus
.x
!= pNewFocus
.x
) || (pFocus
.y
!= pNewFocus
.y
)) && bFollowFocus
)
349 //Update to new position
357 wmId
= LOWORD(wParam
);
358 // Parse the menu selections:
362 DialogBox(hInst
, MAKEINTRESOURCE(IDD_DIALOGOPTIONS
), hWnd
, (DLGPROC
)OptionsProc
);
365 DialogBox(hInst
, MAKEINTRESOURCE(IDD_ABOUTBOX
), hWnd
, (DLGPROC
)AboutProc
);
371 return DefWindowProc(hWnd
, message
, wParam
, lParam
);
376 PAINTSTRUCT PaintStruct
;
378 dc
= BeginPaint(hWnd
, &PaintStruct
);
380 EndPaint(hWnd
, &PaintStruct
);
384 //handle WM_ERASEBKGND by simply returning non-zero because we did all the drawing in WM_PAINT.
387 //Save settings to registry
389 KillTimer (hWnd
, 1);
393 //Load settings from registry
396 //Get the desktop window
397 hDesktopWindow
= GetDesktopWindow();
400 SetTimer (hWnd
, 1, REPAINT_SPEED
, NULL
);
403 return DefWindowProc(hWnd
, message
, wParam
, lParam
);
408 // Message handler for about box.
409 INT_PTR CALLBACK
AboutProc(HWND hDlg
, UINT message
, WPARAM wParam
, LPARAM lParam
)
411 UNREFERENCED_PARAMETER(lParam
);
415 return (INT_PTR
)TRUE
;
418 if (LOWORD(wParam
) == IDOK
|| LOWORD(wParam
) == IDCANCEL
)
420 EndDialog(hDlg
, LOWORD(wParam
));
421 return (INT_PTR
)TRUE
;
425 return (INT_PTR
)FALSE
;
428 // Message handler for options box.
429 INT_PTR CALLBACK
OptionsProc(HWND hDlg
, UINT message
, WPARAM wParam
, LPARAM lParam
)
431 UNREFERENCED_PARAMETER(lParam
);
436 //Add the zoom items....
437 SendDlgItemMessage(hDlg
, IDC_ZOOM
, CB_ADDSTRING
, 0, (LPARAM
)("1"));
438 SendDlgItemMessage(hDlg
, IDC_ZOOM
, CB_ADDSTRING
, 0, (LPARAM
)("2"));
439 SendDlgItemMessage(hDlg
, IDC_ZOOM
, CB_ADDSTRING
, 0, (LPARAM
)("3"));
440 SendDlgItemMessage(hDlg
, IDC_ZOOM
, CB_ADDSTRING
, 0, (LPARAM
)("4"));
441 SendDlgItemMessage(hDlg
, IDC_ZOOM
, CB_ADDSTRING
, 0, (LPARAM
)("5"));
442 SendDlgItemMessage(hDlg
, IDC_ZOOM
, CB_ADDSTRING
, 0, (LPARAM
)("6"));
443 SendDlgItemMessage(hDlg
, IDC_ZOOM
, CB_ADDSTRING
, 0, (LPARAM
)("7"));
444 SendDlgItemMessage(hDlg
, IDC_ZOOM
, CB_ADDSTRING
, 0, (LPARAM
)("8"));
447 SendDlgItemMessage(hDlg
, IDC_ZOOM
, CB_SETCURSEL
, iZoom
- 1, 0);
450 SendDlgItemMessage(hDlg
,IDC_FOLLOWMOUSECHECK
,BM_SETCHECK
, wParam
,0);
453 SendDlgItemMessage(hDlg
,IDC_FOLLOWKEYBOARDCHECK
,BM_SETCHECK
, wParam
,0);
456 SendDlgItemMessage(hDlg
,IDC_FOLLOWTEXTEDITINGCHECK
,BM_SETCHECK
, wParam
,0);
459 SendDlgItemMessage(hDlg
,IDC_INVERTCOLORSCHECK
,BM_SETCHECK
, wParam
,0);
462 SendDlgItemMessage(hDlg
,IDC_STARTMINIMIZEDCHECK
,BM_SETCHECK
, wParam
,0);
465 SendDlgItemMessage(hDlg
,IDC_SHOWMAGNIFIERCHECK
,BM_SETCHECK
, wParam
,0);
467 return (INT_PTR
)TRUE
;
470 switch(LOWORD(wParam
))
474 EndDialog(hDlg
, LOWORD(wParam
));
475 return (INT_PTR
)TRUE
;
477 case IDC_BUTTON_HELP
:
479 MessageBox(hDlg
, TEXT("Magnifier help not available yet!") , TEXT("Help") , MB_OK
);
482 if(HIWORD(wParam
) == CBN_SELCHANGE
)
484 HWND hCombo
= GetDlgItem(hDlg
,IDC_ZOOM
);
486 /* Get index of current selection and the text of that selection. */
487 iZoom
= SendMessage( hCombo
, CB_GETCURSEL
, (WPARAM
) wParam
, (LPARAM
) lParam
) + 1;
489 //Update the magnigier UI
493 case IDC_INVERTCOLORSCHECK
:
494 bInvertColors
= IsDlgButtonChecked (hDlg
, IDC_INVERTCOLORSCHECK
);
497 case IDC_FOLLOWMOUSECHECK
:
498 bFollowMouse
= IsDlgButtonChecked (hDlg
, IDC_FOLLOWMOUSECHECK
);
500 case IDC_FOLLOWKEYBOARDCHECK
:
501 bFollowFocus
= IsDlgButtonChecked (hDlg
, IDC_FOLLOWKEYBOARDCHECK
);
503 case IDC_FOLLOWTEXTEDITINGCHECK
:
504 bFollowCaret
= IsDlgButtonChecked (hDlg
, IDC_FOLLOWTEXTEDITINGCHECK
);
506 case IDC_STARTMINIMIZEDCHECK
:
507 bStartMinimized
= IsDlgButtonChecked (hDlg
, IDC_STARTMINIMIZEDCHECK
);
509 case IDC_SHOWMAGNIFIER
:
510 bShowMagnifier
= IsDlgButtonChecked (hDlg
, IDC_SHOWMAGNIFIERCHECK
);
512 ShowWindow (hMainWnd
, SW_SHOW
);
514 ShowWindow (hMainWnd
, SW_HIDE
);
519 return (INT_PTR
)FALSE
;
522 // Message handler for warning box.
523 INT_PTR CALLBACK
WarningProc(HWND hDlg
, UINT message
, WPARAM wParam
, LPARAM lParam
)
525 UNREFERENCED_PARAMETER(lParam
);
530 return (INT_PTR
)TRUE
;
532 switch(LOWORD(wParam
))
534 case IDC_SHOWWARNINGCHECK
:
535 bShowWarning
= !IsDlgButtonChecked (hDlg
, IDC_SHOWWARNINGCHECK
);
538 if (LOWORD(wParam
) == IDOK
|| LOWORD(wParam
) == IDCANCEL
)
540 EndDialog(hDlg
, LOWORD(wParam
));
541 return (INT_PTR
)TRUE
;
545 return (INT_PTR
)FALSE
;