Thread: Coupla Q's

  1. #1
    The Defective GRAPE Lurker's Avatar
    Join Date
    Feb 2003
    Posts
    949

    Coupla Q's

    1. How would you check the current text in an edit box? Is there a function like GetCurrentText(......)

    2. How can I add text to the edit box at the location of the pointer (not mouse)

    3. Is there a WM_ONMOUSEOVER or similar case to detect when the mouse is moved over something? You know, like WM_LBUTTONDOWN is to pressing the left mouse button.

    Thanks !
    Do not make direct eye contact with me.

  2. #2
    Registered User Bajanine's Avatar
    Join Date
    Dec 2001
    Location
    The most peaks over 10,000 feet!
    Posts
    396
    I can only help you on your first question, try "GetDlgItemText".
    Code:
    GetDlgItemText(hOwner or  hDialogEditField, 
        control identifier, 
        pointer to buffer for text, 
        maximum size of string + NULL);
    Favorite Quote:

    >For that reason someone invented C++.
    BLASPHEMY! Begone from my C board, you foul lover of objects, before the gods of C cast you into the void as punishment for your weakness! There is no penance for saying such things in my presence. You are henceforth excommunicated. Never return to this house, filthy heretic!



  3. #3
    The Defective GRAPE Lurker's Avatar
    Join Date
    Feb 2003
    Posts
    949
    Sorry, but what Im trying to do is get the text in an edit box, like the main typing area in Notepad
    Do not make direct eye contact with me.

  4. #4
    Registered User
    Join Date
    Feb 2003
    Posts
    76
    1. Send the edit control a WM_GETTEXT message.

    2. Look at the EM_REPLACESEL message.

    3. Not defined, but you can roll your own.
    Code:
    BOOL IsMouseOver(HWND hwnd){
        POINT pt;
        HWND hw;
        GetCursorPos(&pt);
        hw=WindowFromPoint(pt);
        if(hw==hwnd)return TRUE;
        if(hw){
         ScreenToClient(hw,&pt);
         hw=ChildWindowFromPoint(hw,pt); // or RealChildWindowFromPoint
        }
        return (hw==hwnd);
    }
    
    // in the window proc...
    
    static BOOL ismouseover=FALSE;
    case WM_CREATE:
     SetTimer(hwnd,1,1,NULL);
     break;
    // ...
    case WM_TIMER:
     switch(wParam){
      case 1:
       if(IsMouseOver(hwnd)&&!ismouseover){
        PostMessage(hwnd,WM_ONMOUSEOVER,0,0); // app-defined message
        ismouseover=TRUE;
       }
       else if(!IsMouseOver(hwnd)&&ismouseover){
        PostMessage(hwnd,WM_ONMOUSEOUT,0,0); // app-defined message
        ismouseover=FALSE;
       }
     }
    Last edited by poccil; 05-23-2003 at 09:24 PM.

  5. #5
    Registered User
    Join Date
    May 2003
    Posts
    1,619

    Re: Coupla Q's

    Originally posted by Lurker
    1. How would you check the current text in an edit box? Is there a function like GetCurrentText(......)
    You use messages. Send it the EM_GETLINECOUNT to find out how many lines there are, and retreive each with EM_GETLINE. WM_GETTEXT will retreive the entirety of the window's text. (but not exceeding 64 kilobytes). EM_STREAMOUT can be used if you need to read more than 64 kilobytes of text.

    2. How can I add text to the edit box at the location of the pointer (not mouse)
    The EM_REPLACESEL message will not only replace characters (if there are characters highlighted), it will insert at the caret if nothing is selected.

    3. Is there a WM_ONMOUSEOVER or similar case to detect when the mouse is moved over something? You know, like WM_LBUTTONDOWN is to pressing the left mouse button.

    Thanks !
    WM_MOUSEMOVE is what you're looking for, I think. You can also use the function TrackMouseEvent() to have it send WM_MOUSEHOVER and WM_MOUSELEAVE events. I think you need to call TrackMouseEvent() once each time the mouse enters your client area.
    Last edited by Cat; 05-23-2003 at 09:28 PM.

  6. #6
    The Defective GRAPE Lurker's Avatar
    Join Date
    Feb 2003
    Posts
    949
    Cat:
    I think Im gonna go your way....how would I do these things? Sorry, but can you give a quick example please? Thankss!!!!!!!
    Do not make direct eye contact with me.

  7. #7
    The Defective GRAPE Lurker's Avatar
    Join Date
    Feb 2003
    Posts
    949
    Also, how can I test if a edit box contains nothing, or ""? Thanks!
    Do not make direct eye contact with me.

  8. #8
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    If it contains nothing, sending WM_GETTEXTLENGTH will return 0. See below for an example.

    Some examples (I'll use TCHAR in the place of characters simply as I'm used to writing code for both ANSI and Unicode; you can use char instead of TCHAR if you're not writing for Unicode.)

    Note you should be using SendMessage() not PostMessage() for these kinds of messages. PostMessage() is not appropriate here.

    Reading the edit box's text (assuming hWnd is the handle to that control):

    Code:
    int length = ::SendMessage(hWnd, WM_GETTEXTLENGTH, 
                         (WPARAM) 0,          // not used; must be zero
                         (LPARAM) 0);           // not used; must be zero
    if (length > 65535){
      /* If this is a RichEdit box, GetText will fail if you're reading this
    much; you should make sure you're never going to hit this
    situation.  I don't *think* the limit applies for non-RichText edit
    boxes*/
    }
    
    
    TCHAR * buffer = new TCHAR[length + 1];
    
    int length2 = ::SendMessage(hwnd,WM_GETTEXT,
                         (WPARAM) length + 1,
                         (LPARAM) buffer);
    
    if (length2 != length){
     /* An error occured and we copied too few characters somehow*/
    }
    
    // Buffer now has the window's text.

    Example: Inserting a line of text. The _T() is only needed for Unicode compatibility and may be omitted.

    Code:
    TCHAR * str = _T("This is inserted.");
    ::SendMessage(hWnd, EM_REPLACESEL,
                            (WPARAM) FALSE,    // Can't UNDO the insertion.
                            (LPARAM) str);     // text string (LPCTSTR)
    For the last, you'd handle WM_MOUSEMOVE in your main window procedure, like all other messages your window intercepts.
    Last edited by Cat; 05-23-2003 at 10:51 PM.

  9. #9
    The Defective GRAPE Lurker's Avatar
    Join Date
    Feb 2003
    Posts
    949
    Thank you very much Cat !
    Do not make direct eye contact with me.

  10. #10
    The Defective GRAPE Lurker's Avatar
    Join Date
    Feb 2003
    Posts
    949
    Hmm...not working. This is what i have (under WM_COMMAND in WndProc):
    Code:
    				case ID_TEST:
    					{
    						HWND hEdit;
    						TCHAR * str = "This is a test.";
    						SendMessage(hEdit, EM_REPLACESEL, (WPARAM)FALSE, (LPARAM)str);
    					}
    					break;
    and then, in the resource file:
    Code:
    			MENUITEM "&Test", ID_TEST
    No error, but the message isnt being put in the edit box.
    Do not make direct eye contact with me.

  11. #11
    The Defective GRAPE Lurker's Avatar
    Join Date
    Feb 2003
    Posts
    949
    Another prob in WM_CLOSE, with no error(not bringng up save dialog box, DOFileSave is the correct function, used in other places.):

    Code:
    			{
    				HWND hEdit;
    				int length = SendMessage(hEdit, WM_GETTEXTLENGTH, (WPARAM) 0, (LPARAM) 0);
    				if(length > 0) {
    					int r = DialogBox(GetModuleHandle(NULL), MAKEINTRESOURCE(ID_QUIT_SAVE), hwnd, SaveDlgProc);
    					if(r == IDOK) {
    						DoFileSave(hwnd);
    						DestroyWindow(hwnd);
    					}
    					if(r == IDCANCEL) {
    						DestroyWindow(hwnd);
    					}
    					if(r == IDOTHER) {}
    					if(r < 0) {
    						MessageBox(hwnd, "The dialog box failed, for some reason.", "Error!",
    							   MB_OK | MB_ICONERROR);
    						DestroyWindow(hwnd);
    					}
    				} else {
    					DestroyWindow(hwnd);
    				}
    			}
    Do not make direct eye contact with me.

  12. #12
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    Originally posted by Lurker
    Hmm...not working. This is what i have (under WM_COMMAND in WndProc):
    Code:
    case ID_TEST:
    {
       HWND hEdit;
       TCHAR * str = "This is a test.";
       SendMessage(hEdit, EM_REPLACESEL, (WPARAM)FALSE, (LPARAM)str);
    }
    break;
    and then, in the resource file:
    Code:
       MENUITEM "&Test", ID_TEST
    No error, but the message isnt being put in the edit box.
    I'll assume you've already tested and verified that it's actually getting to that point (put a MessageBox somewhere to see if you're actually calling that routine or not).

    One problem I see -- you have declared a handle, but you never set it to the handle of your edit box. hEdit is useless unless it actually gets the value of the edit box's handle put into it.

    How are you creating your edit box? E.g. where is the code that actually makes the edit box window?

  13. #13
    The Defective GRAPE Lurker's Avatar
    Join Date
    Feb 2003
    Posts
    949
    Here is the case creating status, tool, and edit bars:
    Code:
    		case WM_SIZE:
    			{
    				HWND hTool, hEdit, hStatus;
    				RECT rcTool;
    				int iToolHeight;
    				RECT rcStatus;
    				int iStatusHeight;
    				int iEditHeight;
    				RECT rcClient;
    				hTool = GetDlgItem(hwnd, ID_MAIN_TOOL);
    				SendMessage(hTool, TB_AUTOSIZE, 0, 0);
    				GetWindowRect(hTool, &rcTool);
    				iToolHeight = rcTool.bottom - rcTool.top;
    				hStatus = GetDlgItem(hwnd, ID_MAIN_STATUS);
    				SendMessage(hStatus, WM_SIZE, 0, 0);
    				GetWindowRect(hStatus, &rcStatus);
    				iStatusHeight = rcStatus.bottom - rcStatus.top;
    				GetClientRect(hwnd, &rcClient);
    				iEditHeight = rcClient.bottom - iToolHeight - iStatusHeight;
    				hEdit = GetDlgItem(hwnd, ID_MAIN_EDIT);
    				SetWindowPos(hEdit, NULL, 0, iToolHeight, rcClient.right, iEditHeight, SWP_NOZORDER);
    			}
    			break;
    using a MsgBox to test now...
    Do not make direct eye contact with me.

  14. #14
    The Defective GRAPE Lurker's Avatar
    Join Date
    Feb 2003
    Posts
    949
    Yes, the message box displays, but the other code doesnt. Any ideas? Thanks again !
    Do not make direct eye contact with me.

  15. #15
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    It looks like you have multiple handles named the same thing. E.g. in your WM_SIZE handler, you declate a handle called hEdit, but in your control handler, you declare a *new* variable called hEdit, but don't make it reference the window.

    Essentially, replace this:

    Code:
    case ID_TEST:
    {
       HWND hEdit;
       TCHAR * str = "This is a test.";
       SendMessage(hEdit, EM_REPLACESEL, (WPARAM)FALSE, (LPARAM)str);
    }
    break;
    with this:

    Code:
    case ID_TEST:
    {
       HWND hEdit = GetDlgItem(hwnd, ID_MAIN_EDIT);
       TCHAR * str = "This is a test.";
       SendMessage(hEdit, EM_REPLACESEL, (WPARAM)FALSE, (LPARAM)str);
    }
    break;
    The first line won't actually create a new edit box, it gets the handle of a box that you already created and identified before. Everytime you need the handle, you have to get it again (or store it somewhere more permanent).

    BTW, the above code might only work if the edit window has focus, so maybe you need this:

    Code:
    case ID_TEST:
    {
       HWND hEdit = GetDlgItem(hwnd, ID_MAIN_EDIT);
       SetFocus(hEdit);
       TCHAR * str = "This is a test.";
       SendMessage(hEdit, EM_REPLACESEL, (WPARAM)FALSE, (LPARAM)str);
    }
    break;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 13
    Last Post: 03-07-2005, 04:49 PM
  2. Several Q's
    By Elyubarov in forum Windows Programming
    Replies: 9
    Last Post: 12-26-2004, 10:36 PM
  3. Q's
    By pktcperlc++java in forum C++ Programming
    Replies: 15
    Last Post: 12-24-2004, 11:36 AM
  4. C and UNIX Q's?
    By Unregistered in forum C Programming
    Replies: 0
    Last Post: 06-13-2002, 05:39 AM
  5. New to C++. 1 or 2 Q's
    By Guardian in forum C++ Programming
    Replies: 5
    Last Post: 04-21-2002, 06:44 AM