Return/Enter

Benny Hill 20 Reputation points
2024-04-13T14:25:18.2166667+00:00

I have made a small windows program that saves names to a text file. Everything works with a button, but how can the names be registered if you just want to press Return/Enter after writing the names? The code is:

#include <windows.h>

#include <stdlib.h>

#include <string.h>

#include <tchar.h>

#include <vector>

#include <fstream>

// Global variables

static TCHAR szWindowClass[] = _T("DesktopApp");

static TCHAR szTitle[] = _T("Windows Desktop Guided Tour Application");

HINSTANCE hInst;

std::vectorstd::wstring names; // Vector to store names

// Forward declarations of functions included in this code module:

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

void SaveNamesToFile(HWND hWnd) {

std::wofstream file("personnamn.txt");

if (file.is_open()) {

    for (const auto& name : names) {

        file << name << std::endl;

    }

    file.close();

}

// Reset edit control

HWND hEdit = GetDlgItem(hWnd, 1);

SetWindowText(hEdit, _T(""));

}

int WINAPI WinMain(

_In_ HINSTANCE hInstance,

_In_opt_ HINSTANCE hPrevInstance,

_In_ LPSTR     lpCmdLine,

_In_ int       nCmdShow

)

{

WNDCLASSEX wcex;

wcex.cbSize = sizeof(WNDCLASSEX);

wcex.style = CS_HREDRAW | CS_VREDRAW;

wcex.lpfnWndProc = WndProc;

wcex.cbClsExtra = 0;

wcex.cbWndExtra = 0;

wcex.hInstance = hInstance;

wcex.hIcon = LoadIcon(wcex.hInstance, IDI_APPLICATION);

wcex.hCursor = LoadCursor(NULL, IDC_ARROW);

wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);

wcex.lpszMenuName = NULL;

wcex.lpszClassName = szWindowClass;

wcex.hIconSm = LoadIcon(wcex.hInstance, IDI_APPLICATION);

if (!RegisterClassEx(&wcex))

{

    MessageBox(NULL,

        _T("Call to RegisterClassEx failed!"),

        _T("Windows Desktop Guided Tour"),

        NULL);

    return 1;

}

hInst = hInstance;

HWND hWnd = CreateWindowEx(

    WS_EX_OVERLAPPEDWINDOW,

    szWindowClass,

    szTitle,

    WS_OVERLAPPEDWINDOW,

    CW_USEDEFAULT, CW_USEDEFAULT,

    800, 600,

    NULL,

    NULL,

    hInstance,

    NULL

);

if (!hWnd)

{

    MessageBox(NULL,

        _T("Call to CreateWindow failed!"),

        _T("Windows Desktop Guided Tour"),

        NULL);

    return 1;

}

ShowWindow(hWnd,

    nCmdShow);

UpdateWindow(hWnd);

// Main message loop:

MSG msg;

while (GetMessage(&msg, NULL, 0, 0))

{

    TranslateMessage(&msg);

    DispatchMessage(&msg);

}

return (int)msg.wParam;

}

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)

{

switch (message)

{

case WM_CREATE:

    // Create edit control to input names

    CreateWindow(_T("EDIT"), _T(""), WS_CHILD | WS_VISIBLE | WS_BORDER | ES_AUTOHSCROLL,

        20, 40, 200, 25, hWnd, (HMENU)1, NULL, NULL);

    // Create button to save names

    CreateWindow(_T("BUTTON"), _T("Spara"), WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,

        20, 80, 100, 30, hWnd, (HMENU)2, NULL, NULL);

    break;

case WM_COMMAND:

    if (LOWORD(wParam) == 2 && HIWORD(wParam) == BN_CLICKED) {

        // Save button clicked

        HWND hEdit = GetDlgItem(hWnd, 1);

        int len = GetWindowTextLength(hEdit);

        if (len > 0) {

            TCHAR* buffer = new TCHAR[len + 1];

            GetWindowText(hEdit, buffer, len + 1);

            names.push_back(buffer);

            delete[] buffer;

            InvalidateRect(hWnd, NULL, TRUE); // Redraw window

            SaveNamesToFile(hWnd); // Save names to file

            SetWindowText(hEdit, _T("")); // Clear edit control

        }

    }

    break;

case WM_PAINT:

{

    PAINTSTRUCT ps;

    HDC hdc = BeginPaint(hWnd, &ps);

    // Display names in a column

    int yPos = 120;

    for (const auto& name : names) {

        TextOut(hdc, 20, yPos, name.c_str(), name.length());

        yPos += 20; // Move to next line

    }

    EndPaint(hWnd, &ps);

    break;

}

case WM_DESTROY:

    PostQuitMessage(0);

    break;

default:

    return DefWindowProc(hWnd, message, wParam, lParam);

    break;

}

return 0;

}

C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,556 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Castorix31 82,031 Reputation points
    2024-04-14T07:55:14.7866667+00:00

    Among the known methods, you can subclass the Edit control and send WM_COMMAND with BN_CLICKED to the main window when WM_KEYDOWN/VK_RETURN, or, as David Lowndes suggested, add IsDialogMessage in the main loop but you must also change the default button, for example by using IDOK (1) as identifier (like DM_SETDEFID for Dialog Boxes)

    1 person found this answer helpful.
    0 comments No comments