-
April 30th, 2019, 04:43 AM
#1
Wm_paint 100% cpu
Hi
I'm getting 100% CPU usage because of WM_PAINT is getting called all the time.
I simplified the code to a maximum to eliminate the culprit but still getting 100% cpu usage.
Code:
#include <windows.h>
#include <time.h>
#include <string>
#include <iostream>
using namespace std;
#pragma comment(lib,"user32.lib")
#pragma comment(lib,"gdi32.lib")
#pragma comment(lib,"shell32.lib")
#pragma comment(linker, "/subsystem:\"console\" /entry:\"WinMainCRTStartup\"")
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HFONT hBig, hFont;
HDC hdc;
int index;
PAINTSTRUCT ps01;
BITMAP bitmap01;
HDC hdcMem01;
HGDIOBJ oldBitmap01;
switch(message)
{
// Draw background image
case WM_PAINT:
cout << "wmpaint: " << endl;
return 0;
case WM_DESTROY:
PostQuitMessage (0);
return 0;
} // message switch
return DefWindowProc(hWnd, message, wParam, lParam);
}
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR lpszArgument, int iCmdShow)
{
static TCHAR szAppName[] = TEXT ("SineWave") ;
HWND hwnd ;
MSG msg ;
WNDCLASS wndclass ;
wndclass.style = CS_HREDRAW | CS_VREDRAW ;
wndclass.lpfnWndProc = WndProc ;
wndclass.cbClsExtra = 0 ;
wndclass.cbWndExtra = 0 ;
wndclass.hInstance = hInstance ;
wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ;
wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;
wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
wndclass.lpszMenuName = NULL ;
wndclass.lpszClassName = szAppName ;
if (!RegisterClass (&wndclass))
{
MessageBox (NULL, TEXT ("Program requires Windows NT!"),
szAppName, MB_ICONERROR) ;
return 0 ;
}
hwnd = CreateWindow (szAppName, TEXT ("Sine Wave Using Polyline"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, hInstance, NULL) ;
ShowWindow (hwnd, iCmdShow) ;
UpdateWindow (hwnd) ;
while (GetMessage (&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
-
April 30th, 2019, 05:57 AM
#2
Re: Wm_paint 100% cpu
You aren't handling the WM_PAINT message properly. The minimum needed is:
Code:
PAINTSTRUCT ps;
HDC hdc;
...
case WM_PAINT:
hdc = BeginPaint(hwnd, &ps);
//TextOut(hdc, 0, 0, "wmpaint", 7);
EndPaint(hwnd, &ps);
return 0L;
It's also not a good idea to try to output to the console during a WM_PAINT message as WM_PAINT messages can occur frequently. Why are you changing the linker options?? If you want a console from within a windows program, then use AllocConsole() see https://docs.microsoft.com/en-us/win...n-of-a-console
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
April 30th, 2019, 10:35 AM
#3
Re: Wm_paint 100% cpu
Thank you very much for your response 2kaud, it solved the problem!
Actually I was handeling the WM_PAINT message but I removed it for simplicity's sake as it was causing the same symptom.
But now I realized that the problem was that I was calling LoadImage() in WM_CREATE:.
Now if I call it in WM_PAINT, there is no more high CPU usage.
Code:
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
hBitmap01 = (HBITMAP)LoadImage(NULL, szFileName, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
if (hBitmap01==NULL)
MessageBox(NULL, "Error Loading Image.", "ERROR", MB_ICONWARNING);
hdcMem01 = CreateCompatibleDC(hdc);
oldBitmap01 = SelectObject(hdcMem01, hBitmap01);
GetObject(hBitmap01, sizeof(bitmap01), &bitmap01);
BitBlt(hdc, 0, 0, bitmap01.bmWidth, bitmap01.bmHeight, hdcMem01, 0, 0, SRCCOPY);
SelectObject(hdcMem01, oldBitmap01);
DeleteDC(hdcMem01);
EndPaint(hWnd, &ps);
return 0;
Im changing the linker options just in order to debug the program. Because I dont want to create a whole project in Visual Studio just for the sake of testing a small program.
Anyway you made my day, Im really grateful.
-
April 30th, 2019, 10:51 AM
#4
Re: Wm_paint 100% cpu
I tried to use AllocConsole() but cout or printf wont print anything to the console.
-
April 30th, 2019, 11:15 AM
#5
Re: Wm_paint 100% cpu
Originally Posted by MasterDucky
I tried to use AllocConsole() but cout or printf wont print anything to the console.
You need to re-direct stdout to the console. See
https://stackoverflow.com/questions/...isplaying-cout
https://www.codeproject.com/articles...-from-a-gui-ap
Try:
Code:
AllocConsole();
static ofstream conout("CONOUT$", ios::out);
cout.rdbuf(conout.rdbuf());
...
cout << "Hello World" << endl;
or
Code:
AllocConsole();
FILE* fp;
freopen_s(&fp, "CONOUT$", "w", stdout);
std::cout.clear();
...
cout << "Hello World" << endl;
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
April 30th, 2019, 12:02 PM
#6
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|