//***** // THIS PROGRAM illustrates the editing of messages using a set of C++ control objects // Particularly powerful is the CEdit class of objects, which is demonstrated here. // This object has cut/paste/copy built in // Because the MsgFolder is not implemented yet, the user must type in the name of the // Message file // This version also implements HOT SPOTS on the main window which simulate the // actions of the three buttons. // ILLUSTRATES: // 1) some primitive drawing to identify the hot spots // 2) Handling of Mouse Clicks // 3) Sending of Messages within an application (hot spot sends button message) // Shows somewhat disconnected relationship between message handlers and message generators // // Also should really define a focus order to be able to tab between edit areas. - next time // AUTHOR: Chris Wild // DATE: September 15. 1997 #include "main.h" #include // Create an instance of the application CMsgApp msgApp; // Init the application and the main window BOOL CMsgApp::InitInstance() { m_pMainWnd = new CMainWindow(); m_pMainWnd->ShowWindow(m_nCmdShow); m_pMainWnd->UpdateWindow(); return TRUE; } // Message map - maps message IDs to their handlers BEGIN_MESSAGE_MAP( CMainWindow, CWnd ) ON_COMMAND(IDC_SAVE, HandleSave) ON_COMMAND(IDC_CLEAR, HandleClear) ON_COMMAND(IDC_LOAD, HandleLoad) ON_WM_LBUTTONUP() ON_WM_PAINT() END_MESSAGE_MAP() // Window constructor CMainWindow::CMainWindow() { // Create the window Create(NULL, "Edit Message Demo", WS_OVERLAPPED | WS_MINIMIZEBOX | WS_SYSMENU, CRect(0,0,500,750)); MakeDlg(); // create edit control windows } CMainWindow::~CMainWindow() { } void CMainWindow::MakeDlg() { CRect rect; // used to get size of window GetClientRect(&rect); // get size of window // Create the labels m_msgIDTitle.Create("Message ID", WS_CHILD|WS_VISIBLE, CRect(CPoint(0,0),CSize(150,24)), this); m_topicTitle.Create("Message Topic", WS_CHILD|WS_VISIBLE, CRect(CPoint(0,60),CSize(150,24)), this); m_textTitle.Create("Message Text", WS_CHILD|WS_VISIBLE, CRect(CPoint(0,120),CSize(150,24)), this); // Create the edit controls // For demo let user type in message file name m_msgID.Create( WS_CHILD|WS_VISIBLE|WS_BORDER|WS_TABSTOP, CRect(CPoint(50,30),CSize(440,24)), this, IDC_MSGID); m_msgID.LimitText(10); // topic edit area m_topic.Create( WS_CHILD|WS_VISIBLE|WS_BORDER|WS_TABSTOP, CRect(CPoint(50,90),CSize(440,24)), this, IDC_TOPIC); m_topic.LimitText(SIZE_TOPIC); // text edit area m_text.Create( WS_CHILD|WS_VISIBLE|WS_BORDER|WS_TABSTOP|WS_VSCROLL|ES_MULTILINE, CRect(CPoint(50,150),CSize(440,450)), this, IDC_TEXT); m_text.LimitText(SIZE_TEXT); // Create buttons m_save.Create("&Save", WS_CHILD|WS_VISIBLE|WS_TABSTOP|BS_PUSHBUTTON, CRect(CPoint(0,600),CSize(100,50)), this, IDC_SAVE); m_load.Create("&Load", WS_CHILD|WS_VISIBLE|WS_TABSTOP, CRect(CPoint(200,600),CSize(100,50)), this, IDC_LOAD); m_clear.Create("&Clear", WS_CHILD|WS_VISIBLE|WS_TABSTOP, CRect(CPoint(400,600),CSize(100,50)), this, IDC_CLEAR); m_topic.SetFocus(); } // Handle the save button void CMainWindow::HandleSave() // Saves the message { CString topic; CString text; CString fileName; HANDLE msgFile; m_topic.GetWindowText(topic); msgApp.msg.SetTopic((LPCTSTR)topic); m_text.GetWindowText(text); msgApp.msg.SetText((LPCTSTR)text); m_msgID.GetWindowText(fileName); msgFile = CreateFile(fileName,GENERIC_WRITE,0,0,CREATE_ALWAYS,0,0); if(msgFile == INVALID_HANDLE_VALUE) { cerr << "Error: " << GetLastError() << " when creating file:" << fileName << endl; ExitProcess(1); } if(!msgApp.msg.WriteMsg(msgFile)) { cerr << " Error writing message msg1\n"; ExitProcess(1); } CloseHandle(msgFile); } void CMainWindow::HandleLoad() // Saves the message { char topic[SIZE_TOPIC]; char text[SIZE_TEXT]; HANDLE msgFile; char fileName[SIZE_FILENAME+1]; m_msgID.GetLine(0, fileName, SIZE_FILENAME); msgFile = CreateFile(fileName,GENERIC_READ,0,0,OPEN_EXISTING,0,0); if(msgFile == INVALID_HANDLE_VALUE) { cerr << "Error: " << GetLastError() << " when opening file: " << fileName << endl; ExitProcess(1); } msgApp.msg.ReadMsg(msgFile); CloseHandle(msgFile); msgApp.msg.GetTopic(topic); msgApp.msg.GetText(text); m_topic.SetWindowText(topic); m_text.SetWindowText(text); } // Handle changes to any edit area void CMainWindow::HandleClear() // Handles Clearing of edit areas { SetDlgItemText(IDC_TOPIC,""); SetDlgItemText(IDC_TEXT,""); } // Check to see if mouse click is in one of the Hot Spots // If so, then send message to appropriate message handler // THis simulates the action of pressing the appropriate buttons void CMainWindow::OnLButtonUp(UINT nFlags, CPoint point) { if(saveHotSpot.PtInRect(point)) SendMessage(WM_COMMAND,IDC_SAVE, NULL); if(loadHotSpot.PtInRect(point)) SendMessage(WM_COMMAND,IDC_LOAD, NULL); if(clearHotSpot.PtInRect(point)) SendMessage(WM_COMMAND,IDC_CLEAR, NULL); } // Repaint the hot spots void CMainWindow::OnPaint() { CPaintDC dc(this); CBrush* pOldBrush; // to restore vurrent brush CBrush redBrush (RGB(255,0,0)); CBrush greenBrush(RGB(0,255,0)); CBrush blueBrush(RGB(0,0,255)); // Now identify the "hot buttons" areas of the window underneath the real buttons pOldBrush = dc.SelectObject(&redBrush); dc.Rectangle(saveHotSpot); dc.SelectObject(&greenBrush); dc.Rectangle(loadHotSpot); dc.SelectObject(&blueBrush); dc.Rectangle(clearHotSpot); dc.SelectObject(pOldBrush); }