/////////////////////////////////////////////////////////////////////////// // CommonControls.cpp - demonstrates use of common controls // // // // Jim Fawcett, CSE775 - Distributed Objects, Spring 2008 // /////////////////////////////////////////////////////////////////////////// /* ver 1.1 : 21 Jan 08 - added listbox ver 1.0 : 28 Jan 07 - first release */ #include "stdafx.h" #include "CommonControls.h" #include #define MAX_LOADSTRING 100 #define IDC_LISTBOX 50 // Global Variables: HINSTANCE hInst; // current instance TCHAR szTitle[MAX_LOADSTRING]; // The title bar text TCHAR szWindowClass[MAX_LOADSTRING]; // the main window class name OPENFILENAME openfile; // structure for OpenFileDialog // 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); //----< Create window and start 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_COMMONCONTROLS, szWindowClass, MAX_LOADSTRING); MyRegisterClass(hInstance); // Perform application initialization: if (!InitInstance (hInstance, nCmdShow)) { return FALSE; } hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_COMMONCONTROLS)); // Main message loop: while (GetMessage(&msg, NULL, 0, 0)) { if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) { TranslateMessage(&msg); DispatchMessage(&msg); } } return (int) msg.wParam; } // //----< define and register windows class >-------------------------------- 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_COMMONCONTROLS)); wcex.hCursor = LoadCursor(NULL, IDC_ARROW); wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); wcex.lpszMenuName = MAKEINTRESOURCE(IDC_COMMONCONTROLS); wcex.lpszClassName = szWindowClass; wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL)); return RegisterClassEx(&wcex); } //----< create window and display it >------------------------------------- BOOL InitInstance(HINSTANCE hInstance, int nCmdShow) { HWND hWnd; hInst = hInstance; // Store instance handle in our global variable hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0, 700, 300, NULL, NULL, hInstance, NULL); if (!hWnd) { return FALSE; } ShowWindow(hWnd, nCmdShow); UpdateWindow(hWnd); return TRUE; } // //----< define structure for OpenFile dialog >----------------------------- void OpenFileInitialize(HWND hWnd) { static TCHAR filterStrs[] = TEXT("Text Files (*.TXT\0*.txt\0")\ TEXT("All Files (*.*)\0*.*\0\0"); openfile.lStructSize = sizeof(OPENFILENAME); openfile.hwndOwner = hWnd; openfile.hInstance = NULL; openfile.lpstrCustomFilter = NULL; openfile.lpstrFilter = filterStrs; openfile.nMaxCustFilter = 0; openfile.nFilterIndex = 0; openfile.lpstrFile = NULL; openfile.nMaxFile = MAX_PATH; openfile.lpstrFileTitle = NULL; openfile.Flags = 0; openfile.nFileOffset = 0; openfile.nFileExtension = 0; openfile.lpstrDefExt = TEXT("txt"); openfile.lCustData = 0L; openfile.lpfnHook = NULL; openfile.lpTemplateName = NULL; } //----< create and display OpenFile dialog >------------------------------- BOOL OpenFileDlg(HWND hWnd, PTSTR fileName, PTSTR dlgTitle) { openfile.hwndOwner = hWnd; openfile.lpstrFile = fileName; openfile.lpstrFileTitle = dlgTitle; openfile.Flags = OFN_HIDEREADONLY | OFN_CREATEPROMPT; return GetOpenFileName(&openfile); } // //----< process windows messages >----------------------------------------- LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { int wmId, wmEvent; PAINTSTRUCT ps; HDC hdc; static HWND hLB = NULL; static TCHAR fileName[MAX_PATH], fileDlgTitle[MAX_PATH]; static TCHAR* text; static int TextX = 20; static int TextY = 20; static int TextYDelta = 20; static int lineNum = 0; static int top, left, width, height; RECT rect; switch (message) { case WM_CREATE: GetClientRect(hWnd,&rect); width = rect.right - rect.left; height = rect.bottom - rect.top - 100; left = (int)(0.1*width); top = 50; width = (int)(0.8*width); hLB = CreateWindow( _T("listbox"), NULL, WS_CHILD | WS_VISIBLE | WS_BORDER | WS_VSCROLL | LBS_NOTIFY, left, top, width, height, hWnd, (HMENU)IDC_LISTBOX, hInst, NULL ); SendMessage(hLB,LB_ADDSTRING,NULL,(LPARAM)_T("CSE775")); SendMessage(hLB,LB_ADDSTRING,NULL,(LPARAM)_T("Distributed Objects")); break; case WM_COMMAND: wmId = LOWORD(wParam); wmEvent = HIWORD(wParam); // Parse the menu selections: switch (wmId) { case IDC_LISTBOX: if(wmEvent == LBN_SELCHANGE) { int sel = (int)SendMessage(hLB,LB_GETCURSEL,0,0); int size = (int)SendMessage(hLB,LB_GETTEXTLEN,sel,0); TCHAR* buffer = (TCHAR*)malloc(2*size*sizeof(TCHAR)); SendMessage(hLB,LB_GETTEXT,sel,(LPARAM)buffer); MessageBox(NULL,buffer,L"item selected",MB_OK); free(buffer); } break; case ID_FILE_OPENFILE: OpenFileInitialize(hWnd); OpenFileDlg(hWnd,fileName,fileDlgTitle); SendMessage(hLB,LB_ADDSTRING,NULL,(LPARAM)fileName); InvalidateRect(hWnd,NULL,TRUE); break; case IDM_ABOUT: DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About); break; case IDM_EXIT: DestroyWindow(hWnd); break; default: return DefWindowProc(hWnd, message, wParam, lParam); } break; case WM_SIZE: GetClientRect(hWnd,&rect); width = rect.right - rect.left; height = rect.bottom - rect.top - 100; left = (int)(0.1*width); top = 50; width = (int)(0.8*width); MoveWindow(hLB,left,top,width,height,true); InvalidateRect(hWnd,NULL,true); break; case WM_PAINT: hdc = BeginPaint(hWnd, &ps); text = L"CSE775 Demonstration of Win32 Windows"; TextOut(hdc,left,TextY,text,(int)_tcslen(text)); 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: return (INT_PTR)TRUE; case WM_COMMAND: if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL) { EndDialog(hDlg, LOWORD(wParam)); return (INT_PTR)TRUE; } break; } return (INT_PTR)FALSE; }