Thread: Edit control window and margins

  1. #1
    Registered User
    Join Date
    Jun 2009
    Posts
    93

    Edit control window and margins

    I've been goofing around making (or trying to make) a simple Windows edit program like Notepad in C language. This endeavor is purely for fun which for the most part it has been. Well, my latest obsticle is creating a margin in the edit control window. Currently, the cursor is jambed all the way against the left hand border and up against the title bar area of the parent window. I'd like to create that small buffer look you see on programs such as Wordpad where the cursor starts a little down from the title bar and a bit away from the left hand border. I've messed around with things like EM_GETRECT, EM_SETRECT, and MoveWindow but each scenerio has left problems. With the EM_GETRECT, EM_SETRECT the new postions (values for left and top rect) disappear everytime the edit window gets repainted, resized, or if word wrap is envoked. When I alter the left and top rect values in the MoveWindow function under WM_SIZE it messes up the verticle and horizonal scroll bar locations.
    I must add that I really don't know much about programming in C-language or any language for that matter. In addition, I'm using ancient programs like: MSC7 (MSVC1.0 ?), MSVC32S, on an old Windows 98 loaded computer for this project.

  2. #2
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    Can you give us a screen shot?
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  3. #3
    Registered User
    Join Date
    Jun 2009
    Posts
    93
    I'm assuming your refering to what I'm trying to accomplish when you say screen shot?
    Unfortunately, I'm unable to attach a picture (screen shot) of what I'm trying to accomplish.... I guess the easiest way to see it first hand is to open up Windows Notepad and hit the spacebar once. That little empty buffer zone between the application's frame and the current position of the cursor is what I'm trying to accomplish. I hope that kinda makes sense.

  4. #4
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    Try GetClientRect() to find the area the edit has to fit in.

    GetClientRect(HMainWindow, &ClientRect);

    Then use InflateRect() with -ive values equal to the required margin before calling MoveWindow() to set the new position.

    InflateRect( &ClientRect, -5, -5);

    MoveWindow(hEdit, ClientRect.left,ClientRect.top, ClientRect.right-ClientRect.left, ClientRect.bottom-ClientRect.top, TRUE); //make sure window repaints

    NOTE: The Edit must have the WS_CHILD style for this to work (as the WS_CHILD style will ensure that the edit is moved relative to the top left of the main windows client area, not the top left of the windows screen area).



    You can also use AdjustWindowRect() if you want to create a set size edit (and need to know how big to make the window before you create it).
    Last edited by novacain; 03-29-2010 at 10:50 PM.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  5. #5
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    I ment what you have done of course.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  6. #6
    Registered User
    Join Date
    Jun 2009
    Posts
    93
    I tried InflateRect() and it messes up the horizontal and verticle scroll bars. It offsets the the same amount as the values entered for the InflateRect() function. After goofing around with the program some more I've had rather good success with the following:

    Code:
    case WM_SIZE:
    	    {
    	    RECT    rc;
    	    HWND    hWndEdit;
    
    	    // resize the edit control to match the client area
    	    hWndEdit = (HWND)GetWindowLong (hWnd, WL_HWNDEDIT);
    	    GetClientRect (hWnd, &rc);
    	    MoveWindow (hWndEdit,
    			rc.left,
    			rc.top,
    			rc.right-rc.left,
    			rc.bottom-rc.top,
    			TRUE);
    
                // set margins for left and top
                SendMessage (hWndEdit, EM_GETRECT, 0, (LPARAM) (RECT FAR*) &rc);
                rc.left = 8;
                rc.top = 2;
                rc.right -= rc.left;
                rc.bottom -= rc.top;
                SendMessage (hWndEdit, EM_SETRECT, 0, (LPARAM) (RECT FAR*) &rc);
    	    }
    	    break;
    The only minor obsticle now is when word wrap is invoked. I placed the same code (EM_GETRECT, EM_SETRECT) in the word wrap module, but edit control doesn't reflect/show the changes unless I make the program Iconic or use the minimize/maximize functions for the program.

  7. #7
    Registered User
    Join Date
    Jun 2009
    Posts
    93
    Well, just to update the folks viewing this thread for info here's where it stands currently. Everything works like a champ including the word wrap function which is where I left off with my problems. So, how did I solve my initial problem you ask....Well, I used the code I posted above which took care of the initial setting of the margins when the Edit control window is created. In addition, this took care of making sure the margins are set when ever the program is resized or returns from an Iconic state (minimized). To solve the word wrap issue I placed the same code (EM_GETRECT, EM_SETRECT stuff) towards the end of my word wrap module just be for ShowWindow() and SetFocus(), etc. What I forgot to do earlier (simple mistake, but I'm a novice here!) was to change the HWND in the SendMessage() function for EM_GETRECT and EM_SETRECT to reflect the new HWND. In my case it was changing from hWndEdit to hWndNew.
    Now, don't ask me how or why those six lines of code set the margins correctly and don't mess up the scroll bars. Well, I have somewhat of a clue but for someone of my caliber of programming knowledge it would be impossible to explain.
    My next challange is using TabbedTextOut in my printer module taking into account a non-common dialog page setup scenerio.....

Popular pages Recent additions subscribe to a feed