|
| 1 | +//********************************************************* |
| 2 | +// |
| 3 | +// Copyright (c) Microsoft. All rights reserved. |
| 4 | +// This code is licensed under the MIT License (MIT). |
| 5 | +// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, |
| 6 | +// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 7 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 8 | +// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
| 9 | +// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
| 10 | +// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH |
| 11 | +// THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 12 | +// |
| 13 | +//********************************************************* |
| 14 | + |
| 15 | +// main.cpp : Defines the entry point for the application. |
| 16 | + |
| 17 | +#include "stdafx.h" |
| 18 | +#include "AdvancedColorImages.h" |
| 19 | +#include "WinComp.h" |
| 20 | + |
| 21 | +using namespace winrt; |
| 22 | +using namespace Windows::UI; |
| 23 | +using namespace Windows::UI::Composition; |
| 24 | +using namespace Windows::UI::Composition::Desktop; |
| 25 | +using namespace Windows::UI::Input; |
| 26 | + |
| 27 | + |
| 28 | +#define MAX_LOADSTRING 100 |
| 29 | + |
| 30 | +// Global Variables: |
| 31 | +HINSTANCE hInst; // current instance |
| 32 | +WCHAR szTitle[MAX_LOADSTRING]; // The title bar text |
| 33 | +WCHAR szWindowClass[MAX_LOADSTRING]; // the main window class name |
| 34 | +HWND m_childHWnd; |
| 35 | +WinComp* m_winComp; |
| 36 | + |
| 37 | +// Forward declarations of functions included in this code module: |
| 38 | +ATOM MyRegisterClass(HINSTANCE hInstance); |
| 39 | +BOOL InitInstance(HINSTANCE, int); |
| 40 | +LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); |
| 41 | +INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM); |
| 42 | + |
| 43 | +int APIENTRY wWinMain(_In_ HINSTANCE hInstance, |
| 44 | + _In_opt_ HINSTANCE hPrevInstance, |
| 45 | + _In_ LPWSTR lpCmdLine, |
| 46 | + _In_ int nCmdShow) |
| 47 | +{ |
| 48 | + |
| 49 | + |
| 50 | + UNREFERENCED_PARAMETER(hPrevInstance); |
| 51 | + UNREFERENCED_PARAMETER(lpCmdLine); |
| 52 | + |
| 53 | + winrt::init_apartment(winrt::apartment_type::single_threaded); |
| 54 | + |
| 55 | + // Initialize global strings |
| 56 | + LoadStringW(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING); |
| 57 | + LoadStringW(hInstance, IDC_ADVANCEDCOLORIMAGES, szWindowClass, MAX_LOADSTRING); |
| 58 | + MyRegisterClass(hInstance); |
| 59 | + // Perform application initialization: |
| 60 | + if (!InitInstance(hInstance, nCmdShow)) |
| 61 | + { |
| 62 | + return FALSE; |
| 63 | + } |
| 64 | + |
| 65 | + HACCEL hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_ADVANCEDCOLORIMAGES)); |
| 66 | + |
| 67 | + MSG msg; |
| 68 | + |
| 69 | + // Main message loop: |
| 70 | + while (GetMessage(&msg, nullptr, 0, 0)) |
| 71 | + { |
| 72 | + if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) |
| 73 | + { |
| 74 | + TranslateMessage(&msg); |
| 75 | + DispatchMessage(&msg); |
| 76 | + } |
| 77 | + } |
| 78 | + return (int)msg.wParam; |
| 79 | +} |
| 80 | + |
| 81 | + |
| 82 | +// |
| 83 | +// FUNCTION: MyRegisterClass() |
| 84 | +// |
| 85 | +// PURPOSE: Registers the window class. |
| 86 | +// |
| 87 | +ATOM MyRegisterClass(HINSTANCE hInstance) |
| 88 | +{ |
| 89 | + WNDCLASSEXW wcex; |
| 90 | + |
| 91 | + wcex.cbSize = sizeof(WNDCLASSEX); |
| 92 | + |
| 93 | + wcex.style = CS_HREDRAW | CS_VREDRAW; |
| 94 | + wcex.lpfnWndProc = WndProc; |
| 95 | + wcex.cbClsExtra = 0; |
| 96 | + wcex.cbWndExtra = 0; |
| 97 | + wcex.hInstance = hInstance; |
| 98 | + wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_ADVANCEDCOLORIMAGES)); |
| 99 | + wcex.hCursor = LoadCursor(nullptr, IDC_ARROW); |
| 100 | + wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1); |
| 101 | + wcex.lpszMenuName = MAKEINTRESOURCEW(IDC_ADVANCEDCOLORIMAGES); |
| 102 | + wcex.lpszClassName = szWindowClass; |
| 103 | + wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL)); |
| 104 | + |
| 105 | + return RegisterClassExW(&wcex); |
| 106 | +} |
| 107 | + |
| 108 | +// |
| 109 | +// FUNCTION: InitInstance(HINSTANCE, int) |
| 110 | +// |
| 111 | +// PURPOSE: Saves instance handle and creates main window |
| 112 | +// |
| 113 | +// COMMENTS: |
| 114 | +// |
| 115 | +// In this function, we save the instance handle in a global variable and |
| 116 | +// create and display the main program window. |
| 117 | +// |
| 118 | +BOOL InitInstance(HINSTANCE hInstance, int nCmdShow) |
| 119 | +{ |
| 120 | + hInst = hInstance; // Store instance handle in our global variable |
| 121 | + |
| 122 | + HWND hWndParent = CreateWindowW(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW, |
| 123 | + CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, nullptr, nullptr, hInstance, nullptr); |
| 124 | + |
| 125 | + //SetWindowPos(hWndParent, HWND_TOPMOST, 0, 0, 300, 300, SWP_SHOWWINDOW); |
| 126 | + if (!hWndParent) |
| 127 | + { |
| 128 | + return FALSE; |
| 129 | + } |
| 130 | + |
| 131 | + //Create the child HWND with the same size as the parent HWND, so it fills up the entire space. |
| 132 | + |
| 133 | + RECT rect; |
| 134 | + ::GetWindowRect(hWndParent, &rect); |
| 135 | + |
| 136 | + m_childHWnd = CreateWindowW(szWindowClass, szTitle, WS_CHILD, |
| 137 | + 0, 0, rect.right - rect.left, rect.bottom - rect.top, |
| 138 | + hWndParent, nullptr, hInstance, nullptr); |
| 139 | + |
| 140 | + if (!m_childHWnd) |
| 141 | + { |
| 142 | + return FALSE; |
| 143 | + } |
| 144 | + m_winComp = new WinComp(); |
| 145 | + // Ensure that the DispatcherQueue is initialized. This is required by the Compositor. |
| 146 | + auto controller = m_winComp->EnsureDispatcherQueue(); |
| 147 | + |
| 148 | + ShowWindow(hWndParent, nCmdShow); |
| 149 | + UpdateWindow(hWndParent); |
| 150 | + ShowWindow(m_childHWnd, nCmdShow); |
| 151 | + UpdateWindow(m_childHWnd); |
| 152 | + m_winComp->Initialize(m_childHWnd); |
| 153 | + m_winComp->PrepareVisuals(); |
| 154 | + m_winComp->ConfigureInteraction(); |
| 155 | + m_winComp->LoadImageFromFileName(L"hdr-image.jxr"); |
| 156 | + m_winComp->UpdateViewPort(true); |
| 157 | + return TRUE; |
| 158 | +} |
| 159 | + |
| 160 | +bool LocateImageFile(HWND hWnd, LPWSTR pszFileName, DWORD cchFileName) |
| 161 | +{ |
| 162 | + pszFileName[0] = L'\0'; |
| 163 | + |
| 164 | + OPENFILENAME ofn; |
| 165 | + ZeroMemory(&ofn, sizeof(ofn)); |
| 166 | + |
| 167 | + ofn.lStructSize = sizeof(ofn); |
| 168 | + ofn.hwndOwner = hWnd; |
| 169 | + ofn.lpstrFilter = L"All Image Files\0" L"*.bmp;*.dib;*.wdp;*.mdp;*.hdp;*.gif;*.png;*.jpg;*.jpeg;*.tif;*.ico;*.jxr\0" |
| 170 | + L"JPEG File Interchange Format\0" L"*.jpg;*.jpeg\0" |
| 171 | + L"JPEG XR Extented Range Format\0" L"*.jxr\0" |
| 172 | + L"Windows Bitmap\0" L"*.bmp;*.dib\0" |
| 173 | + L"High Definition Photo\0" L"*.wdp;*.mdp;*.hdp\0" |
| 174 | + L"Graphics Interchange Format\0" L"*.gif\0" |
| 175 | + L"Portable Network Graphics\0" L"*.png\0" |
| 176 | + L"Tiff File\0" L"*.tif\0" |
| 177 | + L"Icon\0" L"*.ico\0" |
| 178 | + L"All Files\0" L"*.*\0" |
| 179 | + L"\0"; |
| 180 | + ofn.lpstrFile = pszFileName; |
| 181 | + ofn.nMaxFile = cchFileName; |
| 182 | + ofn.lpstrTitle = L"Open Image"; |
| 183 | + ofn.Flags = OFN_FILEMUSTEXIST | OFN_PATHMUSTEXIST; |
| 184 | + |
| 185 | + // Display the Open dialog box. |
| 186 | + return (GetOpenFileName(&ofn) == TRUE); |
| 187 | +} |
| 188 | + |
| 189 | +// |
| 190 | +// FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM) |
| 191 | +// |
| 192 | +// PURPOSE: Processes messages for the main window. |
| 193 | +// |
| 194 | +// WM_COMMAND - process the application menu |
| 195 | +// WM_PAINT - Paint the main window |
| 196 | +// WM_DESTROY - post a quit message and return |
| 197 | +// |
| 198 | +// |
| 199 | +LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) |
| 200 | +{ |
| 201 | + switch (message) |
| 202 | + { |
| 203 | + case WM_COMMAND: |
| 204 | + { |
| 205 | + int wmId = LOWORD(wParam); |
| 206 | + // Parse the menu selections: |
| 207 | + switch (wmId) |
| 208 | + { |
| 209 | + |
| 210 | + case IDM_FILE: |
| 211 | + WCHAR szFileName[MAX_PATH]; |
| 212 | + |
| 213 | + if (LocateImageFile(hWnd, szFileName, ARRAYSIZE(szFileName))) |
| 214 | + { |
| 215 | + m_winComp->LoadImageFromFileName(szFileName); |
| 216 | + } |
| 217 | + else |
| 218 | + { |
| 219 | + MessageBox(hWnd, L"Failed to load image, select a new one.", L"Application Error", MB_ICONEXCLAMATION | MB_OK); |
| 220 | + } |
| 221 | + |
| 222 | + break; |
| 223 | + |
| 224 | + case IDM_ABOUT: |
| 225 | + DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About); |
| 226 | + break; |
| 227 | + case IDM_EXIT: |
| 228 | + DestroyWindow(hWnd); |
| 229 | + break; |
| 230 | + default: |
| 231 | + return DefWindowProc(hWnd, message, wParam, lParam); |
| 232 | + } |
| 233 | + } |
| 234 | + break; |
| 235 | + case WM_POINTERDOWN: |
| 236 | + { |
| 237 | + //Redirect input events to the InteractionTracker for input events. |
| 238 | + PointerPoint pp = PointerPoint::GetCurrentPoint(GET_POINTERID_WPARAM(wParam)); |
| 239 | + m_winComp->TryRedirectForManipulation(pp); |
| 240 | + break; |
| 241 | + } |
| 242 | + |
| 243 | + |
| 244 | + case WM_PAINT: |
| 245 | + { |
| 246 | + PAINTSTRUCT ps; |
| 247 | + HDC hdc = BeginPaint(hWnd, &ps); |
| 248 | + EndPaint(hWnd, &ps); |
| 249 | + } |
| 250 | + break; |
| 251 | + |
| 252 | + case WM_SIZE: |
| 253 | + { |
| 254 | + //Update the child HWND to the new size of the parent HWnd. |
| 255 | + RECT windowRect; |
| 256 | + ::GetWindowRect(hWnd, &windowRect); |
| 257 | + ::SetWindowPos(m_childHWnd, HWND_TOP, 0, 0, windowRect.right - windowRect.left, windowRect.bottom - windowRect.top, SWP_NOZORDER); |
| 258 | + if (m_winComp != nullptr) |
| 259 | + m_winComp->UpdateViewPort(true); |
| 260 | + } |
| 261 | + break; |
| 262 | + case WM_DESTROY: |
| 263 | + PostQuitMessage(0); |
| 264 | + break; |
| 265 | + default: |
| 266 | + return DefWindowProc(hWnd, message, wParam, lParam); |
| 267 | + } |
| 268 | + return 0; |
| 269 | +} |
| 270 | + |
| 271 | +// Message handler for about box. |
| 272 | +INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam) |
| 273 | +{ |
| 274 | + UNREFERENCED_PARAMETER(lParam); |
| 275 | + switch (message) |
| 276 | + { |
| 277 | + case WM_INITDIALOG: |
| 278 | + return (INT_PTR)TRUE; |
| 279 | + |
| 280 | + case WM_COMMAND: |
| 281 | + if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL) |
| 282 | + { |
| 283 | + EndDialog(hDlg, LOWORD(wParam)); |
| 284 | + return (INT_PTR)TRUE; |
| 285 | + } |
| 286 | + break; |
| 287 | + } |
| 288 | + return (INT_PTR)FALSE; |
| 289 | +} |
0 commit comments