zoukankan      html  css  js  c++  java
  • 关于NotePad一些功能的实现方法

    NotePad功能:
    1.向上查找,大小写,全字匹配,利用CFindDlg的基类的成员函数实现;
    switch case
    PreTranslateMessage()函数
    http://blog.sina.com.cn/s/blog_9cd8465f01010cwe.html
    http://blog.csdn.net/liuzhuomju/article/details/7380539

    2.查找对话框只允许出现一次,但是是非模式对话框,为什么?因为1.不阻塞;2....

    利用“if ...else...在主对话框Create CFindReplaceDialog之前判断”实现;

    3.实现保存关闭时对话框的位置/大小和字体的信息,再次打开时加载上次信息

    使用CWinAPP中的Profile相关函数实现退出时记录窗口位置和字体的功能,再次启动时位置和字体加载退出时的状态

    3.1 需要用到CWinApp下的成员函数和成员变量:

    m_pszProfileName  配置文件名(可以保存在注册表中也可以保存在ini配置文件中)

    m_pszRegistryKey  注册表键值

    GetProfileInt()

    WriteProfileInt()

    GetProfileString()

    WriteProfileString()

    SetRegistryKey()

    3.2 保存到配置文件INI中,.ini文件默认保存在C:Windows下

    void CTXTDlg::InitRect()
    {
        int nLeft = AfxGetApp()->GetProfileInt("SETTING","left",-1);
        if(nLeft<0)
            return;
        int nRight = AfxGetApp()->GetProfileInt("SETTING","right",-1);
        if(nRight<0)
            return;
        int nTop = AfxGetApp()->GetProfileInt("SETTING","top",-1);
        if(nTop<0)
            return;
    
        int nBottom = AfxGetApp()->GetProfileInt("SETTING","bottom",-1);
        if(nBottom<0)
            return;
        MoveWindow(nLeft,nTop,nRight-nLeft,nBottom-nTop);
    
    }
    
    void CTXTDlg::OnDestroy() 
    {
        CDialog::OnDestroy();
        CRect rect;
        GetWindowRect(rect);
        theApp.WriteProfileInt("SETTING","left",rect.left);
        theApp.WriteProfileInt("SETTING","right",rect.right);
        theApp.WriteProfileInt("SETTING","top",rect.top);
        theApp.WriteProfileInt("SETTING","bottom",rect.bottom);
    
    }

    3.3 可以利用注册表信息来实现

    a)注册表在cmd-regedit打开

    b)在CWinAPP派生类中初始化时 SetRegistryKey()

    CTXTApp::InitInstance()中SetRegistryKey("TXT");这样WriteProfileInt()和GetProfileInt()是就都在注册表里面进行读写

    3.4 同理字体也可是设置到注册表里面

    void CTXTDlg::SaveFont()
    {
        CFont *pFont = GetDlgItem(IDC_TEXT)->GetFont();
        LOGFONT lf;
        pFont->GetLogFont(&lf);
        theApp.WriteProfileString("FONT","NAME",lf.lfFaceName);
        theApp.WriteProfileInt("FONT","Height",lf.lfHeight);
        theApp.WriteProfileInt("FONT","CharSet",lf.lfCharSet);
        theApp.WriteProfileInt("FONT","Weight",lf.lfWeight);
        theApp.WriteProfileInt("FONT","Italic",lf.lfItalic);
        theApp.WriteProfileInt("FONT","Underlne",lf.lfUnderline);
        theApp.WriteProfileInt("FONT","StrikeOut",lf.lfStrikeOut);
    
    }
    
    
    void CTXTDlg::LoadFont()
    {
        LOGFONT lf = {0};
        CString str = theApp.GetProfileString("FONT","NAME",NULL);
        if(str.GetLength())
        {
            strcpy(lf.lfFaceName,str);
            lf.lfHeight = theApp.GetProfileInt("FONT","Height",0);
            lf.lfCharSet = theApp.GetProfileInt("FONT","CharSet",0);
            lf.lfWeight = theApp.GetProfileInt("FONT","Weight",0);
            lf.lfItalic = theApp.GetProfileInt("FONT","Italic",0);
            lf.lfUnderline = theApp.GetProfileInt("FONT","Underline",0);
            lf.lfStrikeOut = theApp.GetProfileInt("FONT","StrikeOut",0);
            m_font.CreateFontIndirect(&lf);
            GetDlgItem(IDC_TEXT)->SetFont(&m_font);
        }
    }

    在OnInitDialog里面加载,注意!在OnFormatFont里面保存。

    4.PreTranslateMessage函数的使用(在class wizard中建立映射)可以实现快捷键设置的功能
    关于PreTranslateMessage
    MFC消息控制流最具特色的地方是CWnd类的虚拟函数PreTranslateMessage(),通过重载这个函数,我们可以改变MFC的消息控制流程,甚至可以作一个全新的控制流出来。只有穿过消息队列的消息才受PreTranslateMessage()影响,采用SendMessage()或其他类似的方式向窗口直接发送的而不经过消息队列的消息根本不会理睬PreTranslateMessage()的存在。
    顾名思义,PreTranslateMessage就是消息发送给窗口之前,提前拦截消息,然后对消息进行处理(即,改变了MFC的消息控制流程)
    要注意一下两点:
    1)是否调用TranslateMessage()和DispatchMessage()是由一个名称为PreTranslateMessage()函数的返回值决定的,如果该函数返回TRUE,则不会把该消息分发给窗口函数处理。
    2)传给PreTranslateMessage()的消息是未经翻译过的消息,它没有经过TranslateMessage()处理。可以在该函数中使用(pMsg->wParam==VK_RETURN)来拦截回车键。

    5.OnInitDialog在class wizard中添加如果找不到可以到最后一页的过滤器(Message filter)中设置为Dialog

    6.实现快捷键的功能
    定义快捷键的就是自定义加速键
    1)首先在资源文件Accelerator中添加快捷键资源 ID选择你要关联菜单项的名称然后再设置你的快捷键
    2)在.h文件中加入一个HACCEL hAccel;
    3)变量然后在OnInitDialog或初始化中加入 hAccel=::LoadAccelerators(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDR_MENU_MAIN)); IDR_MENU_MAIN为加速键的资源文件名
    4)最后在PreTranslateMessage(MSG* pMsg) 中加入:

        if (m_hAccel)
        {
            if (::TranslateAccelerator(m_hWnd, m_hAccel, pMsg))
                return TRUE;
        }

    这样 以后只要在Accelerator资源文件中添加快捷键就可以了

    7.CFindReplaceDialog()这种非模式对话框建立“替代”和“替代全部”的映射方法:
    方法一:
    1.使用PreTranslateMessage()在消息传递之前拦截消息,提前处理
    2.使用ON_BN_CLICKED(0X400,OnReplace)建立函数和控件的联系
    方法二:
    消息注册,消息映射,对话框创建(还不会用,有点复杂)
    http://blog.sina.com.cn/s/blog_9cd8465f01010cwe.html

  • 相关阅读:
    JSON
    FBV & CBV
    django Tips
    Django2.2
    cookie & session
    ajax请求
    视图函数
    模板语法
    模板继承、组件
    python之路-----多线程与多进程
  • 原文地址:https://www.cnblogs.com/laohan1221/p/5562822.html
Copyright © 2011-2022 走看看