I created a Win32 window with a child window using C++ 2010. In my child window I have a richedit control. It works but there is one problem. When I want to use the WM_PAINT message in the parent window, even if I leave it empty with a breakpoint, it seems to slow down the program only its not really. Only as I try to type in the RichEdit control, it makes it feel very unresponsive. But if I comment out the WM_PAINT: and break, it works as it should. I need the WM_PAINT message to eliminate flicker in the border when you drag the child window using InvalidateRect(hChildWnd, NULL, FALSE); and quite possibly other things if I need to. How can I allow the WM_PAINT message without slowing down the RichEdit control when I type. Thanks in advance and heres the code I currently have:
#include <windows.h>
#include <Richedit.h>
HWND hWnd;
HWND hChildWnd;
HWND hRichEdit;
MSG msg;
WNDCLASSEX wc;
WNDCLASSEX wcChild;
HBRUSH brush;
HINSTANCE hChildInstance = NULL;
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK ChildWindowProcedure (HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszArgument, int nCmdShow);
#define ID_FILE_EXIT 1000
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszArgument, int nCmdShow)
{
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = CS_VREDRAW | CS_HREDRAW | CS_OWNDC;
wc.lpfnWndProc = WindowProcedure;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = NULL;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = CreateSolidBrush(RGB(0, 0, 0));
wc.lpszMenuName = NULL;
wc.lpszClassName = "Window";
wc.hIconSm = NULL;
RegisterClassEx(&wc);
hWnd = CreateWindowEx (0, "Window", "Parent Window", WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN, CW_USEDEFAULT, CW_USEDEFAULT, 800, 600, hWnd, NULL, hInstance, NULL);
wcChild.cbSize = sizeof(WNDCLASSEX);
wcChild.style = CS_VREDRAW | CS_HREDRAW | CS_OWNDC;
wcChild.lpfnWndProc = ChildWindowProcedure;
wcChild.cbClsExtra = 0;
wcChild.cbWndExtra = 0;
wcChild.hInstance = hChildInstance;
wcChild.hIcon = NULL;
wcChild.hCursor = LoadCursor(NULL, IDC_ARROW);
wcChild.hbrBackground = CreateSolidBrush(RGB(0, 0, 0));
wcChild.lpszMenuName = NULL;
wcChild.lpszClassName = "ChildWindow";
wcChild.hIconSm = NULL;
RegisterClassEx(&wcChild);
hChildWnd = CreateWindowEx (0, "ChildWindow", "Child Window", WS_OVERLAPPEDWINDOW | WS_CHILD | WS_VISIBLE, CW_USEDEFAULT, CW_USEDEFAULT, 640, 480, hWnd, NULL, hChildInstance, NULL);
LoadLibrary("riched32.dll");
hRichEdit = CreateWindowEx(WS_EX_CLIENTEDGE, RICHEDIT_CLASS, "", WS_VISIBLE | WS_CHILD | WS_VSCROLL | ES_MULTILINE | ES_LEFT | ES_NOHIDESEL | ES_AUTOVSCROLL, 0, 0, 640, 480, hChildWnd, NULL, hChildInstance, NULL);
if( !hRichEdit ) MessageBox(hWnd,"Couldnt create rich textbox", "Message", MB_OK);
CHARFORMAT2 cf;
cf.cbSize = sizeof(CHARFORMAT2);
cf.dwMask = CFM_COLOR | CFM_BACKCOLOR;
cf.crTextColor = RGB(255, 255, 255);
cf.crBackColor = RGB(0 ,0, 0);
cf.dwEffects = 0 ;
SendMessage(hRichEdit, EM_SETCHARFORMAT, SCF_ALL, (LPARAM)&cf );
SendMessage(hRichEdit, EM_SETBKGNDCOLOR,0, RGB(0, 0, 0));
ShowWindow (hWnd, SW_SHOWMAXIMIZED);
ShowWindow(hRichEdit, nCmdShow);
SetFocus(hRichEdit);
while (GetMessage (&msg, NULL, 0, 0) > 0)
{
TranslateMessage (&msg);
DispatchMessage (&msg);
}
return msg.wParam;
}
LRESULT CALLBACK WindowProcedure (HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg)
{
case WM_CREATE:
{
HMENU hFile = CreateMenu();
HMENU hExit = CreateMenu();
AppendMenu(hFile, MF_POPUP, (UINT_PTR)hExit, "File");
AppendMenu(hExit, MF_STRING, ID_FILE_EXIT, "Exit");
SetMenu(hWnd, hFile);
break;
}
case WM_COMMAND:
{
switch(LOWORD(wParam))
{
case ID_FILE_EXIT:
{
DestroyWindow(hWnd);
return(0);
break;
}
}
break;
}
case WM_DESTROY:
PostQuitMessage (0);
break;
case WM_PAINT: //THIS IS WHERE THE PROBLEM IS!!!
InvalidateRect(hChildWnd, NULL, FALSE);
break;
default:
return DefWindowProc (hWnd, msg, wParam, lParam);
}
return 0;
}
LRESULT CALLBACK ChildWindowProcedure (HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg)
{
default:
return DefWindowProc (hWnd, msg, wParam, lParam);
}
return 0;
}