///////////////////////////////////////////////////////////////////// // FrameWindow.cpp - Demonstrate how to use ATL Window Classes to // // build a framewindow application. // // // // Jim Fawcett, CSE775 - Distributed Objects, Spring 2007 // ///////////////////////////////////////////////////////////////////// #include "stdafx.h" #include "FrameWindow.h" #define MAX_LOADSTRING 100 INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM); //----< constructor establishes window style >----------------------- CMainWindow::CMainWindow() { CWndClassInfo& wci = GetWndClassInfo(); if(!wci.m_atom) { wci.m_wc.lpszMenuName = MAKEINTRESOURCE(IDC_FRAMEWINDOW); wci.m_wc.hIcon = LoadIcon( _AtlBaseModule.GetResourceInstance(), MAKEINTRESOURCE(IDI_SMALL)); wci.m_wc.hbrBackground = CreateSolidBrush(RGB(250,250,200)); wci.m_wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC; } } //----< destructor returns deallocates font object >----------------- CMainWindow::~CMainWindow() { DeleteObject(font); } //----< Helper to create edit controls - could use atlctrls.h >------ HWND CMainWindow:: CreateEditControl(LPCTSTR text, int x, int y, int width, int height, short ID) { hEdit = CreateWindow( _T("Edit"), _T(""), WS_CHILD | WS_VISIBLE | WS_BORDER | ES_LEFT | CS_PARENTDC, x, y, width, height, m_hWnd, (HMENU)ID, hInst, NULL ); ::SendMessage(hEdit,WM_SETTEXT,0,(LPARAM)text); return (HWND)hEdit; } // //----< Event hander for window creation event >--------------------- LRESULT CMainWindow:: OnCreate(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled) { hInst = ((LPCREATESTRUCT)lParam)->hInstance; font = CreateFont( 46, 0, 0, 0, FW_SEMIBOLD, TRUE, FALSE, FALSE, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH, _T("tahoma") ); backgroundColor = RGB(250,250,200); hdc = BeginPaint(&ps); oldfont = SelectObject(hdc, font); oldcolor = SetBkColor(hdc,backgroundColor); EndPaint(&ps); RECT rect; GetClientRect(&rect); int EditWidth = rect.right - rect.left - 50; int EditX = 25; LPCTSTR text = _T("Enter text here"); hEdit1 = CreateEditControl(_T(""),EditX, 20, EditWidth, 20, IDC_EDIT1); SetReadOnly(hEdit1); hEdit2 = CreateEditControl(text, EditX, 60, EditWidth, 20, IDC_EDIT2); return 0; } //----< Event handler for window size change >----------------------- LRESULT CMainWindow:: OnSize(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled) { RECT rect; GetClientRect(&rect); int EditWidth = rect.right - rect.left - 50; int EditX = 25; ::MoveWindow(hEdit1, EditX, 20, EditWidth, 20, TRUE); ::MoveWindow(hEdit2, EditX, 60, EditWidth, 20, TRUE); return 0; } //----< Event handler for paint message >---------------------------- LRESULT CMainWindow:: OnPaint(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled) { hdc = BeginPaint(&ps); RECT rect; GetClientRect(&rect); DrawText( hdc,__T("Hello ATL Windows"), -1, &rect, DT_CENTER | DT_VCENTER | DT_SINGLELINE); EndPaint(&ps); return 0; } // //----< Event handler for left button click >------------------------ LRESULT CMainWindow:: OnLButton(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled) { MessageBox(_T("Left Button Down"),_T("OnLButton"),MB_OK); return 0; } //----< Event handler for widow destruction >------------------------ LRESULT CMainWindow:: OnDestroy(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled) { SelectObject(hdc, &oldfont); SetBkColor(hdc,oldcolor); DeleteObject(font); PostQuitMessage(0); return 0; } //----< Event handler for about box menu selection >----------------- LRESULT CMainWindow:: OnAbout(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled) { MessageBox(_T("ATL Windows Application"),_T("CSE775 - Distributed Objects"),MB_OK); return 0; } //----< Event handler for Exit menu selection >---------------------- LRESULT CMainWindow:: OnExit(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled) { m_hWnd = 0; OnDestroy(WM_DESTROY, 0, 0, bHandled); return 0; } //----< Edit box get line of text helper - stolen from Atlctl.h >---- int CMainWindow:: GetLine(HWND hEdit, int nIndex, LPTSTR lpszBuffer, int nMaxLength) const { *(LPWORD)lpszBuffer = (WORD)nMaxLength; return (int)::SendMessage(hEdit, EM_GETLINE, nIndex, (LPARAM)lpszBuffer); } //----< send text typed into Edit2 to Edit1 >------------------------ LRESULT CMainWindow:: OnEditChange(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled) { wchar_t buffer[255]; int size = GetLine((HWND)hEdit2,0,buffer,255); if(size >= 255) size = 254; buffer[size] = (wchar_t)'\0'; if(size > 0) SendMessage((HWND)hEdit1,WM_SETTEXT,0,(LPARAM)buffer); return 0; } //----< make edit window read only >--------------------------------- BOOL CMainWindow::SetReadOnly(HWND hEdit, BOOL bReadOnly) { return (BOOL)::SendMessage(hEdit, EM_SETREADONLY, bReadOnly, 0L); } // //----< entry point >------------------------------------------------ // // This function is essentially all that is left of the original // Win32 Windows application. Even this has much removed and added // since the wizard was here. int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE /* hPrevInstance */, LPTSTR lpCmdLine, int nCmdShow) { TCHAR szTitle[MAX_LOADSTRING]; // The title bar text LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING); CMainWindow wnd; wnd.Create( 0,CWindow::rcDefault, szTitle, WS_OVERLAPPEDWINDOW, WS_EX_CLIENTEDGE ); if(!wnd) return FALSE; int height = GetSystemMetrics(SM_CYSCREEN)/3; int width = GetSystemMetrics(SM_CXSCREEN)/3; wnd.MoveWindow(width, height, width, height, TRUE); wnd.ShowWindow(nCmdShow); wnd.UpdateWindow(); HACCEL hAccelTable; hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_FRAMEWINDOW)); MSG msg; while (GetMessage(&msg, NULL, 0, 0)) { if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) { TranslateMessage(&msg); DispatchMessage(&msg); } } return (int) msg.wParam; }