how to repaint the bitmap

Discussion in 'Win32' started by rachappa, Dec 19, 2006.

  1. rachappa

    rachappa New Member

    Joined:
    Sep 14, 2006
    Messages:
    9
    Likes Received:
    0
    Trophy Points:
    0
    hi i am written the window application in win32 for the dynamic graph.
    after minimizing and maximizing the window the previous graph will not retain so please help me.. here is my code in win32

    Code:
    #include <windows.h>
    #include <stdio.h>
    #include <time.h>
    #define IDC_STATIC_EDIT 1000
    
    /*  Declare Windows procedure  */
    LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
    void Line_paint(void);
    int DrawChart(HDC htemp);
    HWND HWND_G,hwnd_DESK;      
    WPARAM wParam;
    HDC hDC_AMEL,m_MemDC;
    //HBITMAP hBitmap;
    static HBITMAP hBitmap = NULL,h_MemDC=NULL; 
    static int xValue=50,yValue=50;
    static int firstx=50,firsty=200,secondx=50,secondy=200;
    BITMAPINFOHEADER BitmapInfoHeader;
    BITMAP Bm;
    byte *BmpData;
    BITMAPINFO pbmi;
     
    
    /*  Make the class name into a global variable  */
    char szClassName[ ] = "WindowsApp";
    
    int WINAPI WinMain (HINSTANCE hThisInstance,
                        HINSTANCE hPrevInstance,
                        LPSTR lpszArgument,
                        int nFunsterStil)
    
    {
        HWND hwnd;               /* 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_DBLCLKS;                 /* 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)GetStockObject(WHITE_BRUSH/*LTGRAY_BRUSH*/);
    
        /* 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 */
               DS_CENTER | WS_MINIMIZEBOX | WS_POPUP | WS_CAPTION | 
               WS_SYSMENU |WS_DLGFRAME, /* default window */
               300,       /* Windows decides the position */
               150,       /* where the window ends up on the screen */
               600,                 /* The programs width */
               300,                 /* 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 */
               );
         
           hwnd_DESK=CreateWindowEx (0,    /* Extended possibilites for variation */
               TEXT("STATIC"),         /* Classname */
               NULL,       /* Title Text */
               WS_CHILD|WS_VISIBLE|WS_CLIPCHILDREN  , /* default window */
               40,       /* Windows decides the position */
               30,       /* where the window ends up on the screen */
               400,                 /* The programs width */
               200,                 /* and height in pixels */
               hwnd,        /* The window is a child-window to desktop */
              (HMENU)IDC_STATIC_EDIT,                /* No menu */
               hThisInstance,       /* Program Instance handler */
               NULL                 /* No Window Creation data */
               );
                
        /* Make the window visible on the screen */
        ShowWindow (hwnd, nFunsterStil);
        ShowWindow (hwnd_DESK, 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_GETMINMAXINFO: 
               { 
                    LPMINMAXINFO lpMMI;   
                    
                    break; 
               } 
    
                   
           case WM_MOVE:
                   {   
                   RECT RClient;       
                   InvalidateRect(hwnd_DESK,&RClient,FALSE);  
                   }
             break;
        
            case WM_ERASEBKGND:
               {
                
             /*   RECT RClient;  
                HDC htemp,htemp1; 
                htemp = GetDC(hwnd_DESK);   
                GetClientRect(hwnd_DESK, &RClient);
                int targetWidth,targetHeight;
                targetWidth = RClient.right - RClient.left; 
                targetHeight = RClient.bottom - RClient.top;
                BitBlt(htemp,0,0,targetWidth,targetHeight,hDC_AMEL,4,0,SRCCOPY);
             */
               return 1;
               }
               break;
               
         case WM_PAINT:
                {
                  
                 static int i=10, inc;
                 HDC htemp,htemp1;
                 RECT RClient; 
                 HBITMAP hBitmap;
                 HBRUSH brCross;
                 htemp = GetDC(hwnd_DESK);   
                 GetClientRect(hwnd_DESK, &RClient);
                 int targetWidth,targetHeight;
                 targetWidth = RClient.right - RClient.left; 
                 targetHeight = RClient.bottom - RClient.top;
                          
                          
                 if(hDC_AMEL==NULL)    
                 {
                           hDC_AMEL=CreateCompatibleDC(hDC_AMEL);
                           GetClipBox((HDC)wParam, &RClient); 
                           targetWidth = RClient.right - RClient.left; 
                           targetHeight = RClient.bottom - RClient.top; 
                           hBitmap=CreateCompatibleBitmap(hDC_AMEL,targetWidth,targetHeight); 
                           SelectObject(hDC_AMEL,hBitmap);  
                           // draw the scale...                       
                           SetBkColor(hDC_AMEL,RGB(0,0,0));
                           brCross= CreateHatchBrush(HS_CROSS, RGB(0, 128, 0));
                           FillRect(hDC_AMEL,&RClient,brCross);  
                           SelectObject(hDC_AMEL,brCross);
                          // BitBlt(hDC_AMEL,0,0,targetWidth,targetHeight,hDC_AMEL,0,0,SRCCOPY);
                           Line_paint();
                           InvalidateRect(hwnd_DESK,&RClient,FALSE);  
                  }
                  if(hDC_AMEL!=NULL)   
                  {  
                     	   RClient.left = RClient.right-4;
                 	       SetBkColor(htemp,RGB(0,0,0));
                           brCross= CreateHatchBrush(HS_HORIZONTAL, RGB(0, 128, 0));
                           FillRect(htemp,&RClient,brCross);
                           static BOOL bDrawVerticle = FALSE;
    	                   bDrawVerticle = !bDrawVerticle;
                        // Draw Verticle lines only every two steps
    		               if (bDrawVerticle)
    		                {SetBkColor(htemp,RGB(0,0,0));
                            HPEN Oldpen = CreatePen(PS_SOLID, 1, RGB(0,128,0)); 
                            Oldpen = (HPEN)(SelectObject(htemp, Oldpen)); 
                            MoveToEx(htemp,RClient.right-2,RClient.top, NULL);
                            LineTo(htemp,RClient.right-2,RClient.bottom);
                            SelectObject(htemp,Oldpen);
                         
                        // line drawing...     
                            DrawChart(htemp); 
                            }
                        
                      
                          BitBlt(htemp,0,0,targetWidth,targetHeight,hDC_AMEL,4,0,SRCCOPY);
                       
                       
                         if(!IsIconic(hwnd))
                         {  
                         SendMessage(hwnd_DESK,WM_ICONERASEBKGND, (WPARAM)hDC_AMEL, 0);
                         Line_paint(); 
                         BitBlt(htemp,0,0,targetWidth,targetHeight,(HDC)&m_MemDC,0,0,SRCCOPY); 
                         }  
                                 
                           m_MemDC=CreateCompatibleDC(hDC_AMEL);
                           GetClipBox((HDC)wParam, &RClient); 
                           targetWidth = RClient.right - RClient.left; 
                           targetHeight = RClient.bottom - RClient.top; 
                           hBitmap=CreateCompatibleBitmap(m_MemDC,targetWidth,targetHeight); 
                           SelectObject(m_MemDC,hBitmap); 
                          
                           
                           UpdateWindow(GetDlgItem(hwnd,IDC_STATIC_EDIT));  
                            Sleep(500); 
                         }
                         
                 
                  GetObject(brCross , sizeof(BITMAP),(LPVOID)&brCross);  
                  SelectObject(hDC_AMEL,brCross); 
                  DeleteObject(brCross); 
                  DeleteDC(hDC_AMEL); 
                  ReleaseDC( hwnd_DESK, hDC_AMEL ) ;
              
              
               InvalidateRect(hwnd_DESK,&RClient,FALSE);    
                           
                }
              
                break;
            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;
    }
    
    
    void Line_paint(void)
    {
                 HDC htemp;
                 RECT RClient; 
                // HBITMAP hBitmap;
                 HBRUSH brCross;
                 static int i=0;
                 int targetWidth,targetHeight;
                 GetClientRect(hwnd_DESK, &RClient);
                 hDC_AMEL = GetDC(hwnd_DESK); 
                 targetWidth = RClient.right - RClient.left; 
                 targetHeight = RClient.bottom - RClient.top; 
                 
                 HPEN hBluePen = CreatePen(PS_SOLID, 3, RGB(255, 0, 0) ); 
                 PAINTSTRUCT ps; 
                 hDC_AMEL = BeginPaint(hwnd_DESK, &ps); 
                 hBluePen = static_cast<HPEN>(SelectObject(hDC_AMEL, hBluePen)); 
            
                         if (hDC_AMEL!=NULL)
           	              {
                           htemp=CreateCompatibleDC(hDC_AMEL);
                           GetClipBox((HDC)wParam, &RClient); 
                           targetWidth = RClient.right - RClient.left; 
                           targetHeight = RClient.bottom - RClient.top; 
                           
                           hBitmap=CreateCompatibleBitmap(hDC_AMEL,targetWidth,targetHeight); 
                           SelectObject(hDC_AMEL,hBitmap);  
                           
                           SetBkColor(hDC_AMEL,RGB(0,0,0));
                           brCross= CreateHatchBrush(HS_CROSS, RGB(0, 128, 0));
                           FillRect(hDC_AMEL,&RClient,brCross);  
                           SelectObject(htemp,brCross);
                        // BitBlt(hDC_AMEL,0,0,targetWidth,targetHeight,hDC_AMEL,0,0,SRCCOPY);
                          }
                           m_MemDC=CreateCompatibleDC(hDC_AMEL);
                           GetClipBox((HDC)wParam, &RClient); 
                           targetWidth = RClient.right - RClient.left; 
                           targetHeight = RClient.bottom - RClient.top; 
                           hBitmap=CreateCompatibleBitmap(m_MemDC,targetWidth,targetHeight); 
                           SelectObject(m_MemDC,hBitmap); 
                 
                  GetObject(hBitmap , sizeof(BITMAP),(LPVOID)&hBitmap);  
                  SelectObject(hDC_AMEL, hBitmap); 
                  DeleteObject(hBitmap); 
                  DeleteDC(hDC_AMEL); 
                 
    }
    
    
    
    int DrawChart(HDC htemp)
    {
            // create a blue pen object 
             HPEN hBluePen = CreatePen(PS_SOLID, 3, RGB(0, 0, 255) ); 
             static int inc,Height=0;
             static PAINTSTRUCT ps; 
            
                // associate the pen with the DC 
                hBluePen = static_cast<HPEN>(SelectObject(htemp, hBluePen)); 
                // draw someting to the DC 
                RECT RClient; 
                 int targetWidth,targetHeight;
                 GetClientRect(hwnd_DESK, &RClient);
                 hDC_AMEL = GetDC(hwnd_DESK); 
                 targetWidth = RClient.right - RClient.left; 
                 targetHeight = RClient.bottom - RClient.top; 
                 
                
           if (GetClientRect(hwnd_DESK, &RClient)) 
            {   
                     int array[]={80,100,39,10,15,68,30,40,13,45,10,5,30,4,15,20,49,0,40,9,80,80,100,39,10,15,68,30,40,13,45,10,5,30,4,15,20,49,0,40,9,80,80,100,39,10,15,68,30,40,13,45,10,5,30,4,15,20,49,0,40,9,80,100,39,10,15,68,30,40,13,45,10,5,30,4,15,20,49,0,40,9,80,100,39,10,15,68,30,40,13,45,10,5,30,4,15,20,49,0,40,9,80,100,39,10,15,68,30,40,13,45,10,5,30,4,15,20,49,0,40,9};
                     int TEMP;
                     int midx,midy;
                     
                     HPEN hRedPen = CreatePen(PS_SOLID, 1, RGB(255, 0, 0) );       
                     hRedPen = static_cast<HPEN>(SelectObject(htemp, hRedPen)); 
                     
                 //   for(inc=0;inc<30;inc++)
                    
                    do
                    { 
                      xValue=xValue-3;
                      yValue=yValue-3;
                       
                     TEMP= Height+ array[inc];                
                     printf("x1= %d \t y1= %d \t x2=%d \t y2=%d \n",xValue,250,yValue,TEMP);
                     midx=(xValue+yValue)/2;
                     midy=(200+TEMP)/2;
                     
                     secondx=midx;
                     secondy=midy; 
                  
                     HPEN hRedPen = CreatePen(PS_SOLID, 1, RGB(0,255,0) ); 
                     hRedPen = static_cast<HPEN>(SelectObject(htemp, hRedPen)); 
                     if (!MoveToEx(htemp,targetWidth-8, firsty, NULL) ||!LineTo(htemp, targetWidth,secondy)) 
                     { 
                     MessageBox(NULL,"Paint error","ERROR",0);
                     }
                  
                     hRedPen = CreatePen(PS_SOLID, 5, RGB(0,255,0) );       
                     hRedPen = static_cast<HPEN>(SelectObject(htemp, hRedPen)); 
               /*      
                   if (!MoveToEx(htemp,midx, midy, NULL) ||!LineTo(htemp, midx,midy)) 
                     { 
                     MessageBox(NULL,"Paint error","ERROR",0);
                     }  */
                     xValue=xValue+10;
                     yValue=yValue+10;
                     firstx=midx;
                     firsty=midy;
                     inc++;
                     printf("the inc value %d \n",inc);
                     
                     if(inc==80)
                     inc=0;
                     
                     } while (inc < 10 || inc > 80);
         
            }     
           
          // free the blue pen object 
             DeleteObject(SelectObject(htemp, hBluePen)); 
             // release the DC 
             EndPaint(hwnd_DESK, &ps); 
       return 0;
    }
     
    Last edited by a moderator: Dec 19, 2006
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    In WM_PAINT you need to paint the currently shown graph.
     
  3. rachappa

    rachappa New Member

    Joined:
    Sep 14, 2006
    Messages:
    9
    Likes Received:
    0
    Trophy Points:
    0
    Hi shabbir, good morning,

    In WM_PAINT only i am drawing the grids and graph, how i have to capture the event of window while minimizing. so that i can store the bitmap(i.e., graph i am making the chart and paint to bitmap). so when i am maximizing the window i should place the bitmap in the window which i saved in memory before minimizing the window.

    This is the actual problem for me i am not getting any window control to which is like WM_PAINT (this will execute every time).

    I am drastically stuck with this problem as i am fresher to this programming.
     
  4. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    Have a break point / outputdebugstring in erase background and paint and see when you minimize and maximize which one is called and what happens.

    Also try seeing the resizing of window and if you graph is correct.
     
  5. rachappa

    rachappa New Member

    Joined:
    Sep 14, 2006
    Messages:
    9
    Likes Received:
    0
    Trophy Points:
    0
    Hi Shabbir, I tried in devc++, as i know about compiler there is no break point, so i used the MessageBox() to know the flow. after compiling i am not getting the messagebox message but the window is updating as per the graph is concern when i am closing the window the msg loop will send the all the messgebox() message. first around 8 tp 9 messagebox containing wm_paint afetr that i am going to erasebkgnd, i dont know this will workout for me..
     
  6. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    MessageBox is not the right solution to paint problems as after the message box you will have paint again giving you endless message box to control. Use OutputDebugString or some log to a file.
     
  7. rachappa

    rachappa New Member

    Joined:
    Sep 14, 2006
    Messages:
    9
    Likes Received:
    0
    Trophy Points:
    0
    i use dthe log for the finding the flow i got like this msg when i am compiling the program i will just minimize the window. I m nutralized the maximizing the window. the

    in side WM_MOVE i gave "move" as string for log into file
    in side WM_PAINT i gave "paint" and for WM_BACKGNDERASE: i gave as "erase"



    move
    paint
    paint
    paint
    paint
    paint
    move
    paint
    paint
    erase
    erase
    paint

    so now i got the window by default it will execute the WM_PAINT always, so as i am written my code
    in WM_PAINT only so its repeats as should do. the previous data of window is erased.

    please listen to my quary:
    when i am drawing the lines (graph) if window i minimized can i take the snapshot of bitmap, after restoring the window i want to store the bitmap. is it possible. so i m looking for like this kind of logic
    but i am failing in minimizing and maximizing the window ctrl, and even i have to see hoe to store the bitmap in memory. please please help me......
     
  8. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    The way you are looking is not the solution to the problem because if you are able to take the snapshot of the bitmaps and then restore it but it should be something like the way you have rendered the bitmap you should store the parameters of that when it maximizes it should be able to draw excatly the same bitmap with the same parameters used.

    If its unable to that means you are not drawing the entire image before the minimize and some image portion is there in the buffer and you are drawing over that one which is not the case when you min and max it.
     
  9. rachappa

    rachappa New Member

    Joined:
    Sep 14, 2006
    Messages:
    9
    Likes Received:
    0
    Trophy Points:
    0
    Thanks for giving solution to my view. so can u please tell me, what can i do for restoring the grpah
    i used another ctrl WM_QUERYOPEN in this i am given the array to draw the whole grpah. bcoz WM_QUERYOPEN is gets an event when minmized window going to maximize. so there the window is not restoring but it ll struck their itself.
     
  10. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    There are lots of things you need to be doing and the first one that I have seen is you have invalidated in the WM_PAINT. Do you find any reason to be doing that.

    I guess what needs to be done is explained in my prev post. You need to be doing with care as you have no good experience on handling the issues and after running your program the machine tends to become quite slow and so you need to be handling the paint a bit in a better way.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice