///////////////////////////////////////////////////////////////////////// // FrameWithDialog.cpp - Demonstrates message-based communication // // between parent frame window and child dialog // // // // Jim Fawcett, CSE775 - Distributed Objects, Spring 2005 // ///////////////////////////////////////////////////////////////////////// /* ver 1.1 : 20 January 08 - fixed bug due to incorrect handling of multiple dialogs by limiting creation to one dialog at a time. ver 1.0 : 27 January 07 - first release */ #include "stdafx.h" #include "FrameWithDialog.h" #define MAX_LOADSTRING 100 // Global Variables: HINSTANCE hInst; // current instance HWND hWnd; // main window HWND hModeless; // modeless dialogbox TCHAR szTitle[MAX_LOADSTRING]; // title bar text TCHAR szWindowClass[MAX_LOADSTRING]; // main window class name static TCHAR* text; // display text static TCHAR buffer[150]; // buffer for display static INT count = 0; // number of created dialogs // Forward declarations of functions included in this code module: ATOM MyRegisterClass(HINSTANCE hInstance); BOOL InitInstance(HINSTANCE, int); LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM); BOOL CALLBACK ModelessResponse(HWND, UINT, WPARAM, LPARAM); // //----< WinMain has Message Loop >--------------------------------------- int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow) { UNREFERENCED_PARAMETER(hPrevInstance); UNREFERENCED_PARAMETER(lpCmdLine); MSG msg; HACCEL hAccelTable; // Initialize global strings LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING); LoadString(hInstance, IDC_FRAMEWITHDIALOG, szWindowClass, MAX_LOADSTRING); MyRegisterClass(hInstance); // Perform application initialization: if (!InitInstance (hInstance, nCmdShow)) { return FALSE; } hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_FRAMEWITHDIALOG)); // start message loop: while (GetMessage(&msg, NULL, 0, 0)) { if(hModeless == 0 || !IsDialogMessage(hModeless, &msg)) { if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) { TranslateMessage(&msg); DispatchMessage(&msg); } } } return (int) msg.wParam; } // //----< Establish Framewindow properties >------------------------------- ATOM MyRegisterClass(HINSTANCE hInstance) { WNDCLASSEX wcex; wcex.cbSize = sizeof(WNDCLASSEX); wcex.style = CS_HREDRAW | CS_VREDRAW; wcex.lpfnWndProc = WndProc; wcex.cbClsExtra = 0; wcex.cbWndExtra = 0; wcex.hInstance = hInstance; wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_FRAMEWITHDIALOG)); wcex.hCursor = LoadCursor(NULL, IDC_ARROW); wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); wcex.lpszMenuName = MAKEINTRESOURCE(IDC_FRAMEWITHDIALOG); wcex.lpszClassName = szWindowClass; wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL)); return RegisterClassEx(&wcex); } //----< Start application by creating main Frame window >---------------- BOOL InitInstance(HINSTANCE hInstance, int nCmdShow) { hInst = hInstance; // Store instance handle in global variable hWnd = CreateWindow( szWindowClass, szTitle, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL ); if (!hWnd) { return FALSE; } text = L""; RECT rect; GetWindowRect(hWnd,&rect); MoveWindow(hWnd,rect.left,rect.top,500,300,TRUE); ShowWindow(hWnd, nCmdShow); UpdateWindow(hWnd); return TRUE; } // //----< Message Handler for Frame window >------------------------------- // // WM_COMMAND - process the application menu and dialog events // WM_PAINT - Paint the main window // WM_DESTROY - post a quit message and return // LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { int wmId, wmEvent; PAINTSTRUCT ps; HDC hdc; RECT rect; // int count = 0; switch (message) { case WM_COMMAND: wmId = LOWORD(wParam); wmEvent = HIWORD(wParam); // Parse the menu selections and dialog events: switch (wmId) { case IDM_ABOUT: DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About); break; case IDM_EXIT: DestroyWindow(hWnd); break; case ID_FILE_CREATEDIALOG: if(count == 1) break; count = 1; hModeless = CreateDialog( hInst, // appl with dlg resource MAKEINTRESOURCE(IDD_DIALOG1), // dlg resource template hWnd, // parent ModelessResponse // dlg callback proc ); if(hModeless == NULL) MessageBox(hWnd,L"can't open modeless dialog",L"Error",MB_OK); else { GetWindowRect(hWnd,&rect); // get pos of parent SetWindowPos( hModeless, // modeless dialog HWND_TOP, // move to top 200+rect.left,200+rect.top,0,0, // position relative to parent SWP_NOSIZE | SWP_FRAMECHANGED // don't change size ); ShowWindow(hModeless, SW_SHOW); HWND hEdit = GetDlgItem(hModeless,IDC_EDIT1); SetFocus(hEdit); } break; case IDC_BUTTON1: // never executed because dialog messages are sent to // ModelessResponse, not WndProc MessageBox(0,L"ButtonClicked",L"Test",MB_OK); break; default: return DefWindowProc(hWnd, message, wParam, lParam); } break; case WM_PAINT: hdc = BeginPaint(hWnd, &ps); GetClientRect(hWnd,&rect); DrawText(hdc,text,wcslen(text),&rect,DT_SINGLELINE|DT_CENTER|DT_VCENTER); EndPaint(hWnd, &ps); break; case WM_DESTROY: PostQuitMessage(0); break; default: return DefWindowProc(hWnd, message, wParam, lParam); } return 0; } // //----< Message handler for about box >---------------------------------- INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam) { UNREFERENCED_PARAMETER(lParam); switch (message) { case WM_INITDIALOG: RECT rect; GetWindowRect(hWnd,&rect); MoveWindow(hDlg,rect.left + 280,rect.top + 225,500,300,TRUE); return (INT_PTR)TRUE; case WM_COMMAND: if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL) { EndDialog(hDlg, LOWORD(wParam)); if(LOWORD(wParam) == IDOK) text = L"About box returned OK"; else text = L"Aboutbox returned Cancel"; RECT rect; GetClientRect(hWnd,&rect); InvalidateRect(hWnd,&rect,TRUE); return (INT_PTR)TRUE; } break; } return (INT_PTR)FALSE; } // //----< Message handler for modeless dialogbox >------------------------- BOOL CALLBACK ModelessResponse(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam) { UNREFERENCED_PARAMETER(lParam); switch (message) { case WM_INITDIALOG: text = L"Modeless dialog created"; InvalidateRect(hWnd, NULL, TRUE); return TRUE; case WM_COMMAND: if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL) { DestroyWindow(hModeless); text = L"Modeless dialog destroyed"; InvalidateRect(hWnd, NULL, TRUE); count = 0; return TRUE; } if(LOWORD(wParam) == IDC_BUTTON1) { // button event text = L"Dialog Button clicked"; buffer[0] = wchar_t(0); SetDlgItemText(hModeless,IDC_EDIT1,buffer); InvalidateRect(hWnd, NULL, TRUE); } if(LOWORD(wParam) == IDC_EDIT1) { // something changed in EditBox so show the text GetDlgItemText(hModeless,IDC_EDIT1,buffer,50); text = &buffer[0]; InvalidateRect(hWnd, NULL, TRUE); } break; default: /////////////////////////////////// //Test message traffic /* wsprintf( buffer, L"DialogMessage: 0x%x, HIWORD(wParam): %x, LOWORD(wParam): %x", message, HIWORD(wParam), LOWORD(wParam));*/ ; } return (INT_PTR)FALSE; }