Repaint window help?

Reply

Join Date: Oct 2008
Posts: 9
Reputation: Smed is an unknown quantity at this point 
Solved Threads: 0
Smed Smed is offline Offline
Newbie Poster

Repaint window help?

 
0
  #1
Nov 5th, 2008
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.

  1. #include <windows.h>
  2. #include <tchar.h>
  3. const char ClassName[] = "WndClassName";
  4. int counter = 0;
  5. HWND notepad;
  6. HWND notepad2;
  7. HWND notepad3;
  8.  
  9. LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
  10. switch (msg) {
  11. case WM_CLOSE:
  12. {
  13. DestroyWindow(hwnd);
  14. }
  15. break;
  16. case WM_CREATE:
  17. {
  18. STARTUPINFO si;
  19. PROCESS_INFORMATION pi;
  20. TCHAR tszCommandLine[500];
  21.  
  22. // Initialize the startup info struct
  23. GetStartupInfo(&si);
  24.  
  25. // Call CreateProcess(), mostly defaults. Note that
  26. // the lpCommandLine argument MUST NOT be a constant string.
  27. _tcscpy(tszCommandLine, _T("notepad"));
  28. for(int a=0; a<3; a++){
  29. CreateProcess(NULL, tszCommandLine, NULL, NULL, FALSE, NULL,
  30. NULL, NULL, &si, &pi);
  31. }
  32.  
  33.  
  34. while(!notepad && counter < 10)
  35. {
  36. counter+=1;
  37. DWORD dwMilliseconds = 1000; // create DWORD type variable to hold delay time in milliseconds
  38. Sleep ( dwMilliseconds ); // Sleep for that number of msecs, note bold!
  39. notepad = FindWindow("Notepad", NULL);
  40.  
  41. }
  42. // Check if notepad exists
  43. if (!notepad) {
  44. MessageBox(hwnd, "Notepad not open, closing", "Error",
  45. MB_OK | MB_ICONERROR);
  46. DestroyWindow(hwnd);
  47. }
  48. // Set it as a child window
  49. SetParent(notepad, hwnd);
  50. // Resize and position it to make it visible
  51. SetWindowPos(notepad, NULL, 0, 0, 650, 700, NULL);
  52.  
  53. while(!notepad2 && counter < 10)
  54. {
  55. counter+=1;
  56. DWORD dwMilliseconds = 1000; // create DWORD type variable to hold delay time in milliseconds
  57. Sleep ( dwMilliseconds ); // Sleep for that number of msecs, note bold!
  58. notepad2 = FindWindow("Notepad", NULL);
  59.  
  60. }
  61. // Check if notepad exists
  62. if (!notepad2) {
  63. MessageBox(hwnd, "Notepad not open, closing", "Error",
  64. MB_OK | MB_ICONERROR);
  65. DestroyWindow(hwnd);
  66. }
  67. notepad2 = FindWindow("Notepad", NULL);
  68. // Set it as a child window
  69. SetParent(notepad2, hwnd);
  70. // Resize and position it to make it visible
  71. SetWindowPos(notepad2, NULL, 650, 100, 650, 700, NULL);
  72.  
  73. while(!notepad3 && counter < 15)
  74. {
  75. counter+=1;
  76. DWORD dwMilliseconds = 1000; // create DWORD type variable to hold delay time in milliseconds
  77. Sleep ( dwMilliseconds ); // Sleep for that number of msecs, note bold!
  78. notepad3 = FindWindow("Notepad", NULL);
  79.  
  80. }
  81. // Check if notepad exists
  82. if (!notepad3) {
  83. MessageBox(hwnd, "Notepad not open, closing", "Error",
  84. MB_OK | MB_ICONERROR);
  85. DestroyWindow(hwnd);
  86. }
  87. notepad3 = FindWindow("Notepad", NULL);
  88. // Set it as a child window
  89. SetParent(notepad3, hwnd);
  90. // Resize and position it to make it visible
  91. SetWindowPos(notepad3, NULL, 650, 0, 600, 100, NULL);
  92. }
  93. break;
  94. case WM_DESTROY:
  95. {
  96. PostQuitMessage(0);
  97. }
  98. break;
  99. default:
  100. return DefWindowProc(hwnd, msg, wParam, lParam);
  101. }
  102. return 0;
  103. }
  104.  
  105. int CALLBACK WinMain(HINSTANCE hInstance,
  106. HINSTANCE hPrevInstance,
  107. LPSTR lpCmdLine,
  108. int nShowCmd) {
  109. HWND hwnd;
  110. WNDCLASSEX wc;
  111. MSG msg;
  112.  
  113. wc.cbWndExtra = 0;
  114. wc.cbClsExtra = 0;
  115. wc.cbSize = sizeof WNDCLASSEX;
  116. wc.hbrBackground = (HBRUSH) COLOR_WINDOW;
  117. wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  118. wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  119. wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
  120. wc.hInstance = hInstance;
  121. wc.lpfnWndProc = WndProc;
  122. wc.lpszClassName = ClassName;
  123. wc.lpszMenuName = NULL;
  124. wc.style = 0;
  125.  
  126. if (!RegisterClassEx(&wc)) {
  127. return 1;
  128. }
  129.  
  130. hwnd = CreateWindowEx(
  131. WS_EX_APPWINDOW,
  132. ClassName,
  133. "Notepad Manager",
  134. WS_MINIMIZEBOX | WS_MAXIMIZEBOX|
  135. WS_SYSMENU | WS_CAPTION | WS_THICKFRAME,
  136. CW_USEDEFAULT, CW_USEDEFAULT,
  137. CW_USEDEFAULT, CW_USEDEFAULT,
  138. NULL, NULL, hInstance, NULL);
  139.  
  140. if (!hwnd) {
  141. return 1;
  142. }
  143.  
  144. ShowWindow(hwnd, nShowCmd);
  145. UpdateWindow(hwnd);
  146.  
  147. while (GetMessage(&msg, NULL, 0, 0) > 0) {
  148. TranslateMessage(&msg);
  149. DispatchMessage(&msg);
  150. }
  151.  
  152. return static_cast<int>( msg.wParam );
  153. }
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 1,383
Reputation: William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of 
Solved Threads: 112
Sponsor
William Hemsworth William Hemsworth is offline Offline
Nearly a Posting Virtuoso

Re: Repaint window help?

 
0
  #2
Nov 5th, 2008
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;

[..rest of code...]
I will see what I can do
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 3
Reputation: rdjusr is an unknown quantity at this point 
Solved Threads: 0
rdjusr rdjusr is offline Offline
Newbie Poster

Re: Repaint window help?

 
1
  #3
Nov 5th, 2008
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.

  1. const char ClassName[] = "WndClassName";
  2. int counter = 0;
  3. HWND notepad;
  4. HWND notepad2;
  5. HWND notepad3;
  6.  
  7. LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
  8. {
  9. switch (msg) {
  10. case WM_CLOSE:
  11. {
  12. DestroyWindow(hwnd);
  13. }
  14. break;
  15. case WM_CREATE:
  16. {
  17. STARTUPINFO si;
  18. PROCESS_INFORMATION pi;
  19. TCHAR tszCommandLine[500];
  20.  
  21. // Initialize the startup info struct
  22. GetStartupInfo(&si);
  23.  
  24. // Call CreateProcess(), mostly defaults. Note that
  25. // the lpCommandLine argument MUST NOT be a constant string.
  26. _tcscpy(tszCommandLine, _T("notepad"));
  27. for(int a=0; a<3; a++){
  28. CreateProcess(NULL, tszCommandLine, NULL, NULL, FALSE, NULL,
  29. NULL, NULL, &si, &pi);
  30. }
  31.  
  32.  
  33. while(!notepad && counter < 10)
  34. {
  35. counter+=1;
  36. DWORD dwMilliseconds = 1000; // create DWORD type variable to hold delay time in milliseconds
  37. Sleep ( dwMilliseconds ); // Sleep for that number of msecs, note bold!
  38. notepad = FindWindow("Notepad", NULL);
  39.  
  40. }
  41. // Check if notepad exists
  42. if (!notepad) {
  43. MessageBox(hwnd, "Notepad not open, closing", "Error",
  44. MB_OK | MB_ICONERROR);
  45. DestroyWindow(hwnd);
  46. }
  47. // Set it as a child window
  48. SetParent(notepad, hwnd);
  49. SetWindowLong(notepad, GWL_STYLE, GetWindowLong(notepad, GWL_STYLE) | WS_CHILD);
  50. // Resize and position it to make it visible
  51. SetWindowPos(notepad, NULL, 0, 0, 650, 700, NULL);
  52.  
  53. while(!notepad2 && counter < 10)
  54. {
  55. counter+=1;
  56. DWORD dwMilliseconds = 1000; // create DWORD type variable to hold delay time in milliseconds
  57. Sleep ( dwMilliseconds ); // Sleep for that number of msecs, note bold!
  58. notepad2 = FindWindow("Notepad", NULL);
  59.  
  60. }
  61. // Check if notepad exists
  62. if (!notepad2) {
  63. MessageBox(hwnd, "Notepad not open, closing", "Error",
  64. MB_OK | MB_ICONERROR);
  65. DestroyWindow(hwnd);
  66. }
  67. notepad2 = FindWindow("Notepad", NULL);
  68. // Set it as a child window
  69. SetParent(notepad2, hwnd);
  70. SetWindowLong(notepad2, GWL_STYLE, GetWindowLong(notepad2, GWL_STYLE) | WS_CHILD);
  71. // Resize and position it to make it visible
  72. SetWindowPos(notepad2, NULL, 650, 100, 650, 700, NULL);
  73.  
  74. while(!notepad3 && counter < 15)
  75. {
  76. counter+=1;
  77. DWORD dwMilliseconds = 1000; // create DWORD type variable to hold delay time in milliseconds
  78. Sleep ( dwMilliseconds ); // Sleep for that number of msecs, note bold!
  79. notepad3 = FindWindow("Notepad", NULL);
  80.  
  81. }
  82. // Check if notepad exists
  83. if (!notepad3) {
  84. MessageBox(hwnd, "Notepad not open, closing", "Error",
  85. MB_OK | MB_ICONERROR);
  86. DestroyWindow(hwnd);
  87. }
  88. notepad3 = FindWindow("Notepad", NULL);
  89. // Set it as a child window
  90. SetParent(notepad3, hwnd);
  91. SetWindowLong(notepad3, GWL_STYLE, GetWindowLong(notepad3, GWL_STYLE) | WS_CHILD);
  92. // Resize and position it to make it visible
  93. SetWindowPos(notepad3, NULL, 650, 0, 600, 100, NULL);
  94. }
  95. break;
  96. case WM_SIZE:
  97. InvalidateRect(hwnd, NULL, FALSE);
  98. break;
  99. case WM_DESTROY:
  100. {
  101. PostQuitMessage(0);
  102. }
  103. break;
  104. default:
  105. return DefWindowProc(hwnd, msg, wParam, lParam);
  106. }
  107. return 0;
  108. }
  109.  
  110. int CALLBACK WinMain(HINSTANCE hInstance,
  111. HINSTANCE hPrevInstance,
  112. LPSTR lpCmdLine,
  113. int nShowCmd)
  114. {
  115. HWND hwnd;
  116. WNDCLASSEX wc;
  117. MSG msg;
  118.  
  119. wc.cbWndExtra = 0;
  120. wc.cbClsExtra = 0;
  121. wc.cbSize = sizeof WNDCLASSEX;
  122. wc.hbrBackground = (HBRUSH) COLOR_WINDOW;
  123. wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  124. wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  125. wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
  126. wc.hInstance = hInstance;
  127. wc.lpfnWndProc = WndProc;
  128. wc.lpszClassName = ClassName;
  129. wc.lpszMenuName = NULL;
  130. wc.style = 0;
  131.  
  132. if (!RegisterClassEx(&wc)) {
  133. return 1;
  134. }
  135.  
  136. hwnd = CreateWindowEx(
  137. WS_EX_APPWINDOW,
  138. ClassName,
  139. "Notepad Manager",
  140. WS_MINIMIZEBOX | WS_MAXIMIZEBOX|
  141. WS_SYSMENU | WS_CAPTION | WS_THICKFRAME | WS_CLIPCHILDREN | WS_CLIPSIBLINGS,
  142. CW_USEDEFAULT, CW_USEDEFAULT,
  143. CW_USEDEFAULT, CW_USEDEFAULT,
  144. NULL, NULL, hInstance, NULL);
  145.  
  146. if (!hwnd) {
  147. return 1;
  148. }
  149.  
  150. ShowWindow(hwnd, nShowCmd);
  151. UpdateWindow(hwnd);
  152.  
  153. while (GetMessage(&msg, NULL, 0, 0) > 0) {
  154. TranslateMessage(&msg);
  155. DispatchMessage(&msg);
  156. }
  157.  
  158. return static_cast<int>( msg.wParam );
  159. }
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC