user-0 提问: GDI游戏CPU占用率持高不下..

回复:
user: user-1
content: 不至于啊 我也很好奇 release版本呢？

user: user-0
content: 引用 6 楼 user-2 的回复:你消息循环里放一个PaintBg，PaintBg里又有Sleep(170)，那当然占用率下来了。你原来的满占用是因为消息循环导致的。你现在在消息循环里加Sleep的后果就是cpu占用想上去也上不去。嗯..谢谢你的回复与批评..结贴了

user: user-2
content: 我看lz乃还是花点时间研究消息循环该怎么写吧。现在这个实在是写乱了。

user: user-2
content: 你消息循环里放一个PaintBg，PaintBg里又有Sleep(170)，那当然占用率下来了。你原来的满占用是因为消息循环导致的。你现在在消息循环里加Sleep的后果就是cpu占用想上去也上不去。

user: user-0
content: 引用 2 楼 sniffer12345 的回复:不至于啊 我也很好奇 release版本呢？  引用 3 楼 user-2 的回复:while(TRUE)     {      if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))          {             if (msg.message == WM_QUIT)               break ;              TranslateMessage(&msg) ;               DispatchMessage(&msg) ;          }       }  PeekMessage不会block，那当然满cpu了。这个是有效果的...谢谢...但是我用PeekMessage在循环内多贴了几张Bmp他的CPU占有率就降下来了...这是为什么呢。。我我把代码改了一下 麻烦可以再看一下嘛.. 谢谢了#include <windows.h>  HDC hdc,mdc ; HBITMAP bg ; HWND hwnd ; MSG msg ; WNDCLASS wndclass ; PAINTSTRUCT ps ; RECT rect ;  void PaintBg(HBITMAP hbmp,LPCSTR lptext,HDC hdc,HDC pdc,HWND hwnd) ;  LRESULT CALLBACK WndProc (HWND,UINT,WPARAM,LPARAM) ;  int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE,PSTR,int iCmdShow) { static TCHAR szAppName[] = TEXT ("Sample") ;  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 ("This program requires Windows NT!"),szAppName, MB_ICONERROR) ;   return 0 ; } hwnd = CreateWindow(szAppName,TEXT ("GDI_Game"),WS_OVERLAPPEDWINDOW|WS_BORDER,                     600,320,256,240,NULL,NULL,hInstance,NULL) ;  ShowWindow(hwnd,iCmdShow) ; UpdateWindow(hwnd) ;     while(TRUE) 	{ 	 PaintBg(bg,"sdk.bmp",hdc,mdc,hwnd) ; 	 if (PeekMessage(&msg,NULL,0,0,PM_REMOVE)) 	     {             if (msg.message == WM_QUIT)               break ; 	         TranslateMessage(&msg) ;  	         DispatchMessage(&msg) ; 	     } 	   	  	 }   	return(msg.wParam) ; } LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { switch (message) {   case WM_CREATE:   return 0 ;    case WM_PAINT:   hdc = BeginPaint (hwnd,&ps) ;   EndPaint(hwnd,&ps) ;   return 0 ;     case WM_DESTROY:   DeleteDC(mdc);   DeleteObject(bg);   PostQuitMessage (0) ;   return 0 ; }   return DefWindowProc (hwnd, message, wParam, lParam) ; } void PaintBg(HBITMAP hbmp,LPCSTR lptext,HDC hdc,HDC pdc,HWND hwnd) {        hbmp = (HBITMAP)LoadImage(NULL,"sdk.bmp",IMAGE_BITMAP,256,366,LR_LOADFROMFILE) ; 	   hdc = GetDC(hwnd) ;         pdc = CreateCompatibleDC(hdc) ; 	   Sleep(170) ; 	   SelectObject(pdc,hbmp);        BitBlt(hdc,0,0,256,240,pdc,0,0,SRCCOPY) ; 	   Sleep(170) ; 	   BitBlt(hdc,31,64,192,96,pdc,0,240,SRCCOPY) ; 	   BitBlt(hdc,31,195,192,9,pdc,0,336,SRCCOPY) ; }

user: user-0
content: 引用 2 楼 sniffer12345 的回复:不至于啊 我也很好奇 release版本呢？Release版本也差不多...

user: user-2
content: while(TRUE)     {      if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))          {             if (msg.message == WM_QUIT)               break ;              TranslateMessage(&msg) ;                DispatchMessage(&msg) ;          }       }  PeekMessage不会block，那当然满cpu了。

user: user-0
content: #include <windows.h>  HDC hdc,mdc ; HBITMAP bg ; HWND hwnd ; MSG msg ; WNDCLASS wndclass ; PAINTSTRUCT ps ; RECT rect ;  LRESULT CALLBACK WndProc (HWND,UINT,WPARAM,LPARAM) ; int WINAPI WinMain  (HINSTANCE hInstance, HINSTANCE,PSTR,int iCmdShow) { static TCHAR szAppName[] = TEXT ("Sample") ;  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 ("This program requires Windows NT!"),szAppName, MB_ICONERROR) ;   return 0 ; } hwnd = CreateWindow(szAppName,TEXT ("GDI_Game"),WS_OVERLAPPEDWINDOW,                     600,320,256,240,NULL,NULL,hInstance,NULL) ;  ShowWindow(hwnd,iCmdShow) ; UpdateWindow(hwnd) ;  hdc = GetDC(hwnd) ; mdc = CreateCompatibleDC(hdc) ; bg = (HBITMAP)LoadImage(NULL,"sdk.bmp",IMAGE_BITMAP,256,336,LR_LOADFROMFILE) ; SelectObject(mdc,bg); BitBlt(hdc,0,0,256,240,mdc,0,0,SRCCOPY); while(TRUE) 	{ 	 if (PeekMessage(&msg,NULL,0,0,PM_REMOVE)) 	     {             if (msg.message == WM_QUIT)               break ; 	         TranslateMessage(&msg) ;  	         DispatchMessage(&msg) ; 	     }  	 }   	return(msg.wParam) ; } LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { switch (message) {   case WM_CREATE:   return 0 ;    case WM_PAINT:   hdc = BeginPaint (hwnd,&ps) ;   GetClientRect (hwnd,&rect) ;   EndPaint(hwnd,&ps) ;   return 0 ;     case WM_DESTROY:   DeleteDC(mdc);   DeleteObject(bg);   PostQuitMessage (0) ;   return 0 ; }   return DefWindowProc (hwnd, message, wParam, lParam) ; }