#include #include "modelessDialogue.h" const char className[] = "modelessDialogue"; const char APP_TITLE[] = "Modeless Dialogue Test"; const unsigned short int APP_WIDTH = 240; const unsigned short int APP_HEIGHT = 120; const unsigned short int ADD_NUM = 10; unsigned long int numAdd = 0; HWND handleDigTool = NULL; BOOL CALLBACK digToolEventListener (HWND hwnd, UINT event, WPARAM object, LPARAM lParam); LRESULT CALLBACK mainEventListener (HWND hwnd, UINT event, WPARAM object, LPARAM lParam); BOOL CALLBACK digToolEventListener (HWND hwnd, UINT event, WPARAM object, LPARAM lParam) { if (event == WM_COMMAND) //Action performed (e.g. button click) { int objectID = LOWORD(object); if (objectID == cmdToolAdd) { numAdd += ADD_NUM; MessageBox(hwnd, "Added 10 to the answer", className, MB_OK | MB_ICONINFORMATION); } else if (objectID == cmdToolShow) { char buffer[33]; //Create a buffer array of char itoa(numAdd, buffer, 10); //Convert the integer into chars and put them into the char array MessageBox(hwnd, buffer, className, MB_OK | MB_ICONEXCLAMATION); } } else { return false; } return true; } LRESULT CALLBACK mainEventListener (HWND hwnd, UINT event, WPARAM object, LPARAM lParam) { if (event == WM_CREATE) { handleDigTool = CreateDialog(GetModuleHandle(NULL), MAKEINTRESOURCE(digTool), hwnd, digToolEventListener); if (handleDigTool != NULL) { ShowWindow(handleDigTool, SW_SHOW); } else { MessageBox(hwnd, "Could not create modeless dialogue box", className, MB_OK | MB_ICONINFORMATION); } } else if (event == WM_COMMAND) { int objectID = LOWORD(object); if (objectID == mItemToolShow) { ShowWindow(handleDigTool, SW_SHOW); } else if (objectID == mItemToolHide) { ShowWindow(handleDigTool, SW_HIDE); } else if (objectID == mItemFileExit) { PostMessage(hwnd, WM_CLOSE, 0, 0); } } else if (event == WM_CLOSE) { DestroyWindow(hwnd); } else if (event == WM_DESTROY) { DestroyWindow(handleDigTool); PostQuitMessage(0); } else { return DefWindowProc(hwnd, event, object, lParam); } return 0; } int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR args, int nCmdShow) { WNDCLASSEX wc; //The class settings HWND hwnd; //Main window handler MSG Msg; //Here messages to the application are saved //Step 1: Registering the Window Class wc.cbSize = sizeof(WNDCLASSEX); wc.style = 0; wc.lpfnWndProc = mainEventListener; wc.cbClsExtra = 0; wc.cbWndExtra = 0; wc.hInstance = hInstance; wc.hIcon = LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(imgMainIcon)); wc.hCursor = LoadCursor(NULL, IDC_ARROW); wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); wc.lpszMenuName = MAKEINTRESOURCE(menuSystem); wc.lpszClassName = className; wc.hIconSm = (HICON)LoadImage(GetModuleHandle(NULL), MAKEINTRESOURCE(imgMainIcon), IMAGE_ICON, 16, 16, 0); if(!RegisterClassEx(&wc)) //If the class was not registered successfully { MessageBox(NULL, "Window Registration Failed!", "Error!", MB_ICONEXCLAMATION | MB_OK); return 0; } //Create the window hwnd = CreateWindowEx(WS_EX_CLIENTEDGE, className, APP_TITLE, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, APP_WIDTH, APP_HEIGHT, NULL, NULL, hInstance, NULL); if(hwnd == NULL) //If a problem occured with creating the window { MessageBox(NULL, "Window Creation Failed!", "Error!", MB_ICONEXCLAMATION | MB_OK); return 0; } ShowWindow(hwnd, nCmdShow); UpdateWindow(hwnd); //This keeps looping and processing messages when they become available //The while loop ends when a destroy message is sent while(GetMessage(&Msg, NULL, 0, 0) > 0) { if (!IsDialogMessage(handleDigTool, &Msg)) { TranslateMessage(&Msg); DispatchMessage(&Msg); } } return Msg.wParam; }