Thread: edit controls in dialogs

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    71

    edit controls in dialogs

    I've been trying to look for tutorials on how to use edit boxes in dialogs windows (modeless ones). I haven't had to much luck, can some one explain or point me to a place that does?

  2. #2
    Registered User
    Join Date
    Feb 2002
    Posts
    329
    Try GetDlgItemText()/SetDlgItemText()

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    10

    Smile

    Hellow:

    Using :

    File resource *.rc:
    Code:
    IDD_DIALOGO DIALOG DISCARDABLE 0,0,148,100
    STYLE WS_CHILD|WS_BORDER
    FONT 9, "Verdana"
    BEGIN
           EDITTEXT "",IDC_TEXTO,10,10,100,10,WS_VISIBLE|WS_BORDER
           LTEXT    "",IDC_SALIDA,10, 25, 100, 10, WS_CHILD|WS_VISIBLE|WS_BORDER
    END
    FILE *.h :

    Code:
    #define IDC_TEXTO  125
    #define IDC_SALIDA  126
    File *.cpp; WindowProcedure:
    Code:
    static HWND Number,Salida;
    
       Number = GetDlgItem(hwnd,IDC_TEXTO);
       Salida = GetDlgItem(hwnd,IDC_SALIDA);
       int Var;
    
       LPTSTR strCadena = new char[20],strResult = new char[20];
    
        //GetWindowText(HWND hwnd, LPTSTR cadena, len cadena);
    
       GetWindowText(Number,strCadena,20);
       var = atoi(strCadena) * 2;
       sprintf(strResult,"%0.0d",var);
       SetWindowText(Salida,strResult);
       free(strCadena);
       free(strResult);
    Bye
    Last edited by nostromos; 02-21-2006 at 02:14 PM.

  4. #4
    Registered User
    Join Date
    Feb 2006
    Posts
    71

    ...

    Thanks, but all I really wanted to know was the keyword for making edit controls on my dialog window and I probably could of researched the rest =P(parameters etc).

    Also isn't whats the use of LTEXT there? i read on msdn it left alligns the text but doesnt it already do that by default? (I could be completely off =P )

  5. #5
    Registered User
    Join Date
    Oct 2005
    Posts
    10
    hai:

    My domain the inglish lenguage is very bad, sorry. But, your answer is how to makin the control inside event the dialog window???.

    Not makin from resource file..... The making control inside the WM_INITDIALOG.

    The control making to be possible two way:

    1- CreateWindow

    2- CreateWindowEx

    This site explaing how to user the dialog control :

    http://www.functionx.com/win32/index.htm

    Programming Window not dialog.

    Bye

  6. #6
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    Quote Originally Posted by nostromos
    Code:
    static HWND Number,Salida;
    
       Number = GetDlgItem(hwnd,IDC_TEXTO);
       Salida = GetDlgItem(hwnd,IDC_SALIDA);
       int Var;
    
       LPTSTR strCadena = new char[20],strResult = new char[20];
    
        //GetWindowText(HWND hwnd, LPTSTR cadena, len cadena);
    
       GetWindowText(Number,strCadena,20);
       var = atoi(strCadena) * 2;
       sprintf(strResult,"%0.0d",var);
       SetWindowText(Salida,strResult);
       free(strCadena);
       free(strResult);
    Bye
    If you allocate memory with new, then you must use delete (or delete[] for arrays), if you allocate memory with malloc then you must use free. Please read this for clarification:

    http://www.parashift.com/c++-faq-lit....html#faq-16.3

    ****

    >>all I really wanted to know was the keyword for making edit controls on my dialog window<<

    If you're alluding to resource scripts then refer to resource definition statements on msdn.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  7. #7
    Registered User
    Join Date
    Feb 2006
    Posts
    71

    ...

    Well, I managed to get my base like converter to run through my menu lol, but when I do the exact same thing to create another dialog window for like say, a hex calculator, it doesnt work =/, the weird part is, If I call it Base Window, the hex calc works fine through my base window in my menu.

    Heres main.cpp source:

    Code:
    #include <windows.h>
    #include "hex.h"
    
    const char g_ClassName[]="hwnd window class name";
    const char g_BaseClassName[]="base class name";
    HWND BaseWindow;
    HWND HexWindow;
    HWND hEdit;
    HWND hEdit_Dec,hEdit_Oct,hEdit_Hex,hEdit_Bin;
    HFONT hfDefault = (HFONT)GetStockObject(DEFAULT_GUI_FONT);
    
    BOOL CALLBACK HexWindowProc(HWND hwnd,UINT mess,WPARAM wParam,LPARAM lParam)
    {
        switch(mess)
        {
            case WM_CLOSE:
                 ShowWindow(BaseWindow,SW_HIDE);
                 break;
                 
            case WM_COMMAND:
                 {
                    switch(LOWORD(wParam))
                    {
                        case EXTRA_HEX_CALC_CALL:
                                ShowWindow(HexWindow,SW_SHOW);
                        break;
                    }
                 }
            break;
            
            default:
                 return false;
        }
        return true;
    }
    
    BOOL CALLBACK BaseWindowProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam)
    {
        switch(message)
        {
            case WM_CLOSE:
                 ShowWindow(BaseWindow,SW_HIDE);
                 break;
                 
            case WM_COMMAND:
                 {
                    switch(LOWORD(wParam))
                    {
                        case BASE_CONVERT_CLOSE:
                             ShowWindow(BaseWindow,SW_HIDE);
                             break;
                        
                        case BASE_CONVERT:
                             {
                             int x;
                             char hex[MAX_PATH];
                             char oct[MAX_PATH];
                             char bin[MAX_PATH];
                             
                             x = GetDlgItemInt(BaseWindow,BASE_HEDIT_DEC,NULL,false);
                             
                             SetDlgItemText(BaseWindow,BASE_HEDIT_HEX,itoa(x,hex,16));
                             SetDlgItemText(BaseWindow,BASE_HEDIT_OCT,itoa(x,oct,8));
                             SetDlgItemText(BaseWindow,BASE_HEDIT_BIN,itoa(x,bin,2));
                             }
                        break;
                    }
                 }
            break;
            
            default:
                 return false;
        }
        return true;
    }
    
    LRESULT CALLBACK WndProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam)
    {
            switch(msg)
            {
                case WM_CLOSE:
                     DestroyWindow(hwnd);
                     break;
                
                case WM_DESTROY:
                     PostQuitMessage(0);
                     break;
                
                case WM_COMMAND:
                     {
                        switch(LOWORD(wParam))
                        {
                            case HWND_FILE_EXIT:
                                 PostQuitMessage(0);
                                 break;
                            
                            case EXTRA_BASE_CONVERT_CALL:
                                 ShowWindow(BaseWindow,SW_SHOW);
                                 break;
                        }
                     }
                break;
                
                case WM_CREATE:
                     {
                         BaseWindow = CreateDialog(GetModuleHandle(NULL),MAKEINTRESOURCE(EXTRA_BASE_CONVERT),
                                                   hwnd,BaseWindowProc);
                         
                         ShowWindow(BaseWindow,SW_HIDE);
                         
                         HexWindow = CreateDialog(GetModuleHandle(NULL),MAKEINTRESOURCE(EXTRA_HEX_CALC),
                                                   hwnd,HexWindowProc);
                         
                         ShowWindow(HexWindow,SW_HIDE);
                         
                         hEdit = CreateWindowEx(WS_EX_CLIENTEDGE,
                                                "EDIT",
                                                "",
                                                WS_CHILD | WS_VISIBLE | ES_MULTILINE | WS_VSCROLL,
                                                0,0,
                                                100,100,
                                                hwnd,
                                                (HMENU)HWND_HEDIT,
                                                GetModuleHandle(NULL),
                                                NULL);
                         
                         SendMessage(hEdit,WM_SETFONT,(WPARAM)hfDefault,MAKELPARAM(false,0));
                     }
                break;
                
                case WM_SIZE:
                     {
                         RECT hwnd_cord;
                         GetClientRect(hwnd,&hwnd_cord);
                         
                         SetWindowPos(hEdit,hwnd,0,0,hwnd_cord.right,hwnd_cord.bottom,SWP_NOZORDER);
                     }
                break;
                
                default:
                     return DefWindowProc(hwnd,msg,wParam,lParam);
            }
    }
    
    int WINAPI WinMain(HINSTANCE     hInstance,
                       HINSTANCE hPrevInstance,
                       LPSTR          nCmdLine,
                       int            nCmdShow)
    {
        HWND hwnd;
        MSG Message;
        WNDCLASSEX wc;
        
        wc.cbSize = sizeof(WNDCLASSEX);
        wc.cbWndExtra = 0;
        wc.cbClsExtra = 0;
        wc.style = 0;
        wc.hbrBackground = CreateSolidBrush(RGB(0,0,100));
        wc.hIcon = LoadIcon(NULL,IDI_APPLICATION);
        wc.hIconSm = LoadIcon(NULL,IDI_APPLICATION);
        wc.hInstance = hInstance;
        wc.hCursor = LoadCursor(NULL,IDC_ARROW);
        wc.lpfnWndProc = WndProc;
        wc.lpszMenuName = MAKEINTRESOURCE(HWND_MENU);
        wc.lpszClassName = g_ClassName;
    
        RegisterClassEx(&wc);
        
        hwnd = CreateWindowEx(WS_EX_CLIENTEDGE,
                              g_ClassName,
                              "Hex Editor",
                              WS_OVERLAPPEDWINDOW,
                              CW_USEDEFAULT,CW_USEDEFAULT,
                              CW_USEDEFAULT,CW_USEDEFAULT,
                              HWND_DESKTOP,
                              NULL,
                              hInstance,
                              NULL);
        
        ShowWindow(hwnd,nCmdShow);
        UpdateWindow(hwnd);
        
        while(GetMessage(&Message,NULL,0,0)>0)
        {
            TranslateMessage(&Message);
            DispatchMessage (&Message);
        }
        
        return Message.wParam;
    }
    Heres my hex.h

    Code:
    //menu identifiers
    #define HWND_MENU               100
    
    //hwnd_menu item identifiers
    #define HWND_FILE_EXIT          200
    #define HWND_FILE_OPEN          201
    #define HWND_FILE_SAVEAS        202
    
    //Dialog identifiers
    #define EXTRA_BASE_CONVERT      300
    #define EXTRA_BASE_CONVERT_CALL 301
    #define EXTRA_HEX_CALC_CALL     302
    #define EXTRA_HEX_CALC          303
    
    #define BASE_CONVERT            304
    #define BASE_CONVERT_CLOSE      305
    #define BASE_HEDIT_DEC          306
    #define BASE_HEDIT_BIN          307
    #define BASE_HEDIT_HEX          308
    #define BASE_HEDIT_OCT          309
    
    //Edit box identifiers
    #define HWND_HEDIT              400
    
    //Hex calculator Identifiers
    #define HEX_BUTTON_1            401
    #define HEX_BUTTON_2            402
    #define HEX_BUTTON_3            403
    #define HEX_BUTTON_4            404
    #define HEX_BUTTON_5            405
    #define HEX_BUTTON_6            406
    #define HEX_BUTTON_7            407
    #define HEX_BUTTON_8            408
    #define HEX_BUTTON_9            409
    
    #define HEX_BUTTON_CALC         410
    #define HEX_BUTTON_DIV          411
    #define HEX_BUTTON_MUL          412
    #define HEX_BUTTON_SUB          413
    #define HEX_BUTTON_ADD          414
    #define HEX_HEDIT               415
    #define HEX_CALL                416
    and heres my untitled.rc

    Code:
    #include <windows.h>
    #include "hex.h"
    
    HWND_MENU MENU
    {
        POPUP "&File"
        {
            MENUITEM "&Open",HWND_FILE_OPEN
            MENUITEM SEPARATOR
            MENUITEM "&Save As",HWND_FILE_SAVEAS
            MENUITEM SEPARATOR
            MENUITEM "E&xit",HWND_FILE_EXIT
        }
        POPUP "E&xtras"
        {
            MENUITEM "&Base Converter",EXTRA_BASE_CONVERT_CALL
            MENUITEM "&Hex Calculator",EXTRA_HEX_CALC_CALL
        }
    }
    
    EXTRA_BASE_CONVERT DIALOGEX 0,0,200,100
    STYLE DS_MODALFRAME | WS_POPUP | WS_SYSMENU
    EXSTYLE WS_EX_TOOLWINDOW
    FONT 8,"MS Sans Serif"
    CAPTION "Base Converter"
    {
        DEFPUSHBUTTON "Close",BASE_CONVERT_CLOSE,100,70,50,15
        DEFPUSHBUTTON "Convert",BASE_CONVERT,50,70,50,15
        EDITTEXT      BASE_HEDIT_DEC,10,10,75,13,WS_VISIBLE|WS_BORDER|ES_MULTILINE|ES_NUMBER
        EDITTEXT      BASE_HEDIT_HEX,10,40,75,13,WS_VISIBLE|WS_BORDER|ES_MULTILINE|ES_READONLY
        EDITTEXT      BASE_HEDIT_OCT,105,10,75,13,WS_VISIBLE|WS_BORDER|ES_MULTILINE|ES_READONLY
        EDITTEXT      BASE_HEDIT_BIN,105,40,75,26,WS_VISIBLE|WS_HSCROLL|WS_BORDER|ES_MULTILINE|ES_READONLY
    }
    
    EXTRA_HEX_CALC DIALOGEX 0,0,200,100
    STYLE DS_MODALFRAME | WS_POPUP | WS_SYSMENU
    EXSTYLE WS_EX_TOOLWINDOW
    FONT 8,"MS Sans Serif"
    CAPTION "Hex Calculator"
    {
         DEFPUSHBUTTON "1",HEX_BUTTON_1,0,0,5,5
    }
    I think the problem is my HexWindowProc function in my main.cpp file, but im not sure... I tried running the hex calculator and base converter through the same procedure function, but that didnt work either =/.

  8. #8
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    There seem to be a few problems with this. Firstly, though not necessarily the reason for the failure of the dialog to show, your WndProc function doesn't always return a valid value - if you're going to have a default within a switch statement then just use 'return' and not 'break' or, alternatively, remove the default from the switch altogether and move the DefWindowProc call to the end of the function, outwith the switch.

    You seem to be trying to get both dialogs to show from the parent window's menu. That being the case, you should have a command handler similar to what you have for the hex calc dialog in your WndProc for the main window eg. a call to ShowWindow for the other dialog you have created.

    If your aim is to only have one dialog open at any one time then use modal and not modeless dialogs.

    You should probably consider only creating/destroying the dialogs in response to user action, rather than creating them in the WM_CREATE handler - ie. consider creating them when the respective menu items are selected.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  9. #9
    Registered User
    Join Date
    Feb 2006
    Posts
    71

    thank you

    lol, ya, i just needed to make them be created through the wm_command message, thanks =P. I want to be using modeless dialog box's. Also I now have both the HexWindow and BaseWindow Messages running through one procedure(should I change it this?).

  10. #10
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    If the behaviour/properties of the dialogs are different it's probably better to have different dialog procedures.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  11. #11
    Registered User
    Join Date
    Feb 2006
    Posts
    71

    ...

    ya, i did it anyways, mainly because I dont know if there is any bad affect to using additional procedures(besides that its more code =/ ).

    EDIT:

    When I goto implement the divide button etc. do I want to GetDlgItemText for each of those, and store it in a static char array? and when I want it to print out the hex version, do I just use itoa? or should I make my own function to convert to hex?

    Also, when I have my clear button, I just have it reset my array holding my information?
    Last edited by Homunculus; 02-23-2006 at 05:16 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multiline Edit Box Parser
    By The Brain in forum Windows Programming
    Replies: 6
    Last Post: 11-01-2005, 07:15 PM
  2. Auto Tab Edit Controls
    By willc0de4food in forum Windows Programming
    Replies: 1
    Last Post: 09-07-2005, 01:59 PM
  3. Question about dialogs and displaying/hiding controls
    By Longie in forum Windows Programming
    Replies: 2
    Last Post: 05-11-2005, 05:26 PM
  4. Subclassing controls
    By filler_bunny in forum Windows Programming
    Replies: 3
    Last Post: 04-28-2004, 05:43 PM
  5. Dialog Edit Control and Enter Key
    By Quantrizi in forum Windows Programming
    Replies: 2
    Last Post: 04-14-2004, 07:59 AM