diff --git a/CMakeLists.txt b/CMakeLists.txt index 7fa3545..b6afd7f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,10 @@ cmake_minimum_required(VERSION 3.12) project(petzold_pw5e C) - +# +# NOTE: +# Microsft Visual Studio requires MFC to be installed to compile .rc resources. +# +# set(CMAKE_C_STANDARD 11) set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DSTRICT -DCMAKE_GENERATOR_PLATFORM=x64") @@ -123,32 +127,75 @@ target_link_libraries(Head Comctl32.lib) add_executable(OwnDraw WIN32 "Chapter 09 Child Window Controls/OwnDraw/OwnDraw.c") add_executable(PopPad1 WIN32 "Chapter 09 Child Window Controls/PopPad1/PopPad1.c") +add_executable(IconDemo WIN32 "Chapter 10 Menus and Other Resources/IconDemo/IconDemo.c" + "Chapter 10 Menus and Other Resources/IconDemo/Resource.h" + "Chapter 10 Menus and Other Resources/IconDemo/IconDemo.rc" + "Chapter 10 Menus and Other Resources/IconDemo/IconDemo.ico") -add_executable(IconDemo WIN32 "Chapter 10 Menus and Other Resources/IconDemo/IconDemo.c") +add_executable(MenuDemo WIN32 "Chapter 10 Menus and Other Resources/MenuDemo/MenuDemo.c" + "Chapter 10 Menus and Other Resources/MenuDemo/Resource.h" + "Chapter 10 Menus and Other Resources/MenuDemo/MenuDemo.rc") -# TODO: GetWindowLongPtr() -# add_executable(MenuDemo WIN32 "Chapter 10 Menus and Other Resources/MenuDemo/MenuDemo.c") -# add_executable(NoPopups WIN32 "Chapter 10 Menus and Other Resources/NoPopups/NoPopups.c") -# add_executable(PopMenu WIN32 "Chapter 10 Menus and Other Resources/PopMenu/PopMenu.c") +add_executable(NoPopups WIN32 "Chapter 10 Menus and Other Resources/NoPopups/NoPopups.c" + "Chapter 10 Menus and Other Resources/NoPopups/Resource.h" + "Chapter 10 Menus and Other Resources/NoPopups/NoPopups.rc") -add_executable(PoePoem WIN32 "Chapter 10 Menus and Other Resources/PoePoem/PoePoem.c") -add_executable(PoorMenu WIN32 "Chapter 10 Menus and Other Resources/PoorMenu/PoorMenu.c") -add_executable(PopPad2 WIN32 "Chapter 10 Menus and Other Resources/PopPad2/PopPad2.c") -add_executable(About1 WIN32 "Chapter 11 Dialog Boxes/About1/About1.c") -add_executable(About2 WIN32 "Chapter 11 Dialog Boxes/About2/About2.c") -add_executable(About3 WIN32 "Chapter 11 Dialog Boxes/About3/About3.c") +add_executable(PopMenu WIN32 "Chapter 10 Menus and Other Resources/PopMenu/PopMenu.c" + "Chapter 10 Menus and Other Resources/PopMenu/Resource.h" + "Chapter 10 Menus and Other Resources/PopMenu/PopMenu.rc") -# TODO: GetWindowLongPtr() -# add_executable(Colors2 WIN32 "Chapter 11 Dialog Boxes/Colors2/Colors2.c") +add_executable(PoePoem WIN32 "Chapter 10 Menus and Other Resources/PoePoem/PoePoem.c" + "Chapter 10 Menus and Other Resources/PoePoem/Resource.h" + "Chapter 10 Menus and Other Resources/PoePoem/PoePoem.rc" + "Chapter 10 Menus and Other Resources/PoePoem/PoePoem.ico" + "Chapter 10 Menus and Other Resources/PoePoem/PoePoem.txt") -add_executable(Colors3 WIN32 "Chapter 11 Dialog Boxes/Colors3/Colors3.c") -add_executable(HexCalc WIN32 "Chapter 11 Dialog Boxes/HexCalc/HexCalc.c") +add_executable(PoorMenu WIN32 "Chapter 10 Menus and Other Resources/PoorMenu/PoorMenu.c") -add_executable(PopPad WIN32 "Chapter 11 Dialog Boxes/PopPad3/PopPad.c" - "Chapter 11 Dialog Boxes/PopPad3/PopFile.c" - "Chapter 11 Dialog Boxes/PopPad3/PopFind.c" - "Chapter 11 Dialog Boxes/PopPad3/PopFont.c" - "Chapter 11 Dialog Boxes/PopPad3/PopPrntNull.c") +add_executable(PopPad2 WIN32 "Chapter 10 Menus and Other Resources/PopPad2/PopPad2.c" + "Chapter 10 Menus and Other Resources/PopMenu/Resource.h" + "Chapter 10 Menus and Other Resources/PopMenu/PopMenu.rc") + +add_executable(About1 WIN32 "Chapter 11 Dialog Boxes/About1/About1.c" + "Chapter 11 Dialog Boxes/About1/Resource.h" + "Chapter 11 Dialog Boxes/About1/About1.rc" + "Chapter 11 Dialog Boxes/About1/About1.ico") + +add_executable(About2 WIN32 "Chapter 11 Dialog Boxes/About2/About2.c" + "Chapter 11 Dialog Boxes/About2/Resource.h" + "Chapter 11 Dialog Boxes/About2/About2.rc" + "Chapter 11 Dialog Boxes/About2/About2.ico") + +add_executable(About3 WIN32 "Chapter 11 Dialog Boxes/About3/About3.c" + "Chapter 11 Dialog Boxes/About3/Resource.h" + "Chapter 11 Dialog Boxes/About3/About3.rc" + "Chapter 11 Dialog Boxes/About3/About3.ico") + +add_executable(Colors2 WIN32 "Chapter 11 Dialog Boxes/Colors2/Colors2.c" + "Chapter 11 Dialog Boxes/Colors2/Resource.h" + "Chapter 11 Dialog Boxes/Colors2/Colors2.rc") + +add_executable(Colors3 WIN32 "Chapter 11 Dialog Boxes/Colors3/Colors3.c" + "Chapter 11 Dialog Boxes/HexCalc/Resource.h" + "Chapter 11 Dialog Boxes/HexCalc/HexCalc.rc" + "Chapter 11 Dialog Boxes/HexCalc/HexCalc.dlg" + "Chapter 11 Dialog Boxes/HexCalc/HexCalc.ico") + +add_executable(HexCalc WIN32 "Chapter 11 Dialog Boxes/HexCalc/HexCalc.c" + "Chapter 11 Dialog Boxes/HexCalc/Resource.h" + "Chapter 11 Dialog Boxes/HexCalc/HexCalc.rc" + "Chapter 11 Dialog Boxes/HexCalc/HexCalc.dlg" + "Chapter 11 Dialog Boxes/HexCalc/HexCalc.ico") + +add_executable(PopPad WIN32 "Chapter 11 Dialog Boxes/PopPad/PopPad.c" + "Chapter 11 Dialog Boxes/PopPad/PopFile.c" + "Chapter 11 Dialog Boxes/PopPad/PopFind.c" + "Chapter 11 Dialog Boxes/PopPad/PopFont.c" + "Chapter 11 Dialog Boxes/PopPad/PopPrntNull.c" + "Chapter 11 Dialog Boxes/PopPad/PopPad.h" + "Chapter 11 Dialog Boxes/PopPad/Resource.h" + "Chapter 11 Dialog Boxes/PopPad/PopPad.rc" + "Chapter 11 Dialog Boxes/PopPad/PopPad.ico") add_executable(ClipText WIN32 "Chapter 12 The Clipboard/ClipText/ClipText.c") add_executable(ClipView WIN32 "Chapter 12 The Clipboard/ClipView/ClipView.c") @@ -156,10 +203,10 @@ add_executable(DevCaps2 WIN32 "Chapter 13 Using the Printer/DevCaps2/DevCaps2.c" add_executable(FormFeed WIN32 "Chapter 13 Using the Printer/FormFeed/FormFeed.c" "Chapter 13 Using the Printer/FormFeed/GetPrnDC.c") -add_executable(PopPrnt WIN32 "Chapter 11 Dialog Boxes/PopPad3/PopPad.c" - "Chapter 11 Dialog Boxes/PopPad3/PopFile.c" - "Chapter 11 Dialog Boxes/PopPad3/PopFind.c" - "Chapter 11 Dialog Boxes/PopPad3/PopFont.c" +add_executable(PopPrnt WIN32 "Chapter 11 Dialog Boxes/PopPad/PopPad.c" + "Chapter 11 Dialog Boxes/PopPad/PopFile.c" + "Chapter 11 Dialog Boxes/PopPad/PopFind.c" + "Chapter 11 Dialog Boxes/PopPad/PopFont.c" "Chapter 13 Using the Printer/PopPad/PopPrnt.c") add_executable(Print1 WIN32 "Chapter 13 Using the Printer/Print1/Print1.c" @@ -171,6 +218,7 @@ add_executable(Print2 WIN32 "Chapter 13 Using the Printer/Print2/Print2.c" add_executable(Print3 WIN32 "Chapter 13 Using the Printer/Print3/Print3.c" "Chapter 13 Using the Printer/Print1/Print.c" "Chapter 13 Using the Printer/FormFeed/GetPrnDC.c") + add_executable(BitBlt WIN32 "Chapter 14 Bitmaps and Bitblts/BitBlt/BitBlt.c") add_executable(BitMask WIN32 "Chapter 14 Bitmaps and Bitblts/BitMask/BitMask.c") add_executable(Blowup WIN32 "Chapter 14 Bitmaps and Bitblts/Blowup/Blowup.c") @@ -321,9 +369,8 @@ add_executable(MDIDemo WIN32 "Chapter 19 The Multiple-Document Interface/MDIDemo add_executable(BigJob1 WIN32 "Chapter 20 Multitasking and Multithreading/BigJob1/BigJob1.c") add_executable(BigJob2 WIN32 "Chapter 20 Multitasking and Multithreading/BigJob2/BigJob2.c") -# TODO: GetWindowLong() -# add_executable(Multi1 WIN32 "Chapter 20 Multitasking and Multithreading/Multi1/Multi1.c") -# add_executable(Multi2 WIN32 "Chapter 20 Multitasking and Multithreading/Multi2/Multi2.c") +add_executable(Multi1 WIN32 "Chapter 20 Multitasking and Multithreading/Multi1/Multi1.c") +add_executable(Multi2 WIN32 "Chapter 20 Multitasking and Multithreading/Multi2/Multi2.c") add_executable(RndRctMT WIN32 "Chapter 20 Multitasking and Multithreading/RndRectMT/RndRctMT.c") add_executable(EdrTest WIN32 "Chapter 21 Dynamic-Link Libraries/EdrTest/EdrTest.c" @@ -363,8 +410,8 @@ target_link_libraries(SineWaveGen winmm.lib) add_executable(TestMci WIN32 "Chapter 22 Sound and Music/TextMCI/TestMci.c") target_link_libraries(TestMci winmm.lib) -# TODO: GetWindowLong() -# add_executable(WakeUp WIN32 "Chapter 22 Sound and Music/WakeUp/WakeUp.c") +add_executable(WakeUp WIN32 "Chapter 22 Sound and Music/WakeUp/WakeUp.c") +target_link_libraries(WakeUp winmm.lib comctl32.lib) add_executable(NetTime WIN32 "Chapter 23 A Taste of the Internet/NetTime/NetTime.c") target_link_libraries(NetTime Ws2_32.lib) diff --git a/Chapter 07 The Mouse/04 Checker3/Checker3.c b/Chapter 07 The Mouse/04 Checker3/Checker3.c index 7883599..6c37923 100644 --- a/Chapter 07 The Mouse/04 Checker3/Checker3.c +++ b/Chapter 07 The Mouse/04 Checker3/Checker3.c @@ -120,11 +120,11 @@ LRESULT CALLBACK ChildWndProc(HWND hwnd, UINT message, switch (message) { case WM_CREATE: - SetWindowLong(hwnd, 0, 0); // on/off flag + SetWindowLongPtr(hwnd, GWLP_USERDATA, 0); // on/off flag return 0; case WM_LBUTTONDOWN: - SetWindowLong(hwnd, 0, 1 ^ GetWindowLong(hwnd, 0)); + SetWindowLongPtr(hwnd, GWLP_USERDATA, 1 ^ GetWindowLongPtr(hwnd, GWLP_USERDATA)); InvalidateRect(hwnd, NULL, FALSE); return 0; @@ -134,7 +134,7 @@ LRESULT CALLBACK ChildWndProc(HWND hwnd, UINT message, GetClientRect(hwnd, &rect); Rectangle(hdc, 0, 0, rect.right, rect.bottom); - if (GetWindowLong(hwnd, 0)) + if (GetWindowLongPtr(hwnd, GWLP_USERDATA)) { MoveToEx(hdc, 0, 0, NULL); LineTo(hdc, rect.right, rect.bottom); diff --git a/Chapter 07 The Mouse/05 Checker4/Checker4.c b/Chapter 07 The Mouse/05 Checker4/Checker4.c index 2c9e749..c0e1c53 100644 --- a/Chapter 07 The Mouse/05 Checker4/Checker4.c +++ b/Chapter 07 The Mouse/05 Checker4/Checker4.c @@ -152,7 +152,7 @@ LRESULT CALLBACK ChildWndProc(HWND hwnd, UINT message, switch (message) { case WM_CREATE: - SetWindowLong(hwnd, 0, 0); // on/off flag + SetWindowLongPtr(hwnd, GWLP_USERDATA, 0); // on/off flag return 0; case WM_KEYDOWN: @@ -166,7 +166,7 @@ LRESULT CALLBACK ChildWndProc(HWND hwnd, UINT message, // For Return and Space, to toggle the square // falls through case WM_LBUTTONDOWN: - SetWindowLong(hwnd, 0, 1 ^ GetWindowLong(hwnd, 0)); + SetWindowLongPtr(hwnd, GWLP_USERDATA, 1 ^ GetWindowLongPtr(hwnd, GWLP_USERDATA)); SetFocus(hwnd); InvalidateRect(hwnd, NULL, FALSE); return 0; @@ -174,7 +174,7 @@ LRESULT CALLBACK ChildWndProc(HWND hwnd, UINT message, // For focus messages, invalidate the window for repaint case WM_SETFOCUS: - idFocus = GetWindowLong(hwnd, GWL_ID); + idFocus = GetWindowLongPtr(hwnd, GWLP_ID); // Fall through @@ -190,7 +190,7 @@ LRESULT CALLBACK ChildWndProc(HWND hwnd, UINT message, // Draw the "x" mark - if (GetWindowLong(hwnd, 0)) + if (GetWindowLongPtr(hwnd, GWLP_USERDATA)) { MoveToEx(hdc, 0, 0, NULL); LineTo(hdc, rect.right, rect.bottom); diff --git a/Chapter 10 Menus and Other Resources/IconDemo/IconDemo.c b/Chapter 10 Menus and Other Resources/IconDemo/IconDemo.c index 97b03a2..57da019 100644 --- a/Chapter 10 Menus and Other Resources/IconDemo/IconDemo.c +++ b/Chapter 10 Menus and Other Resources/IconDemo/IconDemo.c @@ -7,7 +7,7 @@ #include #include #include -#include "resource.h" +#include "Resource.h" LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); diff --git a/Chapter 10 Menus and Other Resources/MenuDemo/MenuDemo.c b/Chapter 10 Menus and Other Resources/MenuDemo/MenuDemo.c index c30d832..aff2d56 100644 --- a/Chapter 10 Menus and Other Resources/MenuDemo/MenuDemo.c +++ b/Chapter 10 Menus and Other Resources/MenuDemo/MenuDemo.c @@ -1,19 +1,19 @@ /*----------------------------------------- MENUDEMO.C -- Menu Demonstration - (c) Charles Petzold, 1998 + (c) Charles Petzold, 1998 -----------------------------------------*/ #define WIN32_LEAN_AND_MEAN #include #include #include -#include "resource.h" +#include "Resource.h" #define ID_TIMER 1 -LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ; +LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); -TCHAR szAppName[] = TEXT ("MenuDemo") ; +TCHAR szAppName[] = TEXT("MenuDemo"); int WINAPI _tWinMain( _In_ HINSTANCE hInstance, @@ -21,129 +21,134 @@ int WINAPI _tWinMain( _In_ PTSTR pCmdLine, _In_ int nShowCmd) { - HWND hwnd ; - MSG msg ; - WNDCLASS wndclass ; - - wndclass.style = CS_HREDRAW | CS_VREDRAW ; - wndclass.lpfnWndProc = WndProc ; - wndclass.cbClsExtra = 0 ; - wndclass.cbWndExtra = 0 ; - wndclass.hInstance = hInstance ; - wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ; - wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ; - wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ; - wndclass.lpszMenuName = szAppName ; - wndclass.lpszClassName = szAppName ; - - if (!RegisterClass (&wndclass)) - { - MessageBox (NULL, TEXT ("This program requires Windows NT!"), - szAppName, MB_ICONERROR) ; - return 0 ; - } - - hwnd = CreateWindow (szAppName, TEXT ("Menu Demonstration"), - WS_OVERLAPPEDWINDOW, - CW_USEDEFAULT, CW_USEDEFAULT, - CW_USEDEFAULT, CW_USEDEFAULT, - NULL, NULL, hInstance, NULL) ; - - ShowWindow (hwnd, nShowCmd) ; - UpdateWindow (hwnd) ; - - while (GetMessage (&msg, NULL, 0, 0)) - { - TranslateMessage (&msg) ; - DispatchMessage (&msg) ; - } - return (int)msg.wParam; // WM_QUIT + UNREFERENCED_PARAMETER(hPrevInstance); + UNREFERENCED_PARAMETER(pCmdLine); + + HWND hwnd; + MSG msg; + WNDCLASS wndclass; + + wndclass.style = CS_HREDRAW | CS_VREDRAW; + wndclass.lpfnWndProc = WndProc; + wndclass.cbClsExtra = 0; + wndclass.cbWndExtra = 0; + wndclass.hInstance = hInstance; + wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION); + wndclass.hCursor = LoadCursor(NULL, IDC_ARROW); + wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); + wndclass.lpszMenuName = szAppName; + wndclass.lpszClassName = szAppName; + + if (!RegisterClass(&wndclass)) + { + MessageBox(NULL, TEXT("This program requires Windows NT!"), + szAppName, MB_ICONERROR); + return 0; + } + + hwnd = CreateWindow(szAppName, TEXT("Menu Demonstration"), + WS_OVERLAPPEDWINDOW, + CW_USEDEFAULT, CW_USEDEFAULT, + CW_USEDEFAULT, CW_USEDEFAULT, + NULL, NULL, hInstance, NULL); + + ShowWindow(hwnd, nShowCmd); + UpdateWindow(hwnd); + + while (GetMessage(&msg, NULL, 0, 0)) + { + TranslateMessage(&msg); + DispatchMessage(&msg); + } + return (int)msg.wParam; // WM_QUIT } -LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) +LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { - static int idColor [5] = { WHITE_BRUSH, LTGRAY_BRUSH, GRAY_BRUSH, - DKGRAY_BRUSH, BLACK_BRUSH } ; - static int iSelection = IDM_BKGND_WHITE ; - HMENU hMenu ; - - switch (message) - { - case WM_COMMAND: - hMenu = GetMenu (hwnd) ; - - switch (LOWORD (wParam)) - { - case IDM_FILE_NEW: - case IDM_FILE_OPEN: - case IDM_FILE_SAVE: - case IDM_FILE_SAVE_AS: - MessageBeep (0) ; - return 0 ; - - case IDM_APP_EXIT: - SendMessage (hwnd, WM_CLOSE, 0, 0) ; - return 0 ; - - case IDM_EDIT_UNDO: - case IDM_EDIT_CUT: - case IDM_EDIT_COPY: - case IDM_EDIT_PASTE: - case IDM_EDIT_CLEAR: - MessageBeep (0) ; - return 0 ; - - case IDM_BKGND_WHITE: // Note: Logic below - case IDM_BKGND_LTGRAY: // assumes that IDM_WHITE - case IDM_BKGND_GRAY: // through IDM_BLACK are - case IDM_BKGND_DKGRAY: // consecutive numbers in - case IDM_BKGND_BLACK: // the order shown here. - - CheckMenuItem (hMenu, iSelection, MF_UNCHECKED) ; - iSelection = LOWORD (wParam) ; - CheckMenuItem (hMenu, iSelection, MF_CHECKED) ; - - SetClassLong (hwnd, GCL_HBRBACKGROUND, (LONG) - GetStockObject - (idColor [LOWORD (wParam) - IDM_BKGND_WHITE])) ; - - InvalidateRect (hwnd, NULL, TRUE) ; - return 0 ; - - case IDM_TIMER_START: - if (SetTimer (hwnd, ID_TIMER, 1000, NULL)) - { - EnableMenuItem (hMenu, IDM_TIMER_START, MF_GRAYED) ; - EnableMenuItem (hMenu, IDM_TIMER_STOP, MF_ENABLED) ; - } - return 0 ; - - case IDM_TIMER_STOP: - KillTimer (hwnd, ID_TIMER) ; - EnableMenuItem (hMenu, IDM_TIMER_START, MF_ENABLED) ; - EnableMenuItem (hMenu, IDM_TIMER_STOP, MF_GRAYED) ; - return 0 ; - - case IDM_APP_HELP: - MessageBox (hwnd, TEXT ("Help not yet implemented!"), - szAppName, MB_ICONEXCLAMATION | MB_OK) ; - return 0 ; - - case IDM_APP_ABOUT: - MessageBox (hwnd, TEXT ("Menu Demonstration Program\n") - TEXT ("(c) Charles Petzold, 1998"), - szAppName, MB_ICONINFORMATION | MB_OK) ; - return 0 ; - } - break ; - - case WM_TIMER: - MessageBeep (0) ; - return 0 ; - - case WM_DESTROY: - PostQuitMessage (0) ; - return 0 ; - } - return DefWindowProc (hwnd, message, wParam, lParam) ; + static int idColor[5] = { WHITE_BRUSH, LTGRAY_BRUSH, GRAY_BRUSH, + DKGRAY_BRUSH, BLACK_BRUSH }; + static int iSelection = IDM_BKGND_WHITE; + HMENU hMenu; + + switch (message) + { + case WM_COMMAND: + hMenu = GetMenu(hwnd); + int id = GET_WM_COMMAND_ID(wParam, LPARAM); + + switch (id) + { + case IDM_FILE_NEW: + case IDM_FILE_OPEN: + case IDM_FILE_SAVE: + case IDM_FILE_SAVE_AS: + MessageBeep(0); + return 0; + + case IDM_APP_EXIT: + SendMessage(hwnd, WM_CLOSE, 0, 0); + return 0; + + case IDM_EDIT_UNDO: + case IDM_EDIT_CUT: + case IDM_EDIT_COPY: + case IDM_EDIT_PASTE: + case IDM_EDIT_CLEAR: + MessageBeep(0); + return 0; + + case IDM_BKGND_WHITE: // Note: Logic below + case IDM_BKGND_LTGRAY: // assumes that IDM_WHITE + case IDM_BKGND_GRAY: // through IDM_BLACK are + case IDM_BKGND_DKGRAY: // consecutive numbers in + case IDM_BKGND_BLACK: // the order shown here. + + CheckMenuItem(hMenu, iSelection, MF_UNCHECKED); + iSelection = id; + CheckMenuItem(hMenu, iSelection, MF_CHECKED); + + SetClassLongPtr( + hwnd, + GCLP_HBRBACKGROUND, + (LONG_PTR)GetStockObject(idColor[id - IDM_BKGND_WHITE])); + + InvalidateRect(hwnd, NULL, TRUE); + return 0; + + case IDM_TIMER_START: + if (SetTimer(hwnd, ID_TIMER, 1000, NULL)) + { + EnableMenuItem(hMenu, IDM_TIMER_START, MF_GRAYED); + EnableMenuItem(hMenu, IDM_TIMER_STOP, MF_ENABLED); + } + return 0; + + case IDM_TIMER_STOP: + KillTimer(hwnd, ID_TIMER); + EnableMenuItem(hMenu, IDM_TIMER_START, MF_ENABLED); + EnableMenuItem(hMenu, IDM_TIMER_STOP, MF_GRAYED); + return 0; + + case IDM_APP_HELP: + MessageBox(hwnd, TEXT("Help not yet implemented!"), + szAppName, MB_ICONEXCLAMATION | MB_OK); + return 0; + + case IDM_APP_ABOUT: + MessageBox(hwnd, TEXT("Menu Demonstration Program\n") + TEXT("(c) Charles Petzold, 1998"), + szAppName, MB_ICONINFORMATION | MB_OK); + return 0; + } + break; + + case WM_TIMER: + MessageBeep(0); + return 0; + + case WM_DESTROY: + PostQuitMessage(0); + return 0; + } + return DefWindowProc(hwnd, message, wParam, lParam); } diff --git a/Chapter 10 Menus and Other Resources/NoPopups/NoPopups.c b/Chapter 10 Menus and Other Resources/NoPopups/NoPopups.c index 33c7bdb..0eb265e 100644 --- a/Chapter 10 Menus and Other Resources/NoPopups/NoPopups.c +++ b/Chapter 10 Menus and Other Resources/NoPopups/NoPopups.c @@ -1,14 +1,14 @@ /*------------------------------------------------- NOPOPUPS.C -- Demonstrates No-Popup Nested Menu - (c) Charles Petzold, 1998 + (c) Charles Petzold, 1998 -------------------------------------------------*/ #define WIN32_LEAN_AND_MEAN #include #include -#include "resource.h" +#include "Resource.h" -LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ; +LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); int WINAPI _tWinMain( _In_ HINSTANCE hInstance, @@ -16,100 +16,103 @@ int WINAPI _tWinMain( _In_ PTSTR pCmdLine, _In_ int nShowCmd) { - static TCHAR szAppName[] = TEXT ("NoPopUps") ; - HWND hwnd ; - MSG msg ; - WNDCLASS wndclass ; - - wndclass.style = CS_HREDRAW | CS_VREDRAW ; - wndclass.lpfnWndProc = WndProc ; - wndclass.cbClsExtra = 0 ; - wndclass.cbWndExtra = 0 ; - wndclass.hInstance = hInstance ; - wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ; - wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ; - wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ; - wndclass.lpszMenuName = NULL ; - wndclass.lpszClassName = szAppName ; - - if (!RegisterClass (&wndclass)) - { - MessageBox (NULL, TEXT ("This program requires Windows NT!"), - szAppName, MB_ICONERROR) ; - return 0 ; - } - - hwnd = CreateWindow (szAppName, - TEXT ("No-Popup Nested Menu Demonstration"), - WS_OVERLAPPEDWINDOW, - CW_USEDEFAULT, CW_USEDEFAULT, - CW_USEDEFAULT, CW_USEDEFAULT, - NULL, NULL, hInstance, NULL) ; - - ShowWindow (hwnd, nShowCmd) ; - UpdateWindow (hwnd) ; - - while (GetMessage (&msg, NULL, 0, 0)) - { - TranslateMessage (&msg) ; - DispatchMessage (&msg) ; - } - return (int)msg.wParam; // WM_QUIT + UNREFERENCED_PARAMETER(hPrevInstance); + UNREFERENCED_PARAMETER(pCmdLine); + + static TCHAR szAppName[] = TEXT("NoPopUps"); + HWND hwnd; + MSG msg; + WNDCLASS wndclass; + + wndclass.style = CS_HREDRAW | CS_VREDRAW; + wndclass.lpfnWndProc = WndProc; + wndclass.cbClsExtra = 0; + wndclass.cbWndExtra = 0; + wndclass.hInstance = hInstance; + wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION); + wndclass.hCursor = LoadCursor(NULL, IDC_ARROW); + wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); + wndclass.lpszMenuName = NULL; + wndclass.lpszClassName = szAppName; + + if (!RegisterClass(&wndclass)) + { + MessageBox(NULL, TEXT("This program requires Windows NT!"), + szAppName, MB_ICONERROR); + return 0; + } + + hwnd = CreateWindow(szAppName, + TEXT("No-Popup Nested Menu Demonstration"), + WS_OVERLAPPEDWINDOW, + CW_USEDEFAULT, CW_USEDEFAULT, + CW_USEDEFAULT, CW_USEDEFAULT, + NULL, NULL, hInstance, NULL); + + ShowWindow(hwnd, nShowCmd); + UpdateWindow(hwnd); + + while (GetMessage(&msg, NULL, 0, 0)) + { + TranslateMessage(&msg); + DispatchMessage(&msg); + } + return (int)msg.wParam; // WM_QUIT } -LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) +LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { - static HMENU hMenuMain, hMenuEdit, hMenuFile ; - HINSTANCE hInstance ; - - switch (message) - { - case WM_CREATE: - hInstance = (HINSTANCE) GetWindowLong (hwnd, GWL_HINSTANCE) ; - - hMenuMain = LoadMenu (hInstance, TEXT ("MenuMain")) ; - hMenuFile = LoadMenu (hInstance, TEXT ("MenuFile")) ; - hMenuEdit = LoadMenu (hInstance, TEXT ("MenuEdit")) ; - - SetMenu (hwnd, hMenuMain) ; - return 0 ; - - case WM_COMMAND: - switch (LOWORD (wParam)) - { - case IDM_MAIN: - SetMenu (hwnd, hMenuMain) ; - return 0 ; - - case IDM_FILE: - SetMenu (hwnd, hMenuFile) ; - return 0 ; - - case IDM_EDIT: - SetMenu (hwnd, hMenuEdit) ; - return 0 ; - - case IDM_FILE_NEW: - case IDM_FILE_OPEN: - case IDM_FILE_SAVE: - case IDM_FILE_SAVE_AS: - case IDM_EDIT_UNDO: - case IDM_EDIT_CUT: - case IDM_EDIT_COPY: - case IDM_EDIT_PASTE: - case IDM_EDIT_CLEAR: - MessageBeep (0) ; - return 0 ; - } - break ; - - case WM_DESTROY: - SetMenu (hwnd, hMenuMain) ; - DestroyMenu (hMenuFile) ; - DestroyMenu (hMenuEdit) ; - - PostQuitMessage (0) ; - return 0 ; - } - return DefWindowProc (hwnd, message, wParam, lParam) ; + static HMENU hMenuMain, hMenuEdit, hMenuFile; + HINSTANCE hInstance; + + switch (message) + { + case WM_CREATE: + hInstance = (HINSTANCE)GetWindowLongPtr(hwnd, GWLP_HINSTANCE); + + hMenuMain = LoadMenu(hInstance, TEXT("MenuMain")); + hMenuFile = LoadMenu(hInstance, TEXT("MenuFile")); + hMenuEdit = LoadMenu(hInstance, TEXT("MenuEdit")); + + SetMenu(hwnd, hMenuMain); + return 0; + + case WM_COMMAND: + switch (LOWORD(wParam)) + { + case IDM_MAIN: + SetMenu(hwnd, hMenuMain); + return 0; + + case IDM_FILE: + SetMenu(hwnd, hMenuFile); + return 0; + + case IDM_EDIT: + SetMenu(hwnd, hMenuEdit); + return 0; + + case IDM_FILE_NEW: + case IDM_FILE_OPEN: + case IDM_FILE_SAVE: + case IDM_FILE_SAVE_AS: + case IDM_EDIT_UNDO: + case IDM_EDIT_CUT: + case IDM_EDIT_COPY: + case IDM_EDIT_PASTE: + case IDM_EDIT_CLEAR: + MessageBeep(0); + return 0; + } + break; + + case WM_DESTROY: + SetMenu(hwnd, hMenuMain); + DestroyMenu(hMenuFile); + DestroyMenu(hMenuEdit); + + PostQuitMessage(0); + return 0; + } + return DefWindowProc(hwnd, message, wParam, lParam); } diff --git a/Chapter 10 Menus and Other Resources/PoorMenu/PoorMenu.c b/Chapter 10 Menus and Other Resources/PoorMenu/PoorMenu.c index 8691184..1e4643b 100644 --- a/Chapter 10 Menus and Other Resources/PoorMenu/PoorMenu.c +++ b/Chapter 10 Menus and Other Resources/PoorMenu/PoorMenu.c @@ -1,6 +1,6 @@ /*----------------------------------------- POORMENU.C -- The Poor Person's Menu - (c) Charles Petzold, 1998 + (c) Charles Petzold, 1998 -----------------------------------------*/ #define WIN32_LEAN_AND_MEAN @@ -11,9 +11,9 @@ #define IDM_SYS_HELP 2 #define IDM_SYS_REMOVE 3 -LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ; +LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); -static TCHAR szAppName[] = TEXT ("PoorMenu") ; +static TCHAR szAppName[] = TEXT("PoorMenu"); int WINAPI _tWinMain( _In_ HINSTANCE hInstance, @@ -21,80 +21,83 @@ int WINAPI _tWinMain( _In_ PTSTR pCmdLine, _In_ int nShowCmd) { - HMENU hMenu ; - HWND hwnd ; - MSG msg ; - WNDCLASS wndclass ; - - wndclass.style = CS_HREDRAW | CS_VREDRAW ; - wndclass.lpfnWndProc = WndProc ; - wndclass.cbClsExtra = 0 ; - wndclass.cbWndExtra = 0 ; - wndclass.hInstance = hInstance ; - wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ; - wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ; - wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ; - wndclass.lpszMenuName = NULL ; - wndclass.lpszClassName = szAppName ; - - if (!RegisterClass (&wndclass)) - { - MessageBox (NULL, TEXT ("This program requires Windows NT!"), - szAppName, MB_ICONERROR) ; - return 0 ; - } - - hwnd = CreateWindow (szAppName, TEXT ("The Poor-Person's Menu"), - WS_OVERLAPPEDWINDOW, - CW_USEDEFAULT, CW_USEDEFAULT, - CW_USEDEFAULT, CW_USEDEFAULT, - NULL, NULL, hInstance, NULL) ; - - hMenu = GetSystemMenu (hwnd, FALSE) ; - - AppendMenu (hMenu, MF_SEPARATOR, 0, NULL) ; - AppendMenu (hMenu, MF_STRING, IDM_SYS_ABOUT, TEXT ("About...")) ; - AppendMenu (hMenu, MF_STRING, IDM_SYS_HELP, TEXT ("Help...")) ; - AppendMenu (hMenu, MF_STRING, IDM_SYS_REMOVE, TEXT ("Remove Additions")) ; - - ShowWindow (hwnd, nShowCmd) ; - UpdateWindow (hwnd) ; - - while (GetMessage (&msg, NULL, 0, 0)) - { - TranslateMessage (&msg) ; - DispatchMessage (&msg) ; - } - return (int)msg.wParam; // WM_QUIT + UNREFERENCED_PARAMETER(hPrevInstance); + UNREFERENCED_PARAMETER(pCmdLine); + + HMENU hMenu; + HWND hwnd; + MSG msg; + WNDCLASS wndclass; + + wndclass.style = CS_HREDRAW | CS_VREDRAW; + wndclass.lpfnWndProc = WndProc; + wndclass.cbClsExtra = 0; + wndclass.cbWndExtra = 0; + wndclass.hInstance = hInstance; + wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION); + wndclass.hCursor = LoadCursor(NULL, IDC_ARROW); + wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); + wndclass.lpszMenuName = NULL; + wndclass.lpszClassName = szAppName; + + if (!RegisterClass(&wndclass)) + { + MessageBox(NULL, TEXT("This program requires Windows NT!"), + szAppName, MB_ICONERROR); + return 0; + } + + hwnd = CreateWindow(szAppName, TEXT("The Poor-Person's Menu"), + WS_OVERLAPPEDWINDOW, + CW_USEDEFAULT, CW_USEDEFAULT, + CW_USEDEFAULT, CW_USEDEFAULT, + NULL, NULL, hInstance, NULL); + + hMenu = GetSystemMenu(hwnd, FALSE); + + AppendMenu(hMenu, MF_SEPARATOR, 0, NULL); + AppendMenu(hMenu, MF_STRING, IDM_SYS_ABOUT, TEXT("About...")); + AppendMenu(hMenu, MF_STRING, IDM_SYS_HELP, TEXT("Help...")); + AppendMenu(hMenu, MF_STRING, IDM_SYS_REMOVE, TEXT("Remove Additions")); + + ShowWindow(hwnd, nShowCmd); + UpdateWindow(hwnd); + + while (GetMessage(&msg, NULL, 0, 0)) + { + TranslateMessage(&msg); + DispatchMessage(&msg); + } + return (int)msg.wParam; // WM_QUIT } -LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) +LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { - switch (message) - { - case WM_SYSCOMMAND: - switch (LOWORD (wParam)) - { - case IDM_SYS_ABOUT: - MessageBox (hwnd, TEXT ("A Poor-Person's Menu Program\n") - TEXT ("(c) Charles Petzold, 1998"), - szAppName, MB_OK | MB_ICONINFORMATION) ; - return 0 ; - - case IDM_SYS_HELP: - MessageBox (hwnd, TEXT ("Help not yet implemented!"), - szAppName, MB_OK | MB_ICONEXCLAMATION) ; - return 0 ; - - case IDM_SYS_REMOVE: - GetSystemMenu (hwnd, TRUE) ; - return 0 ; - } - break ; - - case WM_DESTROY: - PostQuitMessage (0) ; - return 0 ; - } - return DefWindowProc (hwnd, message, wParam, lParam) ; + switch (message) + { + case WM_SYSCOMMAND: + switch (LOWORD(wParam)) + { + case IDM_SYS_ABOUT: + MessageBox(hwnd, TEXT("A Poor-Person's Menu Program\n") + TEXT("(c) Charles Petzold, 1998"), + szAppName, MB_OK | MB_ICONINFORMATION); + return 0; + + case IDM_SYS_HELP: + MessageBox(hwnd, TEXT("Help not yet implemented!"), + szAppName, MB_OK | MB_ICONEXCLAMATION); + return 0; + + case IDM_SYS_REMOVE: + GetSystemMenu(hwnd, TRUE); + return 0; + } + break; + + case WM_DESTROY: + PostQuitMessage(0); + return 0; + } + return DefWindowProc(hwnd, message, wParam, lParam); } diff --git a/Chapter 10 Menus and Other Resources/PopMenu/PopMenu.c b/Chapter 10 Menus and Other Resources/PopMenu/PopMenu.c index ecd0557..8158108 100644 --- a/Chapter 10 Menus and Other Resources/PopMenu/PopMenu.c +++ b/Chapter 10 Menus and Other Resources/PopMenu/PopMenu.c @@ -6,6 +6,7 @@ #define WIN32_LEAN_AND_MEAN #include #include +#include #include "Resource.h" LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); @@ -70,6 +71,7 @@ LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) DKGRAY_BRUSH, BLACK_BRUSH }; static int iSelection = IDM_BKGND_WHITE; POINT point; + int id; switch (message) { @@ -88,7 +90,8 @@ LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) return 0; case WM_COMMAND: - switch (LOWORD(wParam)) + id = GET_WM_COMMAND_ID(wParam, lParam); + switch (id) { case IDM_FILE_NEW: case IDM_FILE_OPEN: @@ -109,12 +112,12 @@ LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) case IDM_BKGND_BLACK: // the order shown here. CheckMenuItem(hMenu, iSelection, MF_UNCHECKED); - iSelection = LOWORD(wParam); + iSelection = id; CheckMenuItem(hMenu, iSelection, MF_CHECKED); - SetClassLong(hwnd, GCL_HBRBACKGROUND, (LONG) - GetStockObject - (idColor[LOWORD(wParam) - IDM_BKGND_WHITE])); + SetClassLongPtr(hwnd, + GCLP_HBRBACKGROUND, + (LONG_PTR)GetStockObject(idColor[id - IDM_BKGND_WHITE])); InvalidateRect(hwnd, NULL, TRUE); return 0; diff --git a/Chapter 10 Menus and Other Resources/PopPad2/PopPad2.c b/Chapter 10 Menus and Other Resources/PopPad2/PopPad2.c index e964895..29ace20 100644 --- a/Chapter 10 Menus and Other Resources/PopPad2/PopPad2.c +++ b/Chapter 10 Menus and Other Resources/PopPad2/PopPad2.c @@ -1,6 +1,6 @@ /*----------------------------------------------------- POPPAD2.C -- Popup Editor Version 2 (includes menu) - (c) Charles Petzold, 1998 + (c) Charles Petzold, 1998 -----------------------------------------------------*/ #define WIN32_LEAN_AND_MEAN @@ -10,9 +10,9 @@ #define ID_EDIT 1 -LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM); +LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); -TCHAR szAppName[] = TEXT ("PopPad2") ; +TCHAR szAppName[] = TEXT("PopPad2"); int WINAPI _tWinMain( _In_ HINSTANCE hInstance, @@ -20,182 +20,185 @@ int WINAPI _tWinMain( _In_ PTSTR pCmdLine, _In_ int nShowCmd) { - HACCEL hAccel ; - HWND hwnd ; - MSG msg ; - WNDCLASS wndclass ; - - wndclass.style = CS_HREDRAW | CS_VREDRAW ; - wndclass.lpfnWndProc = WndProc ; - wndclass.cbClsExtra = 0 ; - wndclass.cbWndExtra = 0 ; - wndclass.hInstance = hInstance ; - wndclass.hIcon = LoadIcon (hInstance, szAppName) ; - wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ; - wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ; - wndclass.lpszMenuName = szAppName ; - wndclass.lpszClassName = szAppName ; - - if (!RegisterClass (&wndclass)) - { - MessageBox (NULL, TEXT ("This program requires Windows NT!"), - szAppName, MB_ICONERROR) ; - return 0 ; - } - - hwnd = CreateWindow (szAppName, szAppName, - WS_OVERLAPPEDWINDOW, - GetSystemMetrics (SM_CXSCREEN) / 4, - GetSystemMetrics (SM_CYSCREEN) / 4, - GetSystemMetrics (SM_CXSCREEN) / 2, - GetSystemMetrics (SM_CYSCREEN) / 2, - NULL, NULL, hInstance, NULL) ; - - ShowWindow (hwnd, nShowCmd) ; - UpdateWindow (hwnd) ; - - hAccel = LoadAccelerators (hInstance, szAppName) ; - - while (GetMessage (&msg, NULL, 0, 0)) - { - if (!TranslateAccelerator (hwnd, hAccel, &msg)) - { - TranslateMessage (&msg) ; - DispatchMessage (&msg) ; - } - } - return (int)msg.wParam; // WM_QUIT + UNREFERENCED_PARAMETER(hPrevInstance); + UNREFERENCED_PARAMETER(pCmdLine); + + HACCEL hAccel; + HWND hwnd; + MSG msg; + WNDCLASS wndclass; + + wndclass.style = CS_HREDRAW | CS_VREDRAW; + wndclass.lpfnWndProc = WndProc; + wndclass.cbClsExtra = 0; + wndclass.cbWndExtra = 0; + wndclass.hInstance = hInstance; + wndclass.hIcon = LoadIcon(hInstance, szAppName); + wndclass.hCursor = LoadCursor(NULL, IDC_ARROW); + wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); + wndclass.lpszMenuName = szAppName; + wndclass.lpszClassName = szAppName; + + if (!RegisterClass(&wndclass)) + { + MessageBox(NULL, TEXT("This program requires Windows NT!"), + szAppName, MB_ICONERROR); + return 0; + } + + hwnd = CreateWindow(szAppName, szAppName, + WS_OVERLAPPEDWINDOW, + GetSystemMetrics(SM_CXSCREEN) / 4, + GetSystemMetrics(SM_CYSCREEN) / 4, + GetSystemMetrics(SM_CXSCREEN) / 2, + GetSystemMetrics(SM_CYSCREEN) / 2, + NULL, NULL, hInstance, NULL); + + ShowWindow(hwnd, nShowCmd); + UpdateWindow(hwnd); + + hAccel = LoadAccelerators(hInstance, szAppName); + + while (GetMessage(&msg, NULL, 0, 0)) + { + if (!TranslateAccelerator(hwnd, hAccel, &msg)) + { + TranslateMessage(&msg); + DispatchMessage(&msg); + } + } + return (int)msg.wParam; // WM_QUIT } -AskConfirmation (HWND hwnd) +AskConfirmation(HWND hwnd) { - return MessageBox (hwnd, TEXT ("Really want to close PopPad2?"), - szAppName, MB_YESNO | MB_ICONQUESTION) ; + return MessageBox(hwnd, TEXT("Really want to close PopPad2?"), + szAppName, MB_YESNO | MB_ICONQUESTION); } -LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) +LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { - static HWND hwndEdit ; - int iSelect, iEnable ; - - switch (message) - { - case WM_CREATE: - hwndEdit = CreateWindow (TEXT ("edit"), NULL, - WS_CHILD | WS_VISIBLE | WS_HSCROLL | WS_VSCROLL | - WS_BORDER | ES_LEFT | ES_MULTILINE | - ES_AUTOHSCROLL | ES_AUTOVSCROLL, - 0, 0, 0, 0, hwnd, (HMENU) ID_EDIT, - ((LPCREATESTRUCT) lParam)->hInstance, NULL) ; - return 0 ; - - case WM_SETFOCUS: - SetFocus (hwndEdit) ; - return 0 ; - - case WM_SIZE: - MoveWindow (hwndEdit, 0, 0, LOWORD (lParam), HIWORD (lParam), TRUE) ; - return 0 ; - - case WM_INITMENUPOPUP: - if (lParam == 1) - { - EnableMenuItem ((HMENU) wParam, IDM_EDIT_UNDO, - SendMessage (hwndEdit, EM_CANUNDO, 0, 0) ? - MF_ENABLED : MF_GRAYED) ; - - EnableMenuItem ((HMENU) wParam, IDM_EDIT_PASTE, - IsClipboardFormatAvailable (CF_TEXT) ? - MF_ENABLED : MF_GRAYED) ; - - iSelect = SendMessage (hwndEdit, EM_GETSEL, 0, 0) ; - - if (HIWORD (iSelect) == LOWORD (iSelect)) - iEnable = MF_GRAYED ; - else - iEnable = MF_ENABLED ; - - EnableMenuItem ((HMENU) wParam, IDM_EDIT_CUT, iEnable) ; - EnableMenuItem ((HMENU) wParam, IDM_EDIT_COPY, iEnable) ; - EnableMenuItem ((HMENU) wParam, IDM_EDIT_CLEAR, iEnable) ; - return 0 ; - } - break ; - - case WM_COMMAND: - if (lParam) - { - if (LOWORD (lParam) == ID_EDIT && - (HIWORD (wParam) == EN_ERRSPACE || - HIWORD (wParam) == EN_MAXTEXT)) - MessageBox (hwnd, TEXT ("Edit control out of space."), - szAppName, MB_OK | MB_ICONSTOP) ; - return 0 ; - } - else switch (LOWORD (wParam)) - { - case IDM_FILE_NEW: - case IDM_FILE_OPEN: - case IDM_FILE_SAVE: - case IDM_FILE_SAVE_AS: - case IDM_FILE_PRINT: - MessageBeep (0) ; - return 0 ; - - case IDM_APP_EXIT: - SendMessage (hwnd, WM_CLOSE, 0, 0) ; - return 0 ; - - case IDM_EDIT_UNDO: - SendMessage (hwndEdit, WM_UNDO, 0, 0) ; - return 0 ; - - case IDM_EDIT_CUT: - SendMessage (hwndEdit, WM_CUT, 0, 0) ; - return 0 ; - - case IDM_EDIT_COPY: - SendMessage (hwndEdit, WM_COPY, 0, 0) ; - return 0 ; - - case IDM_EDIT_PASTE: - SendMessage (hwndEdit, WM_PASTE, 0, 0) ; - return 0 ; - - case IDM_EDIT_CLEAR: - SendMessage (hwndEdit, WM_CLEAR, 0, 0) ; - return 0 ; - - case IDM_EDIT_SELECT_ALL: - SendMessage (hwndEdit, EM_SETSEL, 0, -1) ; - return 0 ; - - case IDM_HELP_HELP: - MessageBox (hwnd, TEXT ("Help not yet implemented!"), - szAppName, MB_OK | MB_ICONEXCLAMATION) ; - return 0 ; - - case IDM_APP_ABOUT: - MessageBox (hwnd, TEXT ("POPPAD2 (c) Charles Petzold, 1998"), - szAppName, MB_OK | MB_ICONINFORMATION) ; - return 0 ; - } - break ; - - case WM_CLOSE: - if (IDYES == AskConfirmation (hwnd)) - DestroyWindow (hwnd) ; - return 0 ; - - case WM_QUERYENDSESSION: - if (IDYES == AskConfirmation (hwnd)) - return 1 ; - else - return 0 ; - - case WM_DESTROY: - PostQuitMessage (0) ; - return 0 ; - } - return DefWindowProc (hwnd, message, wParam, lParam) ; + static HWND hwndEdit; + int iSelect, iEnable; + + switch (message) + { + case WM_CREATE: + hwndEdit = CreateWindow(TEXT("edit"), NULL, + WS_CHILD | WS_VISIBLE | WS_HSCROLL | WS_VSCROLL | + WS_BORDER | ES_LEFT | ES_MULTILINE | + ES_AUTOHSCROLL | ES_AUTOVSCROLL, + 0, 0, 0, 0, hwnd, (HMENU)ID_EDIT, + ((LPCREATESTRUCT)lParam)->hInstance, NULL); + return 0; + + case WM_SETFOCUS: + SetFocus(hwndEdit); + return 0; + + case WM_SIZE: + MoveWindow(hwndEdit, 0, 0, LOWORD(lParam), HIWORD(lParam), TRUE); + return 0; + + case WM_INITMENUPOPUP: + if (lParam == 1) + { + EnableMenuItem((HMENU)wParam, IDM_EDIT_UNDO, + SendMessage(hwndEdit, EM_CANUNDO, 0, 0) ? + MF_ENABLED : MF_GRAYED); + + EnableMenuItem((HMENU)wParam, IDM_EDIT_PASTE, + IsClipboardFormatAvailable(CF_TEXT) ? + MF_ENABLED : MF_GRAYED); + + iSelect = SendMessage(hwndEdit, EM_GETSEL, 0, 0); + + if (HIWORD(iSelect) == LOWORD(iSelect)) + iEnable = MF_GRAYED; + else + iEnable = MF_ENABLED; + + EnableMenuItem((HMENU)wParam, IDM_EDIT_CUT, iEnable); + EnableMenuItem((HMENU)wParam, IDM_EDIT_COPY, iEnable); + EnableMenuItem((HMENU)wParam, IDM_EDIT_CLEAR, iEnable); + return 0; + } + break; + + case WM_COMMAND: + if (lParam) + { + if (LOWORD(lParam) == ID_EDIT && + (HIWORD(wParam) == EN_ERRSPACE || + HIWORD(wParam) == EN_MAXTEXT)) + MessageBox(hwnd, TEXT("Edit control out of space."), + szAppName, MB_OK | MB_ICONSTOP); + return 0; + } + else switch (LOWORD(wParam)) + { + case IDM_FILE_NEW: + case IDM_FILE_OPEN: + case IDM_FILE_SAVE: + case IDM_FILE_SAVE_AS: + case IDM_FILE_PRINT: + MessageBeep(0); + return 0; + + case IDM_APP_EXIT: + SendMessage(hwnd, WM_CLOSE, 0, 0); + return 0; + + case IDM_EDIT_UNDO: + SendMessage(hwndEdit, WM_UNDO, 0, 0); + return 0; + + case IDM_EDIT_CUT: + SendMessage(hwndEdit, WM_CUT, 0, 0); + return 0; + + case IDM_EDIT_COPY: + SendMessage(hwndEdit, WM_COPY, 0, 0); + return 0; + + case IDM_EDIT_PASTE: + SendMessage(hwndEdit, WM_PASTE, 0, 0); + return 0; + + case IDM_EDIT_CLEAR: + SendMessage(hwndEdit, WM_CLEAR, 0, 0); + return 0; + + case IDM_EDIT_SELECT_ALL: + SendMessage(hwndEdit, EM_SETSEL, 0, -1); + return 0; + + case IDM_HELP_HELP: + MessageBox(hwnd, TEXT("Help not yet implemented!"), + szAppName, MB_OK | MB_ICONEXCLAMATION); + return 0; + + case IDM_APP_ABOUT: + MessageBox(hwnd, TEXT("POPPAD2 (c) Charles Petzold, 1998"), + szAppName, MB_OK | MB_ICONINFORMATION); + return 0; + } + break; + + case WM_CLOSE: + if (IDYES == AskConfirmation(hwnd)) + DestroyWindow(hwnd); + return 0; + + case WM_QUERYENDSESSION: + if (IDYES == AskConfirmation(hwnd)) + return 1; + else + return 0; + + case WM_DESTROY: + PostQuitMessage(0); + return 0; + } + return DefWindowProc(hwnd, message, wParam, lParam); } diff --git a/Chapter 11 Dialog Boxes/About2/About2.c b/Chapter 11 Dialog Boxes/About2/About2.c index 4a4f5a8..1e2f766 100644 --- a/Chapter 11 Dialog Boxes/About2/About2.c +++ b/Chapter 11 Dialog Boxes/About2/About2.c @@ -1,6 +1,6 @@ /*------------------------------------------ ABOUT2.C -- About Box Demo Program No. 2 - (c) Charles Petzold, 1998 + (c) Charles Petzold, 1998 ------------------------------------------*/ #define WIN32_LEAN_AND_MEAN @@ -8,11 +8,11 @@ #include #include "Resource.h" -LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ; -BOOL CALLBACK AboutDlgProc (HWND, UINT, WPARAM, LPARAM) ; +LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); +BOOL CALLBACK AboutDlgProc(HWND, UINT, WPARAM, LPARAM); -int iCurrentColor = IDC_BLACK, - iCurrentFigure = IDC_RECT ; +int iCurrentColor = IDC_BLACK, +iCurrentFigure = IDC_RECT; int WINAPI _tWinMain( _In_ HINSTANCE hInstance, @@ -20,171 +20,174 @@ int WINAPI _tWinMain( _In_ PTSTR pCmdLine, _In_ int nShowCmd) { - static TCHAR szAppName[] = TEXT ("About2") ; - MSG msg ; - HWND hwnd ; - WNDCLASS wndclass ; - - wndclass.style = CS_HREDRAW | CS_VREDRAW ; - wndclass.lpfnWndProc = WndProc ; - wndclass.cbClsExtra = 0 ; - wndclass.cbWndExtra = 0 ; - wndclass.hInstance = hInstance ; - wndclass.hIcon = LoadIcon (hInstance, szAppName) ; - wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ; - wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ; - wndclass.lpszMenuName = szAppName ; - wndclass.lpszClassName = szAppName ; - - if (!RegisterClass (&wndclass)) - { - MessageBox (NULL, TEXT ("This program requires Windows NT!"), - szAppName, MB_ICONERROR) ; - return 0 ; - } - - hwnd = CreateWindow (szAppName, TEXT ("About Box Demo Program"), - WS_OVERLAPPEDWINDOW, - CW_USEDEFAULT, CW_USEDEFAULT, - CW_USEDEFAULT, CW_USEDEFAULT, - NULL, NULL, hInstance, NULL) ; - - ShowWindow (hwnd, nShowCmd) ; - UpdateWindow (hwnd) ; - - while (GetMessage (&msg, NULL, 0, 0)) - { - TranslateMessage (&msg) ; - DispatchMessage (&msg) ; - } - return (int)msg.wParam; // WM_QUIT + UNREFERENCED_PARAMETER(hPrevInstance); + UNREFERENCED_PARAMETER(pCmdLine); + + static TCHAR szAppName[] = TEXT("About2"); + MSG msg; + HWND hwnd; + WNDCLASS wndclass; + + wndclass.style = CS_HREDRAW | CS_VREDRAW; + wndclass.lpfnWndProc = WndProc; + wndclass.cbClsExtra = 0; + wndclass.cbWndExtra = 0; + wndclass.hInstance = hInstance; + wndclass.hIcon = LoadIcon(hInstance, szAppName); + wndclass.hCursor = LoadCursor(NULL, IDC_ARROW); + wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); + wndclass.lpszMenuName = szAppName; + wndclass.lpszClassName = szAppName; + + if (!RegisterClass(&wndclass)) + { + MessageBox(NULL, TEXT("This program requires Windows NT!"), + szAppName, MB_ICONERROR); + return 0; + } + + hwnd = CreateWindow(szAppName, TEXT("About Box Demo Program"), + WS_OVERLAPPEDWINDOW, + CW_USEDEFAULT, CW_USEDEFAULT, + CW_USEDEFAULT, CW_USEDEFAULT, + NULL, NULL, hInstance, NULL); + + ShowWindow(hwnd, nShowCmd); + UpdateWindow(hwnd); + + while (GetMessage(&msg, NULL, 0, 0)) + { + TranslateMessage(&msg); + DispatchMessage(&msg); + } + return (int)msg.wParam; // WM_QUIT } -void PaintWindow (HWND hwnd, int iColor, int iFigure) +void PaintWindow(HWND hwnd, int iColor, int iFigure) { - static COLORREF crColor[8] = { RGB ( 0, 0, 0), RGB ( 0, 0, 255), - RGB ( 0, 255, 0), RGB ( 0, 255, 255), - RGB (255, 0, 0), RGB (255, 0, 255), - RGB (255, 255, 0), RGB (255, 255, 255) } ; - - HBRUSH hBrush ; - HDC hdc ; - RECT rect ; - - hdc = GetDC (hwnd) ; - GetClientRect (hwnd, &rect) ; - hBrush = CreateSolidBrush (crColor[iColor - IDC_BLACK]) ; - hBrush = (HBRUSH) SelectObject (hdc, hBrush) ; - - if (iFigure == IDC_RECT) - Rectangle (hdc, rect.left, rect.top, rect.right, rect.bottom) ; - else - Ellipse (hdc, rect.left, rect.top, rect.right, rect.bottom) ; - - DeleteObject (SelectObject (hdc, hBrush)) ; - ReleaseDC (hwnd, hdc) ; + static COLORREF crColor[8] = { RGB(0, 0, 0), RGB(0, 0, 255), + RGB(0, 255, 0), RGB(0, 255, 255), + RGB(255, 0, 0), RGB(255, 0, 255), + RGB(255, 255, 0), RGB(255, 255, 255) }; + + HBRUSH hBrush; + HDC hdc; + RECT rect; + + hdc = GetDC(hwnd); + GetClientRect(hwnd, &rect); + hBrush = CreateSolidBrush(crColor[iColor - IDC_BLACK]); + hBrush = (HBRUSH)SelectObject(hdc, hBrush); + + if (iFigure == IDC_RECT) + Rectangle(hdc, rect.left, rect.top, rect.right, rect.bottom); + else + Ellipse(hdc, rect.left, rect.top, rect.right, rect.bottom); + + DeleteObject(SelectObject(hdc, hBrush)); + ReleaseDC(hwnd, hdc); } -void PaintTheBlock (HWND hCtrl, int iColor, int iFigure) +void PaintTheBlock(HWND hCtrl, int iColor, int iFigure) { - InvalidateRect (hCtrl, NULL, TRUE) ; - UpdateWindow (hCtrl) ; - PaintWindow (hCtrl, iColor, iFigure) ; + InvalidateRect(hCtrl, NULL, TRUE); + UpdateWindow(hCtrl); + PaintWindow(hCtrl, iColor, iFigure); } -LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) +LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { - static HINSTANCE hInstance ; - PAINTSTRUCT ps ; - - switch (message) - { - case WM_CREATE: - hInstance = ((LPCREATESTRUCT) lParam)->hInstance ; - return 0 ; - - case WM_COMMAND: - switch (LOWORD (wParam)) - { - case IDM_APP_ABOUT: - if (DialogBox (hInstance, TEXT ("AboutBox"), hwnd, AboutDlgProc)) - InvalidateRect (hwnd, NULL, TRUE) ; - return 0 ; - } - break ; - - case WM_PAINT: - BeginPaint (hwnd, &ps) ; - EndPaint (hwnd, &ps) ; - - PaintWindow (hwnd, iCurrentColor, iCurrentFigure) ; - return 0 ; - - case WM_DESTROY: - PostQuitMessage (0) ; - return 0 ; - } - return DefWindowProc (hwnd, message, wParam, lParam) ; + static HINSTANCE hInstance; + PAINTSTRUCT ps; + + switch (message) + { + case WM_CREATE: + hInstance = ((LPCREATESTRUCT)lParam)->hInstance; + return 0; + + case WM_COMMAND: + switch (LOWORD(wParam)) + { + case IDM_APP_ABOUT: + if (DialogBox(hInstance, TEXT("AboutBox"), hwnd, AboutDlgProc)) + InvalidateRect(hwnd, NULL, TRUE); + return 0; + } + break; + + case WM_PAINT: + BeginPaint(hwnd, &ps); + EndPaint(hwnd, &ps); + + PaintWindow(hwnd, iCurrentColor, iCurrentFigure); + return 0; + + case WM_DESTROY: + PostQuitMessage(0); + return 0; + } + return DefWindowProc(hwnd, message, wParam, lParam); } -BOOL CALLBACK AboutDlgProc (HWND hDlg, UINT message, - WPARAM wParam, LPARAM lParam) +BOOL CALLBACK AboutDlgProc(HWND hDlg, UINT message, + WPARAM wParam, LPARAM lParam) { - static HWND hCtrlBlock ; - static int iColor, iFigure ; - - switch (message) - { - case WM_INITDIALOG: - iColor = iCurrentColor ; - iFigure = iCurrentFigure ; - - CheckRadioButton (hDlg, IDC_BLACK, IDC_WHITE, iColor) ; - CheckRadioButton (hDlg, IDC_RECT, IDC_ELLIPSE, iFigure) ; - - hCtrlBlock = GetDlgItem (hDlg, IDC_PAINT) ; - - SetFocus (GetDlgItem (hDlg, iColor)) ; - return FALSE ; - - case WM_COMMAND: - switch (LOWORD (wParam)) - { - case IDOK: - iCurrentColor = iColor ; - iCurrentFigure = iFigure ; - EndDialog (hDlg, TRUE) ; - return TRUE ; - - case IDCANCEL: - EndDialog (hDlg, FALSE) ; - return TRUE ; - - case IDC_BLACK: - case IDC_RED: - case IDC_GREEN: - case IDC_YELLOW: - case IDC_BLUE: - case IDC_MAGENTA: - case IDC_CYAN: - case IDC_WHITE: - iColor = LOWORD (wParam) ; - CheckRadioButton (hDlg, IDC_BLACK, IDC_WHITE, LOWORD (wParam)) ; - PaintTheBlock (hCtrlBlock, iColor, iFigure) ; - return TRUE ; - - case IDC_RECT: - case IDC_ELLIPSE: - iFigure = LOWORD (wParam) ; - CheckRadioButton (hDlg, IDC_RECT, IDC_ELLIPSE, LOWORD (wParam)) ; - PaintTheBlock (hCtrlBlock, iColor, iFigure) ; - return TRUE ; - } - break ; - - case WM_PAINT: - PaintTheBlock (hCtrlBlock, iColor, iFigure) ; - break ; - } - return FALSE ; + static HWND hCtrlBlock; + static int iColor, iFigure; + + switch (message) + { + case WM_INITDIALOG: + iColor = iCurrentColor; + iFigure = iCurrentFigure; + + CheckRadioButton(hDlg, IDC_BLACK, IDC_WHITE, iColor); + CheckRadioButton(hDlg, IDC_RECT, IDC_ELLIPSE, iFigure); + + hCtrlBlock = GetDlgItem(hDlg, IDC_PAINT); + + SetFocus(GetDlgItem(hDlg, iColor)); + return FALSE; + + case WM_COMMAND: + switch (LOWORD(wParam)) + { + case IDOK: + iCurrentColor = iColor; + iCurrentFigure = iFigure; + EndDialog(hDlg, TRUE); + return TRUE; + + case IDCANCEL: + EndDialog(hDlg, FALSE); + return TRUE; + + case IDC_BLACK: + case IDC_RED: + case IDC_GREEN: + case IDC_YELLOW: + case IDC_BLUE: + case IDC_MAGENTA: + case IDC_CYAN: + case IDC_WHITE: + iColor = LOWORD(wParam); + CheckRadioButton(hDlg, IDC_BLACK, IDC_WHITE, LOWORD(wParam)); + PaintTheBlock(hCtrlBlock, iColor, iFigure); + return TRUE; + + case IDC_RECT: + case IDC_ELLIPSE: + iFigure = LOWORD(wParam); + CheckRadioButton(hDlg, IDC_RECT, IDC_ELLIPSE, LOWORD(wParam)); + PaintTheBlock(hCtrlBlock, iColor, iFigure); + return TRUE; + } + break; + + case WM_PAINT: + PaintTheBlock(hCtrlBlock, iColor, iFigure); + break; + } + return FALSE; } diff --git a/Chapter 11 Dialog Boxes/About3/About3.c b/Chapter 11 Dialog Boxes/About3/About3.c index bff1624..1ec69ca 100644 --- a/Chapter 11 Dialog Boxes/About3/About3.c +++ b/Chapter 11 Dialog Boxes/About3/About3.c @@ -1,6 +1,6 @@ /*------------------------------------------ ABOUT3.C -- About Box Demo Program No. 3 - (c) Charles Petzold, 1998 + (c) Charles Petzold, 1998 ------------------------------------------*/ #define WIN32_LEAN_AND_MEAN @@ -8,9 +8,9 @@ #include #include "Resource.h" -LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ; -BOOL CALLBACK AboutDlgProc (HWND, UINT, WPARAM, LPARAM) ; -LRESULT CALLBACK EllipPushWndProc (HWND, UINT, WPARAM, LPARAM) ; +LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); +BOOL CALLBACK AboutDlgProc(HWND, UINT, WPARAM, LPARAM); +LRESULT CALLBACK EllipPushWndProc(HWND, UINT, WPARAM, LPARAM); int WINAPI _tWinMain( _In_ HINSTANCE hInstance, @@ -18,144 +18,147 @@ int WINAPI _tWinMain( _In_ PTSTR pCmdLine, _In_ int nShowCmd) { - static TCHAR szAppName[] = TEXT ("About3") ; - MSG msg ; - HWND hwnd ; - WNDCLASS wndclass ; - - wndclass.style = CS_HREDRAW | CS_VREDRAW ; - wndclass.lpfnWndProc = WndProc ; - wndclass.cbClsExtra = 0 ; - wndclass.cbWndExtra = 0 ; - wndclass.hInstance = hInstance ; - wndclass.hIcon = LoadIcon (hInstance, szAppName) ; - wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ; - wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ; - wndclass.lpszMenuName = szAppName ; - wndclass.lpszClassName = szAppName ; - - if (!RegisterClass (&wndclass)) - { - MessageBox (NULL, TEXT ("This program requires Windows NT!"), - szAppName, MB_ICONERROR) ; - return 0 ; - } - - wndclass.style = CS_HREDRAW | CS_VREDRAW ; - wndclass.lpfnWndProc = EllipPushWndProc ; - wndclass.cbClsExtra = 0 ; - wndclass.cbWndExtra = 0 ; - wndclass.hInstance = hInstance ; - wndclass.hIcon = NULL ; - wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ; - wndclass.hbrBackground = (HBRUSH) (COLOR_BTNFACE + 1) ; - wndclass.lpszMenuName = NULL ; - wndclass.lpszClassName = TEXT ("EllipPush") ; - - RegisterClass (&wndclass) ; - - hwnd = CreateWindow (szAppName, TEXT ("About Box Demo Program"), - WS_OVERLAPPEDWINDOW, - CW_USEDEFAULT, CW_USEDEFAULT, - CW_USEDEFAULT, CW_USEDEFAULT, - NULL, NULL, hInstance, NULL) ; - - ShowWindow (hwnd, nShowCmd) ; - UpdateWindow (hwnd) ; - - while (GetMessage (&msg, NULL, 0, 0)) - { - TranslateMessage (&msg) ; - DispatchMessage (&msg) ; - } - return (int)msg.wParam; // WM_QUIT + UNREFERENCED_PARAMETER(hPrevInstance); + UNREFERENCED_PARAMETER(pCmdLine); + + static TCHAR szAppName[] = TEXT("About3"); + MSG msg; + HWND hwnd; + WNDCLASS wndclass; + + wndclass.style = CS_HREDRAW | CS_VREDRAW; + wndclass.lpfnWndProc = WndProc; + wndclass.cbClsExtra = 0; + wndclass.cbWndExtra = 0; + wndclass.hInstance = hInstance; + wndclass.hIcon = LoadIcon(hInstance, szAppName); + wndclass.hCursor = LoadCursor(NULL, IDC_ARROW); + wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); + wndclass.lpszMenuName = szAppName; + wndclass.lpszClassName = szAppName; + + if (!RegisterClass(&wndclass)) + { + MessageBox(NULL, TEXT("This program requires Windows NT!"), + szAppName, MB_ICONERROR); + return 0; + } + + wndclass.style = CS_HREDRAW | CS_VREDRAW; + wndclass.lpfnWndProc = EllipPushWndProc; + wndclass.cbClsExtra = 0; + wndclass.cbWndExtra = 0; + wndclass.hInstance = hInstance; + wndclass.hIcon = NULL; + wndclass.hCursor = LoadCursor(NULL, IDC_ARROW); + wndclass.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1); + wndclass.lpszMenuName = NULL; + wndclass.lpszClassName = TEXT("EllipPush"); + + RegisterClass(&wndclass); + + hwnd = CreateWindow(szAppName, TEXT("About Box Demo Program"), + WS_OVERLAPPEDWINDOW, + CW_USEDEFAULT, CW_USEDEFAULT, + CW_USEDEFAULT, CW_USEDEFAULT, + NULL, NULL, hInstance, NULL); + + ShowWindow(hwnd, nShowCmd); + UpdateWindow(hwnd); + + while (GetMessage(&msg, NULL, 0, 0)) + { + TranslateMessage(&msg); + DispatchMessage(&msg); + } + return (int)msg.wParam; // WM_QUIT } -LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) +LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { - static HINSTANCE hInstance ; - - switch (message) - { - case WM_CREATE : - hInstance = ((LPCREATESTRUCT) lParam)->hInstance ; - return 0 ; - - case WM_COMMAND : - switch (LOWORD (wParam)) - { - case IDM_APP_ABOUT : - DialogBox (hInstance, TEXT ("AboutBox"), hwnd, AboutDlgProc) ; - return 0 ; - } - break ; - - case WM_DESTROY : - PostQuitMessage (0) ; - return 0 ; - } - return DefWindowProc (hwnd, message, wParam, lParam) ; + static HINSTANCE hInstance; + + switch (message) + { + case WM_CREATE: + hInstance = ((LPCREATESTRUCT)lParam)->hInstance; + return 0; + + case WM_COMMAND: + switch (LOWORD(wParam)) + { + case IDM_APP_ABOUT: + DialogBox(hInstance, TEXT("AboutBox"), hwnd, AboutDlgProc); + return 0; + } + break; + + case WM_DESTROY: + PostQuitMessage(0); + return 0; + } + return DefWindowProc(hwnd, message, wParam, lParam); } -BOOL CALLBACK AboutDlgProc (HWND hDlg, UINT message, - WPARAM wParam, LPARAM lParam) +BOOL CALLBACK AboutDlgProc(HWND hDlg, UINT message, + WPARAM wParam, LPARAM lParam) { - switch (message) - { - case WM_INITDIALOG : - return TRUE ; - - case WM_COMMAND : - switch (LOWORD (wParam)) - { - case IDOK : - EndDialog (hDlg, 0) ; - return TRUE ; - } - break ; - } - return FALSE ; + switch (message) + { + case WM_INITDIALOG: + return TRUE; + + case WM_COMMAND: + switch (LOWORD(wParam)) + { + case IDOK: + EndDialog(hDlg, 0); + return TRUE; + } + break; + } + return FALSE; } -LRESULT CALLBACK EllipPushWndProc (HWND hwnd, UINT message, - WPARAM wParam, LPARAM lParam) +LRESULT CALLBACK EllipPushWndProc(HWND hwnd, UINT message, + WPARAM wParam, LPARAM lParam) { - TCHAR szText[40] ; - HBRUSH hBrush ; - HDC hdc ; - PAINTSTRUCT ps ; - RECT rect ; - - switch (message) - { - case WM_PAINT : - GetClientRect (hwnd, &rect) ; - GetWindowText (hwnd, szText, sizeof (szText)) ; - - hdc = BeginPaint (hwnd, &ps) ; - - hBrush = CreateSolidBrush (GetSysColor (COLOR_WINDOW)) ; - hBrush = (HBRUSH) SelectObject (hdc, hBrush) ; - SetBkColor (hdc, GetSysColor (COLOR_WINDOW)) ; - SetTextColor (hdc, GetSysColor (COLOR_WINDOWTEXT)) ; - - Ellipse (hdc, rect.left, rect.top, rect.right, rect.bottom) ; - DrawText (hdc, szText, -1, &rect, - DT_SINGLELINE | DT_CENTER | DT_VCENTER) ; - - DeleteObject (SelectObject (hdc, hBrush)) ; - - EndPaint (hwnd, &ps) ; - return 0 ; - - case WM_KEYUP : - if (wParam != VK_SPACE) - break ; - // fall through - case WM_LBUTTONUP : - SendMessage (GetParent (hwnd), WM_COMMAND, - GetWindowLong (hwnd, GWL_ID), (LPARAM) hwnd) ; - return 0 ; - } - return DefWindowProc (hwnd, message, wParam, lParam) ; + TCHAR szText[40]; + HBRUSH hBrush; + HDC hdc; + PAINTSTRUCT ps; + RECT rect; + + switch (message) + { + case WM_PAINT: + GetClientRect(hwnd, &rect); + GetWindowText(hwnd, szText, sizeof(szText)); + + hdc = BeginPaint(hwnd, &ps); + + hBrush = CreateSolidBrush(GetSysColor(COLOR_WINDOW)); + hBrush = (HBRUSH)SelectObject(hdc, hBrush); + SetBkColor(hdc, GetSysColor(COLOR_WINDOW)); + SetTextColor(hdc, GetSysColor(COLOR_WINDOWTEXT)); + + Ellipse(hdc, rect.left, rect.top, rect.right, rect.bottom); + DrawText(hdc, szText, -1, &rect, + DT_SINGLELINE | DT_CENTER | DT_VCENTER); + + DeleteObject(SelectObject(hdc, hBrush)); + + EndPaint(hwnd, &ps); + return 0; + + case WM_KEYUP: + if (wParam != VK_SPACE) + break; + // fall through + case WM_LBUTTONUP: + SendMessage(GetParent(hwnd), WM_COMMAND, + GetWindowLongPtr(hwnd, GWLP_ID), (LPARAM)hwnd); + return 0; + } + return DefWindowProc(hwnd, message, wParam, lParam); } diff --git a/Chapter 11 Dialog Boxes/Colors2/Colors2.c b/Chapter 11 Dialog Boxes/Colors2/Colors2.c index cf6274c..e53eea4 100644 --- a/Chapter 11 Dialog Boxes/Colors2/Colors2.c +++ b/Chapter 11 Dialog Boxes/Colors2/Colors2.c @@ -1,16 +1,16 @@ /*------------------------------------------------ COLORS2.C -- Version using Modeless Dialog Box - (c) Charles Petzold, 1998 + (c) Charles Petzold, 1998 ------------------------------------------------*/ #define WIN32_LEAN_AND_MEAN -#include #include +#include -LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ; -BOOL CALLBACK ColorScrDlg (HWND, UINT, WPARAM, LPARAM) ; +LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); +BOOL CALLBACK ColorScrDlg(HWND, UINT, WPARAM, LPARAM); -HWND hDlgModeless ; +HWND hDlgModeless; int WINAPI _tWinMain( _In_ HINSTANCE hInstance, @@ -18,123 +18,131 @@ int WINAPI _tWinMain( _In_ PTSTR pCmdLine, _In_ int nShowCmd) { - static TCHAR szAppName[] = TEXT ("Colors2") ; - HWND hwnd ; - MSG msg ; - WNDCLASS wndclass ; - - wndclass.style = CS_HREDRAW | CS_VREDRAW ; - wndclass.lpfnWndProc = WndProc ; - wndclass.cbClsExtra = 0 ; - wndclass.cbWndExtra = 0 ; - wndclass.hInstance = hInstance ; - wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ; - wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ; - wndclass.hbrBackground = CreateSolidBrush (0L) ; - wndclass.lpszMenuName = NULL ; - wndclass.lpszClassName = szAppName ; - - if (!RegisterClass (&wndclass)) - { - MessageBox (NULL, TEXT ("This program requires Windows NT!"), - szAppName, MB_ICONERROR) ; - return 0 ; - } - - hwnd = CreateWindow (szAppName, TEXT ("Color Scroll"), - WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN, - CW_USEDEFAULT, CW_USEDEFAULT, - CW_USEDEFAULT, CW_USEDEFAULT, - NULL, NULL, hInstance, NULL) ; - - ShowWindow (hwnd, nShowCmd) ; - UpdateWindow (hwnd) ; - - hDlgModeless = CreateDialog (hInstance, TEXT ("ColorScrDlg"), - hwnd, ColorScrDlg) ; - - while (GetMessage (&msg, NULL, 0, 0)) - { - if (hDlgModeless == 0 || !IsDialogMessage (hDlgModeless, &msg)) - { - TranslateMessage (&msg) ; - DispatchMessage (&msg) ; - } - } - return (int)msg.wParam; // WM_QUIT + UNREFERENCED_PARAMETER(hPrevInstance); + UNREFERENCED_PARAMETER(pCmdLine); + + static TCHAR szAppName[] = TEXT("Colors2"); + HWND hwnd; + MSG msg; + WNDCLASS wndclass; + + wndclass.style = CS_HREDRAW | CS_VREDRAW; + wndclass.lpfnWndProc = WndProc; + wndclass.cbClsExtra = 0; + wndclass.cbWndExtra = 0; + wndclass.hInstance = hInstance; + wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION); + wndclass.hCursor = LoadCursor(NULL, IDC_ARROW); + wndclass.hbrBackground = CreateSolidBrush(0L); + wndclass.lpszMenuName = NULL; + wndclass.lpszClassName = szAppName; + + if (!RegisterClass(&wndclass)) + { + MessageBox(NULL, TEXT("This program requires Windows NT!"), + szAppName, MB_ICONERROR); + return 0; + } + + hwnd = CreateWindow(szAppName, TEXT("Color Scroll"), + WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN, + CW_USEDEFAULT, CW_USEDEFAULT, + CW_USEDEFAULT, CW_USEDEFAULT, + NULL, NULL, hInstance, NULL); + + ShowWindow(hwnd, nShowCmd); + UpdateWindow(hwnd); + + hDlgModeless = CreateDialog(hInstance, TEXT("ColorScrDlg"), + hwnd, ColorScrDlg); + + while (GetMessage(&msg, NULL, 0, 0)) + { + if (hDlgModeless == 0 || !IsDialogMessage(hDlgModeless, &msg)) + { + TranslateMessage(&msg); + DispatchMessage(&msg); + } + } + return (int)msg.wParam; // WM_QUIT } -LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) +LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { - switch (message) - { - case WM_DESTROY : - DeleteObject ((HGDIOBJ) SetClassLong (hwnd, GCL_HBRBACKGROUND, - (LONG) GetStockObject (WHITE_BRUSH))) ; - PostQuitMessage (0) ; - return 0 ; - } - return DefWindowProc (hwnd, message, wParam, lParam) ; + switch (message) + { + case WM_DESTROY: + DeleteObject( + (HGDIOBJ)SetClassLongPtr( + hwnd, + GCLP_HBRBACKGROUND, + (LONG_PTR)GetStockObject(WHITE_BRUSH))); + PostQuitMessage(0); + return 0; + } + return DefWindowProc(hwnd, message, wParam, lParam); } -BOOL CALLBACK ColorScrDlg (HWND hDlg, UINT message, - WPARAM wParam, LPARAM lParam) +BOOL CALLBACK ColorScrDlg(HWND hDlg, UINT message, + WPARAM wParam, LPARAM lParam) { - static int iColor[3] ; - HWND hwndParent, hCtrl ; - int iCtrlID, iIndex ; - - switch (message) - { - case WM_INITDIALOG : - for (iCtrlID = 10 ; iCtrlID < 13 ; iCtrlID++) - { - hCtrl = GetDlgItem (hDlg, iCtrlID) ; - SetScrollRange (hCtrl, SB_CTL, 0, 255, FALSE) ; - SetScrollPos (hCtrl, SB_CTL, 0, FALSE) ; - } - return TRUE ; - - case WM_VSCROLL : - hCtrl = (HWND) lParam ; - iCtrlID = GetWindowLong (hCtrl, GWL_ID) ; - iIndex = iCtrlID - 10 ; - hwndParent = GetParent (hDlg) ; - - switch (LOWORD (wParam)) - { - case SB_PAGEDOWN : - iColor[iIndex] += 15 ; // fall through - case SB_LINEDOWN : - iColor[iIndex] = min (255, iColor[iIndex] + 1) ; - break ; - case SB_PAGEUP : - iColor[iIndex] -= 15 ; // fall through - case SB_LINEUP : - iColor[iIndex] = max (0, iColor[iIndex] - 1) ; - break ; - case SB_TOP : - iColor[iIndex] = 0 ; - break ; - case SB_BOTTOM : - iColor[iIndex] = 255 ; - break ; - case SB_THUMBPOSITION : - case SB_THUMBTRACK : - iColor[iIndex] = HIWORD (wParam) ; - break ; - default : - return FALSE ; - } - SetScrollPos (hCtrl, SB_CTL, iColor[iIndex], TRUE) ; - SetDlgItemInt (hDlg, iCtrlID + 3, iColor[iIndex], FALSE) ; - - DeleteObject ((HGDIOBJ) SetClassLong (hwndParent, GCL_HBRBACKGROUND, - (LONG) CreateSolidBrush ( - RGB (iColor[0], iColor[1], iColor[2])))) ; - - InvalidateRect (hwndParent, NULL, TRUE) ; - return TRUE ; - } - return FALSE ; + static int iColor[3]; + HWND hwndParent, hCtrl; + int iCtrlID, iIndex; + + switch (message) + { + case WM_INITDIALOG: + for (iCtrlID = 10; iCtrlID < 13; iCtrlID++) + { + hCtrl = GetDlgItem(hDlg, iCtrlID); + SetScrollRange(hCtrl, SB_CTL, 0, 255, FALSE); + SetScrollPos(hCtrl, SB_CTL, 0, FALSE); + } + return TRUE; + + case WM_VSCROLL: + hCtrl = (HWND)lParam; + iCtrlID = GetWindowLongPtr(hCtrl, GWLP_ID); + iIndex = iCtrlID - 10; + hwndParent = GetParent(hDlg); + + switch (LOWORD(wParam)) + { + case SB_PAGEDOWN: + iColor[iIndex] += 15; // fall through + case SB_LINEDOWN: + iColor[iIndex] = min(255, iColor[iIndex] + 1); + break; + case SB_PAGEUP: + iColor[iIndex] -= 15; // fall through + case SB_LINEUP: + iColor[iIndex] = max(0, iColor[iIndex] - 1); + break; + case SB_TOP: + iColor[iIndex] = 0; + break; + case SB_BOTTOM: + iColor[iIndex] = 255; + break; + case SB_THUMBPOSITION: + case SB_THUMBTRACK: + iColor[iIndex] = HIWORD(wParam); + break; + default: + return FALSE; + } + SetScrollPos(hCtrl, SB_CTL, iColor[iIndex], TRUE); + SetDlgItemInt(hDlg, iCtrlID + 3, iColor[iIndex], FALSE); + + DeleteObject( + (HGDIOBJ)SetClassLongPtr( + hwndParent, + GCLP_HBRBACKGROUND, + (LONG_PTR)CreateSolidBrush(RGB(iColor[0], iColor[1], iColor[2])))); + + InvalidateRect(hwndParent, NULL, TRUE); + return TRUE; + } + return FALSE; } diff --git a/Chapter 11 Dialog Boxes/Colors3/Colors3.c b/Chapter 11 Dialog Boxes/Colors3/Colors3.c index 99b33ec..9ee951b 100644 --- a/Chapter 11 Dialog Boxes/Colors3/Colors3.c +++ b/Chapter 11 Dialog Boxes/Colors3/Colors3.c @@ -1,6 +1,6 @@ /*---------------------------------------------- COLORS3.C -- Version using Common Dialog Box - (c) Charles Petzold, 1998 + (c) Charles Petzold, 1998 ----------------------------------------------*/ #define WIN32_LEAN_AND_MEAN @@ -14,18 +14,23 @@ int WINAPI _tWinMain( _In_ PTSTR pCmdLine, _In_ int nShowCmd) { - static CHOOSECOLOR cc ; - static COLORREF crCustColors[16] ; + UNREFERENCED_PARAMETER(hInstance); + UNREFERENCED_PARAMETER(hPrevInstance); + UNREFERENCED_PARAMETER(pCmdLine); + UNREFERENCED_PARAMETER(nShowCmd); - cc.lStructSize = sizeof (CHOOSECOLOR) ; - cc.hwndOwner = NULL ; - cc.hInstance = NULL ; - cc.rgbResult = RGB (0x80, 0x80, 0x80) ; - cc.lpCustColors = crCustColors ; - cc.Flags = CC_RGBINIT | CC_FULLOPEN ; - cc.lCustData = 0 ; - cc.lpfnHook = NULL ; - cc.lpTemplateName = NULL ; + static CHOOSECOLOR cc; + static COLORREF crCustColors[16]; - return ChooseColor (&cc) ; + cc.lStructSize = sizeof(CHOOSECOLOR); + cc.hwndOwner = NULL; + cc.hInstance = NULL; + cc.rgbResult = RGB(0x80, 0x80, 0x80); + cc.lpCustColors = crCustColors; + cc.Flags = CC_RGBINIT | CC_FULLOPEN; + cc.lCustData = 0; + cc.lpfnHook = NULL; + cc.lpTemplateName = NULL; + + return ChooseColor(&cc); } diff --git a/Chapter 11 Dialog Boxes/HexCalc/HexCalc.c b/Chapter 11 Dialog Boxes/HexCalc/HexCalc.c index 7cb30fd..9f919b2 100644 --- a/Chapter 11 Dialog Boxes/HexCalc/HexCalc.c +++ b/Chapter 11 Dialog Boxes/HexCalc/HexCalc.c @@ -1,13 +1,13 @@ /*---------------------------------------- HEXCALC.C -- Hexadecimal Calculator - (c) Charles Petzold, 1998 + (c) Charles Petzold, 1998 ----------------------------------------*/ #define WIN32_LEAN_AND_MEAN #include #include -LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ; +LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); int WINAPI _tWinMain( _In_ HINSTANCE hInstance, @@ -15,135 +15,138 @@ int WINAPI _tWinMain( _In_ PTSTR pCmdLine, _In_ int nShowCmd) { - static TCHAR szAppName[] = TEXT ("HexCalc") ; - HWND hwnd ; - MSG msg ; - WNDCLASS wndclass ; - - wndclass.style = CS_HREDRAW | CS_VREDRAW; - wndclass.lpfnWndProc = WndProc ; - wndclass.cbClsExtra = 0 ; - wndclass.cbWndExtra = DLGWINDOWEXTRA ; // Note! - wndclass.hInstance = hInstance ; - wndclass.hIcon = LoadIcon (hInstance, szAppName) ; - wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ; - wndclass.hbrBackground = (HBRUSH) (COLOR_BTNFACE + 1) ; - wndclass.lpszMenuName = NULL ; - wndclass.lpszClassName = szAppName ; - - if (!RegisterClass (&wndclass)) - { - MessageBox (NULL, TEXT ("This program requires Windows NT!"), - szAppName, MB_ICONERROR) ; - return 0 ; - } - - hwnd = CreateDialog (hInstance, szAppName, 0, NULL) ; - - ShowWindow (hwnd, nShowCmd) ; - - while (GetMessage (&msg, NULL, 0, 0)) - { - TranslateMessage (&msg) ; - DispatchMessage (&msg) ; - } - return (int)msg.wParam; // WM_QUIT + UNREFERENCED_PARAMETER(hPrevInstance); + UNREFERENCED_PARAMETER(pCmdLine); + + static TCHAR szAppName[] = TEXT("HexCalc"); + HWND hwnd; + MSG msg; + WNDCLASS wndclass; + + wndclass.style = CS_HREDRAW | CS_VREDRAW; + wndclass.lpfnWndProc = WndProc; + wndclass.cbClsExtra = 0; + wndclass.cbWndExtra = DLGWINDOWEXTRA; // Note! + wndclass.hInstance = hInstance; + wndclass.hIcon = LoadIcon(hInstance, szAppName); + wndclass.hCursor = LoadCursor(NULL, IDC_ARROW); + wndclass.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1); + wndclass.lpszMenuName = NULL; + wndclass.lpszClassName = szAppName; + + if (!RegisterClass(&wndclass)) + { + MessageBox(NULL, TEXT("This program requires Windows NT!"), + szAppName, MB_ICONERROR); + return 0; + } + + hwnd = CreateDialog(hInstance, szAppName, 0, NULL); + + ShowWindow(hwnd, nShowCmd); + + while (GetMessage(&msg, NULL, 0, 0)) + { + TranslateMessage(&msg); + DispatchMessage(&msg); + } + return (int)msg.wParam; // WM_QUIT } -void ShowNumber (HWND hwnd, UINT iNumber) +void ShowNumber(HWND hwnd, UINT iNumber) { - TCHAR szBuffer[20] ; + TCHAR szBuffer[20]; - wsprintf (szBuffer, TEXT ("%X"), iNumber) ; - SetDlgItemText (hwnd, VK_ESCAPE, szBuffer) ; + wsprintf(szBuffer, TEXT("%X"), iNumber); + SetDlgItemText(hwnd, VK_ESCAPE, szBuffer); } -DWORD CalcIt (UINT iFirstNum, int iOperation, UINT iNum) +DWORD CalcIt(UINT iFirstNum, int iOperation, UINT iNum) { - switch (iOperation) - { - case '=': return iNum ; - case '+': return iFirstNum + iNum ; - case '-': return iFirstNum - iNum ; - case '*': return iFirstNum * iNum ; - case '&': return iFirstNum & iNum ; - case '|': return iFirstNum | iNum ; - case '^': return iFirstNum ^ iNum ; - case '<': return iFirstNum << iNum ; - case '>': return iFirstNum >> iNum ; - case '/': return iNum ? iFirstNum / iNum: MAXDWORD ; - case '%': return iNum ? iFirstNum % iNum: MAXDWORD ; - default : return 0 ; - } + switch (iOperation) + { + case '=': return iNum; + case '+': return iFirstNum + iNum; + case '-': return iFirstNum - iNum; + case '*': return iFirstNum * iNum; + case '&': return iFirstNum & iNum; + case '|': return iFirstNum | iNum; + case '^': return iFirstNum ^ iNum; + case '<': return iFirstNum << iNum; + case '>': return iFirstNum >> iNum; + case '/': return iNum ? iFirstNum / iNum : MAXDWORD; + case '%': return iNum ? iFirstNum % iNum : MAXDWORD; + default: return 0; + } } -LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) +LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { - static BOOL bNewNumber = TRUE ; - static int iOperation = '=' ; - static UINT iNumber, iFirstNum ; - HWND hButton ; - - switch (message) - { - case WM_KEYDOWN: // left arrow --> backspace - if (wParam != VK_LEFT) - break ; - wParam = VK_BACK ; - // fall through - case WM_CHAR: - if ((wParam = (WPARAM) CharUpper ((TCHAR *) wParam)) == VK_RETURN) - wParam = '=' ; - - if (hButton = GetDlgItem (hwnd, wParam)) - { - SendMessage (hButton, BM_SETSTATE, 1, 0) ; - Sleep (100) ; - SendMessage (hButton, BM_SETSTATE, 0, 0) ; - } - else - { - MessageBeep (0) ; - break ; - } - // fall through - case WM_COMMAND: - SetFocus (hwnd) ; - - if (LOWORD (wParam) == VK_BACK) // backspace - ShowNumber (hwnd, iNumber /= 16) ; - - else if (LOWORD (wParam) == VK_ESCAPE) // escape - ShowNumber (hwnd, iNumber = 0) ; - - else if (isxdigit (LOWORD (wParam))) // hex digit - { - if (bNewNumber) - { - iFirstNum = iNumber ; - iNumber = 0 ; - } - bNewNumber = FALSE ; - - if (iNumber <= MAXDWORD >> 4) - ShowNumber (hwnd, iNumber = 16 * iNumber + wParam - - (isdigit (wParam) ? '0': 'A' - 10)) ; - else - MessageBeep (0) ; - } - else // operation - { - if (!bNewNumber) - ShowNumber (hwnd, iNumber = - CalcIt (iFirstNum, iOperation, iNumber)) ; - bNewNumber = TRUE ; - iOperation = LOWORD (wParam) ; - } - return 0 ; - - case WM_DESTROY: - PostQuitMessage (0) ; - return 0 ; - } - return DefWindowProc (hwnd, message, wParam, lParam) ; + static BOOL bNewNumber = TRUE; + static int iOperation = '='; + static UINT iNumber, iFirstNum; + HWND hButton; + + switch (message) + { + case WM_KEYDOWN: // left arrow --> backspace + if (wParam != VK_LEFT) + break; + wParam = VK_BACK; + // fall through + case WM_CHAR: + if ((wParam = (WPARAM)CharUpper((TCHAR*)wParam)) == VK_RETURN) + wParam = '='; + + if (hButton = GetDlgItem(hwnd, wParam)) + { + SendMessage(hButton, BM_SETSTATE, 1, 0); + Sleep(100); + SendMessage(hButton, BM_SETSTATE, 0, 0); + } + else + { + MessageBeep(0); + break; + } + // fall through + case WM_COMMAND: + SetFocus(hwnd); + + if (LOWORD(wParam) == VK_BACK) // backspace + ShowNumber(hwnd, iNumber /= 16); + + else if (LOWORD(wParam) == VK_ESCAPE) // escape + ShowNumber(hwnd, iNumber = 0); + + else if (isxdigit(LOWORD(wParam))) // hex digit + { + if (bNewNumber) + { + iFirstNum = iNumber; + iNumber = 0; + } + bNewNumber = FALSE; + + if (iNumber <= MAXDWORD >> 4) + ShowNumber(hwnd, iNumber = 16 * iNumber + wParam - + (isdigit(wParam) ? '0' : 'A' - 10)); + else + MessageBeep(0); + } + else // operation + { + if (!bNewNumber) + ShowNumber(hwnd, iNumber = + CalcIt(iFirstNum, iOperation, iNumber)); + bNewNumber = TRUE; + iOperation = LOWORD(wParam); + } + return 0; + + case WM_DESTROY: + PostQuitMessage(0); + return 0; + } + return DefWindowProc(hwnd, message, wParam, lParam); } diff --git a/Chapter 11 Dialog Boxes/PopPad3/PopFile.c b/Chapter 11 Dialog Boxes/PopPad/PopFile.c similarity index 100% rename from Chapter 11 Dialog Boxes/PopPad3/PopFile.c rename to Chapter 11 Dialog Boxes/PopPad/PopFile.c diff --git a/Chapter 11 Dialog Boxes/PopPad3/PopFind.c b/Chapter 11 Dialog Boxes/PopPad/PopFind.c similarity index 100% rename from Chapter 11 Dialog Boxes/PopPad3/PopFind.c rename to Chapter 11 Dialog Boxes/PopPad/PopFind.c diff --git a/Chapter 11 Dialog Boxes/PopPad3/PopFont.c b/Chapter 11 Dialog Boxes/PopPad/PopFont.c similarity index 100% rename from Chapter 11 Dialog Boxes/PopPad3/PopFont.c rename to Chapter 11 Dialog Boxes/PopPad/PopFont.c diff --git a/Chapter 11 Dialog Boxes/PopPad3/PopPad.c b/Chapter 11 Dialog Boxes/PopPad/PopPad.c similarity index 100% rename from Chapter 11 Dialog Boxes/PopPad3/PopPad.c rename to Chapter 11 Dialog Boxes/PopPad/PopPad.c diff --git a/Chapter 11 Dialog Boxes/PopPad3/PopPad.h b/Chapter 11 Dialog Boxes/PopPad/PopPad.h similarity index 77% rename from Chapter 11 Dialog Boxes/PopPad3/PopPad.h rename to Chapter 11 Dialog Boxes/PopPad/PopPad.h index f5bcfe5..b9f375a 100644 --- a/Chapter 11 Dialog Boxes/PopPad3/PopPad.h +++ b/Chapter 11 Dialog Boxes/PopPad/PopPad.h @@ -1,7 +1,7 @@ #pragma once /*--------------------------------------- - POPPAD.H -- Popup Editor - (c) Charles Petzold, 1998 + POPPAD.H -- Popup Editor + (c) Charles Petzold, 1998 ---------------------------------------*/ #include @@ -19,9 +19,9 @@ BOOL PopFileWrite(HWND, PTSTR); HWND PopFindFindDlg(HWND); HWND PopFindReplaceDlg(HWND); -BOOL PopFindFindText(HWND, int *, LPFINDREPLACE); -BOOL PopFindReplaceText(HWND, int *, LPFINDREPLACE); -BOOL PopFindNextText(HWND, int *); +BOOL PopFindFindText(HWND, int*, LPFINDREPLACE); +BOOL PopFindReplaceText(HWND, int*, LPFINDREPLACE); +BOOL PopFindNextText(HWND, int*); BOOL PopFindValidFind(void); // Functions in POPFONT.C diff --git a/Chapter 11 Dialog Boxes/PopPad3/PopPad.ico b/Chapter 11 Dialog Boxes/PopPad/PopPad.ico similarity index 100% rename from Chapter 11 Dialog Boxes/PopPad3/PopPad.ico rename to Chapter 11 Dialog Boxes/PopPad/PopPad.ico diff --git a/Chapter 11 Dialog Boxes/PopPad3/PopPad.rc b/Chapter 11 Dialog Boxes/PopPad/PopPad.rc similarity index 100% rename from Chapter 11 Dialog Boxes/PopPad3/PopPad.rc rename to Chapter 11 Dialog Boxes/PopPad/PopPad.rc diff --git a/Chapter 11 Dialog Boxes/PopPad3/PopPrntNull.c b/Chapter 11 Dialog Boxes/PopPad/PopPrntNull.c similarity index 66% rename from Chapter 11 Dialog Boxes/PopPad3/PopPrntNull.c rename to Chapter 11 Dialog Boxes/PopPad/PopPrntNull.c index d2190ee..7e31065 100644 --- a/Chapter 11 Dialog Boxes/PopPad3/PopPrntNull.c +++ b/Chapter 11 Dialog Boxes/PopPad/PopPrntNull.c @@ -5,8 +5,7 @@ #define WIN32_LEAN_AND_MEAN #include -BOOL PopPrntPrintFile (HINSTANCE hInst, HWND hwnd, HWND hwndEdit, - PTSTR pstrTitleName) +BOOL PopPrntPrintFile(HINSTANCE hInst, HWND hwnd, HWND hwndEdit, PTSTR pstrTitleName) { - return FALSE ; + return FALSE; } diff --git a/Chapter 11 Dialog Boxes/PopPad3/Resource.h b/Chapter 11 Dialog Boxes/PopPad/Resource.h similarity index 100% rename from Chapter 11 Dialog Boxes/PopPad3/Resource.h rename to Chapter 11 Dialog Boxes/PopPad/Resource.h diff --git a/Chapter 19 The Multiple-Document Interface/MDIDemo/MDIDemo.c b/Chapter 19 The Multiple-Document Interface/MDIDemo/MDIDemo.c index 9e9d651..09428f7 100644 --- a/Chapter 19 The Multiple-Document Interface/MDIDemo/MDIDemo.c +++ b/Chapter 19 The Multiple-Document Interface/MDIDemo/MDIDemo.c @@ -317,7 +317,7 @@ LRESULT CALLBACK HelloWndProc(HWND hwnd, UINT message, pHelloData->iColor = IDM_COLOR_BLACK; pHelloData->clrText = RGB(0, 0, 0); - SetWindowLong(hwnd, 0, (long)pHelloData); + SetWindowLongPtr(hwnd, GWLP_USERDATA, (long)pHelloData); // Save some window handles @@ -335,7 +335,7 @@ LRESULT CALLBACK HelloWndProc(HWND hwnd, UINT message, case IDM_COLOR_WHITE: // Change the text color - pHelloData = (PHELLODATA)GetWindowLong(hwnd, 0); + pHelloData = (PHELLODATA)GetWindowLongPtr(hwnd, GWLP_USERDATA); hMenu = GetMenu(hwndFrame); @@ -354,7 +354,7 @@ LRESULT CALLBACK HelloWndProc(HWND hwnd, UINT message, hdc = BeginPaint(hwnd, &ps); - pHelloData = (PHELLODATA)GetWindowLong(hwnd, 0); + pHelloData = (PHELLODATA)GetWindowLongPtr(hwnd, GWLP_USERDATA); SetTextColor(hdc, pHelloData->clrText); GetClientRect(hwnd, &rect); @@ -374,7 +374,7 @@ LRESULT CALLBACK HelloWndProc(HWND hwnd, UINT message, // Check or uncheck menu item - pHelloData = (PHELLODATA)GetWindowLong(hwnd, 0); + pHelloData = (PHELLODATA)GetWindowLongPtr(hwnd, GWLP_USERDATA); CheckMenuItem(hMenuHello, pHelloData->iColor, (lParam == (LPARAM)hwnd) ? MF_CHECKED : MF_UNCHECKED); @@ -397,7 +397,7 @@ LRESULT CALLBACK HelloWndProc(HWND hwnd, UINT message, break; // i.e., call DefMDIChildProc case WM_DESTROY: - pHelloData = (PHELLODATA)GetWindowLong(hwnd, 0); + pHelloData = (PHELLODATA)GetWindowLongPtr(hwnd, GWLP_USERDATA); HeapFree(GetProcessHeap(), 0, pHelloData); return 0; } @@ -425,7 +425,7 @@ LRESULT CALLBACK RectWndProc(HWND hwnd, UINT message, pRectData = (PRECTDATA)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(RECTDATA)); - SetWindowLong(hwnd, 0, (long)pRectData); + SetWindowLongPtr(hwnd, GWLP_USERDATA, (long)pRectData); // Start the timer going @@ -441,7 +441,7 @@ LRESULT CALLBACK RectWndProc(HWND hwnd, UINT message, if (wParam != SIZE_MINIMIZED) { - pRectData = (PRECTDATA)GetWindowLong(hwnd, 0); + pRectData = (PRECTDATA)GetWindowLongPtr(hwnd, GWLP_USERDATA); pRectData->cxClient = GET_X_LPARAM(lParam); pRectData->cyClient = GET_Y_LPARAM(lParam); @@ -451,7 +451,7 @@ LRESULT CALLBACK RectWndProc(HWND hwnd, UINT message, case WM_TIMER: // Display a random rectangle - pRectData = (PRECTDATA)GetWindowLong(hwnd, 0); + pRectData = (PRECTDATA)GetWindowLongPtr(hwnd, GWLP_USERDATA); xLeft = rand() % pRectData->cxClient; xRight = rand() % pRectData->cxClient; @@ -491,7 +491,7 @@ LRESULT CALLBACK RectWndProc(HWND hwnd, UINT message, return 0; case WM_DESTROY: - pRectData = (PRECTDATA)GetWindowLong(hwnd, 0); + pRectData = (PRECTDATA)GetWindowLongPtr(hwnd, GWLP_USERDATA); HeapFree(GetProcessHeap(), 0, pRectData); KillTimer(hwnd, 1); return 0; diff --git a/Chapter 20 Multitasking and Multithreading/Multi1/Multi1.c b/Chapter 20 Multitasking and Multithreading/Multi1/Multi1.c index 3c7275c..cdcc220 100644 --- a/Chapter 20 Multitasking and Multithreading/Multi1/Multi1.c +++ b/Chapter 20 Multitasking and Multithreading/Multi1/Multi1.c @@ -240,7 +240,7 @@ LRESULT APIENTRY WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) switch (message) { case WM_CREATE: - hInstance = (HINSTANCE)GetWindowLong(hwnd, GWL_HINSTANCE); + hInstance = (HINSTANCE)GetWindowLongPtr(hwnd, GWLP_HINSTANCE); wndclass.style = CS_HREDRAW | CS_VREDRAW; wndclass.cbClsExtra = 0; diff --git a/Chapter 20 Multitasking and Multithreading/Multi2/Multi2.c b/Chapter 20 Multitasking and Multithreading/Multi2/Multi2.c index 9f5788b..48fe87b 100644 --- a/Chapter 20 Multitasking and Multithreading/Multi2/Multi2.c +++ b/Chapter 20 Multitasking and Multithreading/Multi2/Multi2.c @@ -330,7 +330,7 @@ LRESULT APIENTRY WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) switch (message) { case WM_CREATE: - hInstance = (HINSTANCE)GetWindowLong(hwnd, GWL_HINSTANCE); + hInstance = (HINSTANCE)GetWindowLongPtr(hwnd, GWLP_HINSTANCE); wndclass.style = CS_HREDRAW | CS_VREDRAW; wndclass.cbClsExtra = 0; diff --git a/Chapter 22 Sound and Music/WakeUp/WakeUp.c b/Chapter 22 Sound and Music/WakeUp/WakeUp.c index 47ae04d..cacb50b 100644 --- a/Chapter 22 Sound and Music/WakeUp/WakeUp.c +++ b/Chapter 22 Sound and Music/WakeUp/WakeUp.c @@ -5,6 +5,7 @@ #define WIN32_LEAN_AND_MEAN #include +#include #include #include @@ -124,7 +125,7 @@ LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) case WM_CREATE: // Some initialization stuff - hInstance = (HINSTANCE)GetWindowLong(hwnd, GWL_HINSTANCE); + hInstance = (HINSTANCE)GetWindowLong(hwnd, GWLP_HINSTANCE); icex.dwSize = sizeof(icex); icex.dwICC = ICC_DATE_CLASSES; @@ -180,11 +181,11 @@ LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) // Subclass the three child windows SubbedProc[ID_TIMEPICK] = (WNDPROC) - SetWindowLong(hwndDTP, GWL_WNDPROC, (LONG)SubProc); + SetWindowLongPtr(hwndDTP, GWLP_WNDPROC, (LONG)SubProc); SubbedProc[ID_CHECKBOX] = (WNDPROC) - SetWindowLong(hwndCheck, GWL_WNDPROC, (LONG)SubProc); + SetWindowLongPtr(hwndCheck, GWLP_WNDPROC, (LONG)SubProc); SubbedProc[ID_PUSHBTN] = (WNDPROC) - SetWindowLong(hwndPush, GWL_WNDPROC, (LONG)SubProc); + SetWindowLongPtr(hwndPush, GWLP_WNDPROC, (LONG)SubProc); // Set the date and time picker control to the current time // plus 9 hours, rounded down to next lowest hour @@ -323,7 +324,7 @@ LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) LRESULT CALLBACK SubProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { - int idNext, id = GetWindowLong(hwnd, GWL_ID); + int idNext, id = GetWindowLongPtr(hwnd, GWLP_ID); switch (message) { diff --git a/Chapter 23 A Taste of the Internet/NetTime/NetTime.c b/Chapter 23 A Taste of the Internet/NetTime/NetTime.c index fca834e..c2f0d63 100644 --- a/Chapter 23 A Taste of the Internet/NetTime/NetTime.c +++ b/Chapter 23 A Taste of the Internet/NetTime/NetTime.c @@ -4,8 +4,8 @@ -------------------------------------------------------*/ #define WIN32_LEAN_AND_MEAN -#include #include +#include #include #include "./resource.h" @@ -207,7 +207,7 @@ UINT_PTR CALLBACK MainDlg(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) GetWindowText(hwndButton, szOKLabel, sizeof(szOKLabel) / sizeof(TCHAR)); SetWindowText(hwndButton, TEXT("Cancel")); - SetWindowLong(hwndButton, GWL_ID, IDCANCEL); + SetWindowLongPtr(hwndButton, GWLP_ID, IDCANCEL); return TRUE; case IDCANCEL: @@ -215,7 +215,7 @@ UINT_PTR CALLBACK MainDlg(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) sock = 0; WSACleanup(); SetWindowText(hwndButton, szOKLabel); - SetWindowLong(hwndButton, GWL_ID, IDOK); + SetWindowLongPtr(hwndButton, GWLP_ID, IDOK); KillTimer(hwnd, ID_TIMER); EditPrintf(hwndEdit, TEXT("\r\nSocket closed.\r\n")); diff --git a/Chapter 23 A Taste of the Internet/UpdDemo/UpdDemo.c b/Chapter 23 A Taste of the Internet/UpdDemo/UpdDemo.c index e2b9d6c..4edc141 100644 --- a/Chapter 23 A Taste of the Internet/UpdDemo/UpdDemo.c +++ b/Chapter 23 A Taste of the Internet/UpdDemo/UpdDemo.c @@ -53,7 +53,7 @@ LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); BOOL CALLBACK DlgProc(HWND, UINT, WPARAM, LPARAM); VOID FtpThread(PVOID); VOID ButtonSwitch(HWND, HWND, TCHAR*); -FILELIST* GetFileList(VOID); +FILELIST* GetFileList(VOID); int Compare(const FILEINFO*, const FILEINFO*); // A couple globals @@ -424,7 +424,7 @@ VOID ButtonSwitch(HWND hwndStatus, HWND hwndButton, TCHAR * szText) SetWindowText(hwndStatus, TEXT("Internet Session Cancelled")); SetWindowText(hwndButton, TEXT("OK")); - SetWindowLong(hwndButton, GWL_ID, IDOK); + SetWindowLongPtr(hwndButton, GWLP_ID, IDOK); } /*----------------------------------------------------------------------- diff --git a/README.md b/README.md index a4bca80..ed8c3eb 100644 --- a/README.md +++ b/README.md @@ -18,12 +18,6 @@ This projects is being tweaked to use CMake. This works with both - Microsoft Visual Studio 2019 - JetBrains CLion with MinGW -Samples that require `Get/GetWindowLong()` do not work yet. These need to - be refactored to use `Get/GetWindowLongPtr().` - -Samples that require resources (anything in a `.rc` file) do not work yet. - The resource files need to be added to `CMakeLists.txt` - Updating C source code ----------------------