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
34 LRESULT WINAPI
MainWndProc(HWND
, UINT
, WPARAM
, LPARAM
);
36 BOOLEAN bolWM_KEYDOWN
;
39 WinMain(HINSTANCE hInstance
,
40 HINSTANCE hPrevInstance
,
50 wc
.lpszClassName
= "HelloClass";
51 wc
.lpfnWndProc
= MainWndProc
;
52 wc
.style
= CS_VREDRAW
| CS_HREDRAW
;
53 wc
.hInstance
= hInstance
;
54 wc
.hIcon
= LoadIcon(NULL
, (LPCTSTR
)IDI_APPLICATION
);
55 wc
.hCursor
= LoadCursor(NULL
, (LPCTSTR
)IDC_ARROW
);
56 wc
.hbrBackground
= (HBRUSH
)GetStockObject(GRAY_BRUSH
);
57 wc
.lpszMenuName
= NULL
;
60 if (RegisterClass(&wc
) == 0)
62 fprintf(stderr
, "RegisterClass failed (last error 0x%lX)\n",
67 hWnd
= CreateWindow("HelloClass",
69 WS_OVERLAPPEDWINDOW
|WS_HSCROLL
|WS_VSCROLL
,
70 0, //Position; you can use CW_USEDEFAULT, too
80 fprintf(stderr
, "CreateWindow failed (last error 0x%lX)\n",
85 tf
= CreateFontA(14, 0, 0, TA_BASELINE
, FW_NORMAL
, FALSE
, FALSE
, FALSE
,
86 ANSI_CHARSET
, OUT_DEFAULT_PRECIS
, CLIP_DEFAULT_PRECIS
,
87 DEFAULT_QUALITY
, FIXED_PITCH
|FF_DONTCARE
, "Timmons");
89 ShowWindow(hWnd
, nCmdShow
);
91 while(GetMessage(&msg
, NULL
, 0, 0))
93 TranslateMessage(&msg
);
94 DispatchMessage(&msg
);
102 #define CTRLC 1 /* Define our HotKeys */
103 #define ALTF1 2 /* Define our HotKeys */
105 LRESULT CALLBACK
MainWndProc(HWND hWnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
107 PAINTSTRUCT ps
; /* Also used during window drawing */
108 HDC hDC
; /* A device context used for drawing */
109 RECT rc
, clr
, wir
; /* A rectangle used during drawing */
110 char spr
[100], sir
[100];
111 static HBRUSH hbrWhite
=NULL
, hbrGray
=NULL
, hbrBlack
=NULL
, hbrRed
=NULL
, hbrBlue
=NULL
, hbrYellow
=NULL
;
113 /* The window handle for the "Click Me" button. */
114 static HWND hwndButton
= 0;
115 static int cx
, cy
; /* Height and width of our button. */
125 text
[0] = (TCHAR
)wParam
;
131 TextOut(hDC
, 400, 10, "WM CHAR:", strlen("WM CHAR:"));
136 TextOut(hDC
, 400, 10, "WM_CHAR:", strlen("WM_CHAR:"));
139 TextOut(hDC
, 530, 10, text
, strlen(text
));
142 // Make a line depending on the typed key
147 FillRect(hDC
, &Rect
, hbrWhite
);
151 FillRect(hDC
, &Rect
, hbrRed
);
155 Rect
.right
= 310 +text
[0]*2;
157 HBRUSH hbrCustom
= CreateSolidBrush ( RGB(text
[0], text
[0], text
[0]));
158 FillRect(hDC
, &Rect
, hbrCustom
);
159 DeleteObject ( hbrCustom
);
163 ReleaseDC(hWnd
, hDC
);
173 text
[0] = (TCHAR
)wParam
;
177 /* Write in window */
182 FillRect(hDC
, &Rect
, hbrWhite
);
185 TextOut(hDC
, 400, 30, "WM KEYDOWN:", strlen("WM KEYDOWN:"));
190 TextOut(hDC
, 400, 30, "WM_KEYDOWN:", strlen("WM_KEYDOWN:"));
193 TextOut(hDC
, 530, 30, text
, strlen(text
));
194 ReleaseDC(hWnd
, hDC
);
204 text
[0] = (TCHAR
)wParam
;
208 /* Write in window */
213 FillRect(hDC
, &Rect
, hbrWhite
);
214 TextOut(hDC
, 400, 50, "WM_KEYUP:", strlen("WM_KEYUP:"));
215 TextOut(hDC
, 530, 50, text
, strlen(text
));
216 ReleaseDC(hWnd
, hDC
);
233 FillRect(hDC
, &Rect
, hbrRed
);
239 FillRect(hDC
, &Rect
, hbrBlack
);
241 ReleaseDC(hWnd
, hDC
);
256 FillRect(hDC
, &Rect
, hbrRed
);
262 FillRect(hDC
, &Rect
, hbrGray
);
264 ReleaseDC(hWnd
, hDC
);
279 FillRect(hDC
, &Rect
, hbrBlue
);
285 FillRect(hDC
, &Rect
, hbrBlack
);
287 ReleaseDC(hWnd
, hDC
);
302 FillRect(hDC
, &Rect
, hbrBlue
);
308 FillRect(hDC
, &Rect
, hbrGray
);
310 ReleaseDC(hWnd
, hDC
);
325 FillRect(hDC
, &Rect
, hbrYellow
);
331 FillRect(hDC
, &Rect
, hbrBlack
);
333 ReleaseDC(hWnd
, hDC
);
348 FillRect(hDC
, &Rect
, hbrYellow
);
354 FillRect(hDC
, &Rect
, hbrGray
);
356 ReleaseDC(hWnd
, hDC
);
370 fwKeys
= wParam
; // key flags
371 x
= LOWORD(lParam
); // horizontal position of cursor
372 y
= HIWORD(lParam
); // vertical position of cursor
378 FillRect(hDC
, &Rect
, hbrWhite
);
380 temp
= _sntprintf ( text
, sizeof(text
)/sizeof(*text
), _T("x: %d"), x
);
381 TextOut(hDC
,10,100,text
,strlen(text
));
382 temp
= _sntprintf ( text
, sizeof(text
)/sizeof(*text
), _T("y: %d"), y
);
383 TextOut(hDC
,10,120,text
,strlen(text
));
393 TextOut(hDC
,10,140,"Control",strlen("Control"));
396 TextOut(hDC
,10,160,"Shift",strlen("Shift"));
399 TextOut(hDC
,10,180,"Left",strlen("Left"));
400 FillRect(hDC
, &Rect
, hbrRed
);
403 TextOut(hDC
,10,200,"Middle",strlen("Middle"));
404 FillRect(hDC
, &Rect
, hbrBlue
);
407 TextOut(hDC
,10,220,"Right",strlen("Right"));
408 FillRect(hDC
, &Rect
, hbrYellow
);
411 ReleaseDC(hWnd
, hDC
);
423 SCROLLINFO Scrollparameter
;
424 nScrollCode
= (int) LOWORD(wParam
); // scroll bar value
425 nPos
= (short int) HIWORD(wParam
); // scroll box position
426 hwndScrollBar
= (HWND
) lParam
; // handle to scroll bar
429 Scrollparameter
.cbSize
= sizeof(Scrollparameter
);
430 Scrollparameter
.fMask
= SIF_ALL
;
431 GetScrollInfo ( hWnd
, SB_HORZ
, &Scrollparameter
);
437 FillRect(hDC
, &Rect
, hbrWhite
);
439 switch ( nScrollCode
)
441 case SB_ENDSCROLL
: //Ends scroll.
442 TextOut(hDC
,200,120,"SB_ENDSCROLL ",16);
443 Scrollparameter
.nPos
= Scrollparameter
.nPos
;
445 case SB_LEFT
: //Scrolls to the upper left.
446 TextOut(hDC
,200,140,"SB_LEFT ",16);
447 Scrollparameter
.nPos
= Scrollparameter
.nMin
;
449 case SB_RIGHT
: //Scrolls to the lower right.
450 TextOut(hDC
,200,160,"SB_RIGHT ",16);
451 Scrollparameter
.nPos
= Scrollparameter
.nMax
;
453 case SB_LINELEFT
: //Scrolls left by one unit.
454 TextOut(hDC
,200,180,"SB_LINELEFT ",16);
455 Scrollparameter
.nPos
--;
457 case SB_LINERIGHT
: //Scrolls right by one unit.
458 TextOut(hDC
,200,200,"SB_LINERIGHT ",16);
459 Scrollparameter
.nPos
++;
461 case SB_PAGELEFT
: //Scrolls left by the width of the window.
462 TextOut(hDC
,200,220,"SB_PAGELEFT ",16);
463 Scrollparameter
.nPos
-= Scrollparameter
.nPage
;
465 case SB_PAGERIGHT
: //Scrolls right by the width of the window.
466 TextOut(hDC
,200,240,"PAGERIGHT ",16);
467 Scrollparameter
.nPos
+= Scrollparameter
.nPage
;
469 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.
470 TextOut(hDC
,200,260,"SB_THUMBPOSITION",16);
471 Scrollparameter
.nPos
= Scrollparameter
.nTrackPos
;
473 case SB_THUMBTRACK
: //
474 TextOut(hDC
,200,280,"SB_THUMBTRACK ",16);
475 Scrollparameter
.nPos
= Scrollparameter
.nTrackPos
;
480 hWnd
, // handle to window with scroll bar
481 SB_HORZ
, // scroll bar flag
482 &Scrollparameter
, // pointer to structure with scroll parameters
485 temp
= _sntprintf ( text
, sizeof(text
)/sizeof(*text
), _T("Horizontal: %d"), Scrollparameter
.nPos
);
486 TextOut(hDC
,200,100,text
,strlen(text
));
487 ReleaseDC(hWnd
, hDC
);
499 SCROLLINFO Scrollparameter
;
500 nScrollCode
= (int) LOWORD(wParam
); // scroll bar value
501 nPos
= (short int) HIWORD(wParam
); // scroll box position
502 hwndScrollBar
= (HWND
) lParam
; // handle to scroll bar
505 Scrollparameter
.cbSize
= sizeof(Scrollparameter
);
506 Scrollparameter
.fMask
= SIF_ALL
;
507 GetScrollInfo ( hWnd
, SB_VERT
, &Scrollparameter
);
513 FillRect(hDC
, &Rect
, hbrWhite
);
515 switch ( nScrollCode
)
517 case SB_ENDSCROLL
: //Ends scroll.
518 TextOut(hDC
,400,120,"SB_ENDSCROLL ",16);
519 Scrollparameter
.nPos
= Scrollparameter
.nPos
;
521 case SB_LEFT
: //Scrolls to the upper left.
522 TextOut(hDC
,400,140,"SB_LEFT ",16);
523 Scrollparameter
.nPos
= Scrollparameter
.nMin
;
525 case SB_RIGHT
: //Scrolls to the lower right.
526 TextOut(hDC
,400,160,"SB_RIGHT ",16);
527 Scrollparameter
.nPos
= Scrollparameter
.nMax
;
529 case SB_LINELEFT
: //Scrolls left by one unit.
530 TextOut(hDC
,400,180,"SB_LINELEFT ",16);
531 Scrollparameter
.nPos
--;
533 case SB_LINERIGHT
: //Scrolls right by one unit.
534 TextOut(hDC
,400,200,"SB_LINERIGHT ",16);
535 Scrollparameter
.nPos
++;
537 case SB_PAGELEFT
: //Scrolls left by the width of the window.
538 TextOut(hDC
,400,220,"SB_PAGELEFT ",16);
539 Scrollparameter
.nPos
-= Scrollparameter
.nPage
;
541 case SB_PAGERIGHT
: //Scrolls right by the width of the window.
542 TextOut(hDC
,400,240,"PAGERIGHT ",16);
543 Scrollparameter
.nPos
+= Scrollparameter
.nPage
;
545 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.
546 TextOut(hDC
,400,260,"SB_THUMBPOSITION",16);
547 Scrollparameter
.nPos
= Scrollparameter
.nTrackPos
;
549 case SB_THUMBTRACK
: //
550 TextOut(hDC
,400,280,"SB_THUMBTRACK ",16);
551 Scrollparameter
.nPos
= Scrollparameter
.nTrackPos
;
556 hWnd
, // handle to window with scroll bar
557 SB_VERT
, // scroll bar flag
558 &Scrollparameter
, // pointer to structure with scroll parameters
561 temp
= _sntprintf ( text
, sizeof(text
)/sizeof(*text
), _T("Vertical: %d"), Scrollparameter
.nPos
);
562 TextOut(hDC
,400,100,text
,strlen(text
));
563 ReleaseDC(hWnd
, hDC
);
571 MessageBox(hWnd
, "You just pressed Ctrl+C", "Hotkey", MB_OK
| MB_ICONINFORMATION
);
574 MessageBox(hWnd
, "You just pressed Ctrl+Alt+F1", "Hotkey", MB_OK
| MB_ICONINFORMATION
);
580 UnregisterHotKey(hWnd
, CTRLC
);
581 UnregisterHotKey(hWnd
, ALTF1
);
583 DeleteObject ( hbrWhite
);
584 DeleteObject ( hbrGray
);
585 DeleteObject ( hbrBlack
);
586 DeleteObject ( hbrRed
);
587 DeleteObject ( hbrBlue
);
588 DeleteObject ( hbrYellow
);
593 /* Register a Ctrl+Alt+C hotkey*/
594 RegisterHotKey(hWnd
, CTRLC
, MOD_CONTROL
, VK_C
);
595 RegisterHotKey(hWnd
, ALTF1
, MOD_CONTROL
| MOD_ALT
, VK_F1
);
597 hbrWhite
= CreateSolidBrush ( RGB(0xFF, 0xFF, 0xFF));
598 hbrGray
= CreateSolidBrush ( RGB(0xAF, 0xAF, 0xAF));
599 hbrBlack
= CreateSolidBrush ( RGB(0x00, 0x00, 0x00));
600 hbrRed
= CreateSolidBrush ( RGB(0xFF, 0x00, 0x00));
601 hbrBlue
= CreateSolidBrush ( RGB(0x00, 0x00, 0xFF));
602 hbrYellow
= CreateSolidBrush ( RGB(0xFF, 0xFF, 0x00));
605 si
.cbSize
= sizeof(si
);
612 SetScrollInfo ( hWnd
, SB_HORZ
, &si
, FALSE
);
613 SetScrollInfo ( hWnd
, SB_VERT
, &si
, FALSE
);
616 /* The window is being created. Create our button
620 /* First we use the system fixed font size to choose
621 * a nice button size. */
623 SelectObject (hDC
, GetStockObject (SYSTEM_FIXED_FONT
));
624 GetTextMetrics (hDC
, &tm
);
625 cx
= tm
.tmAveCharWidth
* 30;
626 cy
= (tm
.tmHeight
+ tm
.tmExternalLeading
) * 2;
627 ReleaseDC (hWnd
, hDC
);
629 /* Now create the button */
630 hwndButton
= CreateWindow (
631 "button", /* Builtin button class */
633 WS_CHILD
| WS_VISIBLE
| BS_PUSHBUTTON
,
635 hWnd
, /* Parent is this window. */
636 (HMENU
) 1, /* Control ID: 1 */
637 ((LPCREATESTRUCT
) lParam
)->hInstance
,
646 hDC
= BeginPaint(hWnd
, &ps
);
647 TextOut(hDC
, 10, 10, "Hello World from ReactOS!",
648 strlen("Hello World from ReactOS!"));
649 TextOut(hDC
, 10, 80, "Press Ctrl+C or Ctrl+Alt+F1 to test Hotkey support.",
650 strlen("Press Ctrl+C or Ctrl+Alt+F1 to test Hotkey support."));
651 GetClientRect(hWnd
, &clr
);
652 GetWindowRect(hWnd
, &wir
);
653 sprintf(spr
, "%lu,%lu,%lu,%lu ", clr
.left
, clr
.top
, clr
.right
, clr
.bottom
);
654 sprintf(sir
, "%lu,%lu,%lu,%lu ", wir
.left
, wir
.top
, wir
.right
, wir
.bottom
);
655 TextOut(hDC
, 10, 30, spr
, 20);
656 TextOut(hDC
, 10, 50, sir
, 20);
658 /* Draw "Hello, World" in the middle of the upper
659 * half of the window. */
660 rc
.bottom
= rc
.bottom
/ 2;
661 DrawText (hDC
, "Hello, World", -1, &rc
,
662 DT_SINGLELINE
| DT_CENTER
| DT_VCENTER
);
664 EndPaint (hWnd
, &ps
);
669 /* The window size is changing. If the button exists
670 * then place it in the center of the bottom half of
673 (wParam
== SIZEFULLSCREEN
||
674 wParam
== SIZENORMAL
)
677 rc
.left
= (LOWORD(lParam
) - cx
) / 2;
678 rc
.top
= HIWORD(lParam
) * 3 / 4 - cy
/ 2;
681 rc
.left
, rc
.top
, cx
, cy
, TRUE
);
686 /* Check the control ID, notification code and
687 * control handle to see if this is a button click
688 * message from our child button. */
689 if (LOWORD(wParam
) == 1 &&
690 HIWORD(wParam
) == BN_CLICKED
&&
691 (HWND
) lParam
== hwndButton
)
693 /* Our button was clicked. Close the window. */
694 DestroyWindow (hWnd
);
700 return DefWindowProc(hWnd
, msg
, wParam
, lParam
);