键盘事件响应

    技术2022-05-18  8

    1、创建基于单文档工程:KeyInput

    2、在CKeyInputView中添加成员函数:

    public:

    bool m_bShiftDown;

    CPoint m_ptCharacter;

    3CKeyInputView()中初始化:

    m_bShiftDown = ture;

    m_ptCharacter.x = 0;

    m_ptCharacter.y = 0;

    4CKeyInputView()中添加消息响应函数:

    WM_KEYDOWN  WM_CHAR  WM_SETFOCUS

    5OnKeyDown中:

    if(nChar == VK_SHIFT)

    {

        m_bShiftDown = m_bShiftDown ? false:ture;

    }

    6OnSetFocus中:

    CreateSolidCaret(3,18);//建立光标

        SetCaretPos(m_ptCharacter);//设定光标位置

        ShowCaret();//显示光标

    7OnChar中:

    if(m_bShiftDown)

        {

           if(nChar == 13)//回车

           {

               m_ptCharacter.x = 0;

               m_ptCharacter.y += 25;

               SetCaretPos(m_ptCharacter);

               ShowCaret();

           }else{

               CClientDC dc(this);

               HideCaret();

               dc.TextOut(m_ptCharacter.x,m_ptCharacter.y,

    (LPCTSTR)&nChar);

               CSize textsize;

               textsize = dc.GetTextExtent((LPCTSTR)&nChar);

               m_ptCharacter.x += textsize.cx;

               SetCaretPos(m_ptCharacter);

               ShowCaret();

           }

        }

     

    注:

    GetDlgItem(IDC_INPUT)->SetWindowText(“模拟输入”);

    GetDlgItem(IDC_INPUT)->SetWindowText(“停止输入”);

     

    SetTimer(1,500,NULL);

    OnTimer(){}

     


    最新回复(0)