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 );
}

Recommended Answers

All 2 Replies

Now that i've had a better look at what you've got sofar, a way I think you could go about fixing this problem is by using the WM_ERASEBKGND message. By doing that all the notepad windows will be painted perfectly.
However, you are going to have to manually paint only the parts of the main window which don't have a notepad window over it (which may be harder than it sounds).

#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_ERASEBKGND:
         {
            // Overridden
         }
         break;
      case WM_CLOSE:
         {
            DestroyWindow(hwnd);
         }
         break;

[B][..rest of code...][/B]

I will see what I can do :)

First: you should add WS_CLIPCHILDREN and WS_CLIPSIBLINGS as window styles for main frame.
Second: to set notepad windows as child its not efficient to only set parent. You should also change notepad windows style to WS_CHILD using SetWindowLong function.

Here is the modified code. It almost works fine. Consider the WM_SIZE message. There could be a better way to handle WM_SIZE message instead of redrawing all window.

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);
			  SetWindowLong(notepad, GWL_STYLE, GetWindowLong(notepad, GWL_STYLE) | WS_CHILD);
			  // 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);
			  SetWindowLong(notepad2, GWL_STYLE, GetWindowLong(notepad2, GWL_STYLE) | WS_CHILD);
			  // 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);
			  SetWindowLong(notepad3, GWL_STYLE, GetWindowLong(notepad3, GWL_STYLE) | WS_CHILD);
			  // Resize and position it to make it visible
			  SetWindowPos(notepad3, NULL, 650, 0, 600, 100, NULL);
		  }
		  break;
	  case WM_SIZE:
		  InvalidateRect(hwnd, NULL, FALSE);
		  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 | WS_CLIPCHILDREN | WS_CLIPSIBLINGS,
		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 );
}
commented: Good advice ;) +4
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.