MFC work

来源:互联网 发布:vb程序设计培训班 编辑:程序博客网 时间:2024/06/01 23:45
// SettingDlg.cpp : 实现文件//#include "stdafx.h"#include "TextHandle.h"#include "SettingDlg.h"#include "afxdialogex.h"// CSettingDlg 对话框IMPLEMENT_DYNAMIC(CSettingDlg, CDialogEx)CSettingDlg::CSettingDlg(CWnd* pParent /*=NULL*/): CDialogEx(CSettingDlg::IDD, pParent){m_clrFont = RGB(255, 0, 0);}CSettingDlg::~CSettingDlg(){}void CSettingDlg::DoDataExchange(CDataExchange* pDX){CDialogEx::DoDataExchange(pDX);}BEGIN_MESSAGE_MAP(CSettingDlg, CDialogEx)ON_BN_CLICKED(IDC_BUTTON1, &CSettingDlg::OnBnClickedButton1)ON_BN_CLICKED(IDC_BUTTON2, &CSettingDlg::OnBnClickedButton2)END_MESSAGE_MAP()// CSettingDlg 消息处理程序void CSettingDlg::OnBnClickedButton1(){// TODO: 在此添加控件通知处理程序代码 CColorDialog clrDlg(m_clrFont); if (IDOK == clrDlg.DoModal())    { m_clrFont = clrDlg.GetColor(); }}void CSettingDlg::OnBnClickedButton2(){// TODO: 在此添加控件通知处理程序代码CString strFontName;    // 字体名称   LOGFONT lf;             // LOGFONT变量        // 将lf所有字节清零    memset(&lf, 0, sizeof(LOGFONT));     // 将lf中的元素字体名设为“宋体”    _tcscpy_s(lf.lfFaceName, LF_FACESIZE, _T("宋体"));           // 构造字体对话框,初始选择字体名为“宋体”   CFontDialog fontDlg(&lf);   if (IDOK == fontDlg.DoModal())     // 显示字体对话框   {           // 如果m_font已经关联了一个字体资源对象,则释放它           if (m_font.m_hObject)           {               m_font.DeleteObject();           }           // 使用选定字体的LOGFONT创建新的字体   //         m_font.CreateFontIndirect(fontDlg.m_cf.lpLogFont);   //        // 获取编辑框IDC_FONT_EDIT的CWnd指针,并设置其字体   //        GetDlgItem(IDC_FONT_EDIT)->SetFont(&m_font);   //  //         // 如果用户选择了字体对话框的OK按钮,则获取被选择字体的名称并显示到编辑框里   //         strFontName = fontDlg.m_cf.lpLogFont->lfFaceName;   //         SetDlgItemText(IDC_FONT_EDIT, strFontName);        }  }
// TextHandleView.h : CTextHandleView 类的接口//#pragma once#include "atltypes.h"class CTextHandleCntrItem;class CTextHandleView : public CRichEditView{protected: // 仅从序列化创建CTextHandleView();DECLARE_DYNCREATE(CTextHandleView)// 特性public:CTextHandleDoc* GetDocument() const;// 操作public:// 重写public:virtual BOOL PreCreateWindow(CREATESTRUCT& cs);protected:virtual void OnInitialUpdate(); // 构造后第一次调用// 实现public:virtual ~CTextHandleView();#ifdef _DEBUGvirtual void AssertValid() const;virtual void Dump(CDumpContext& dc) const;#endifprotected:// 生成的消息映射函数protected:afx_msg void OnDestroy();afx_msg void OnFilePrintPreview();afx_msg void OnRButtonUp(UINT nFlags, CPoint point);afx_msg void OnContextMenu(CWnd* pWnd, CPoint point);DECLARE_MESSAGE_MAP()public:virtual BOOL PreTranslateMessage(MSG* pMsg);void GetCaretInfo(int & nCurrentRow, int & nCurrentCol);afx_msg void OnEnterIdle(UINT nWhy, CWnd* pWho);afx_msg void OnLButtonDown(UINT nFlags, CPoint point);afx_msg void OnLButtonUp(UINT nFlags, CPoint point);afx_msg void OnMouseMove(UINT nFlags, CPoint point);private:CRichEditCtrl *m_pRichEditCtrl;int m_nOrgCaretPos;public:afx_msg void OnChar(UINT nChar, UINT nRepCnt, UINT nFlags);afx_msg void OnLButtonDblClk(UINT nFlags, CPoint point);int m_nSelStart;int m_nSelEnd;BOOL m_bLBtnDown;BOOL SetCaret(void);afx_msg void OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags);BOOL IsVisibleRange(LONG nChar);afx_msg void On32771();};#ifndef _DEBUG  // TextHandleView.cpp 中的调试版本inline CTextHandleDoc* CTextHandleView::GetDocument() const   { return reinterpret_cast<CTextHandleDoc*>(m_pDocument); }#endif
// TextHandleView.cpp : CTextHandleView 类的实现//#include "stdafx.h"// SHARED_HANDLERS 可以在实现预览、缩略图和搜索筛选器句柄的// ATL 项目中进行定义,并允许与该项目共享文档代码。#ifndef SHARED_HANDLERS#include "TextHandle.h"#endif#include "TextHandleDoc.h"#include "CntrItem.h"#include "resource.h"#include "TextHandleView.h"#include "SettingDlg.h"#ifdef _DEBUG#define new DEBUG_NEW#endif// CTextHandleViewIMPLEMENT_DYNCREATE(CTextHandleView, CRichEditView)BEGIN_MESSAGE_MAP(CTextHandleView, CRichEditView)ON_WM_DESTROY()ON_WM_CONTEXTMENU()ON_WM_RBUTTONUP()ON_WM_ENTERIDLE()ON_WM_LBUTTONDOWN()ON_WM_LBUTTONUP()ON_WM_MOUSEMOVE()ON_WM_CHAR()ON_WM_LBUTTONDBLCLK()ON_WM_KEYDOWN()ON_COMMAND(ID_32771, &CTextHandleView::On32771)END_MESSAGE_MAP()// CTextHandleView 构造/析构CTextHandleView::CTextHandleView(): m_nOrgCaretPos(0), m_nSelStart(0), m_nSelEnd(0), m_bLBtnDown(FALSE){// TODO: 在此处添加构造代码m_pRichEditCtrl = NULL;}CTextHandleView::~CTextHandleView(){}BOOL CTextHandleView::PreCreateWindow(CREATESTRUCT& cs){// TODO: 在此处通过修改//  CREATESTRUCT cs 来修改窗口类或样式return CRichEditView::PreCreateWindow(cs);}void CTextHandleView::OnInitialUpdate(){CRichEditView::OnInitialUpdate();m_pRichEditCtrl = &GetRichEditCtrl();// 设置打印边距(720 缇 = 1/2 英寸)SetMargins(CRect(720, 720, 720, 720));}void CTextHandleView::OnDestroy(){// 析构时停用此项;这在// 使用拆分视图时非常重要    COleClientItem* pActiveItem = GetDocument()->GetInPlaceActiveItem(this);   if (pActiveItem != NULL && pActiveItem->GetActiveView() == this)   {      pActiveItem->Deactivate();      ASSERT(GetDocument()->GetInPlaceActiveItem(this) == NULL);   }   CRichEditView::OnDestroy();}void CTextHandleView::OnRButtonUp(UINT /* nFlags */, CPoint point){ClientToScreen(&point);OnContextMenu(this, point);}void CTextHandleView::OnContextMenu(CWnd* /* pWnd */, CPoint point){#ifndef SHARED_HANDLERStheApp.GetContextMenuManager()->ShowPopupMenu(IDR_POPUP_EDIT, point.x, point.y, this, TRUE);#endif}// CTextHandleView 诊断#ifdef _DEBUGvoid CTextHandleView::AssertValid() const{CRichEditView::AssertValid();}void CTextHandleView::Dump(CDumpContext& dc) const{CRichEditView::Dump(dc);}CTextHandleDoc* CTextHandleView::GetDocument() const // 非调试版本是内联的{ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CTextHandleDoc)));return (CTextHandleDoc*)m_pDocument;}#endif //_DEBUG// CTextHandleView 消息处理程序BOOL CTextHandleView::PreTranslateMessage(MSG* pMsg){// TODO: 在此添加专用代码和/或调用基类if (pMsg->message == WM_KEYDOWN){if (pMsg->wParam == VK_LEFT || pMsg->wParam == VK_BACK){int nRow = 0, nCol = 0;GetCaretInfo(nRow, nCol);// TRACE("nRow:%d nCol:%d\n", nRow, nCol);if (nCol == 1){return TRUE;}}if (pMsg->wParam == VK_UP || pMsg->wParam == VK_DOWN){return TRUE;}}return CRichEditView::PreTranslateMessage(pMsg);}void CTextHandleView::GetCaretInfo(int &nCurrentRow, int &nCurrentCol){CRichEditCtrl& theCtrl = this->GetRichEditCtrl();// 获取当前行号nCurrentRow = theCtrl.LineFromChar(-1);int nLineStartPos = theCtrl.LineIndex(-1); // 首字符索引TRACE("nLineStartPos: %d\n", nLineStartPos);long nSelStart, nSelEnd;theCtrl.GetSel(nSelStart, nSelEnd);TRACE("nSelStart: %dnSelEnd: %d\n", nSelStart, nSelEnd);nCurrentCol = nSelStart - nLineStartPos + 1;/*CPoint VarCharPoint;         // 指定字符的位置CPoint CurrPoint;            // 当前光标位置int LineFirstIndex;          // 当前行首字符位置int Length;                  // 当前行长度int CurrentCharIndex;        // 当前编辑光标所在字符序号。int CurrentLine;             // 当前编辑光标所在的行号int CurrentRow;              // 当前编辑光标所在的列号CRichEditCtrl &edit = GetRichEditCtrl();CurrPoint = edit.GetCaretPos();        //获取光标位置LineFirstIndex = edit.LineIndex(-1);   // 获取当前行首字符位置Length = edit.LineLength(-1);          // 获取当前行长度.int i = 0;for (; i < Length; i++){VarCharPoint = edit.GetCharPos(LineFirstIndex);if (VarCharPoint.x >= CurrPoint.x){CurrentCharIndex = LineFirstIndex;break;}LineFirstIndex++;}CurrentRow = i; //列号CurrentLine = edit.LineFromChar(CurrentCharIndex); //行号*/}void CTextHandleView::OnEnterIdle(UINT nWhy, CWnd* pWho){CRichEditView::OnEnterIdle(nWhy, pWho);TRACE("OnEnterIdle");// TODO: 在此处添加消息处理程序代码}void CTextHandleView::OnLButtonDown(UINT nFlags, CPoint point){// TODO: 在此添加消息处理程序代码和/或调用默认值m_bLBtnDown = TRUE;m_nSelStart = m_pRichEditCtrl->CharFromPos(point);if (SetCaret()){m_pRichEditCtrl->SetSel(m_nOrgCaretPos, m_nOrgCaretPos);}else{m_pRichEditCtrl->SetSel(m_nSelStart, m_nSelStart);m_pRichEditCtrl->SetCaretPos(CPoint(-1, -1));}//CRichEditView::OnLButtonDown(nFlags, point);}void CTextHandleView::OnMouseMove(UINT nFlags, CPoint point){// TODO: 在此添加消息处理程序代码和/或调用默认值if (m_bLBtnDown){m_nSelEnd = m_pRichEditCtrl->CharFromPos(point);m_pRichEditCtrl->SetSel(m_nSelStart, m_nSelEnd);}//CRichEditView::OnMouseMove(nFlags, point);}void CTextHandleView::OnLButtonUp(UINT nFlags, CPoint point){// TODO: 在此添加消息处理程序代码和/或调用默认值m_bLBtnDown = FALSE;SetCaret();//CRichEditView::OnLButtonUp(nFlags, point);}void CTextHandleView::OnLButtonDblClk(UINT nFlags, CPoint point){// TODO: 在此添加消息处理程序代码和/或调用默认值//CRichEditView::OnLButtonDblClk(nFlags, point);}void CTextHandleView::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags){// TODO: 在此添加消息处理程序代码和/或调用默认值long nSelStart = 0, nSelEnd = 0;m_pRichEditCtrl->GetSel(nSelStart, nSelEnd);if (nSelEnd - nSelStart != 0) // 中文咋办{return;}CRichEditView::OnChar(nChar, nRepCnt, nFlags);}BOOL CTextHandleView::SetCaret(void){if (m_pRichEditCtrl){CPoint ptCaret = m_pRichEditCtrl->PosFromChar(m_nOrgCaretPos);CRect rectClient;m_pRichEditCtrl->GetClientRect(rectClient);if (rectClient.PtInRect(ptCaret)) // 光标可见,闪一下{m_pRichEditCtrl->SetCaretPos(ptCaret);return TRUE;}else{m_pRichEditCtrl->SetCaretPos(CPoint(-1, -1)); // 隐藏光标return FALSE;}}}BOOL CTextHandleView::IsVisibleRange(LONG nChar){CPoint ptCaret = m_pRichEditCtrl->PosFromChar(nChar);CRect rectClient;m_pRichEditCtrl->GetClientRect(rectClient);return rectClient.PtInRect(ptCaret); }void CTextHandleView::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags){// TODO: 在此添加消息处理程序代码和/或调用默认值long nSelStart = 0, nSelEnd = 0;m_pRichEditCtrl->GetSel(nSelStart, nSelEnd);if (nSelEnd - nSelStart != 0) {return;}CRichEditView::OnKeyDown(nChar, nRepCnt, nFlags);CPoint pt = m_pRichEditCtrl->GetCaretPos();m_nOrgCaretPos = m_pRichEditCtrl->CharFromPos(pt);TRACE("m_bOrgCaretPos:%d\n", m_nOrgCaretPos);}void CTextHandleView::On32771(){// TODO: 在此添加命令处理程序代码CSettingDlg dlgSetting;if (IDOK == dlgSetting.DoModal())   {;}}
void CTest_menuView::OnTest() {// TODO: Add your command handler code hereMessageBox("View click!");static bool bClick = false;bClick = !bClick;CMainFrame *pFrame = (CMainFrame *)AfxGetMainWnd();CMenu *pmenu =  pFrame->GetMenu();pFrame->m_bAutoMenuEnable = false;SetTimer(1, 1000, NULL);   if (bClick){pmenu->GetSubMenu(0)->CheckMenuItem(0, MF_BYPOSITION | MF_CHECKED);pmenu->GetSubMenu(0)->CheckMenuItem(1, MF_BYPOSITION | MF_CHECKED);pmenu->GetSubMenu(0)->EnableMenuItem(0, MF_BYPOSITION | MF_ENABLED);pmenu->GetSubMenu(0)->EnableMenuItem(1, MF_BYPOSITION | MF_ENABLED);}else{pmenu->GetSubMenu(0)->CheckMenuItem(0, MF_BYPOSITION | MF_UNCHECKED);pmenu->GetSubMenu(0)->CheckMenuItem(1, MF_BYPOSITION | MF_UNCHECKED);// MF_BYCOMMANDpmenu->GetSubMenu(0)->EnableMenuItem(0, MF_BYPOSITION | MF_DISABLED | MF_GRAYED);pmenu->GetSubMenu(0)->EnableMenuItem(1, MF_BYPOSITION | MF_DISABLED | MF_GRAYED);}}void CTest_menuView::OnTimer(UINT nIDEvent) {// TODO: Add your message handler code here and/or call defaultCString strTime;      // 获取系统当前时间,并保存到curTime   CTime curTime = CTime::GetCurrentTime();   // 格式化curTime,将字符串保存到strTime   strTime = curTime.Format(_T("%H:%M:%S"));   // 在状态栏的时间窗格中显示系统时间字符串   CStatusBar* pStatusBar = (CStatusBar*)AfxGetMainWnd()->GetDescendantWindow(AFX_IDW_STATUS_BAR);pStatusBar->SetPaneText(4, strTime);   pStatusBar->SetWindowText("Hello world");CView::OnTimer(nIDEvent);}




0 0