// Hello585.cpp // This program is an extension to the Hello program. // It demonstrates the use of controls and resources: // buttons, menus, dialogs, and bitmaps. // Loosely based on examples in: // Programming Windows 95 with MFC by Jeff Prosise, // Microsoft Press, 1996. // Mike Barnes #include #include "Hello585.h" #include "resource.h" CMyApp myApp; BOOL CMyApp::InitInstance () { m_pMainWnd = new CMainWindow; m_pMainWnd->ShowWindow (m_nCmdShow); m_pMainWnd->UpdateWindow (); return TRUE; } // CMainWindow message map and member functions // Message maps use macro expansion to setup message handlers. BEGIN_MESSAGE_MAP (CMainWindow, CFrameWnd) ON_WM_CREATE() ON_WM_PAINT () ON_WM_SIZE () ON_WM_LBUTTONUP () ON_COMMAND_RANGE (IDM_ARIAL, IDM_COURIER, OnFont) ON_COMMAND (IDM_USAGE, OnUsage) ON_COMMAND (IDM_ABOUT, OnAbout) END_MESSAGE_MAP () // MainWindow constructor CMainWindow::CMainWindow () { HICON hIcon = AfxGetApp() -> LoadIcon(IDI_ICON1); // create main window, menu & load keyboard accelerators CRect rect; rect.SetRect(0, 0, minWidth, minHeight); // (x,y, x1, y1) m_clientWidth = minWidth; m_clientHeight = minHeight; Create (NULL, "Hello CS 585", WS_OVERLAPPEDWINDOW, rect, NULL, MAKEINTRESOURCE(IDR_MENU1)); LoadAccelTable(MAKEINTRESOURCE(IDR_ACCELERATOR1)); SetIcon(hIcon, TRUE); // set application icon // get client rect after menu loaded // set up button pallet, and push buttons m_buttonPanel = new CPanelWindow (this); // create some fonts m_CourierFont.CreatePointFont(180, "Courier New", NULL); m_TimesFont.CreatePointFont(140, "Times New Roman", NULL); m_ArialFont.CreatePointFont(100, "Arial", NULL); // miscellaneous settings strcpy(message, "Click left button in client area. "); m_CurrentColor = 0; m_CurrentFont = IDM_ARIAL; } void CMainWindow::OnPaint () { // get a paint device context for this window CPaintDC dc (this); CRect rect; GetClientRect(&rect); // subtract button pallette from client rect for text rect.SetRect(rect.left + palletteWidth, rect.top, rect.right, rect.bottom); dc.DrawText (message, -1, &rect, DT_SINGLELINE | DT_VCENTER | DT_CENTER ); } void CMainWindow::OnSize (UINT nType, int width, int height) { CRect rect; int padHeight, lastY1, lastY2; // MessageBox("On Size"); // for debugging // get new window size and compute height GetClientRect(&rect); if(((rect.bottom - rect.top) < minHeight) || ((rect.right - rect.left) < minWidth)) { MoveWindow(0, 0, minWidth, minHeight); m_clientHeight = minHeight - menuHeight;} else m_clientHeight = rect.bottom - rect.top; // change size of pallette window m_buttonPanel->MoveWindow(0, 0, palletteWidth, m_clientHeight); // determine padding and setup 3 buttons padHeight = (m_clientHeight - palletteHeight) / 2; lastY1 = padHeight; lastY2 = lastY1 + buttonHeight; m_buttonPanel->m_ctlRed.MoveWindow(buttonX1, lastY1, buttonWidth, buttonHeight); lastY1 = lastY2 + padY; lastY2 = lastY1 + buttonHeight; m_buttonPanel->m_ctlBlue.MoveWindow(buttonX1, lastY1, buttonWidth, buttonHeight); lastY1 = lastY2 + padY; lastY2 = lastY1 + buttonHeight; m_buttonPanel->m_ctlGreen.MoveWindow(buttonX1, lastY1, buttonWidth, buttonHeight); } void CMainWindow::OnLButtonUp (UINT nFlags, CPoint point) { CClientDC dc (this); // get a client DC not paint DC. switch(m_CurrentFont) { case IDM_ARIAL : dc.SelectObject(&m_ArialFont); break; case IDM_TIMES : dc.SelectObject(&m_TimesFont); break; case IDM_COURIER : dc.SelectObject(&m_CourierFont); } dc.SetTextColor(crColors[m_CurrentColor]); dc.TextOut (point.x, point.y, "Hi CS 585!"); } void CMainWindow::OnFont (UINT nID) { // update font checked item status for (int i = IDM_ARIAL; i <= IDM_COURIER; i++) GetMenu()->CheckMenuItem(i, MF_UNCHECKED); GetMenu()->CheckMenuItem(nID, MF_CHECKED); m_CurrentFont = nID; // store current font } void CMainWindow::OnAbout () { CDialog aboutDlg(IDD_ABOUT_DIALOG, this); aboutDlg.DoModal(); } void CMainWindow::OnUsage () { CDialog usageDlg(IDD_USAGE_DIALOG, this); usageDlg.DoModal(); } // CPanel Message Map and Member Functions BEGIN_MESSAGE_MAP (CPanelWindow, CWnd) ON_WM_PAINT() ON_CONTROL_RANGE(BN_CLICKED, IDC_RED, IDC_GREEN, OnButton) END_MESSAGE_MAP () CPanelWindow :: CPanelWindow(CMainWindow * myParent) { CRect rect; int padHeight, lastY1, lastY2; rect.SetRect(0, 0, palletteWidth, myParent->m_clientHeight - menuHeight); Create(NULL, "",WS_CHILD |WS_VISIBLE | WS_DLGFRAME, rect, myParent, NULL); padHeight = (myParent->m_clientHeight - palletteHeight)/2; lastY1 = padHeight; lastY2 = padHeight + buttonHeight; rect.SetRect(buttonX1, lastY1, buttonX2, lastY2); m_ctlRed.Create("&Red", WS_CHILD | WS_VISIBLE, rect, this, IDC_RED); lastY1 = lastY2 + padY; lastY2 = lastY1 + buttonHeight; rect.SetRect(buttonX1, lastY1 , buttonX2, lastY2); m_ctlBlue.Create("&Blue", WS_CHILD | WS_VISIBLE, rect, this, IDC_BLUE); lastY1 = lastY2 + padY; lastY2 = lastY1 + buttonHeight; rect.SetRect(buttonX1, lastY1 , buttonX2, lastY2); m_ctlGreen.Create("&Green", WS_CHILD | WS_VISIBLE, rect, this, IDC_GREEN); } void CPanelWindow::OnButton(UINT nID) { // MessageBox("On Button clicked CPanelWindow"); // for debugging m_CurrentColor = nID - IDC_RED; } void CPanelWindow::OnPaint () { CPaintDC dc (this); CRect rect; GetClientRect(&rect); dc.FillSolidRect(rect, crColors[3]); }