Home > Net >  problem with edit control in winapi and c
problem with edit control in winapi and c

Time:09-28

I'm making a basic calculator with a C interface with the help of Winapi, I've already done the graphic part (I'll leave a screenshot below) and now I need to finish the code.

I could already add text to the Edit Control using the buttons, I want that when selecting buttons 1 and 2 the number 12 is typed, the problem is that when doing so, one number is overwritten over another, if I type button 1 and then 2 is written the number 1, then it is erased and 2 is written.

Do you know how I can fix it? I would also like to know how I can save the data that is inside the Edit Control in a variable and then do the operations.

Excuse me if they are somewhat basic things, I am starting with Winapi, I leave you what I have of the code, thank you very much :)

enter image description here

#include<windows.h>
#include"resource.h"
HWND text;
BOOL CALLBACK mycallback(HWND window, UINT message, WPARAM wParam, LPARAM lParam) {
    
    switch (message) {
    case WM_CLOSE: {
        DestroyWindow(window);
        break;
    }
    case WM_DESTROY: {
        PostQuitMessage(0);
        break;
    }
    case WM_INITDIALOG:
    {
        text = GetDlgItem(window, IDC_EDIT1);
        break;
    }
    case WM_COMMAND:
        switch (LOWORD(wParam))
        {
        case IDC_BUTTON1:
        {
            SendMessage(text, WM_SETTEXT, 0, (LPARAM)"1");
            break;
            
        }
        case IDC_BUTTON2:
        {
            SendMessage(text, WM_SETTEXT, 0, (LPARAM)"2");
            break;
        }
        case IDC_BUTTON3:
        {

            SendMessage(text, WM_SETTEXT, 0, (LPARAM)"3");
            break;
        }
        case IDC_BUTTON4:
        {
            SendMessage(text, WM_SETTEXT, 0, (LPARAM)"4");
            break;
        }
        case IDC_BUTTON5:
        {
            SendMessage(text, WM_SETTEXT, 0, (LPARAM)"5");
            break;
        }
        case IDC_BUTTON6:
        {
            SendMessage(text, WM_SETTEXT, 0, (LPARAM)"6");
            break;
        }
        case IDC_BUTTON7:
        {
            SendMessage(text, WM_SETTEXT, 0, (LPARAM)"7");
            break;
        }
        case IDC_BUTTON8:
        {
            SendMessage(text, WM_SETTEXT, 0, (LPARAM)"8");
            break;
        }
        case IDC_BUTTON9:
        {
            SendMessage(text, WM_SETTEXT, 0, (LPARAM)"9");
            break;
        }
        case IDC_BUTTON10:
        {
            SendMessage(text, WM_SETTEXT, 0,(LPARAM)"0");
            break;
        }
        }
    }
    return false;
}

int WINAPI WinMain(HINSTANCE a, HINSTANCE b, LPSTR c, int d) {
    DialogBox(a, MAKEINTRESOURCE(IDD_DIALOG1), NULL, mycallback);
    return 0;
}

CodePudding user response:

The simplest way would be to get the text of the control first, append a new digit to it, and then set it.

See GetWindowText and SetWindowText

CodePudding user response:

You could ask for the current text using GetWindowText, append the new text, and follow up with another call to SetWindowText.

Or simply use the Edit control's built-in functionality, and send it an EM_REPLACESEL message. If there is no selection, the new text is inserted at the current caret position.

Reading text back into a variable is best handled in response to an EN_CHANGE notification.

  • Related