Now I have tried everything but the Enter-button seems not be working like my save-button. If you can find a solution I would be so glad to know. Here's my code:
#include <windows.h>
#include <tchar.h>
#include <fstream>
#include <string>
// Global variables
static TCHAR szWindowClass[] = _T("DesktopApp");
static TCHAR szTitle[] = _T("Namnlista");
// Function to save names to a text file
void SaveNamesToFile(const std::wstring& filePath, const std::wstring& name) {
std::wofstream outFile(filePath, std::ios::app);
if (outFile.is_open()) {
outFile << name << std::endl;
outFile.close();
}
}
// Forward declarations of functions included in this code module:
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
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!"),
szTitle,
NULL);
return 1;
}
// Create the text file with full path
TCHAR szFilePath[MAX_PATH];
GetModuleFileName(NULL, szFilePath, MAX_PATH);
std::wstring fullPath(szFilePath);
fullPath = fullPath.substr(0, fullPath.find_last_of('\\'));
fullPath += _T("\\names.txt");
std::wofstream createFile(fullPath);
// The parameters to CreateWindowEx explained:
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!"),
szTitle,
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 | ES_WANTRETURN,
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);
SaveNamesToFile(_T("C:\\Mr T\\Mitt Visual Studio mapp\\Namnlista2\\names.txt"), buffer); // Update with your full path
delete[] buffer;
SetWindowText(hEdit, _T("")); // Clear edit control
}
}
break;
case WM_KEYDOWN:
// Check if Enter key is pressed
if (wParam == VK_RETURN) {
// Simulate a button click for Spara button
SendMessage(hWnd, WM_COMMAND, MAKEWPARAM(2, BN_CLICKED), (LPARAM)GetDlgItem(hWnd, 2));
}
break;
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hWnd, &ps);
EndPaint(hWnd, &ps);
break;
}
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
break;
}
return 0;
}