Code:
#include <windows.h>
#include <stdio.h>
#define ID_ADD 1001
#define ID_SAVE 1002
#define ID_EDIT 1003
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_CREATE:
{
HWND edit = CreateWindowEx(WS_EX_CLIENTEDGE,
"Edit",
"",
WS_BORDER | WS_CHILD | WS_VISIBLE | ES_LEFT,
30, 60, 420, 20,
hwnd,
(HMENU) ID_EDIT,
(HINSTANCE) GetWindowLong(hwnd, GWL_HINSTANCE),
NULL);
HWND add = CreateWindow("Button",
"Add",
WS_CHILD | WS_VISIBLE | ES_LEFT,
30, 280, 60, 18,
hwnd,
(HMENU) ID_ADD,
(HINSTANCE) GetWindowLong(hwnd, GWL_HINSTANCE),
NULL);
HWND save = CreateWindow("Button",
"save",
WS_CHILD | WS_VISIBLE | ES_LEFT,
100, 280, 60, 18,
hwnd,
(HMENU) ID_SAVE,
(HINSTANCE) GetWindowLong(hwnd, GWL_HINSTANCE),
NULL);
}
break;
case WM_COMMAND:
switch(LOWORD(wParam))
{
case ID_ADD:
{
OPENFILENAME ofn;
char szFileName[MAX_PATH] = "";
ZeroMemory(&ofn, sizeof(OPENFILENAME));
ofn.lStructSize = sizeof(OPENFILENAME);
ofn.hwndOwner = hwnd;
ofn.lpstrFilter = "Text Files (*.txt)\0*.txt\0All Files (*.*)\0*.*\0";
ofn.lpstrFile = szFileName;
ofn.nMaxFile = MAX_PATH;
ofn.lpstrDefExt = "txt";
ofn.Flags = OFN_EXPLORER | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT;
if (GetSaveFileName(&ofn))
{
HWND edit2 = GetDlgItem(hwnd, ID_EDIT);
SendMessage(edit2, WM_SETTEXT, 0, (LPARAM)szFileName);
}
}
break;
case ID_SAVE:
{
char abc[255];
HWND edit2 = GetDlgItem(hwnd, ID_EDIT);
SendMessage(edit2, WM_GETTEXT, 255, (LPARAM)abc);
MessageBox(NULL, abc, NULL, MB_OK);
FILE * file_config;
file_config = fopen("file.conf", "wb+");
if (ferror (file_config))MessageBox(NULL, "error 2", NULL, MB_OK);
if (file_config == NULL) MessageBox(NULL, "Error opening config file", "Error!", MB_ICONERROR | MB_OK);
int writeresult = fwrite(abc, 1, strlen(abc), file_config);
fclose(file_config);
if (writeresult != strlen(abc));
else
{
char a[500];
sprintf(a,"written -> %d\n",writeresult);
MessageBox(NULL, a, NULL, MB_OK);
}
}
break;
}
break;
case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
return 0;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
WNDCLASSEX wc;
HWND hwnd;
MSG Msg;
//Step 1: Registering the Window Class
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = 0;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
//wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW);
wc.lpszMenuName = NULL;
wc.lpszClassName = "1";
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
if(!RegisterClassEx(&wc))
{
MessageBox(NULL, "Window Registration Failed!", "Error!", MB_ICONEXCLAMATION | MB_OK);
return 0;
}
// Step 2: Creating the Window
hwnd = CreateWindowEx(
1,
//WS_EX_CLIENTEDGE,
"1",
"The title of my window",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, 600, 500,
NULL, NULL, hInstance, NULL);
if(hwnd == NULL)
{
MessageBox(NULL, "Window Creation Failed!", "Error!",
MB_ICONEXCLAMATION | MB_OK);
return 0;
}
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
// Step 3: The Message Loop
while(GetMessage(&Msg, NULL, 0, 0) > 0)
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
return Msg.wParam;
}