如何将边框和背景颜色绘制到窗口的极限?

Roy Li - MSFT 33,371 信誉分 Microsoft 供应商
2024-06-14T03:33:16.05+00:00
#include <Windows.h>
#include <Richedit.h>
HWND hwnd;
HWND panel;
HWND richEdit;
WNDPROC oldStaticProc;
int WINAPI WinMain(HINSTANCE, HINSTANCE, PSTR, int);
LRESULT CALLBACK WindowProcedure(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
LRESULT CALLBACK StaticProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
#if _DEBUG
#pragma comment( linker, "/subsystem:console" )
int main(int argc, const char** argv) {
return WinMain(GetModuleHandle(NULL), NULL, GetCommandLineA(), SW_SHOWDEFAULT);
}
#else
#pragma comment( linker, "/subsystem:windows" )
#endif
#pragma comment(lib, "opengl32.lib")
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow) {
LoadLibrary(TEXT("Msftedit.dll"));
int width = 800;
int height = 600;
WNDCLASS wc = { };
const wchar_t* CLASS_NAME = L"OpenGL and RichEdit Message Processing";
wc.lpfnWndProc = WindowProcedure;
wc.hInstance = hInstance;
wc.lpszClassName = CLASS_NAME;
if (!RegisterClass(&wc))
{
    MessageBox(NULL, L"Window Registration Failed!", L"Error!", MB_ICONEXCLAMATION | MB_OK);
}
hwnd = CreateWindowEx(
    0,
    CLASS_NAME,
    L"OpenGL RichEdit Control",
    WS_OVERLAPPEDWINDOW,
    CW_USEDEFAULT, CW_USEDEFAULT, width, height,
    NULL,
    NULL,
    hInstance,
    NULL
);
if (hwnd == NULL)
{
    MessageBox(NULL, L"Window Creation Failed!", L"Error!", MB_ICONEXCLAMATION | MB_OK);
}
if (hwnd != NULL)
{
    ShowWindow(hwnd, SW_SHOW);
    UpdateWindow(hwnd);
}
//GetClientRect(hwnd, &rect_hwnd);
panel = CreateWindowEx(
    0,
    L"STATIC",
    NULL,
    WS_CHILD | WS_VISIBLE,
    0, height - 150, width, 150,
    hwnd,
    NULL,
    hInstance,
    NULL
);
InvalidateRect(panel, NULL, TRUE);
oldStaticProc = (WNDPROC)SetWindowLongPtr(panel, GWLP_WNDPROC, (LONG_PTR)StaticProc);
richEdit = CreateWindowEx(
    WS_EX_CLIENTEDGE,
    RICHEDIT_CLASS,
    L"Type here...",
    WS_CHILD | WS_VISIBLE | WS_VSCROLL | ES_MULTILINE | ES_AUTOVSCROLL,
    0, 0, width, 150,
    panel,
    NULL,
    hInstance,
    NULL
);
MSG msg = { };
while (GetMessage(&msg, NULL, 0, 0))
{
    TranslateMessage(&msg);
    DispatchMessage(&msg);
}
return 0;
}
LRESULT CALLBACK WindowProcedure(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
static HBRUSH hBrush = CreateSolidBrush(RGB(0, 255, 0));
switch (msg)
{
case WM_PAINT:
{
    if (hwnd == panel) // only draw border for the panel
    {
    }
    break;
}
case WM_CTLCOLORSTATIC:
{
    //HDC hdcStatic = (HDC)wParam;
    //SetBkColor(hdcStatic, RGB(0, 255, 0));
    //return (INT_PTR)hBrush;
}
case WM_SIZE:
{
    int width = LOWORD(lParam);
    int height = HIWORD(lParam);
    SetWindowPos(panel, NULL, 0, height - 150, width, 150, SWP_NOZORDER);
    SetWindowPos(richEdit, NULL, 0, 0, width, 150, SWP_NOZORDER);
    break;
}
case WM_CLOSE:
    PostQuitMessage(0);
    break;
default:
    return DefWindowProc(hwnd, msg, wParam, lParam);
}
return 0;
}
LRESULT CALLBACK StaticProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg)
{
case WM_ERASEBKGND:
    // Prevent the background from being erased
    return TRUE;
case WM_PAINT:
    PAINTSTRUCT ps;
    HDC hdc = BeginPaint(hwnd, &ps);
    // Create a rectangle that is slightly smaller than the panel
    RECT rect;
    GetClientRect(hwnd, &rect);
    rect.left += 2;
    rect.top += 2;
    rect.right -= 2;
    rect.bottom -= 2;
    // Create a pen (for drawing lines) that is red and 2 pixels wide
    HPEN hPen = CreatePen(PS_SOLID, 2, RGB(255, 0, 0));
    // Select the pen into the device context
    HPEN hOldPen = (HPEN)SelectObject(hdc, hPen);
    // Draw a rectangle that is the same size as the panel
    Rectangle(hdc, rect.left, rect.top, rect.right, rect.bottom);
    // Clean up: deselect the pen, delete it, and end the paint operation
    SelectObject(hdc, hOldPen);
    DeleteObject(hPen);
    EndPaint(hwnd, &ps);
    break;
default:
    return CallWindowProc(oldStaticProc, hwnd, msg, wParam, lParam);
}
return 0;

我想在面板的极限内绘制绿色的边框和背景颜色。到目前为止,我有:'''严重性代码描述项目文件行抑制状态详细信息 错误 C2361 “hOldPen”的初始化被“默认”标签 demo-win32 C:Usersfrancsourcereposdemo-win32main.cpp 169''' 跳过。 我将需要开始以更新面板限制的方式调整窗口大小。有没有办法在极限处获得 1px 边框图,并为称为面板的 HWND 获得绿色背景颜色?

此问题由: How to draw a border and background color to the limits of a window? - Microsoft Q&A总结而来

Windows API - Win32
Windows API - Win32
一组适用于桌面和服务器应用程序的核心 Windows 应用程序编程接口 (API)。 以前称为 Win32 API。
105 个问题
0 个注释 无注释
{count} 票

1 个答案

排序依据: 非常有帮助
  1. Tong Xu - MSFT 2,471 信誉分 Microsoft 供应商
    2024-06-14T05:28:20.4533333+00:00

    该错误意味着“默认”标签跳过了“标识符”的初始化。

    只需在 switch 语句之前初始化 hPenhOldPenhdc 即可解决它。

    并删除WM_PAINT中的 hPenhOldPenhdc 类型,以避免重新定义。

    HPEN hPen = {};HPEN hOldPen = {};HDC hdc = {};

    程序运行良好:
    clientrichedit

    0 个注释 无注释

你的答案

问题作者可以将答案标记为“接受的答案”,这有助于用户了解已解决作者问题的答案。