How to draw a border and background color to the limits of a window?

Francesco Amatto 45 Reputation points
2024-03-11T00:17:38.78+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;

I would like to draw a border and background color of green to the limits of panel. So far I have: ```Severity Code Description Project File Line Suppression State Details

Error C2361 initialization of 'hOldPen' is skipped by 'default' label demo-win32 C:\Users\franc\source\repos\demo-win32\main.cpp 169```.

I will need to start to resize the window in a way that updates the limits of panel. Is there some way to get a 1px border drawing at the limits and a green background color for the HWND called panel?

Windows API - Win32
Windows API - Win32
A core set of Windows application programming interfaces (APIs) for desktop and server applications. Previously known as Win32 API.
2,427 questions
C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,540 questions
0 comments No comments
{count} votes

Accepted answer
  1. Tong Xu - MSFT 1,361 Reputation points Microsoft Vendor
    2024-03-11T02:13:01.27+00:00

    Hello, @Francesco Amatto
    Welcome to Microsoft Q&A!

    The error means initialization of 'identifier' is skipped by 'default' label.
    Just initializing the hPen, hOldPen and hdc before the switch statement will solve it.
    And delete the types of hPen, hOldPen and hdc in WM_PAINT to avoid redefination.
    HPEN hPen = {}; HPEN hOldPen = {}; HDC hdc = {};
    Program runs well:
    clientrichedit

    Thank you.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful