Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define WIN32_LEAN_AND_MEAN
- #define _CRT_SECURE_NO_WARNINGS
- #define _SCL_SECURE_NO_WARNINGS
- #include <Windows.h>
- #include <cstdio>
- #include <utility>
- #include <string>
- #include <vector>
- struct TextEdit {
- std::wstring buffer;
- int caret = 0;
- wchar_t const* Buffer() {
- return buffer.data();
- }
- int Length() {
- return buffer.size();
- }
- void NormalizeCaret() {
- if (caret < 0) {
- caret = 0;
- } else if (caret > Length()) {
- caret = buffer.size();
- }
- }
- void Insert(wchar_t ch) {
- NormalizeCaret();
- buffer.insert(caret, 1, ch);
- caret += 1;
- }
- void Insert(std::wstring const& str) {
- NormalizeCaret();
- if (!str.empty()) {
- buffer.insert(caret, str);
- caret += str.size();
- }
- }
- void EraseLeft() {
- NormalizeCaret();
- if (caret > 0) {
- buffer.erase(caret - 1, 1);
- caret -= 1;
- }
- }
- void EraseRight() {
- NormalizeCaret();
- if (caret < Length()) {
- buffer.erase(caret, 1);
- }
- }
- void MoveLeft() {
- if (caret > 0) {
- caret -= 1;
- }
- }
- void MoveRight() {
- if (caret < Length()) {
- caret += 1;
- }
- }
- void MoveHome() {
- caret = 0;
- }
- void MoveEnd() {
- caret = buffer.size();
- }
- };
- TextEdit Current;
- HPEN CaretPen;
- void WindowPaint(HWND hwnd) {
- RECT clientrect;
- GetClientRect(hwnd, &clientrect);
- SIZE wndsize = {clientrect.right - clientrect.left, clientrect.bottom - clientrect.top};
- PAINTSTRUCT ps;
- HDC wnddc = BeginPaint(hwnd, &ps);
- HBITMAP bitmap = CreateCompatibleBitmap(wnddc, wndsize.cx, wndsize.cy);
- HDC dc = CreateCompatibleDC(wnddc);
- SelectObject(dc, bitmap);
- RECT wndrect = {0, 0, wndsize.cx, wndsize.cy};
- HBRUSH brush = CreateSolidBrush(RGB(255, 255, 255));
- FillRect(dc, &wndrect, brush);
- TEXTMETRICW textmetric;
- GetTextMetricsW(dc, &textmetric);
- int currentx = textmetric.tmHeight;
- int currenty = textmetric.tmHeight;
- SIZE textsize;
- TextOutW(dc, currentx, currenty, Current.Buffer(), Current.Length());
- GetTextExtentPointW(dc, Current.Buffer(), Current.caret, &textsize);
- SelectObject(dc, CaretPen);
- MoveToEx(dc, currentx + textsize.cx, currenty, nullptr);
- LineTo(dc, currentx + textsize.cx, currenty + textmetric.tmHeight);
- BitBlt(wnddc, 0, 0, wndsize.cx, wndsize.cy, dc, 0, 0, SRCCOPY);
- DeleteObject(brush);
- DeleteDC(dc);
- DeleteObject(bitmap);
- EndPaint(hwnd, &ps);
- }
- LRESULT CALLBACK WindowProc(
- HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam
- ) {
- switch (uMsg) {
- case WM_CREATE:
- CaretPen = CreatePen(PS_SOLID, 0, RGB(0, 0, 0));
- return 0;
- case WM_DESTROY:
- DeleteObject(CaretPen);
- PostQuitMessage(0);
- return 0;
- case WM_CHAR:
- wprintf(L"WM_CHAR %#4llx'%lc' %3lli\n", wParam, (wchar_t)wParam, (lParam >> 16) & 0xff);
- if (wParam == 8) {
- Current.EraseLeft();
- } else if (wParam >= 32) {
- Current.Insert(wParam);
- }
- InvalidateRect(hwnd, nullptr, false);
- return 0;
- case WM_KEYDOWN:
- wprintf(L"WM_KEYDOWN %4lli %3lli\n", wParam, (lParam >> 16) & 0xff);
- if (wParam == VK_LEFT) {
- Current.MoveLeft();
- InvalidateRect(hwnd, nullptr, false);
- } else if (wParam == VK_RIGHT) {
- Current.MoveRight();
- InvalidateRect(hwnd, nullptr, false);
- } else if (wParam == VK_HOME || wParam == VK_PRIOR || wParam == VK_UP) {
- Current.MoveHome();
- InvalidateRect(hwnd, nullptr, false);
- } else if (wParam == VK_END || wParam == VK_NEXT || wParam == VK_DOWN) {
- Current.MoveEnd();
- InvalidateRect(hwnd, nullptr, false);
- } else if (wParam == VK_DELETE) {
- Current.EraseRight();
- InvalidateRect(hwnd, nullptr, false);
- }
- return 0;
- case WM_KEYUP:
- return 0;
- case WM_PAINT:
- WindowPaint(hwnd);
- return 0;
- default:
- return DefWindowProcW(hwnd, uMsg, wParam, lParam);
- }
- }
- int CALLBACK WinMain(
- HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
- AllocConsole();
- freopen("CONOUT$", "w", stdout);
- const DWORD WindowStyle = WS_OVERLAPPEDWINDOW;
- WNDCLASSW wndclass;
- wndclass.style = CS_VREDRAW | CS_HREDRAW;
- wndclass.lpfnWndProc = &WindowProc;
- wndclass.cbClsExtra = 8;
- wndclass.cbWndExtra = 0;
- wndclass.hInstance = hInstance;
- wndclass.hIcon = LoadIcon(0, IDI_APPLICATION);
- wndclass.hCursor = LoadCursor(0, IDC_ARROW);
- wndclass.hbrBackground = 0;
- wndclass.lpszMenuName = 0;
- wndclass.lpszClassName = L"MyWindowClass";
- ATOM wndclassatom = RegisterClassW(&wndclass);
- RECT WindowRect = {0, 0, 800, 600};
- AdjustWindowRect(
- &WindowRect, WindowStyle, false);
- HWND hwnd = CreateWindowW(
- (LPCWSTR)wndclassatom,
- L"window",
- WindowStyle,
- 20,
- 20,
- WindowRect.right - WindowRect.left,
- WindowRect.bottom - WindowRect.top,
- 0,
- 0,
- hInstance,
- 0);
- ShowWindow(hwnd, SW_SHOW);
- UpdateWindow(hwnd);
- MSG message = {0, 0, 0, 0};
- while (GetMessageW(&message, nullptr, 0, 0)) {
- TranslateMessage(&message);
- DispatchMessageW(&message);
- }
- return 0;
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement