WM_PAINT Problem



  • hi leute,

    hab mir ein kleines tool geschrieben, welches mir farbwerte ausgibt und pixel vergrößert darstellt.

    Nun funktioniert es für 30 - 90 sekunden, hört dann aber auf werte zu übermitteln.

    kann mir jmd sagen woran das liegt?

    Hab im taskmanager beobachtet, dass das programm mit der zeit immer größer wird.
    Was bedeutet das, was kann ich tun?

    danke schonmal
    mfg brums

    ps: schuldigung für den codesalat 🙂

    #include<windows.h>
    #include<stdlib.h>
    #include<stdio.h>
    using namespace std;
    /*  Declare Windows procedure  */
    LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
    
    const int size = 30;
    const int posx=194, posy=69;
    HBRUSH brush;
    HWND hwnd; 
    POINT pos;
    PAINTSTRUCT pinfo;
    HDC hdc;
    char szClassName[ ] = "WindowsApp";
    RECT rect[30];
    
    union
    {
     unsigned int 		alle;
    	struct
     	{
        unsigned int    red: 8,
                        green: 8,
                        blue: 8;
    	};
    }BGr;
    
    struct
    {
    			int 	x,
    					y;
       unsigned int 	farbe;
    			int		rot,
    					blau, 
    					gruen;
    } poos[30];
    
    inline unsigned int pgc( int x, int y, int Nr_struct)// default RGB, else ( bsp. mode = 1 ) BGR;
    {
    
        HDC screen = GetDC(NULL);
        COLORREF farbescreen = GetPixel(screen, x , y ); // pos sollte in jedem programm sein!
    	poos[Nr_struct].x = x;
    	poos[Nr_struct].y = y;
    	poos[Nr_struct].farbe = farbescreen;
    
    	ReleaseDC(NULL,screen);
    	return farbescreen;
    
    }
    inline unsigned int pgc_main( int x, int y, int Nr_struct)// default RGB, else ( bsp. mode = 1 ) BGR;
    {
    
        HDC screen = GetDC(NULL);
        COLORREF farbescreen = GetPixel(screen, x , y ); // pos sollte in jedem programm sein!
    	BGr.alle = farbescreen;
    	poos[Nr_struct].x = x;
    	poos[Nr_struct].y = y;
    
    	poos[Nr_struct].farbe = farbescreen;
    	poos[Nr_struct].rot = BGr.red;
    	poos[Nr_struct].gruen = BGr.green;
    	poos[Nr_struct].blau = BGr.blue;
    
    	ReleaseDC(NULL,screen);
    	return farbescreen;
    
    }
    POINT pointprev;
    
    /*  Make the class name into a global variable  */
    
    int WINAPI WinMain (HINSTANCE hThisInstance,
                        HINSTANCE hPrevInstance,
                        LPSTR lpszArgument,
                        int nFunsterStil)
    
    {
                      /* This is the handle for our window */
        MSG messages;            /* Here messages to the application are saved */
        WNDCLASSEX wincl;        /* Data structure for the windowclass */
    
        /* The Window structure */
        wincl.hInstance = hThisInstance;
        wincl.lpszClassName = szClassName;
        wincl.lpfnWndProc = WindowProcedure;      /* This function is called by windows */
        wincl.style = CS_BYTEALIGNWINDOW	;                 /* Catch double-clicks */
        wincl.cbSize = sizeof (WNDCLASSEX);
    
        /* Use default icon and mouse-pointer */
        wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
        wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
        wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
        wincl.lpszMenuName = NULL;                 /* No menu */
        wincl.cbClsExtra = 0;                      /* No extra bytes after the window class */
        wincl.cbWndExtra = 0;                      /* structure or the window instance */
        /* Use Windows's default color as the background of the window */
        wincl.hbrBackground = (HBRUSH) COLOR_WINDOW
    ;
    
        /* Register the window class, and if it fails quit the program */
        if (!RegisterClassEx (&wincl))
            return 0;
    
        /* The class is registered, let's create the program*/
        hwnd = CreateWindowEx (
               0,                   /* Extended possibilites for variation */
               szClassName	,         /* Classname */
               "Windows App",       /* Title Text */
               WS_OVERLAPPEDWINDOW, /* default window */
               CW_USEDEFAULT,       /* Windows decides the position */
               CW_USEDEFAULT,       /* where the window ends up on the screen */
               300,                 /* The programs width */
               201,                 /* and height in pixels */
               HWND_DESKTOP,        /* The window is a child-window to desktop */
               NULL,                /* No menu */
               hThisInstance,       /* Program Instance handler */
               NULL                 /* No Window Creation data */
               );
    
        /* Make the window visible on the screen */
        ShowWindow (hwnd, nFunsterStil);
    
        /* Run the message loop. It will run until GetMessage() returns 0 */
        while (GetMessage (&messages, NULL, 0, 0))
        {
            /* Translate virtual-key messages into character messages */
            TranslateMessage(&messages);
            /* Send message to WindowProcedure */
            DispatchMessage(&messages);
        }
    
        /* The program return-value is 0 - The value that PostQuitMessage() gave */
        return messages.wParam;
    }
    
    /*  This function is called by the Windows function DispatchMessage()  */
    
    LRESULT CALLBACK WindowProcedure(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
        switch (message)                  /* handle the messages */
        {
    		case WM_PAINT:			
    
    			GetCursorPos( &pos );
    
    			//---------- Mittelpunkt-----
    
    			pgc_main( pos.x	, pos.y , 8);
    
    			//---------- Andere ---------
    			pgc( pos.x-1, pos.y-1	, 0);
    			pgc( pos.x  , pos.y-1 	, 1);
    			pgc( pos.x+1, pos.y-1 	, 2);
    			pgc( pos.x+1, pos.y		, 3);
    			pgc( pos.x+1, pos.y+1 	, 4);
    			pgc( pos.x  , pos.y+1 	, 5);
    			pgc( pos.x-1, pos.y+1 	, 6);
    			pgc( pos.x-1, pos.y 	, 7);			
    
    			pgc( pos.x-1, pos.y-2	, 9);
    			pgc( pos.x+0, pos.y-2	, 10);
    			pgc( pos.x+1, pos.y-2	, 11);
    			pgc( pos.x+2, pos.y-2	, 12);
    			pgc( pos.x+2, pos.y-1	, 13);
    			pgc( pos.x+2, pos.y-0	, 14);
    			pgc( pos.x+2, pos.y+1	, 15);
    			pgc( pos.x+2, pos.y+2	, 16);
    			pgc( pos.x+1, pos.y+2	, 17);
    			pgc( pos.x+0, pos.y+2	, 18);
    			pgc( pos.x-1, pos.y+2	, 19);
    			pgc( pos.x-2, pos.y+2	, 20);
    			pgc( pos.x-2, pos.y+1	, 21);
    			pgc( pos.x-2, pos.y+0	, 22);
    			pgc( pos.x-2, pos.y-1	, 23);
    			pgc( pos.x-2, pos.y-2	, 24);
    
    			rect[8].left = posx+1;
    			rect[8].top  = posy+1;
    			rect[8].right = posx+size-1;
    			rect[8].bottom= posy+size-1;
    
    			//Koordinaten
    			for( int u = 0; u <8; ++u)
    			{				
    				rect[u].left 	=posx+ (poos[8].x-poos[u].x)*-size;
    				rect[u].top 	=posy+  (poos[8].y-poos[u].y)*-size;
    				rect[u].right 	=rect[u].left + size ;
    				rect[u].bottom 	=rect[u].top + size ;
    			}			
    			for( int pe = 9; pe < 25; ++pe)
    			{
    				rect[pe].left 	=posx+ (poos[8].x-poos[pe].x)*-size;
    				rect[pe].top 	=posy+  (poos[8].y-poos[pe].y)*-size;
    				rect[pe].right 	=rect[pe].left + size ;
    				rect[pe].bottom 	=rect[pe].top + size ;
    			}
    
    			//Bereite text vor
    			char all[50],red[50], gruen[50], blau[50];
    			sprintf( all, "Farbe: %i                   ", BGr.alle);
    			sprintf( red, "Rot: %i                 ", BGr.red);
    			sprintf( gruen, "Gruen: %i                 ", BGr.green);
    			sprintf( blau, "Blau: %i                ", BGr.blue);
    
    			 hdc= GetDC( hwnd );
    
    			//Schreib Text
    			TextOut( hdc, 10, 10, all, strlen(all));
    			TextOut( hdc, 10, 30, red, strlen(red));
    			TextOut( hdc, 10, 50, gruen, strlen(gruen));
    			TextOut( hdc, 10, 70, blau, strlen(blau));	
    
    			//Male Rechtecke
    			for( int popo = 0; popo < 25; ++popo)
    			{
    				brush = CreateSolidBrush( poos[popo].farbe );
    				FillRect( hdc, &rect[popo], brush );
    			}
    
    			//Viereck in der mitte
    			for( int k = 0; k < size; ++k)
    			{
    				SetPixel( hdc, posx, posy+k, 0);
    				SetPixel( hdc, posx+k, posy, 0);
    				SetPixel( hdc, posx+size-k-1, posy+size-1, 0);
    				SetPixel( hdc, posx+size-1, posy+size-1-k, 0);
    			}
    
    			ReleaseDC( hwnd, hdc);
    			Sleep(80);	
    			return 0;
    
            case WM_DESTROY:
                PostQuitMessage (0);       /* send a WM_QUIT to the message queue */
                break;
            default:                      /* for messages that we don't deal with */
                return DefWindowProc(hwnd, message, wParam, lParam);
        }
    
        return 0;
    }
    


  • Der mit CreateSolidBrush () erzeugte Brush muss nach Gebrauch wieder freigegeben werden :

    //Male Rechtecke
    for( int popo = 0; popo < 25; ++popo)
    {
     brush = CreateSolidBrush( poos[popo].farbe );
     FillRect( hdc, &rect[popo], brush );
     DeleteObject (brush); // <- freigeben !
    }
    

Anmelden zum Antworten