Creating a simple To-Do app with C++ using the Windows API (windows.h) involves a fair amount of code because you're dealing directly with Windows controls and messages. Here's a basic example that showcases how to create a window, add some controls (like buttons, a list box), and handle user input to create a simple To-Do application.
Requirements:
- A Windows development environment (Visual Studio or MinGW).
- Basic knowledge of Windows programming.
Simple To-Do App using C++ and Windows API
- Include necessary headers and define resource IDs.
#include <windows.h>
#include <string>
#include <vector>
// Resource IDs
#define IDC_ADD_BUTTON 101
#define IDC_REMOVE_BUTTON 102
#define IDC_TASK_EDIT 103
#define IDC_TASK_LIST 104
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
std::vector<std::string> tasks;
void RefreshTaskList(HWND hwndList) {
SendMessage(hwndList, LB_RESETCONTENT, 0, 0);
for (const auto& task : tasks) {
SendMessage(hwndList, LB_ADDSTRING, 0, (LPARAM)task.c_str());
}
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR, int nCmdShow) {
// Register the window class
const char CLASS_NAME[] = "SimpleToDoApp";
WNDCLASS wc = {};
wc.lpfnWndProc = WindowProc;
wc.hInstance = hInstance;
wc.lpszClassName = CLASS_NAME;
wc.hCursor = LoadCursor(nullptr, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
RegisterClass(&wc);
// Create the window
HWND hwnd = CreateWindowEx(
0,
CLASS_NAME,
"Simple To-Do App",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, 400, 300,
nullptr,
nullptr,
hInstance,
nullptr);
if (hwnd == nullptr) {
return 0;
}
ShowWindow(hwnd, nCmdShow);
// Run the message loop
MSG msg = {};
while (GetMessage(&msg, nullptr, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
static HWND hwndEdit, hwndAddButton, hwndRemoveButton, hwndListBox;
switch (uMsg) {
case WM_CREATE: {
hwndEdit = CreateWindow("EDIT", "",
WS_CHILD | WS_VISIBLE | WS_BORDER,
10, 10, 260, 25,
hwnd, (HMENU)IDC_TASK_EDIT,
GetModuleHandle(nullptr), nullptr);
hwndAddButton = CreateWindow("BUTTON", "Add Task",
WS_CHILD | WS_VISIBLE | BS_DEFPUSHBUTTON,
280, 10, 100, 25,
hwnd, (HMENU)IDC_ADD_BUTTON,
GetModuleHandle(nullptr), nullptr);
hwndRemoveButton = CreateWindow("BUTTON", "Remove Task",
WS_CHILD | WS_VISIBLE | BS_DEFPUSHBUTTON,
10, 50, 370, 25,
hwnd, (HMENU)IDC_REMOVE_BUTTON,
GetModuleHandle(nullptr), nullptr);
hwndListBox = CreateWindow("LISTBOX", "",
WS_CHILD | WS_VISIBLE | WS_BORDER | LBS_NOTIFY,
10, 90, 370, 160,
hwnd, (HMENU)IDC_TASK_LIST,
GetModuleHandle(nullptr), nullptr);
break;
}
case WM_COMMAND: {
if (LOWORD(wParam) == IDC_ADD_BUTTON) {
char buffer[256];
GetWindowText(hwndEdit, buffer, sizeof(buffer));
tasks.push_back(buffer);
RefreshTaskList(hwndListBox);
SetWindowText(hwndEdit, "");
} else if (LOWORD(wParam) == IDC_REMOVE_BUTTON) {
int index = SendMessage(hwndListBox, LB_GETCURSEL, 0, 0);
if (index != LB_ERR) {
tasks.erase(tasks.begin() + index);
RefreshTaskList(hwndListBox);
}
}
break;
}
case WM_DESTROY: {
PostQuitMessage(0);
break;
}
default:
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
return 0;
}
Explanation:
-
Headers and Resource IDs:
- Include
<windows.h> for Windows API functions and <string>, <vector> for managing tasks.
- Define resource IDs for controls.
-
Main Function:
- Register a window class.
- Create the window.
- Run the message loop to process Windows messages.
-
Window Procedure:
- Handle
WM_CREATE to create edit box, buttons, and list box.
- Handle
WM_COMMAND to manage button clicks:
- On
Add Task button click, get text from the edit control, add it to the task list, and update the list box.
- On
Remove Task button click, remove the selected item from the list of tasks and update the list box.
- Handle
WM_DESTROY to post quit message.
Building:
- Open this code in Visual Studio or another IDE that supports Windows development.
- Compile and run the application.
This example provides a basic UI to add and remove tasks and uses standard Windows controls. For more advanced features, consider using frameworks like MFC, Qt, or modern UI technologies.