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 ZeroMemory(&cinfo
, sizeof(CURSORINFO
));
188 ZeroMemory(&iinfo
, sizeof(ICONINFO
));
189 cinfo
.cbSize
= sizeof(cinfo
);
190 GetCursorInfo(&cinfo
);
191 GetIconInfo(cinfo
.hCursor
, &iinfo
);
193 /* Create a memory DC compatible with client area DC.*/
194 HdcStrech
= CreateCompatibleDC(desktopHdc
);
196 /* Create a bitmap compatible with the client area DC.*/
197 HbmpStrech
= CreateCompatibleBitmap(
202 /* Select our bitmap in memory DC and save the old one.*/
203 hOld
= SelectObject (HdcStrech
, HbmpStrech
);
205 /* Paint the screen bitmap to our in memory DC */
217 /* Draw the mouse pointer in the right position */
220 pMouse
.x
- iinfo
.xHotspot
, // - 10,
221 pMouse
.y
- iinfo
.yHotspot
, // - 10,
224 Width
= (R
.right
- R
.left
);
225 Height
= (R
.bottom
- R
.top
);
227 AppWidth
= (appRect
.right
- appRect
.left
);
228 AppHeight
= (appRect
.bottom
- appRect
.top
);
230 blitAreaWidth
= AppWidth
/ iZoom
;
231 blitAreaHeight
= AppHeight
/ iZoom
;
233 blitAreaX
= (cp
.x
) - (blitAreaWidth
/2);
234 blitAreaY
= (cp
.y
) - (blitAreaHeight
/2);
246 if (blitAreaX
> (Width
- blitAreaWidth
))
248 blitAreaX
= (Width
- blitAreaWidth
);
251 if (blitAreaY
> (Height
- blitAreaHeight
))
253 blitAreaY
= (Height
- blitAreaHeight
);
261 /* Blast the stretched image from memory DC to window DC.*/
277 DeleteObject(iinfo
.hbmMask
);
279 DeleteObject(iinfo
.hbmColor
);
280 SelectObject (HdcStrech
, hOld
);
281 DeleteObject (HbmpStrech
);
282 DeleteDC (HdcStrech
);
283 ReleaseDC(hDesktopWindow
, desktopHdc
);
287 // FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
289 // PURPOSE: Processes messages for the main window.
291 // WM_COMMAND - process the application menu
292 // WM_PAINT - Paint the main window
293 // WM_DESTROY - post a quit message and return
296 LRESULT CALLBACK
WndProc(HWND hWnd
, UINT message
, WPARAM wParam
, LPARAM lParam
)
307 HWND hwnd1
, hwnd2
, hwnd3
;
311 //Get current mouse position
312 GetCursorPos (&pNewMouse
);
315 hwnd1
= GetForegroundWindow ();
316 a
= GetWindowThreadProcessId(hwnd1
, NULL
);
317 b
= GetCurrentThreadId();
318 AttachThreadInput (a
, b
, TRUE
);
321 GetCaretPos( &pNewCaret
);
322 ClientToScreen (hwnd2
, (LPPOINT
) &pNewCaret
);
323 AttachThreadInput (a
, b
, FALSE
);
325 //Get current control focus
327 GetWindowRect (hwnd3
, &controlRect
);
328 pNewFocus
.x
= controlRect
.left
;
329 pNewFocus
.y
= controlRect
.top
;
331 //If mouse has moved ....
332 if (((pMouse
.x
!= pNewMouse
.x
) || (pMouse
.y
!= pNewMouse
.y
)) && bFollowMouse
)
334 //Update to new position
339 else if (((pCaret
.x
!= pNewCaret
.x
) || (pCaret
.y
!= pNewCaret
.y
)) && bFollowCaret
)
341 //Update to new position
346 else if (((pFocus
.x
!= pNewFocus
.x
) || (pFocus
.y
!= pNewFocus
.y
)) && bFollowFocus
)
348 //Update to new position
356 wmId
= LOWORD(wParam
);
357 // Parse the menu selections:
361 DialogBox(hInst
, MAKEINTRESOURCE(IDD_DIALOGOPTIONS
), hWnd
, (DLGPROC
)OptionsProc
);
364 DialogBox(hInst
, MAKEINTRESOURCE(IDD_ABOUTBOX
), hWnd
, (DLGPROC
)AboutProc
);
370 return DefWindowProc(hWnd
, message
, wParam
, lParam
);
375 PAINTSTRUCT PaintStruct
;
377 dc
= BeginPaint(hWnd
, &PaintStruct
);
379 EndPaint(hWnd
, &PaintStruct
);
383 //handle WM_ERASEBKGND by simply returning non-zero because we did all the drawing in WM_PAINT.
386 //Save settings to registry
388 KillTimer (hWnd
, 1);
392 //Load settings from registry
395 //Get the desktop window
396 hDesktopWindow
= GetDesktopWindow();
399 SetTimer (hWnd
, 1, REPAINT_SPEED
, NULL
);
402 return DefWindowProc(hWnd
, message
, wParam
, lParam
);
407 // Message handler for about box.
408 INT_PTR CALLBACK
AboutProc(HWND hDlg
, UINT message
, WPARAM wParam
, LPARAM lParam
)
410 UNREFERENCED_PARAMETER(lParam
);
414 return (INT_PTR
)TRUE
;
417 if (LOWORD(wParam
) == IDOK
|| LOWORD(wParam
) == IDCANCEL
)
419 EndDialog(hDlg
, LOWORD(wParam
));
420 return (INT_PTR
)TRUE
;
424 return (INT_PTR
)FALSE
;
427 // Message handler for options box.
428 INT_PTR CALLBACK
OptionsProc(HWND hDlg
, UINT message
, WPARAM wParam
, LPARAM lParam
)
430 UNREFERENCED_PARAMETER(lParam
);
435 //Add the zoom items....
436 SendDlgItemMessage(hDlg
, IDC_ZOOM
, CB_ADDSTRING
, 0, (LPARAM
)("1"));
437 SendDlgItemMessage(hDlg
, IDC_ZOOM
, CB_ADDSTRING
, 0, (LPARAM
)("2"));
438 SendDlgItemMessage(hDlg
, IDC_ZOOM
, CB_ADDSTRING
, 0, (LPARAM
)("3"));
439 SendDlgItemMessage(hDlg
, IDC_ZOOM
, CB_ADDSTRING
, 0, (LPARAM
)("4"));
440 SendDlgItemMessage(hDlg
, IDC_ZOOM
, CB_ADDSTRING
, 0, (LPARAM
)("5"));
441 SendDlgItemMessage(hDlg
, IDC_ZOOM
, CB_ADDSTRING
, 0, (LPARAM
)("6"));
442 SendDlgItemMessage(hDlg
, IDC_ZOOM
, CB_ADDSTRING
, 0, (LPARAM
)("7"));
443 SendDlgItemMessage(hDlg
, IDC_ZOOM
, CB_ADDSTRING
, 0, (LPARAM
)("8"));
446 SendDlgItemMessage(hDlg
, IDC_ZOOM
, CB_SETCURSEL
, iZoom
- 1, 0);
449 SendDlgItemMessage(hDlg
,IDC_FOLLOWMOUSECHECK
,BM_SETCHECK
, wParam
,0);
452 SendDlgItemMessage(hDlg
,IDC_FOLLOWKEYBOARDCHECK
,BM_SETCHECK
, wParam
,0);
455 SendDlgItemMessage(hDlg
,IDC_FOLLOWTEXTEDITINGCHECK
,BM_SETCHECK
, wParam
,0);
458 SendDlgItemMessage(hDlg
,IDC_INVERTCOLORSCHECK
,BM_SETCHECK
, wParam
,0);
461 SendDlgItemMessage(hDlg
,IDC_STARTMINIMIZEDCHECK
,BM_SETCHECK
, wParam
,0);
464 SendDlgItemMessage(hDlg
,IDC_SHOWMAGNIFIERCHECK
,BM_SETCHECK
, wParam
,0);
466 return (INT_PTR
)TRUE
;
469 switch(LOWORD(wParam
))
473 EndDialog(hDlg
, LOWORD(wParam
));
474 return (INT_PTR
)TRUE
;
476 case IDC_BUTTON_HELP
:
478 MessageBox(hDlg
, TEXT("Magnifier help not available yet!") , TEXT("Help") , MB_OK
);
481 if(HIWORD(wParam
) == CBN_SELCHANGE
)
483 HWND hCombo
= GetDlgItem(hDlg
,IDC_ZOOM
);
485 /* Get index of current selection and the text of that selection. */
486 iZoom
= SendMessage( hCombo
, CB_GETCURSEL
, (WPARAM
) wParam
, (LPARAM
) lParam
) + 1;
488 //Update the magnigier UI
492 case IDC_INVERTCOLORSCHECK
:
493 bInvertColors
= IsDlgButtonChecked (hDlg
, IDC_INVERTCOLORSCHECK
);
496 case IDC_FOLLOWMOUSECHECK
:
497 bFollowMouse
= IsDlgButtonChecked (hDlg
, IDC_FOLLOWMOUSECHECK
);
499 case IDC_FOLLOWKEYBOARDCHECK
:
500 bFollowFocus
= IsDlgButtonChecked (hDlg
, IDC_FOLLOWKEYBOARDCHECK
);
502 case IDC_FOLLOWTEXTEDITINGCHECK
:
503 bFollowCaret
= IsDlgButtonChecked (hDlg
, IDC_FOLLOWTEXTEDITINGCHECK
);
505 case IDC_STARTMINIMIZEDCHECK
:
506 bStartMinimized
= IsDlgButtonChecked (hDlg
, IDC_STARTMINIMIZEDCHECK
);
508 case IDC_SHOWMAGNIFIER
:
509 bShowMagnifier
= IsDlgButtonChecked (hDlg
, IDC_SHOWMAGNIFIERCHECK
);
511 ShowWindow (hMainWnd
, SW_SHOW
);
513 ShowWindow (hMainWnd
, SW_HIDE
);
518 return (INT_PTR
)FALSE
;
521 // Message handler for warning box.
522 INT_PTR CALLBACK
WarningProc(HWND hDlg
, UINT message
, WPARAM wParam
, LPARAM lParam
)
524 UNREFERENCED_PARAMETER(lParam
);
529 return (INT_PTR
)TRUE
;
531 switch(LOWORD(wParam
))
533 case IDC_SHOWWARNINGCHECK
:
534 bShowWarning
= !IsDlgButtonChecked (hDlg
, IDC_SHOWWARNINGCHECK
);
537 if (LOWORD(wParam
) == IDOK
|| LOWORD(wParam
) == IDCANCEL
)
539 EndDialog(hDlg
, LOWORD(wParam
));
540 return (INT_PTR
)TRUE
;
544 return (INT_PTR
)FALSE
;