Solved

Edit controls through windows api

Posted on 1998-09-28
3
859 Views
Last Modified: 2013-12-03
I am (somewhat slowly) teaching myself how to program
windows via its own API rather than OWL/MFC/VCL
but am finding documentation somewhat scanty,
could someone supply me with a 'C' like example of how
to create an edit box or combobox. [Edit box the most essential]

I ask for C and not C++ as I'm making my own notes and
documentation against it for my own reference before encapsulating it into a C++ Object

This kind of help would be greatly appreciated

Best Regards
Matlock
0
Comment
Question by:Matlock
3 Comments
 
LVL 2

Accepted Solution

by:
cessi0g earned 200 total points
ID: 1414866
These are two examples from MSDN

-----------------------

Simple Word Processing with an Edit Control
The following example implements much of the functionality of a simple word processor by filling the client area of a window with a multiline edit control. The system automatically performs wordwrap operations for this edit control and also handles the processing for the vertical scroll bar (created by specifying ES_AUTOVSCROLL in the call to the CreateWindow function). The WM_COMMAND message processes menu items; they allow the user to undo the previous action, cut or copy selections to the clipboard, paste text from the clipboard, and delete the current selection.

LONG APIENTRY MainWndProc(
HWND hwnd,                // window handle
UINT message,             // type of message
WPARAM wParam,            // additional information
LPARAM lParam)            // additional information
{
    static HWND hwndEdit;
 
    CHAR lpszTrouble[] = "When in the Course of human Events " 
                         "it becomes necessary for one People " 
                         "to dissolve the Political Bands which " 
                         "have connected them with another, and " 
                         "to assume among the Powers of the " 
                         "Earth, the separate and equal Station " 
                         "to which the Laws of Nature and of " 
                         "Nature's God entitle them, a decent " 
                         "Respect to the Opinions of Mankind " 
                         "requires that they should declare the " 
                         "causes which impel them to the " 
                         "Separation. ";
 
    switch (message)
    {
        case WM_CREATE:
            hwndEdit = CreateWindow(
                "EDIT",     // predefined class
                NULL,       // no window title
                WS_CHILD | WS_VISIBLE | WS_VSCROLL |
                    ES_LEFT | ES_MULTILINE | ES_AUTOVSCROLL,
                0, 0, 0, 0, // set size in WM_SIZE message
                hwnd,       // parent window
                (HMENU) ID_EDITCHILD, // edit control ID
                (HINSTANCE) GetWindowLong(hwnd, GWL_HINSTANCE),
                NULL);                // pointer not needed
 
             // Add text to the window.
 
             SendMessage(hwndEdit, WM_SETTEXT, 0,
                (LPARAM) lpszTrouble);
 
            return 0;
 
        case WM_COMMAND:
            switch (wParam)
            {
                case IDM_EDUNDO:

                    // Send WM_UNDO only if there is something
                    // to be undone.
 
                    if (SendMessage(hwndEdit, EM_CANUNDO, 0, 0))
                        SendMessage(hwndEdit, WM_UNDO, 0, 0);
                    else
                    {
                        MessageBox(hwndEdit,
                            "Nothing to undo.",
                            "Undo notification", MB_OK);
                    }
                    break;
 
                case IDM_EDCUT:
                    SendMessage(hwndEdit, WM_CUT, 0, 0);
                    break;
 
                case IDM_EDCOPY:
                    SendMessage(hwndEdit, WM_COPY, 0, 0);
                    break;
 
                case IDM_EDPASTE:
                    SendMessage(hwndEdit, WM_PASTE, 0, 0);
                    break;
 
                case IDM_EDDEL:
                    SendMessage(hwndEdit, WM_CLEAR, 0, 0);
                    break;
 
                case IDM_PASSWORD:
                    DialogBox(hinst, // current instance
                        "PassBox",   // resource to use
                         hwnd,       // parent handle
                         (DLGPROC) PassProc);
                    break;
 
                case IDM_WRAP:
                    SendMessage(hwndEdit,
                        EM_SETWORDBREAKPROC,
                        (WPARAM) 0,
                        (LPARAM) (EDITWORDBREAKPROC) WordBreakProc);
                    SendMessage(hwndEdit,
                        EM_FMTLINES,
                        (WPARAM) TRUE,
                        (LPARAM) 0);
                    SendMessage(hwndEdit,
                        EM_SETSEL,
                        0, -1); // select all text
                    SendMessage(hwndEdit, WM_CUT, 0, 0);
                    SendMessage(hwndEdit, WM_PASTE, 0, 0);
                    break;
 
                case IDM_ABOUT:
                    DialogBox(hinst, // current instance
                        "AboutBox",  // resource to use
                         hwnd,       // parent handle
                         (DLGPROC) About);
                    break;
 
                default:
                    return DefWindowProc(hwnd, message, wParam, lParam);
            }
            break;
 
        case WM_SETFOCUS:
            SetFocus(hwndEdit);
            return 0;
 
        case WM_SIZE:
 
            // Make the edit control the size of the window's
            // client area.
 
            MoveWindow(hwndEdit,
                0, 0,           // starting x- and y-coordinates
                LOWORD(lParam), // width of client area
                HIWORD(lParam), // height of client area
                TRUE);          // repaint window
            return 0;
 
        case WM_DESTROY:
            PostQuitMessage(0);
            return 0;
 
        default:
            return DefWindowProc(hwnd, message, wParam, lParam);
    }
    return NULL;
}
 
------------------------

Using Single-Line Edit Controls
The example in this section demonstrates how to use a window procedure to produce a dialog box that prompts the user to enter a password.

The single-line edit control in the Password dialog box has the ES_PASSWORD style. By default, edit controls with this style display an asterisk for each character the user types. This example, however, uses the EM_SETPASSWORDCHAR message to change the default character from an asterisk to a plus sign (+).

This window procedure changes the default push button from Cancel to OK as soon as the user enters text in the edit control. If the user presses the OK button, the window procedure uses the EM_LINELENGTH and EM_GETLINE messages to retrieve the text.

LRESULT CALLBACK PassProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
    CHAR lpszPassword[16];
    WORD cchPassword;
 
    switch (message)
    {
        case WM_INITDIALOG:
 
            // Set password character to a plus sign (+)
 
            SendDlgItemMessage(hDlg,
                IDE_PASSWORDEDIT,
                EM_SETPASSWORDCHAR,
                (WPARAM) '+',
                (LPARAM) 0);
 
            // Set the default push button to "Cancel."
 
            SendMessage(hDlg,
                DM_SETDEFID,
                (WPARAM) IDCANCEL,
                (LPARAM) 0);
 
            return TRUE;
 
        case WM_COMMAND:
 
            // Set the default push button to "OK" when the user
            // enters text.
 
            if(HIWORD (wParam) == EN_CHANGE && 
                    LOWORD(wParam) == IDE_PASSWORDEDIT)
            {
                SendMessage(hDlg,
                    DM_SETDEFID,
                    (WPARAM) IDOK,
                    (LPARAM) 0);
            }
 
            switch(wParam)
            {
                case IDOK:
 
                    // Get number of characters.
 
                    cchPassword = (WORD) SendDlgItemMessage(hDlg,
                        IDE_PASSWORDEDIT,
                        EM_LINELENGTH,
                        (WPARAM) 0,
                        (LPARAM) 0);
 
                    if (cchPassword >= 16)
                    {
                        MessageBox(hDlg,
                            "Too many characters.",
                            "Error",
                            MB_OK);
                        EndDialog(hDlg, TRUE);
                        return FALSE;
                    }
                    else if (cchPassword == 0)
                    {
                        MessageBox(hDlg,
                            "No characters entered.",
                            "Error",
                            MB_OK);
                        EndDialog(hDlg, TRUE);
                        return FALSE;
                    }
 
                    // Put the number of characters into first word
                    // of buffer.
 
                    *((LPWORD)lpszPassword) = cchPassword;
 
                    // Get the characters.
 
                    SendDlgItemMessage(hDlg,
                        IDE_PASSWORDEDIT,
                        EM_GETLINE,
                        (WPARAM) 0,       // line 0
                        (LPARAM) lpszPassword);
 
                    // Null-terminate the string.
 
                    lpszPassword[cchPassword] = 0;
 
                    MessageBox(hDlg,
                        lpszPassword,
                        "Did it work?",
                        MB_OK);
 
                    // Call a local password-parsing function.
 
                    ParsePassword(lpszPassword);
 
                    EndDialog(hDlg, TRUE);
                    return TRUE;
 
                case IDCANCEL:
                    EndDialog(hDlg, TRUE);
                    return TRUE;
            }
            return 0;
    }
    return FALSE;
        UNREFERENCED_PARAMETER(lParam);
}
 
 

0
 
LVL 8

Expert Comment

by:Answers2000
ID: 1414867
Use CreateWindow API to create the window, which is defined :-

HWND CreateWindow( LPCTSTR lpClassName,
 // pointer to registered class name
 
LPCTSTR lpWindowName,
 // pointer to window name
 
DWORD dwStyle,
 // window style
 
int x,
 // horizontal position of window
 
int y,
 // vertical position of window
 
int nWidth,
 // window width
 
int nHeight,
 // window height
 
HWND hWndParent,
 // handle to parent or owner window
 
HMENU hMenu,
 // handle to menu or child-window identifier
 
HANDLE hInstance,
 // handle to application instance
 
LPVOID lpParam
 // pointer to window-creation data
 
);
 
e.g.

HWND hWndEdit = CreateWindow( "edit", "hello world", WS_CHILD | ES_LEFT, x, y, width, height, hWndParent /* Parent Window */, (HMENU)1000 /* Unique control id */, hInstance, NULL ) ;

x,y, width, height are coordinates of the control

Then show it
ShowWindow( hWndEdit, SW_SHOW ) ;
UpdateWindow( hWndEdit ) ;

To set the text use SetWindowText on hWndEdit
SetWindowText( hWndEdit, "Some words" ) ;

Send EM_ messages to manipulate the edit control.

BTW ES_LEFT in the CreateWindow call is the "style" of the edit control.

For combo box first param of CreateWindow is "combobox",  styles of combos generally begin with CBS_ and the combo messages begin with CB_


0
 

Author Comment

by:Matlock
ID: 1414868
I'm really impressed,
thanks very much
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article surveys and compares options for encoding and decoding base64 data.  It includes source code in C++ as well as examples of how to use standard Windows API functions for these tasks. We'll look at the algorithms — how encoding and decodi…
What my article will show is if you ever had to do processing to a listbox without being able to just select all the items in it. My software Visual Studio 2008 crystal report v11 My issue was I wanted to add crystal report to a form and show…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now