Displaying Cursor Position in the Status Bar

In many applications in which you use CEditView and CRichEditView classes it may be convineinent to let you user know the position of the cursor, much like the IDE in Visual C++. After some quick searching through MSDN and the CG discussion forms, I found out how easy it would be to add this feature.

  • Add a new entry to your string table with an ID of ID_INDICATOR_CURPOS and a caption of "Ln %d, Col %d  ". The extra spaces are to give you a little bit more room in the panel so when you get over 100 lines, the text doesn't get clipped.
  • Add ID_INDICATOR_CURPOS to your indicators[] array in the MainFrm.cpp file.
  • In the message map in the class declaration for CMainFrame, add the following function signiture:
    afx_msg void OnUpdateCurPosIndicator(CCmdUI *pCmdUI);
  • Now go the the acutal message map in the MainFrm.cpp file and add the following macro call:
    ON_UPDATE_COMMAND_UI(ID_INDICATOR_CURPOS, 
                         OnUpdateCurPosIndicator)
    
  • Lastly, create the function body in the MainFrm.cpp file
    void CMainFrame::OnUpdateCurPosIndicator(CCmdUI *pCmdUI)
    {
     CString strCurPos;
     int nLineNum, nColNum;
     int nSelStart, nSelEnd;
    
     // you're going to have to get a pointer
     // to the edit control in the view
     m_wndEditCtrl->GetSel(nSelStart, nSelEnd);  
                                                      
     nLineNum = m_wndEditCtrl->LineFromChar(nSelStart);
     
     nColNum = nSelStart - m_wndEditCtrl->LineIndex(nLineNum);
    
     strCurPos.Format(ID_INDICATOR_CURPOS, 
                      nLineNum+1, 
                      nColNum+1);
     
     m_wndStatusBar.SetPaneText(
      m_wndStatusBar.CommandToIndex(ID_INDICATOR_CURPOS), 
      strCurPos);
    }
    

    And that's about it. You can download a sample project if you wish, altough there's nothing special in it, really. Only thing that may be of any use is getting that pointer to the edit control.

    Downloads

    Download demo project - 38 Kb
    Download source - 2 Kb


  • Comments

    • Smart code, but how to show a graphics cursor position?

      Posted by Legacy on 11/15/2003 12:00am

      Originally posted by: Tom

      I creating a simple app for my own, but i have very big problem to show a graphics cursor.My app is SDI type, I want to show mouse (graphics - not text) cursor position in a status bar. Is there any truly programmer?? very thanx

      Reply
    • Give reply

      Posted by Legacy on 05/02/2003 12:00am

      Originally posted by: surumoradiya

      how i can attach status bar with the dialog box ?

      Reply
    • I fixed Problem in MDI!!~

      Posted by Legacy on 01/22/2003 12:00am

      Originally posted by: Jin Ki Byung

      Hi~
      
      

      I solved the problem in MDI.

      This code is that.


      void CMyMDIChildView::OnUpdateIndicatorPosition(CCmdUI* pCmdUI)
      {
      CString strCurPos;
      long nLineNum, nColNum;
      long nSelStart, nSelEnd;

      CRichEditCtrl& rRich = GetRichEditCtrl();
      rRich.GetSel ( nSelStart, nSelEnd );

      nLineNum = rRich.LineFromChar ( nSelStart );
      nColNum = nSelStart - rRich.LineIndex ( nLineNum );

      strCurPos.Format ( "Ln %d, Col %d", nLineNum+1, nColNum+1 );

      pCmdUI->SetText ( strCurPos );

      return;
      }

      This function is including in MDI child view class.


      Reply
    • solved the problem in MDI. ^_^

      Posted by Legacy on 12/13/2002 12:00am

      Originally posted by: Y.F. Hao

      With the mouse moving, the status bar display the corresponding coordinations. hehe. It is nice codes.

      Reply
    • I have a problem with the Example as MDI!

      Posted by Legacy on 10/25/2002 12:00am

      Originally posted by: asmodia

      Hi,

      can you or any send me the complete MDI Projekt?

      please.

      by

      asmodia

      Reply
    • MDI Example

      Posted by Legacy on 10/12/2002 12:00am

      Originally posted by: Ted

      Add the ID_INDICATOR_??? to the CMainFrame class.
      
      Add the handler to the CChildFrame class.

      void CChildFrame::OnUpdateCurPos( CCmdUI *pCmdUI )
      {
      CEditView *pView = (CEditView *)GetActiveView();

      if (pView != NULL)
      {
      () + 1 );
      pCmdUI->SetText( txt );
      }
      }

      Reply
    • The pane indicates always "Ln %d, Col %d"wherever the cursor is!!!!

      Posted by Legacy on 08/14/2002 12:00am

      Originally posted by: lynda

      Hi i'm lynda.
      I did the same method than shown in your example, but the pane indicates always "Ln %d, Col %d" wherever the cursor is located.
      Thanks for any suggestions!!

      Reply
    • Good technic^^!!

      Posted by Legacy on 07/11/2002 12:00am

      Originally posted by: SM

      this technic is that wanted technic^^

      good!!^^
      good!!^^
      good!!^^

      Reply
    • What does you're going to have to get a pointer... ... mean?

      Posted by Legacy on 12/31/2001 12:00am

      Originally posted by: bn37_

      What does "you're going to have to get a pointer to the edit control in the view" mean?

      I followed the directions, but came up with errors.

      Reply
    • How to use it with MDI application?

      Posted by Legacy on 12/03/2001 12:00am

      Originally posted by: Alessandro

      I can't use this code with my MDI editor, it gives me an access violation. Any suggestion? thanx

      Reply
    • Loading, Please Wait ...

    Leave a Comment
    • Your email address will not be published. All fields are required.

    Top White Papers and Webcasts

    • Live Event Date: July 22, 2015 @ 2:00 p.m. ET / 11:00 a.m. PT Today's increasingly mobile workforce creates new challenges for organizations that rely heavily on collaboration. Businesses need to be both secure and collaborative, and they can't sacrifice one over the other. The plethora of devices and clouds used by mobile employees only complicates these challenges. Attend this eSeminar and learn how your business can enable mobile access to files from anywhere while it helps to maintain information …

    • Thanks to wide spread cloud hosting and innovations small businesses can meet and exceed the legacy systems of goliath corporations. Explore the freedom to work how you want, with a phone system that will adapt to your evolving needs and actually save you lots of expense—read Get an Enterprise Phone System without High Cost and Complexity. The article clearly illustrates: The only hardware you'll need is phone equipment for advanced voice and fax. How to join all your employees, mobile devices, …

    Most Popular Programming Stories

    More for Developers

    RSS Feeds

    Thanks for your registration, follow us on our social networks to keep up-to-date