RichEdit's text not visible when window first pops up.

Francesco Amatto 45 Reputation points
2024-03-11T04:37:56.38+00:00
#include <Windows.h>
#include <Richedit.h>
HWND hwnd;
HWND hwnd_richedit_panel;
HWND richEdit;
WNDPROC oldStaticProc;
HWND hwnd_green_panel;
WNDPROC oldGreenProc;
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);
LRESULT CALLBACK GreenPanelProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
#define RICHEDIT_CLASS L"RICHEDIT50W"
#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) {
    HMODULE hMod = LoadLibrary(TEXT("Msftedit.dll"));
    if (!hMod)
    {
        // Handle the error
        MessageBox(NULL, L"Failed to load Msftedit.dll", L"Error", MB_OK | MB_ICONERROR);
        return 1; // Or some other error handling
    }
  
    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);
    }
    // Subclass the "STATIC" class
    WNDCLASS wcStatic = { };
    GetClassInfo(NULL, L"STATIC", &wcStatic);
    oldStaticProc = wcStatic.lpfnWndProc;
    wcStatic.lpfnWndProc = StaticProc;
    wcStatic.lpszClassName = L"STATIC_SUBCLASS";
    RegisterClass(&wcStatic);
    hwnd_richedit_panel = CreateWindowEx(
        0,
        L"STATIC_SUBCLASS",
        NULL,
        WS_CHILD | WS_VISIBLE,
        0, height - 150, width, 150,
        hwnd,
        NULL,
        hInstance,
        NULL
    );
    if (hwnd != NULL)
    {
        ShowWindow(hwnd, SW_SHOW);
        UpdateWindow(hwnd);
    }
    if (hwnd_richedit_panel != NULL)
    {
        InvalidateRect(hwnd_richedit_panel, NULL, TRUE);
        UpdateWindow(hwnd_richedit_panel);
    }
    // Move the creation of the richEdit control here
    richEdit = CreateWindowEx(
        WS_EX_CLIENTEDGE,
        RICHEDIT_CLASS,
        L"Type here...",
        WS_CHILD | WS_VISIBLE | WS_VSCROLL | ES_MULTILINE | ES_AUTOVSCROLL,
        0, 0, width, 150,
        hwnd_richedit_panel,
        NULL,
        hInstance,
        NULL
    );
    if (richEdit == NULL)
    {
        // Handle the error
        MessageBox(NULL, L"RichEdit Creation Failed...", L"Error!", MB_ICONEXCLAMATION | MB_OK);
        return 1; // Or some other error handling
    }
    // After creating the RichEdit control
    BOOL result = SendMessage(richEdit, WM_SETTEXT, 0, (LPARAM)L"Hello, World!");
    if (!result)
    {
        // Handle the error
        DWORD dwError = GetLastError();
        MessageBox(NULL, L"SendMessage Failed...", L"Error!", MB_ICONEXCLAMATION | MB_OK);
        return 1; // Or some other error handling
    }
    SetWindowPos(richEdit, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
    SendMessage(richEdit, EM_SETSEL, 0, 0);
    SendMessage(richEdit, EM_SCROLLCARET, 0, 0);
    // Show the RichEdit control and force it to repaint
    ShowWindow(richEdit, SW_SHOW);
    UpdateWindow(richEdit);
    RedrawWindow(richEdit, NULL, NULL, RDW_INVALIDATE | RDW_UPDATENOW | RDW_ERASE);
    // In your WinMain function, after creating hwnd_richedit_panel:
    // Subclass the "STATIC" class for the green panel
    WNDCLASS wcGreen = { };
    GetClassInfo(NULL, L"STATIC", &wcGreen);
    oldGreenProc = wcGreen.lpfnWndProc;
    wcGreen.lpfnWndProc = GreenPanelProc;
    wcGreen.lpszClassName = L"GREEN_SUBCLASS";
    RegisterClass(&wcGreen);
    hwnd_green_panel = CreateWindowEx(
        0,
        L"GREEN_SUBCLASS",
        NULL,
        WS_CHILD | WS_VISIBLE,
        0, 0, width, height - 150,
        hwnd,
        NULL,
        hInstance,
        NULL
    );
    if (richEdit == NULL)
    {
        DWORD dwError = GetLastError();
        LPVOID lpMsgBuf;
        FormatMessage(
            FORMAT_MESSAGE_ALLOCATE_BUFFER |
            FORMAT_MESSAGE_FROM_SYSTEM |
            FORMAT_MESSAGE_IGNORE_INSERTS,
            NULL,
            dwError,
            MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
            (LPTSTR)&lpMsgBuf,
            0, NULL);
        MessageBox(NULL, (LPCTSTR)lpMsgBuf, L"Error", MB_OK | MB_ICONERROR);
        LocalFree(lpMsgBuf);
    }
    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_ERASEBKGND:
        return TRUE;
    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(hwnd_richedit_panel, NULL, 0, height - 150, width, 150, SWP_NOZORDER);
        SetWindowPos(hwnd_green_panel, NULL, 0, 0, width, height - 150, SWP_NOZORDER);
        // Adjust the size and position of richEdit to fill the entire client area of hwnd_richedit_panel
        SetWindowPos(richEdit, NULL, 0, 0, width, 150, SWP_NOZORDER);
        InvalidateRect(hwnd_richedit_panel, NULL, TRUE);
        InvalidateRect(hwnd_green_panel, NULL, TRUE);
        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)
{
    HPEN hPen = {}; HPEN hOldPen = {}; HDC hdc = {};
    switch (msg)
    {
    case WM_ERASEBKGND:
    {
        HDC hdc = (HDC)wParam;
        RECT rect;
        GetClientRect(hwnd, &rect);
        FillRect(hdc, &rect, (HBRUSH)(COLOR_WINDOW + 1));
        return TRUE;
    }
    case WM_PAINT:
        PAINTSTRUCT ps;
        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 = CreatePen(PS_SOLID, 2, RGB(255, 0, 0));
        // Select the pen into the device context
        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;
}
LRESULT CALLBACK GreenPanelProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    HPEN hPen = {}; HPEN hOldPen = {}; HDC hdc = {};
    switch (msg)
    {
    case WM_ERASEBKGND:
    {
        //HDC hdc = (HDC)wParam;
        //RECT rect;
        //GetClientRect(hwnd, &rect);
        //HBRUSH hbr = CreateSolidBrush(RGB(0, 255, 0)); // Change to the color you want
        //FillRect(hdc, &rect, hbr);
        //DeleteObject(hbr);
        return TRUE;
    }
    case WM_PAINT:
        PAINTSTRUCT ps;
        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 green and 2 pixels wide
        hPen = CreatePen(PS_SOLID, 2, RGB(0, 255, 0));
        // Select the pen into the device context
        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(oldGreenProc, hwnd, msg, wParam, lParam);
    }
    return 0;
}

The "Hello World!" text is not visible when the window first pops up on the screen however it does display if the window is re-sized by dragging out or in the corner of the window with the mouse. How can I modify the code so that the "Hello World!" text is visible when the window first opens to the screen?

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. Jeanine Zhang-MSFT 9,181 Reputation points Microsoft Vendor
    2024-03-11T08:05:54.7966667+00:00

    Hello,

    Welcome to Microsoft Q&A!

    As far as I'm concerned, you should creat hwnd_green_panel first and then create the hwnd_richedit_panel.

    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow) {
    
    	HMODULE hMod = LoadLibrary(TEXT("Msftedit.dll"));
    
    	if (!hMod)
    
    	{
    
    		// Handle the error
    
    		MessageBox(NULL, L"Failed to load Msftedit.dll", L"Error", MB_OK | MB_ICONERROR);
    
    		return 1; // Or some other error handling
    
    	}
    
    
    	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);
    
    	}
    
    
    
    	// In your WinMain function, after creating hwnd_richedit_panel:
    
    	// Subclass the "STATIC" class for the green panel
    
    	WNDCLASS wcGreen = { };
    
    	GetClassInfo(NULL, L"STATIC", &wcGreen);
    
    	oldGreenProc = wcGreen.lpfnWndProc;
    
    	wcGreen.lpfnWndProc = GreenPanelProc;
    
    	wcGreen.lpszClassName = L"GREEN_SUBCLASS";
    
    	RegisterClass(&wcGreen);
    
    	hwnd_green_panel = CreateWindowEx(
    
    		0,
    
    		L"GREEN_SUBCLASS",
    
    		NULL,
    
    		WS_CHILD | WS_VISIBLE,
    
    		0, 0, width, height - 150,
    
    		hwnd,
    
    		NULL,
    
    		hInstance,
    
    		NULL
    
    	);
    
    
    
    
    
    	// Subclass the "STATIC" class
    
    	WNDCLASS wcStatic = { };
    
    	GetClassInfo(NULL, L"STATIC", &wcStatic);
    
    	oldStaticProc = wcStatic.lpfnWndProc;
    
    	wcStatic.lpfnWndProc = StaticProc;
    
    	wcStatic.lpszClassName = L"STATIC_SUBCLASS";
    
    	RegisterClass(&wcStatic);
    
    	hwnd_richedit_panel = CreateWindowEx(
    
    		0,
    
    		L"STATIC_SUBCLASS",
    
    		NULL,
    
    		WS_CHILD | WS_VISIBLE,
    
    		0, height - 150, width, 150,
    
    		hwnd,
    
    		NULL,
    
    		hInstance,
    
    		NULL
    
    	);
    
    	if (hwnd != NULL)
    
    	{
    
    		ShowWindow(hwnd, SW_SHOW);
    
    		UpdateWindow(hwnd);
    
    	}
    
    	if (hwnd_richedit_panel != NULL)
    
    	{
    
    		InvalidateRect(hwnd_richedit_panel, NULL, TRUE);
    
    		UpdateWindow(hwnd_richedit_panel);
    
    	}
    
    	// Move the creation of the richEdit control here
    
    	richEdit = CreateWindowEx(
    
    		WS_EX_CLIENTEDGE,
    
    		RICHEDIT_CLASS,
    
    		L"Type here...",
    
    		WS_CHILD | WS_VISIBLE | WS_VSCROLL | ES_MULTILINE | ES_AUTOVSCROLL,
    
    		0, 0, width, 150,
    
    		hwnd_richedit_panel,
    
    		NULL,
    
    		hInstance,
    
    		NULL
    
    	);
    
    	if (richEdit == NULL)
    
    	{
    
    		// Handle the error
    
    		MessageBox(NULL, L"RichEdit Creation Failed...", L"Error!", MB_ICONEXCLAMATION | MB_OK);
    
    		return 1; // Or some other error handling
    
    	}
    
    	// After creating the RichEdit control
    
    	BOOL result = SendMessage(richEdit, WM_SETTEXT, 0, (LPARAM)L"Hello, World!");
    
    	if (!result)
    
    	{
    
    		// Handle the error
    
    		DWORD dwError = GetLastError();
    
    		MessageBox(NULL, L"SendMessage Failed...", L"Error!", MB_ICONEXCLAMATION | MB_OK);
    
    		return 1; // Or some other error handling
    
    	}
    
    	SetWindowPos(richEdit, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
    
    	SendMessage(richEdit, EM_SETSEL, 0, 0);
    
    	SendMessage(richEdit, EM_SCROLLCARET, 0, 0);
    
    	// Show the RichEdit control and force it to repaint
    
    	ShowWindow(richEdit, SW_SHOW);
    
    	UpdateWindow(richEdit);
    
    	RedrawWindow(richEdit, NULL, NULL, RDW_INVALIDATE | RDW_UPDATENOW | RDW_ERASE);
    
    
    	if (richEdit == NULL)
    
    	{
    
    		DWORD dwError = GetLastError();
    
    		LPVOID lpMsgBuf;
    
    		FormatMessage(
    
    			FORMAT_MESSAGE_ALLOCATE_BUFFER |
    
    			FORMAT_MESSAGE_FROM_SYSTEM |
    
    			FORMAT_MESSAGE_IGNORE_INSERTS,
    
    			NULL,
    
    			dwError,
    
    			MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
    
    			(LPTSTR)&lpMsgBuf,
    
    			0, NULL);
    
    		MessageBox(NULL, (LPCTSTR)lpMsgBuf, L"Error", MB_OK | MB_ICONERROR);
    
    		LocalFree(lpMsgBuf);
    
    	}
    
    	MSG msg = { };
    
    	while (GetMessage(&msg, NULL, 0, 0))
    
    	{
    
    		TranslateMessage(&msg);
    
    		DispatchMessage(&msg);
    
    	}
    
    	return 0;
    
    }
    
    

    User's image

    Thank you.

    Jeanine


    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 comments No comments

1 additional answer

Sort by: Most helpful
  1. RLWA32 40,656 Reputation points
    2024-03-11T14:28:42.19+00:00

    Sample

    Give this code a try -

    #include <Windows.h>
    #include <Richedit.h>
    #include <tchar.h>
    WNDPROC oldStaticProc;
    WNDPROC oldGreenProc;
    int WINAPI _tWinMain(HINSTANCE, HINSTANCE, PTSTR, int);
    LRESULT CALLBACK WindowProcedure(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
    LRESULT CALLBACK StaticProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
    LRESULT CALLBACK GreenPanelProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
    HINSTANCE g_hInstance;
    int width = 800;
    int height = 600;
    #if _DEBUG
    #pragma comment( linker, "/subsystem:console" )
    int _tmain(int argc, const TCHAR** argv) {
        return _tWinMain(GetModuleHandle(NULL), NULL, GetCommandLine(), SW_SHOWDEFAULT);
    }
    #else
    #pragma comment( linker, "/subsystem:windows" )
    #endif
    #pragma comment(lib, "opengl32.lib")
    int WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PTSTR szCmdLine, int iCmdShow) {
        g_hInstance = hInstance;
        HMODULE hMod = LoadLibrary(TEXT("Msftedit.dll"));
        if (!hMod)
        {
            // Handle the error
            MessageBox(NULL, TEXT("Failed to load Msftedit.dll"), TEXT("Error"), MB_OK | MB_ICONERROR);
            return 1; // Or some other error handling
        }
        WNDCLASS wc = { };
        LPCTSTR CLASS_NAME = TEXT("OpenGL and RichEdit Message Processing");
        wc.lpfnWndProc = WindowProcedure;
        wc.hInstance = hInstance;
        wc.lpszClassName = CLASS_NAME;
        wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
        wc.style = CS_HREDRAW | CS_VREDRAW;
        wc.hCursor = LoadCursor(NULL, IDC_ARROW);
        if (!RegisterClass(&wc))
        {
            MessageBox(NULL, TEXT("Window Registration Failed!"), TEXT("Error!"), MB_ICONEXCLAMATION | MB_OK);
            return 1;
        }
        WNDCLASS wcStatic = { };
        GetClassInfo(NULL, TEXT("STATIC"), &wcStatic);
        oldStaticProc = wcStatic.lpfnWndProc;
        wcStatic.lpfnWndProc = StaticProc;
        wcStatic.lpszClassName = TEXT("STATIC_SUBCLASS");
        if (!RegisterClass(&wcStatic))
        {
            MessageBox(NULL, TEXT("STATIC_SUBCLASS Registration Failed!"), TEXT("Error!"), MB_ICONEXCLAMATION | MB_OK);
            return 1;
        }
        WNDCLASS wcGreen = { };
        GetClassInfo(NULL, TEXT("STATIC"), &wcGreen);
        oldGreenProc = wcGreen.lpfnWndProc;
        wcGreen.lpfnWndProc = GreenPanelProc;
        wcGreen.lpszClassName = TEXT("GREEN_SUBCLASS");
        if (!RegisterClass(&wcGreen))
        {
            MessageBox(NULL, TEXT("GREEN_SUBCLASS Registration Failed!"), TEXT("Error!"), MB_ICONEXCLAMATION | MB_OK);
            return 1;
        }
        HWND hwnd = CreateWindowEx(
            0,
            CLASS_NAME,
            TEXT("OpenGL RichEdit Control"),
            WS_OVERLAPPEDWINDOW,
            CW_USEDEFAULT, CW_USEDEFAULT, width, height,
            NULL,
            NULL,
            hInstance,
            NULL
        );
        if (hwnd == NULL)
        {
            MessageBox(NULL, TEXT("Window Creation Failed!"), TEXT("Error!"), MB_ICONEXCLAMATION | MB_OK);
            return 1;
        }
        ShowWindow(hwnd, iCmdShow);
        UpdateWindow(hwnd);
        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 HWND hwnd_green_panel = NULL, hwnd_richedit_panel = NULL;
        switch (msg)
        {
        case WM_CREATE:
        {
            RECT rc;
            GetClientRect(hwnd, &rc);
            hwnd_green_panel = CreateWindowEx(
                0,
                TEXT("GREEN_SUBCLASS"),
                NULL,
                WS_CHILD | WS_VISIBLE,
                0, 0, rc.right, rc.bottom - 150,
                hwnd,
                NULL,
                g_hInstance,
                NULL
            );
            hwnd_richedit_panel = CreateWindowEx(
                0,
                TEXT("STATIC_SUBCLASS"),
                NULL,
                WS_CHILD | WS_VISIBLE,
                0, rc.bottom - 150, rc.right, 150,
                hwnd,
                NULL,
                g_hInstance,
                NULL
            );
            if (!hwnd_green_panel || !hwnd_richedit_panel)
                return -1;
        }
        break;
        case WM_SIZE:
        {
            RECT rc;
            GetClientRect(hwnd, &rc);
            MoveWindow(hwnd_richedit_panel, 0, rc.bottom - 150, rc.right, 150, TRUE);
            MoveWindow(hwnd_green_panel, 0, 0, rc.right, rc.bottom - 150, TRUE);
        }
        break;
        case WM_DESTROY:
            PostQuitMessage(0);
            break;
        default:
            return DefWindowProc(hwnd, msg, wParam, lParam);
        }
        return 0;
    }
    LRESULT CALLBACK StaticProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
    {
        HPEN hPen = {}; HPEN hOldPen = {}; HDC hdc = {};
        switch (msg)
        {
        case WM_CREATE:
        {
            RECT rc;
            GetClientRect(hwnd, &rc);
            HWND richEdit = CreateWindowExW(
                WS_EX_CLIENTEDGE,
                MSFTEDIT_CLASS,
                L"Type here...",
                WS_CHILD | WS_VISIBLE | WS_VSCROLL | ES_MULTILINE | ES_AUTOVSCROLL,
                4, 4, rc.right - 8, rc.bottom - 8,  // Leave room for the red border
                hwnd,
                NULL,
                g_hInstance,
                NULL
            );
            if (!richEdit)
                return -1;
            LRESULT result = SendMessage(richEdit, WM_SETTEXT, 0, (LPARAM) TEXT("Hello, World!"));
            SendMessage(richEdit, EM_SETSEL, 0, 0);
            SendMessage(richEdit, EM_SCROLLCARET, 0, 0);
        }
        break;
        case WM_ERASEBKGND:
        {
            HDC hdc = (HDC)wParam;
            RECT rect;
            GetClientRect(hwnd, &rect);
            FillRect(hdc, &rect, (HBRUSH) GetStockObject(GRAY_BRUSH));
            return TRUE;
        }
        case WM_PAINT:
            PAINTSTRUCT ps;
            hdc = BeginPaint(hwnd, &ps);
            // Create a rectangle that is slightly smaller than the panel
            RECT rect;
            GetClientRect(hwnd, &rect);
            InflateRect(&rect, -2, -2);
            // Create a pen (for drawing lines) that is red and 2 pixels wide
            hPen = CreatePen(PS_SOLID, 2, RGB(255, 0, 0));
            // Select the pen into the device context
            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;
    }
    LRESULT CALLBACK GreenPanelProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
    {
        HPEN hPen = {}; HPEN hOldPen = {}; HDC hdc = {};
        switch (msg)
        {
        case WM_ERASEBKGND:
            return TRUE;
        case WM_PAINT:
            PAINTSTRUCT ps;
            hdc = BeginPaint(hwnd, &ps);
            // Create a rectangle that is slightly smaller than the panel
            RECT rect;
            GetClientRect(hwnd, &rect);
            InflateRect(&rect, -2, -2);
            // Create a pen (for drawing lines) that is green and 2 pixels wide
            hPen = CreatePen(PS_SOLID, 2, RGB(0, 255, 0));
            // Select the pen into the device context
            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(oldGreenProc, hwnd, msg, wParam, lParam);
        }
        return 0;
    }
    
    
    0 comments No comments