Thread: Text Box

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    67

    Smile Text Box

    Hello everyone

    To begin with, I have had some GUI programming experience with Python's Tkinter.
    Manipulating the widgets is easy for prototyping. I am beginning to learn GUI programming with C++ now in order to get more control, as well as make individual .EXE executables that are otherwise interpreted in Python.

    The compiler I am using right now is DevC++.

    Here is my current code:

    Code:
    #include <windows.h>
    
    const char ClassName[] = "WindowClass";
    
    // Message Queue Commands
    LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
    {
        switch(msg) // if&else commands to follow when interpreting the current message in queue
        {
            case WM_LBUTTONDOWN: //if message was a left button click
                MessageBox(hwnd, //if left button clicked inside current handle?
                           "Content", //Popup Window Content
                           "Current Directory", //Popup Window Title
                           MB_OK | MB_ICONINFORMATION); //Okay button
            break; //without break, program would close
            case WM_CLOSE:
                DestroyWindow(hwnd);
            break;
            case WM_DESTROY: //if message was the cancel button [x]???
                PostQuitMessage(0);
            break;
            default:
                return DefWindowProc(hwnd, msg, wParam, lParam);
        }
        return 0;
    }
    
    //WinMain() instead of main()
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
    {
        //Window class
        WNDCLASSEX wc;
        //Window class parameters
        wc.cbSize        = sizeof(WNDCLASSEX); //???
        wc.style         = 0; //???
        wc.lpfnWndProc   = WndProc; //???
        wc.cbClsExtra    = 0; //???
        wc.cbWndExtra    = 0; //???
        wc.hInstance     = hInstance; //???
        wc.hIcon         = LoadIcon(NULL,IDI_APPLICATION); //Executable's desktop icon
        wc.hCursor       = LoadCursor(NULL, IDC_ARROW); //Cursor
        wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); //Window color
        wc.lpszMenuName  = NULL; //No menu
        wc.lpszClassName = ClassName; //???
        wc.hIconSm       = LoadIcon(NULL, IDI_APPLICATION); //Window icon? , Sm = small?
    
        if(!RegisterClassEx(&wc)) //error handling
        {
            MessageBox(NULL, "Registration Failure", "Error", MB_OK); //Okay button
            return 0; //return fail
        }
    
        ////////////////////////////////////////////////////////////////////////////
    
        //The actual window
        HWND MyParentWindow;
        //The window's parameters
        MyParentWindow = CreateWindowEx(
            0, //Style
            ClassName, //Current class name , Does this mean MyParentWindow takes on wc's characteristics via the name ClassName?
            "Title", //Title
            WS_OVERLAPPEDWINDOW, //minimize, maximize, close
            0,//position x in pixels relative to screen
            0,//position y in pixels relative to screen
            500,//size x dimension in pixels
            500,//size y dimension in pixels
            NULL, //MyParentWindow is the parent window and it does not have a parent, thus NULL
            NULL, //no menu was defined
            hInstance, //???
            NULL); //???
    
        if(MyParentWindow == NULL) //error handling
        {
            MessageBox(NULL, "MyParentWindow Creation Failure", "Error", MB_OK); //okay button
            return 0;
        }
    
        ShowWindow(MyParentWindow, nCmdShow); //show the window called "MyParentWindow"
        UpdateWindow(MyParentWindow); //update the window called "MyParentWindow"
    
        ////////////////////////////////////////////////////////////////////////////
    
        // Message loop
        MSG Msg;
        //GetMessage() gets a message from message queue.
        while(GetMessage(&Msg, NULL, 0, 0) > 0) //while there are more than 0 messages?
        {
            TranslateMessage(&Msg); //current message interpreted???
            DispatchMessage(&Msg); //result of current message???
        }
        return Msg.wParam; //return result???
    }
    This summons a blank window whose internal region is 500x500 pixels squared.
    When you click the region it opens up a message box.
    Copy and pasting the code directly into an updated DevC++ compiler should work as is.


    How do I put a text box in that region instead?
    Do I have to define new content in an RC Resource file and use it in the CPP source file?

    Thank you everyone for your time
    I am new to C++ GUI programming, but I hope to learn.

  2. #2
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    Quote Originally Posted by CPlus View Post
    How do I put a text box in that region instead?
    Do I have to define new content in an RC Resource file and use it in the CPP source file?

    You can create a dialog/window in the IDE's resource editor (quicker and often easier)

    or

    Create the controls at run time (sometimes required and more 'dynamic').


    Then you have to handle msgs from the controls in your callback (ie WM_COMMAND -> EN_CHANGE shows text was entered).

    Have a look here for the general idea.
    "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

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    67

    Smile

    I have a new question, but here is the updated source code with the text box whose size is equal to the maximum region's size:

    Code:
    #include <windows.h>
    #define TextBoxDefined   101
    
    const char ClassName[] = "WindowClass";
    
    // Message Queue Commands
    LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
    {
        switch(msg) // if&else commands to follow when interpreting the current message in queue
        {
            case WM_CREATE:
            {
                HWND TextBox;
                TextBox = CreateWindowEx(WS_EX_CLIENTEDGE,
                                       "EDIT",
                                       "Predefined Text",
                                       WS_CHILD | WS_VISIBLE | WS_VSCROLL | WS_HSCROLL | ES_MULTILINE | ES_AUTOVSCROLL | ES_AUTOHSCROLL,
                                       0,
                                       0,
                                       0,
                                       0,
                                       hwnd,
                                       (HMENU)TextBoxDefined,
                                       GetModuleHandle(NULL),
                                       NULL);
                if(TextBox == NULL)
                    MessageBox(hwnd, "Text Box creation failed", "Error", MB_OK | MB_ICONERROR);
    
                HFONT hfDefault;
                hfDefault = (HFONT)GetStockObject(DEFAULT_GUI_FONT);
    
                SendMessage(TextBox, WM_SETFONT, (WPARAM)hfDefault, MAKELPARAM(FALSE,0));
            }
            break;
            
            case WM_SIZE:
            {
                HWND TextBox;
                TextBox = GetDlgItem(hwnd, TextBoxDefined);
                
                RECT FullWindow;
                GetClientRect(hwnd, &FullWindow);
                SetWindowPos(TextBox, //this object being our textbox
                             NULL, //???
                             0, //horizontal position from left
                             0, //vertical position from right
                             FullWindow.right, //full window horiztonal size
                             FullWindow.bottom, //full window vertical size
                             SWP_NOZORDER); //???
            }
            break;
            
            case WM_CLOSE:
                DestroyWindow(hwnd);
            break;
            case WM_DESTROY: //if message was the cancel button [x]???
                PostQuitMessage(0);
            break;
            default:
                return DefWindowProc(hwnd, msg, wParam, lParam);
        }
        return 0;
    }
    
    //WinMain() instead of main()
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
    {
     //Window class
        WNDCLASSEX wc;
        //Window class parameters
        wc.cbSize        = sizeof(WNDCLASSEX); //???
        wc.style         = 0; //???
        wc.lpfnWndProc   = WndProc; //???
        wc.cbClsExtra    = 0; //???
        wc.cbWndExtra    = 0; //???
        wc.hInstance     = hInstance; //???
        wc.hIcon         = LoadIcon(NULL,IDI_APPLICATION); //Executable's desktop icon
        wc.hCursor       = LoadCursor(NULL, IDC_ARROW); //Cursor
        wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); //Window color
        wc.lpszMenuName  = NULL; //No menu
        wc.lpszClassName = ClassName; //???
        wc.hIconSm       = LoadIcon(NULL, IDI_APPLICATION); //Window icon? , Sm = small?
    
        if(!RegisterClassEx(&wc)) //error handling
        {
            MessageBox(NULL, "Registration Failure", "Error", MB_OK); //Okay button
            return 0; //return fail
        }
    
        ////////////////////////////////////////////////////////////////////////////
    
     //The actual window
        HWND MyParentWindow;
        //The window's parameters
        MyParentWindow = CreateWindowEx(
            0, //Style
            ClassName, //Current class name , Does this mean MyParentWindow takes on wc's characteristics via the name ClassName?
            "Title", //Title
            WS_OVERLAPPEDWINDOW, //minimize, maximize, close
            0,//position x in pixels relative to screen
            0,//position y in pixels relative to screen
            500,//size x dimension in pixels
            500,//size y dimension in pixels
            NULL, //MyParentWindow is the parent window and it does not have a parent, thus NULL
            NULL, //no menu was defined
            hInstance, //???
            NULL); //???
    
        if(MyParentWindow == NULL) //error handling
        {
            MessageBox(NULL, "MyParentWindow Creation Failure", "Error", MB_OK); //okay button
            return 0;
        }
    
        ShowWindow(MyParentWindow, nCmdShow); //show the window called "MyParentWindow"
        UpdateWindow(MyParentWindow); //update the window called "MyParentWindow"
    
        ////////////////////////////////////////////////////////////////////////////
    
     // Message loop
        MSG Msg;
        //GetMessage() gets a message from message queue.
        while(GetMessage(&Msg, NULL, 0, 0) > 0) //while there are more than 0 messages?
        {
            TranslateMessage(&Msg); //current message interpreted???
            DispatchMessage(&Msg); //result of current message???
        }
        return Msg.wParam; //return result???
    }
    My new question is this:
    How do I edit the background color of this textbox?

    If it is at all possible, I would greatly appreciate to see how to use the GetWindowText() function in an example, and how to use case WM_KEYDOWN to associate key strokes with certain actions (such as using CTRL+A to select all of the text).

    These are the documentations I am looking at:
    GetTextFunction()
    and
    WM_KEYDOWN

    The information is there, I admit, but it's always nice to see concrete examples since I'm a little confused on the order of execution and the code placements.

    My guess is that the case WM_KEYDOWN would be used first, with the specific keys associated (how would I use a key combination like CTRL+A, or even simple letters like 'A'?), and then the GetWindowText() would be applied. How would I store the content inside a character array?

    Thank you everyone

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    214
    For the color : WM_CTRLCOLOREDIT message.

  5. #5
    Registered User
    Join Date
    Feb 2010
    Posts
    67
    Thanks DaveH

    (By the way, I think it's WM_CTLCOLOREDIT instead of WM_CTRLCOLOREDIT)
    (Not sure)


    I added this to my cases:

    Code:
            case WM_CTLCOLOREDIT:
            {
                    SetBkColor((HDC)wParam,RGB(63,127,255)); //paints up to current cursor position
                    return (int)CreateSolidBrush(RGB(63,127,255)); //paints the rest
            }
            break;
    When I have several edit boxes, it colors them all in with the color RGB(63,127,255).
    Is there a way to select which edit boxes will be colored and which will not?
    Last edited by CPlus; 07-19-2010 at 11:18 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 02-13-2008, 02:59 PM
  2. Automatically enter text in a dialog box
    By leojose in forum Windows Programming
    Replies: 6
    Last Post: 12-13-2005, 11:59 AM
  3. Dynamically add statis text to a dialog box
    By earth_angel in forum Windows Programming
    Replies: 8
    Last Post: 06-23-2005, 01:28 PM
  4. Sending text to an edit box
    By ColdFire in forum Windows Programming
    Replies: 1
    Last Post: 09-24-2002, 07:46 PM
  5. Getting my text from edit box
    By Marky_Mark in forum Windows Programming
    Replies: 2
    Last Post: 11-16-2001, 01:06 PM

Tags for this Thread