2 * ReactOS Winhello - Not So Simple Win32 Windowing test
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 /* What do we test with this app?
20 * - Windows and Class creation
22 * - Some font rendering in the Window
38 LRESULT WINAPI
MainWndProc(HWND
, UINT
, WPARAM
, LPARAM
);
40 BOOLEAN bolWM_KEYDOWN
;
43 WinMain(HINSTANCE hInstance
,
44 HINSTANCE hPrevInstance
,
54 wc
.lpszClassName
= "HelloClass";
55 wc
.lpfnWndProc
= MainWndProc
;
56 wc
.style
= CS_VREDRAW
| CS_HREDRAW
;
57 wc
.hInstance
= hInstance
;
58 wc
.hIcon
= LoadIcon(NULL
, (LPCTSTR
)IDI_APPLICATION
);
59 wc
.hCursor
= LoadCursor(NULL
, (LPCTSTR
)IDC_ARROW
);
60 wc
.hbrBackground
= (HBRUSH
)GetStockObject(GRAY_BRUSH
);
61 wc
.lpszMenuName
= NULL
;
64 if (RegisterClass(&wc
) == 0)
66 fprintf(stderr
, "RegisterClass failed (last error 0x%lX)\n",
71 hWnd
= CreateWindow("HelloClass",
73 WS_OVERLAPPEDWINDOW
|WS_HSCROLL
|WS_VSCROLL
,
74 0, //Position; you can use CW_USEDEFAULT, too
84 fprintf(stderr
, "CreateWindow failed (last error 0x%lX)\n",
89 tf
= CreateFontA(14, 0, 0, TA_BASELINE
, FW_NORMAL
, FALSE
, FALSE
, FALSE
,
90 ANSI_CHARSET
, OUT_DEFAULT_PRECIS
, CLIP_DEFAULT_PRECIS
,
91 DEFAULT_QUALITY
, FIXED_PITCH
|FF_DONTCARE
, "Timmons");
93 ShowWindow(hWnd
, nCmdShow
);
95 while(GetMessage(&msg
, NULL
, 0, 0))
97 TranslateMessage(&msg
);
98 DispatchMessage(&msg
);
106 #define CTRLC 1 /* Define our HotKeys */
107 #define ALTF1 2 /* Define our HotKeys */
109 LRESULT CALLBACK
MainWndProc(HWND hWnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
111 PAINTSTRUCT ps
; /* Also used during window drawing */
112 HDC hDC
; /* A device context used for drawing */
113 RECT rc
= {0,0,0,0}, clr
, wir
; /* A rectangle used during drawing */
114 char spr
[100], sir
[100];
115 static HBRUSH hbrWhite
=NULL
, hbrGray
=NULL
, hbrBlack
=NULL
, hbrRed
=NULL
, hbrBlue
=NULL
, hbrYellow
=NULL
;
117 /* The window handle for the "Click Me" button. */
118 static HWND hwndButton
= 0;
119 static int cx
, cy
; /* Height and width of our button. */
128 text
[0] = (TCHAR
)wParam
;
134 TextOut(hDC
, 400, 10, "WM CHAR:", strlen("WM CHAR:"));
139 TextOut(hDC
, 400, 10, "WM_CHAR:", strlen("WM_CHAR:"));
142 TextOut(hDC
, 530, 10, text
, strlen(text
));
145 // Make a line depending on the typed key
150 FillRect(hDC
, &Rect
, hbrWhite
);
154 FillRect(hDC
, &Rect
, hbrRed
);
158 Rect
.right
= 310 +text
[0]*2;
160 HBRUSH hbrCustom
= CreateSolidBrush ( RGB(text
[0], text
[0], text
[0]));
161 FillRect(hDC
, &Rect
, hbrCustom
);
162 DeleteObject ( hbrCustom
);
166 ReleaseDC(hWnd
, hDC
);
175 text
[0] = (TCHAR
)wParam
;
179 /* Write in window */
184 FillRect(hDC
, &Rect
, hbrWhite
);
187 TextOut(hDC
, 400, 30, "WM KEYDOWN:", strlen("WM KEYDOWN:"));
192 TextOut(hDC
, 400, 30, "WM_KEYDOWN:", strlen("WM_KEYDOWN:"));
195 TextOut(hDC
, 530, 30, text
, strlen(text
));
196 ReleaseDC(hWnd
, hDC
);
205 text
[0] = (TCHAR
)wParam
;
209 /* Write in window */
214 FillRect(hDC
, &Rect
, hbrWhite
);
215 TextOut(hDC
, 400, 50, "WM_KEYUP:", strlen("WM_KEYUP:"));
216 TextOut(hDC
, 530, 50, text
, strlen(text
));
217 ReleaseDC(hWnd
, hDC
);
234 FillRect(hDC
, &Rect
, hbrRed
);
240 FillRect(hDC
, &Rect
, hbrBlack
);
242 ReleaseDC(hWnd
, hDC
);
257 FillRect(hDC
, &Rect
, hbrRed
);
263 FillRect(hDC
, &Rect
, hbrGray
);
265 ReleaseDC(hWnd
, hDC
);
280 FillRect(hDC
, &Rect
, hbrBlue
);
286 FillRect(hDC
, &Rect
, hbrBlack
);
288 ReleaseDC(hWnd
, hDC
);
303 FillRect(hDC
, &Rect
, hbrBlue
);
309 FillRect(hDC
, &Rect
, hbrGray
);
311 ReleaseDC(hWnd
, hDC
);
326 FillRect(hDC
, &Rect
, hbrYellow
);
332 FillRect(hDC
, &Rect
, hbrBlack
);
334 ReleaseDC(hWnd
, hDC
);
349 FillRect(hDC
, &Rect
, hbrYellow
);
355 FillRect(hDC
, &Rect
, hbrGray
);
357 ReleaseDC(hWnd
, hDC
);
371 fwKeys
= wParam
; // key flags
372 x
= LOWORD(lParam
); // horizontal position of cursor
373 y
= HIWORD(lParam
); // vertical position of cursor
379 FillRect(hDC
, &Rect
, hbrWhite
);
381 temp
= _sntprintf ( text
, sizeof(text
)/sizeof(*text
), _T("x: %d"), x
);
382 TextOut(hDC
,10,100,text
,strlen(text
));
383 temp
= _sntprintf ( text
, sizeof(text
)/sizeof(*text
), _T("y: %d"), y
);
384 TextOut(hDC
,10,120,text
,strlen(text
));
394 TextOut(hDC
,10,140,"Control",strlen("Control"));
397 TextOut(hDC
,10,160,"Shift",strlen("Shift"));
400 TextOut(hDC
,10,180,"Left",strlen("Left"));
401 FillRect(hDC
, &Rect
, hbrRed
);
404 TextOut(hDC
,10,200,"Middle",strlen("Middle"));
405 FillRect(hDC
, &Rect
, hbrBlue
);
408 TextOut(hDC
,10,220,"Right",strlen("Right"));
409 FillRect(hDC
, &Rect
, hbrYellow
);
412 ReleaseDC(hWnd
, hDC
);
424 SCROLLINFO Scrollparameter
;
425 nScrollCode
= (int) LOWORD(wParam
); // scroll bar value
426 nPos
= (short int) HIWORD(wParam
); // scroll box position
427 hwndScrollBar
= (HWND
) lParam
; // handle to scroll bar
430 Scrollparameter
.cbSize
= sizeof(Scrollparameter
);
431 Scrollparameter
.fMask
= SIF_ALL
;
432 GetScrollInfo ( hWnd
, SB_HORZ
, &Scrollparameter
);
438 FillRect(hDC
, &Rect
, hbrWhite
);
440 switch ( nScrollCode
)
442 case SB_ENDSCROLL
: //Ends scroll.
443 TextOut(hDC
,200,120,"SB_ENDSCROLL ",16);
444 Scrollparameter
.nPos
= Scrollparameter
.nPos
;
446 case SB_LEFT
: //Scrolls to the upper left.
447 TextOut(hDC
,200,140,"SB_LEFT ",16);
448 Scrollparameter
.nPos
= Scrollparameter
.nMin
;
450 case SB_RIGHT
: //Scrolls to the lower right.
451 TextOut(hDC
,200,160,"SB_RIGHT ",16);
452 Scrollparameter
.nPos
= Scrollparameter
.nMax
;
454 case SB_LINELEFT
: //Scrolls left by one unit.
455 TextOut(hDC
,200,180,"SB_LINELEFT ",16);
456 Scrollparameter
.nPos
--;
458 case SB_LINERIGHT
: //Scrolls right by one unit.
459 TextOut(hDC
,200,200,"SB_LINERIGHT ",16);
460 Scrollparameter
.nPos
++;
462 case SB_PAGELEFT
: //Scrolls left by the width of the window.
463 TextOut(hDC
,200,220,"SB_PAGELEFT ",16);
464 Scrollparameter
.nPos
-= Scrollparameter
.nPage
;
466 case SB_PAGERIGHT
: //Scrolls right by the width of the window.
467 TextOut(hDC
,200,240,"PAGERIGHT ",16);
468 Scrollparameter
.nPos
+= Scrollparameter
.nPage
;
470 case SB_THUMBPOSITION
: //The user has dragged the scroll box (thumb) and released the mouse button. The nPos parameter indicates the position of the scroll box at the end of the drag operation.
471 TextOut(hDC
,200,260,"SB_THUMBPOSITION",16);
472 Scrollparameter
.nPos
= Scrollparameter
.nTrackPos
;
474 case SB_THUMBTRACK
: //
475 TextOut(hDC
,200,280,"SB_THUMBTRACK ",16);
476 Scrollparameter
.nPos
= Scrollparameter
.nTrackPos
;
481 hWnd
, // handle to window with scroll bar
482 SB_HORZ
, // scroll bar flag
483 &Scrollparameter
, // pointer to structure with scroll parameters
486 temp
= _sntprintf ( text
, sizeof(text
)/sizeof(*text
), _T("Horizontal: %d"), Scrollparameter
.nPos
);
487 TextOut(hDC
,200,100,text
,strlen(text
));
488 ReleaseDC(hWnd
, hDC
);
500 SCROLLINFO Scrollparameter
;
501 nScrollCode
= (int) LOWORD(wParam
); // scroll bar value
502 nPos
= (short int) HIWORD(wParam
); // scroll box position
503 hwndScrollBar
= (HWND
) lParam
; // handle to scroll bar
506 Scrollparameter
.cbSize
= sizeof(Scrollparameter
);
507 Scrollparameter
.fMask
= SIF_ALL
;
508 GetScrollInfo ( hWnd
, SB_VERT
, &Scrollparameter
);
514 FillRect(hDC
, &Rect
, hbrWhite
);
516 switch ( nScrollCode
)
518 case SB_ENDSCROLL
: //Ends scroll.
519 TextOut(hDC
,400,120,"SB_ENDSCROLL ",16);
520 Scrollparameter
.nPos
= Scrollparameter
.nPos
;
522 case SB_LEFT
: //Scrolls to the upper left.
523 TextOut(hDC
,400,140,"SB_LEFT ",16);
524 Scrollparameter
.nPos
= Scrollparameter
.nMin
;
526 case SB_RIGHT
: //Scrolls to the lower right.
527 TextOut(hDC
,400,160,"SB_RIGHT ",16);
528 Scrollparameter
.nPos
= Scrollparameter
.nMax
;
530 case SB_LINELEFT
: //Scrolls left by one unit.
531 TextOut(hDC
,400,180,"SB_LINELEFT ",16);
532 Scrollparameter
.nPos
--;
534 case SB_LINERIGHT
: //Scrolls right by one unit.
535 TextOut(hDC
,400,200,"SB_LINERIGHT ",16);
536 Scrollparameter
.nPos
++;
538 case SB_PAGELEFT
: //Scrolls left by the width of the window.
539 TextOut(hDC
,400,220,"SB_PAGELEFT ",16);
540 Scrollparameter
.nPos
-= Scrollparameter
.nPage
;
542 case SB_PAGERIGHT
: //Scrolls right by the width of the window.
543 TextOut(hDC
,400,240,"PAGERIGHT ",16);
544 Scrollparameter
.nPos
+= Scrollparameter
.nPage
;
546 case SB_THUMBPOSITION
: //The user has dragged the scroll box (thumb) and released the mouse button. The nPos parameter indicates the position of the scroll box at the end of the drag operation.
547 TextOut(hDC
,400,260,"SB_THUMBPOSITION",16);
548 Scrollparameter
.nPos
= Scrollparameter
.nTrackPos
;
550 case SB_THUMBTRACK
: //
551 TextOut(hDC
,400,280,"SB_THUMBTRACK ",16);
552 Scrollparameter
.nPos
= Scrollparameter
.nTrackPos
;
557 hWnd
, // handle to window with scroll bar
558 SB_VERT
, // scroll bar flag
559 &Scrollparameter
, // pointer to structure with scroll parameters
562 temp
= _sntprintf ( text
, sizeof(text
)/sizeof(*text
), _T("Vertical: %d"), Scrollparameter
.nPos
);
563 TextOut(hDC
,400,100,text
,strlen(text
));
564 ReleaseDC(hWnd
, hDC
);
572 MessageBox(hWnd
, "You just pressed Ctrl+C", "Hotkey", MB_OK
| MB_ICONINFORMATION
);
575 MessageBox(hWnd
, "You just pressed Ctrl+Alt+F1", "Hotkey", MB_OK
| MB_ICONINFORMATION
);
581 UnregisterHotKey(hWnd
, CTRLC
);
582 UnregisterHotKey(hWnd
, ALTF1
);
584 DeleteObject ( hbrWhite
);
585 DeleteObject ( hbrGray
);
586 DeleteObject ( hbrBlack
);
587 DeleteObject ( hbrRed
);
588 DeleteObject ( hbrBlue
);
589 DeleteObject ( hbrYellow
);
596 /* Register a Ctrl+Alt+C hotkey*/
597 RegisterHotKey(hWnd
, CTRLC
, MOD_CONTROL
, VK_C
);
598 RegisterHotKey(hWnd
, ALTF1
, MOD_CONTROL
| MOD_ALT
, VK_F1
);
600 hbrWhite
= CreateSolidBrush ( RGB(0xFF, 0xFF, 0xFF));
601 hbrGray
= CreateSolidBrush ( RGB(0xAF, 0xAF, 0xAF));
602 hbrBlack
= CreateSolidBrush ( RGB(0x00, 0x00, 0x00));
603 hbrRed
= CreateSolidBrush ( RGB(0xFF, 0x00, 0x00));
604 hbrBlue
= CreateSolidBrush ( RGB(0x00, 0x00, 0xFF));
605 hbrYellow
= CreateSolidBrush ( RGB(0xFF, 0xFF, 0x00));
607 si
.cbSize
= sizeof(si
);
614 SetScrollInfo ( hWnd
, SB_HORZ
, &si
, FALSE
);
615 SetScrollInfo ( hWnd
, SB_VERT
, &si
, FALSE
);
618 /* The window is being created. Create our button
621 /* First we use the system fixed font size to choose
622 * a nice button size. */
624 SelectObject (hDC
, GetStockObject (SYSTEM_FIXED_FONT
));
625 GetTextMetrics (hDC
, &tm
);
626 cx
= tm
.tmAveCharWidth
* 30;
627 cy
= (tm
.tmHeight
+ tm
.tmExternalLeading
) * 2;
628 ReleaseDC (hWnd
, hDC
);
630 /* Now create the button */
631 hwndButton
= CreateWindow (
632 "button", /* Builtin button class */
634 WS_CHILD
| WS_VISIBLE
| BS_PUSHBUTTON
,
636 hWnd
, /* Parent is this window. */
637 (HMENU
) 1, /* Control ID: 1 */
638 ((LPCREATESTRUCT
) lParam
)->hInstance
,
647 hDC
= BeginPaint(hWnd
, &ps
);
648 TextOut(hDC
, 10, 10, "Hello World from ReactOS!",
649 strlen("Hello World from ReactOS!"));
650 TextOut(hDC
, 10, 80, "Press Ctrl+C or Ctrl+Alt+F1 to test Hotkey support.",
651 strlen("Press Ctrl+C or Ctrl+Alt+F1 to test Hotkey support."));
652 GetClientRect(hWnd
, &clr
);
653 GetWindowRect(hWnd
, &wir
);
654 sprintf(spr
, "%lu,%lu,%lu,%lu ", clr
.left
, clr
.top
, clr
.right
, clr
.bottom
);
655 sprintf(sir
, "%lu,%lu,%lu,%lu ", wir
.left
, wir
.top
, wir
.right
, wir
.bottom
);
656 TextOut(hDC
, 10, 30, spr
, 20);
657 TextOut(hDC
, 10, 50, sir
, 20);
659 /* Draw "Hello, World" in the middle of the upper
660 * half of the window. */
661 rc
.bottom
= rc
.bottom
/ 2;
662 DrawText (hDC
, "Hello, World", -1, &rc
,
663 DT_SINGLELINE
| DT_CENTER
| DT_VCENTER
);
665 EndPaint (hWnd
, &ps
);
670 /* The window size is changing. If the button exists
671 * then place it in the center of the bottom half of
674 (wParam
== SIZEFULLSCREEN
||
675 wParam
== SIZENORMAL
)
678 rc
.left
= (LOWORD(lParam
) - cx
) / 2;
679 rc
.top
= HIWORD(lParam
) * 3 / 4 - cy
/ 2;
682 rc
.left
, rc
.top
, cx
, cy
, TRUE
);
687 /* Check the control ID, notification code and
688 * control handle to see if this is a button click
689 * message from our child button. */
690 if (LOWORD(wParam
) == 1 &&
691 HIWORD(wParam
) == BN_CLICKED
&&
692 (HWND
) lParam
== hwndButton
)
694 /* Our button was clicked. Close the window. */
695 DestroyWindow (hWnd
);
701 return DefWindowProc(hWnd
, msg
, wParam
, lParam
);