Iowa15
Iowa15

Reputation: 3079

How to create an embedded text input box in win32 windows

I want to create a little box embedded in my main window that can accept text input and contain buttons. When I say embedded, I mean I want it to be integrated in my main window seamlessly without its own close button. I have searched google endlessly and have not found an answer that does not involve mfc or .net. How can I do this in a raw win32 application. Please add your code to my skeleton shown below. Thanks!

#include <windows.h>

LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
                    PSTR szCmdLine, int iCmdShow)
{
     static TCHAR szAppName[] = TEXT ("App") ;
     HWND         hwnd ;
     MSG          msg ;
     WNDCLASS     wndclass ;

     wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
     wndclass.lpfnWndProc   = WndProc ;
     wndclass.cbClsExtra    = 0 ;
     wndclass.cbWndExtra    = 0 ;
     wndclass.hInstance     = hInstance ;
     wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
     wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
     wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
     wndclass.lpszMenuName  = NULL ;
     wndclass.lpszClassName = szAppName ;

     if (!RegisterClass (&wndclass))
     {
          MessageBox (NULL, TEXT ("This program requires Windows NT!"),
                      szAppName, MB_ICONERROR) ;
          return 0 ;
     }

     hwnd = CreateWindow (szAppName,                  // window class name
                          TEXT ("App"), // window caption
                          WS_OVERLAPPEDWINDOW,        // window style
                          CW_USEDEFAULT,              // initial x position
                          CW_USEDEFAULT,              // initial y position
                          CW_USEDEFAULT,              // initial x size
                          CW_USEDEFAULT,              // initial y size
                          NULL,                       // parent window handle
                          NULL,                       // window menu handle
                          hInstance,                  // program instance handle
                          NULL) ;                     // creation parameters

     ShowWindow (hwnd, iCmdShow) ;
     UpdateWindow (hwnd) ;

     while (GetMessage (&msg, NULL, 0, 0))
     {
          TranslateMessage (&msg) ;
          DispatchMessage (&msg) ;
     }
     return msg.wParam ;
}

LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
     HDC         hdc ;
     PAINTSTRUCT ps ;
     RECT        rect ;

     switch (message)
     {
     case WM_CREATE:

          return 0 ;

     case WM_PAINT:
          hdc = BeginPaint (hwnd, &ps) ;



          EndPaint (hwnd, &ps) ;
          return 0 ;

     case WM_DESTROY:
          PostQuitMessage (0) ;
          return 0 ;
     }
     return DefWindowProc (hwnd, message, wParam, lParam) ;
}

Upvotes: 3

Views: 17226

Answers (3)

YangZai
YangZai

Reputation: 323

I think Edit Control may meet your needs. The link guides to documents of Microsoft about Edit Control.

An edit control is a rectangular control window typically used in a dialog box to permit the user to enter and edit text by typing on the keyboard.

Upvotes: 0

jsist
jsist

Reputation: 5253

What all messages do you want to receive?

For instance, WM_COMMAND message is sent when button is pressed. Handle these messages as follows:

LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    HDC         hdc ;
    PAINTSTRUCT ps;
    RECT        rect ;

    switch (message)
        {
        case WM_CREATE:
            return 0 ;

        case WM_COMMAND:
            {
            switch(LOWORD(wParam))
                {
                case IDC_BUTTON:
                    /*
                    IDC_BUTTON is id given to button
                    This id is specified while creating window. If you are using
                    CreateWindow then it will be as follows:

                    CreateWindow("BUTTON", 0, WS_CHILD|WS_VISIBLE, 70, 70, 80, 25, g_hWnd, (HMENU)IDC_BUTTON, hInst, 0);

                    and in resource.rc file or in the beginning of code. Do "#define IDC_BUTTON                  3456"
                    3456 is just a reference you can give any other no as well. Prefer using bigger number so that they can not conflict with existing ones.
                    */

                    //do whatever you want to do here when button is pressed
                    break
                }
            }
            break;

        case WM_PAINT:
            hdc = BeginPaint (hwnd, &ps) ;

            EndPaint (hwnd, &ps) ;
            return 0 ;

        case WM_DESTROY:
            PostQuitMessage (0) ;
            return 0 ;
        }

    return DefWindowProc (hwnd, message, wParam, lParam) ;
}

Upvotes: 4

Superman
Superman

Reputation: 3081

Your question is not very clear but the way you create text boxes and buttons dynamically in Win32 is to use the CreateWindow function. Win32 registers special classes for the controls, which you can use to pass as the first parameter to CreateWindow.

To create a text box, the call is -

CreateWindow("EDIT", 0, WS_BORDER|WS_CHILD|WS_VISIBLE, 56, 10, 50, 18, g_hWnd, 0, hInst, 0);

To Create a button -

CreateWindow("BUTTON", 0, WS_CHILD|WS_VISIBLE, 70, 70, 80, 25, g_hWnd, 0, hInst, 0);

You have to specify the WS_CHILD and WS_VISIBLE styles and also provide a handle to the main window as its parent.

Upvotes: 6

Related Questions