Warum macht mein Programm 100% CPU-Auslastung???



  • wie schon gefragt:
    Warum macht mein Programm immer 97%-100% CPU-Auslastung???
    hier ist es:

    #define WIN32_LEAN_AND_MEAN //tells the compiler not to include MFC overhead
    
    #include <Windows.h>
    #include <Winuser.h>
    
    #ifndef WS_EX_LAYERED
    #define WS_EX_LAYERED 0x00080000
    #endif
    #ifndef LWA_COLORKEY
    #define LWA_COLORKEY 1
    #endif
    #ifndef LWA_ALPHA
    #define LWA_ALPHA 2
    #endif
    #define COLOR_TRANSPARENT    RGB(128,128,128)
    
    int SetWindowTransparent_l(HWND hwnd, int iAlpha)
    {
      typedef DWORD (WINAPI *PSLWA)(HWND, DWORD, BYTE, DWORD);
      PSLWA            pSetLayeredWindowAttributes;
      HMODULE        hDLL;
      SetWindowLong (hwnd , GWL_EXSTYLE , GetWindowLong (hwnd , GWL_EXSTYLE ) | WS_EX_LAYERED ) ;
      hDLL = LoadLibrary ("user32.dll");
      if(!hDLL)
        MessageBox(NULL,TEXT("No Handle"),TEXT("ERROR"),MB_OK);
      pSetLayeredWindowAttributes = (PSLWA) GetProcAddress(hDLL,"SetLayeredWindowAttributes");
      if (pSetLayeredWindowAttributes != NULL)
      {
        pSetLayeredWindowAttributes (hwnd,COLOR_TRANSPARENT, iAlpha, LWA_COLORKEY|LWA_ALPHA);
      }
      return (0);
    }
    
    void bmpshow(HWND hwnd,HBITMAP hbitmap,int xp,int yp,int xs,int ys,int xe,int ye){
    			HDC hdc = GetDC(hwnd); 
    			HDC image_dc = CreateCompatibleDC(hdc);	
    			SelectObject(image_dc,hbitmap);
    			BitBlt(hdc,xp,yp,xe,ye,image_dc,xs,ys,SRCCOPY); 
    			ReleaseDC(hwnd, hdc); 
    			DeleteDC(image_dc);
    }
    
    HBITMAP Table   = (HBITMAP)LoadImage(GetModuleHandle(NULL),"tbl\\tbl_Window.bmp" ,IMAGE_BITMAP,0,0,LR_LOADFROMFILE);
    HBITMAP SysBtns = (HBITMAP)LoadImage(GetModuleHandle(NULL),"tbl\\tbl_SysBtns.bmp",IMAGE_BITMAP,0,0,LR_LOADFROMFILE);
    
    void table(HWND hwnd,int x1,int y1,int x2,int y2){
    			x2=x1+x2;
    			y2=y1+y2;
    			bmpshow(hwnd,Table,x1   ,y1		,   0,   0,24      ,24 );
    			bmpshow(hwnd,Table,x1+24,y1		,  24,   0,x2-x1-48,24 );
    			bmpshow(hwnd,Table,x2-24,y1		,2072,   0,24      ,24 );
    
    			bmpshow(hwnd,Table,x1   ,y1+24	,   0,  24,2       ,y2-y1-26);
    			bmpshow(hwnd,Table,x1+2 ,y1+24	,  24,  24,x2-x1-4 ,y2-y1-26);
    			bmpshow(hwnd,Table,x2-2 ,y1+24	,2072,  24,2       ,y2-y1-26);
    
    			bmpshow(hwnd,Table,x1   ,y2-2	,   0,1304,2       ,2  );
    			bmpshow(hwnd,Table,x1+2 ,y2-2	,  24,1304,x2-x1-4 ,2  );
    			bmpshow(hwnd,Table,x2-2 ,y2-2	,2072,1304,2       ,2  );
    }
    
    void ShowSysBtn(HWND hwnd)
    {
    bmpshow(hwnd,SysBtns,570,2,0, 0,20,20);
    bmpshow(hwnd,SysBtns,595,2,0,20,20,20);
    }
    
    void CheckSysBtn(HWND hwnd,POINT MousePos,int Item)
    {
    int x=MousePos.x;
    int y=MousePos.y;
    	if(x>570 && x<590 && y>2 && y<22){
    		bmpshow(hwnd,SysBtns,570,2,Item*20, 0,20,20);
    		if(Item==2){ShowWindow(hwnd,SW_MINIMIZE);}
    	}
    	else
    	{
    		bmpshow(hwnd,SysBtns,570,2, 0, 0,20,20);
    	}
    	if(x>595 && x<615 && y>2 && y<22){
    		bmpshow(hwnd,SysBtns,595,2,Item*20,20,20,20);
    		if(Item==2){SendMessage(hwnd, WM_DESTROY, 0, 0);}
    	}
    	else
    	{
    		bmpshow(hwnd,SysBtns,595,2, 0,20,20,20);
    	}
    }
    
    LRESULT CALLBACK WindowProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
    {
    	POINT MousePos;
    
    	if(msg==WM_CREATE)
    		{
    		SetWindowTransparent_l(hwnd,255);
    		table(hwnd,0 ,0  ,640,480);
    		ShowSysBtn(hwnd);
    		}
    	if(msg==WM_LBUTTONUP)
    		{
    		MousePos.x = LOWORD(lparam);
            MousePos.y = HIWORD(lparam);
    		CheckSysBtn(hwnd,MousePos,0);
    		}
    	if(msg==WM_MOUSEMOVE)
    		{
    		MousePos.x = LOWORD(lparam);
            MousePos.y = HIWORD(lparam);
    		CheckSysBtn(hwnd,MousePos,1);
    		}
    	if(msg==WM_LBUTTONDOWN)
    		{    
    		MousePos.x = LOWORD(lparam);
            MousePos.y = HIWORD(lparam);
    		CheckSysBtn(hwnd,MousePos,2);
    			if(MousePos.x<550 && MousePos.y<24){
    			ReleaseCapture();
    			SendMessage(hwnd, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);          
    			UpdateWindow(hwnd);
    			}
    		}
    	if(msg==WM_PAINT)
    		{
    		table(hwnd,0 ,0  ,640,480);
    		ShowSysBtn(hwnd);
    		}
    
    	if(msg==WM_DESTROY)
    		{
    			PostQuitMessage(0);
    			return 0;
    		}
    	return(DefWindowProc(hwnd, msg, wparam, lparam));
    }
    
    int WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hprevinstance, LPSTR lpcmdline, int ncmdshow)
    {
    
    	WNDCLASSEX window; //blank window, we're using the extended windows class (WNDCLASSEX)
    	HWND hwnd; //window handle
    	MSG msg; //message
    
    	window.cbClsExtra = 0; //extra class stuff, set to 0 for now
    	window.cbSize = sizeof(WNDCLASSEX); //set to the size of the WINDOWCLASSEX structure
    	window.cbWndExtra = 0; //extra class stuff, set to 0 for now
    	window.hbrBackground = (HBRUSH)GetStockObject(GRAY_BRUSH);//BLACK_BRUSH); //sets the color of the brush to paint in, notice the type cast to HBRUSH, this is because GetStockObject() can be used for more tehn just getting the brushcolor
    	window.hCursor = LoadCursor(NULL, IDC_ARROW); //cursor for your app, sets it to the standard arrow
    	window.hIcon = LoadIcon(NULL, IDI_APPLICATION); //icon of your program
    	window.hIconSm = LoadIcon(NULL, IDI_APPLICATION); //icon that appears on the titlebar and when minimized
    	window.hInstance = hinstance; //the application instance
    	window.lpfnWndProc = WindowProc; //name of the function that takes care of the messages
    	window.lpszClassName = "WINCLASS1"; //name of the class
    	window.lpszMenuName = NULL; //menu, dont worry about this now (maybe in a later example, just set it to NULL
    	window.style = CS_DBLCLKS | CS_OWNDC | CS_HREDRAW | CS_VREDRAW;
    
    	if(!RegisterClassEx(&window))
    		return 0;
    
    	if(!(hwnd = CreateWindowEx(
    		NULL,//WS_EX_TRANSPARENT, //extended style
    		"WINCLASS1", //name of the class
    		"... by K4!53R", //title of the window
    		WS_OVERLAPPED | WS_VISIBLE | WS_POPUP, //common flags, you DO want to see your window, right?
    		0, 0, //initial x and y postion
    		650, 515, //initial height and width
    		NULL, //handle to parent, set to NULL so the desktop is the parent
    		NULL, //handle to menu, we didnt use a menu now so dont worry about it
    		hinstance, //instance of application
    		NULL ))) //extra param
    	return 0;
    
    	while(true)
    	{
    		if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
    		{
    			if(msg.message == WM_QUIT)
    				break;
    			TranslateMessage(&msg);
    			DispatchMessage(&msg);
    		}
    	}
    	return(msg.wParam);
    }
    


  • weil es wahrscheinlich dauernd durchrattert ohne pause...



  • wie meinst du das???



  • Selbst das hier macht die gleiche auslastung bei mir:

    #define WIN32_LEAN_AND_MEAN
    
    #include <Windows.h>
    #include <Winuser.h>
    
    LRESULT CALLBACK WindowProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
    {
    	if(msg==WM_DESTROY)
    		{
    			PostQuitMessage(0);
    			return 0;
    		}
    	return(DefWindowProc(hwnd, msg, wparam, lparam));
    }
    
    int WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hprevinstance, LPSTR lpcmdline, int ncmdshow)
    {
    	WNDCLASSEX window; 
    	HWND hwnd; 
    	MSG msg;
    
    	window.cbClsExtra = 0; 
    	window.cbSize = sizeof(WNDCLASSEX); 
    	window.cbWndExtra = 0; 
    	window.hbrBackground = (HBRUSH)GetStockObject(GRAY_BRUSH);
    	window.hCursor = LoadCursor(NULL, IDC_ARROW);
    	window.hIcon = LoadIcon(NULL, IDI_APPLICATION); 
    	window.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
    	window.hInstance = hinstance;
    	window.lpfnWndProc = WindowProc;
    	window.lpszClassName = "WINCLASS1";
    	window.lpszMenuName = NULL; 
    	window.style = CS_DBLCLKS | CS_OWNDC | CS_HREDRAW | CS_VREDRAW;
    
    	if(!RegisterClassEx(&window))
    		return 0;
    
    	if(!(hwnd = CreateWindowEx(
    		NULL,//WS_EX_TRANSPARENT, 
    		"WINCLASS1",
    		"... by K4!53R",
    		WS_OVERLAPPED | WS_VISIBLE | WS_POPUP, 
    		0, 0,
    		650, 515, 
    		NULL, 
    		NULL,
    		hinstance, 
    		NULL )))
    	return 0;
    
    	while(true)
    	{
    		if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
    		{
    			if(msg.message == WM_QUIT)
    				break;
    			TranslateMessage(&msg);
    			DispatchMessage(&msg);
    		}
    	}
    	return(msg.wParam);
    }
    


  • AAAHHH - schon erledigt!
    😃 👍 ⚠



  • Deine Nachrichtenschleife ist unvollständig. Das was du machst, wird idR nur bei Spielen verwndet, wo der Game-Loop sozusagen immer läuft.
    Ansonsten solltest du statt PeekMessage eher GetMessage verwenden bzw. WaitMessage aufrufen, sofern keine weiteren Nachrichten anstehen.



  • Kennst du switch()? 🙄



  • wie gesagt - schon erledigt!
    genauso hab ich es dann nämlich auch gelöst...

    sicher kenn ich switch, musste ich zwischendurch aber umändern weil ich es etwas anders brauchte, aber trotzdem danke für den tip!
    😉


Anmelden zum Antworten