I'm currently working on a project that manages multiple notepad windows in a MDI client area. It runs just as I want it to, except that the notepad windows seem to freeze up and become glitchy inside of the parent window. I was told to look into using InvalidateRect, but i'm not sure how to implement it into my program. Here is my code, I would appreciate any help.
#include <windows.h>
#include <tchar.h>
const char ClassName[] = "WndClassName";
int counter = 0;
HWND notepad;
HWND notepad2;
HWND notepad3;
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
switch (msg) {
case WM_CLOSE:
{
DestroyWindow(hwnd);
}
break;
case WM_CREATE:
{
STARTUPINFO si;
PROCESS_INFORMATION pi;
TCHAR tszCommandLine[500];
// Initialize the startup info struct
GetStartupInfo(&si);
// Call CreateProcess(), mostly defaults. Note that
// the lpCommandLine argument MUST NOT be a constant string.
_tcscpy(tszCommandLine, _T("notepad"));
for(int a=0; a<3; a++){
CreateProcess(NULL, tszCommandLine, NULL, NULL, FALSE, NULL,
NULL, NULL, &si, &pi);
}
while(!notepad && counter < 10)
{
counter+=1;
DWORD dwMilliseconds = 1000; // create DWORD type variable to hold delay time in milliseconds
Sleep ( dwMilliseconds ); // Sleep for that number of msecs, note bold!
notepad = FindWindow("Notepad", NULL);
}
// Check if notepad exists
if (!notepad) {
MessageBox(hwnd, "Notepad not open, closing", "Error",
MB_OK | MB_ICONERROR);
DestroyWindow(hwnd);
}
// Set it as a child window
SetParent(notepad, hwnd);
// Resize and position it to make it visible
SetWindowPos(notepad, NULL, 0, 0, 650, 700, NULL);
while(!notepad2 && counter < 10)
{
counter+=1;
DWORD dwMilliseconds = 1000; // create DWORD type variable to hold delay time in milliseconds
Sleep ( dwMilliseconds ); // Sleep for that number of msecs, note bold!
notepad2 = FindWindow("Notepad", NULL);
}
// Check if notepad exists
if (!notepad2) {
MessageBox(hwnd, "Notepad not open, closing", "Error",
MB_OK | MB_ICONERROR);
DestroyWindow(hwnd);
}
notepad2 = FindWindow("Notepad", NULL);
// Set it as a child window
SetParent(notepad2, hwnd);
// Resize and position it to make it visible
SetWindowPos(notepad2, NULL, 650, 100, 650, 700, NULL);
while(!notepad3 && counter < 15)
{
counter+=1;
DWORD dwMilliseconds = 1000; // create DWORD type variable to hold delay time in milliseconds
Sleep ( dwMilliseconds ); // Sleep for that number of msecs, note bold!
notepad3 = FindWindow("Notepad", NULL);
}
// Check if notepad exists
if (!notepad3) {
MessageBox(hwnd, "Notepad not open, closing", "Error",
MB_OK | MB_ICONERROR);
DestroyWindow(hwnd);
}
notepad3 = FindWindow("Notepad", NULL);
// Set it as a child window
SetParent(notepad3, hwnd);
// Resize and position it to make it visible
SetWindowPos(notepad3, NULL, 650, 0, 600, 100, NULL);
}
break;
case WM_DESTROY:
{
PostQuitMessage(0);
}
break;
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
return 0;
}
int CALLBACK WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nShowCmd) {
HWND hwnd;
WNDCLASSEX wc;
MSG msg;
wc.cbWndExtra = 0;
wc.cbClsExtra = 0;
wc.cbSize = sizeof WNDCLASSEX;
wc.hbrBackground = (HBRUSH) COLOR_WINDOW;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
wc.hInstance = hInstance;
wc.lpfnWndProc = WndProc;
wc.lpszClassName = ClassName;
wc.lpszMenuName = NULL;
wc.style = 0;
if (!RegisterClassEx(&wc)) {
return 1;
}
hwnd = CreateWindowEx(
WS_EX_APPWINDOW,
ClassName,
"Notepad Manager",
WS_MINIMIZEBOX | WS_MAXIMIZEBOX|
WS_SYSMENU | WS_CAPTION | WS_THICKFRAME,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, hInstance, NULL);
if (!hwnd) {
return 1;
}
ShowWindow(hwnd, nShowCmd);
UpdateWindow(hwnd);
while (GetMessage(&msg, NULL, 0, 0) > 0) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return static_cast<int>( msg.wParam );
}