// Edit_1.cpp : Defines the entry point for the application. // #include "stdafx.h" #include "Edit_1.h" #include <windowsx.h> #include <RichEdit.h> #include <commctrl.h> #pragma comment(lib,"Comctl32.lib") #define MAX_LOADSTRING 100 #define IDC_PASTE 101 // Global Variables: HINSTANCE hInst; // current instance WCHAR szTitle[MAX_LOADSTRING]; // The title bar text WCHAR szWindowClass[MAX_LOADSTRING]; // the main window class name HWND hwndEdit; HMENU m_hMenu; // Forward declarations of functions included in this code module: ATOM MyRegisterClass(HINSTANCE hInstance); BOOL InitInstance(HINSTANCE, int); LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM); //LRESULT CALLBACK MyProc(HWND, UINT, WPARAM, LPARAM,UINT_PTR, DWORD_PTR); LRESULT CALLBACK RichEditProc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp, UINT_PTR, DWORD_PTR); int APIENTRY wWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPWSTR lpCmdLine, _In_ int nCmdShow) { UNREFERENCED_PARAMETER(hPrevInstance); UNREFERENCED_PARAMETER(lpCmdLine); // TODO: Place code here. // Initialize global strings LoadStringW(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING); LoadStringW(hInstance, IDC_EDIT1, szWindowClass, MAX_LOADSTRING); MyRegisterClass(hInstance); // Perform application initialization: if (!InitInstance (hInstance, nCmdShow)) { return FALSE; } HACCEL hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_EDIT1)); MSG msg; // Main message loop: while (GetMessage(&msg, nullptr, 0, 0)) { if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) { TranslateMessage(&msg); DispatchMessage(&msg); } } return (int) msg.wParam; } // // FUNCTION: MyRegisterClass() // // PURPOSE: Registers the window class. // ATOM MyRegisterClass(HINSTANCE hInstance) { WNDCLASSEXW 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(hInstance, MAKEINTRESOURCE(IDI_EDIT1)); wcex.hCursor = LoadCursor(nullptr, IDC_ARROW); wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+3); wcex.lpszMenuName = MAKEINTRESOURCEW(IDC_EDIT1); wcex.lpszClassName = szWindowClass; wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL)); WNDCLASSEXW mywcex; return RegisterClassExW(&wcex); } // // FUNCTION: InitInstance(HINSTANCE, int) // // PURPOSE: Saves instance handle and creates main window // // COMMENTS: // // In this function, we save the instance handle in a global variable and // create and display the main program window. // BOOL InitInstance(HINSTANCE hInstance, int nCmdShow) { hInst = hInstance; // Store instance handle in our global variable LoadLibrary(TEXT("Msftedit.dll")); HWND hWnd = CreateWindowW(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, nullptr, nullptr, hInstance, nullptr); if (!hWnd) { return FALSE; } ShowWindow(hWnd, nCmdShow); UpdateWindow(hWnd); return TRUE; } // // FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM) // // PURPOSE: Processes messages for the main window. // // WM_COMMAND - process the application menu // WM_PAINT - Paint the main window // WM_DESTROY - post a quit message and return // // LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { MENUITEMINFO mii; WCHAR lpszText[] = L"Paste"; switch (message) { case WM_CREATE: { hwndEdit = CreateWindowEx(0, MSFTEDIT_CLASS, TEXT("EDIT"), WS_VISIBLE | WS_CHILD | WS_BORDER | ES_LEFT, 20, 20, 100, 30, hWnd, NULL, hInst, NULL); HWND hwndEdit1 = CreateWindowEx(0, MSFTEDIT_CLASS, TEXT("EDIT"), WS_VISIBLE | WS_CHILD | WS_BORDER | ES_LEFT, 150, 20, 100, 30, hWnd, NULL, hInst, NULL); SetWindowSubclass(hwndEdit, RichEditProc, 0, 0); SetWindowPos(hwndEdit, NULL, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_FRAMECHANGED); } break; case WM_COMMAND: { int wmId = LOWORD(wParam); // Parse the menu selections: switch (wmId) { case IDC_PASTE: { SetForegroundWindow(hwndEdit); INPUT ip; ip.type = INPUT_KEYBOARD; ip.ki.wScan = 0; ip.ki.time = 0; ip.ki.dwExtraInfo = 0; // Press the "Ctrl" key ip.ki.wVk = VK_CONTROL; ip.ki.dwFlags = 0; // 0 for key press SendInput(1, &ip, sizeof(INPUT)); // Press the "V" key ip.ki.wVk = 'V'; ip.ki.dwFlags = 0; // 0 for key press SendInput(1, &ip, sizeof(INPUT)); // Release the "V" key ip.ki.wVk = 'V'; ip.ki.dwFlags = KEYEVENTF_KEYUP; SendInput(1, &ip, sizeof(INPUT)); // Release the "Ctrl" key ip.ki.wVk = VK_CONTROL; ip.ki.dwFlags = KEYEVENTF_KEYUP; SendInput(1, &ip, sizeof(INPUT)); } break; case IDM_ABOUT: DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About); break; case IDM_EXIT: DestroyWindow(hWnd); break; default: return DefWindowProc(hWnd, message, wParam, lParam); } } break; case WM_CONTEXTMENU: if ((HWND)wParam == hwndEdit) { m_hMenu = CreatePopupMenu(); InsertMenu(m_hMenu, 0, MF_BYCOMMAND | MF_STRING | MF_ENABLED, IDC_PASTE, L"Paste"); TrackPopupMenu(m_hMenu, TPM_TOPALIGN | TPM_LEFTALIGN, GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam), 0, hWnd, NULL); } break; case WM_PAINT: { PAINTSTRUCT ps; HDC hdc = BeginPaint(hWnd, &ps); // TODO: Add any drawing code that uses hdc here... EndPaint(hWnd, &ps); } break; case WM_DESTROY: PostQuitMessage(0); break; default: return DefWindowProc(hWnd, message, wParam, lParam); } return 0; } // Message handler for about box. INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam) { UNREFERENCED_PARAMETER(lParam); switch (message) { case WM_INITDIALOG: return (INT_PTR)TRUE; case WM_COMMAND: if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL) { EndDialog(hDlg, LOWORD(wParam)); return (INT_PTR)TRUE; } break; } return (INT_PTR)FALSE; } LRESULT CALLBACK RichEditProc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp, UINT_PTR, DWORD_PTR) { static int border_thickness = 1; TCHAR szText[40]; HBRUSH hBrush; RECT rect; switch (msg) { case WM_NCPAINT: { HDC hdc = GetWindowDC(hwnd); RECT rc; GetClientRect(hwnd, &rc); rc.right += 2 * border_thickness + 1; rc.bottom += 2 * border_thickness + 1; HBRUSH hbrush = (HBRUSH)GetStockObject(NULL_BRUSH); HPEN hpen = CreatePen(PS_SOLID, 2 * border_thickness, RGB(255, 0, 0)); HBRUSH oldbrush = (HBRUSH)SelectObject(hdc, hbrush); HPEN oldpen = (HPEN)SelectObject(hdc, hpen); Rectangle(hdc, rc.left, rc.top, rc.right, rc.bottom); SelectObject(hdc, oldpen); SelectObject(hdc, oldbrush); DeleteObject(hpen); DeleteObject(hbrush); ReleaseDC(hwnd, hdc); return 0; } case WM_NCCALCSIZE: if (lp) { NCCALCSIZE_PARAMS* sz = (NCCALCSIZE_PARAMS*)lp; InflateRect(&sz->rgrc[0], -border_thickness, -border_thickness); return 0; } case WM_DESTROY: PostQuitMessage(0); break; default: return DefSubclassProc(hwnd, msg, wp, lp); } return 0; }
Debug:
Note: 左边的是自绘的边框
参考: https://stackoverflow.com/questions/59732985/winapi-painting-rich-edit-thin-border