Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. moving graphics in a win32 c++ window

moving graphics in a win32 c++ window

Scheduled Pinned Locked Moved C / C++ / MFC
c++graphics
6 Posts 3 Posters 8 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    Calin Negru
    wrote on last edited by
    #1

    I`m trying to animate things in a basic window. The code I have should get a rectangle (rc) move to the right however that does not happen, it`s all static.

    LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
    {
    static RECT rcBmp;
    static HDC hdcCompat;
    static HBITMAP hbmp;
    static RECT rc;
    static RECT rc2;
    static RECT rc3;
    static RECT rc4;
    HBRUSH hbrWhite, hbrGray;
    const COLORREF SomeColor = RGB(255,255,255);
    const COLORREF SomeColor2 = RGB(0,0,1);
    hbrWhite = CreateSolidBrush(SomeColor);
    hbrGray = CreateSolidBrush(SomeColor2);

    int y = 0;
    HDC hdc = (HDC) wParam; 
    
    
    switch (uMsg)
    {
    	case WM\_CREATE: 
    	
    	return 0L; 
    

    case WM_ERASEBKGND:

    GetClientRect(hwnd, &rc); 
    SetMapMode(hdc, MM\_ANISOTROPIC); 
    SetWindowExtEx(hdc, 100, 100, NULL); 
    SetViewportExtEx(hdc, rc.right, rc.bottom, NULL); 
    FillRect(hdc, &rc, hbrWhite); 
    
    
       
       
    return 0;
    
    case WM\_DESTROY:
        PostQuitMessage(0);
        return 0;
    
    	
           
    case WM\_PAINT:
        {
            PAINTSTRUCT ps;
    		
            HDC hdc = BeginPaint(hwnd, &ps);
    
            
    
           
    		 SetRect(&rc,x, 20, 40, 40); 
    		FillRect(hdc, &rc, hbrGray); 
    		SetRect(&rc2,60, 60, 80, 80); 
    		FillRect(hdc, &rc2, hbrGray); 
    
            EndPaint(hwnd, &ps);
        }
        return 0;
    
    }
    return DefWindowProc(hwnd, uMsg, wParam, lParam);
    

    }
    void GameUpdate()
    {

    x = x +1;
    

    }

    [Edit] it looks like WM_PAINT isn`t triggered every frame, only if I minimize and then maximize the window a change takes place

    L G 3 Replies Last reply
    0
    • C Calin Negru

      I`m trying to animate things in a basic window. The code I have should get a rectangle (rc) move to the right however that does not happen, it`s all static.

      LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
      {
      static RECT rcBmp;
      static HDC hdcCompat;
      static HBITMAP hbmp;
      static RECT rc;
      static RECT rc2;
      static RECT rc3;
      static RECT rc4;
      HBRUSH hbrWhite, hbrGray;
      const COLORREF SomeColor = RGB(255,255,255);
      const COLORREF SomeColor2 = RGB(0,0,1);
      hbrWhite = CreateSolidBrush(SomeColor);
      hbrGray = CreateSolidBrush(SomeColor2);

      int y = 0;
      HDC hdc = (HDC) wParam; 
      
      
      switch (uMsg)
      {
      	case WM\_CREATE: 
      	
      	return 0L; 
      

      case WM_ERASEBKGND:

      GetClientRect(hwnd, &rc); 
      SetMapMode(hdc, MM\_ANISOTROPIC); 
      SetWindowExtEx(hdc, 100, 100, NULL); 
      SetViewportExtEx(hdc, rc.right, rc.bottom, NULL); 
      FillRect(hdc, &rc, hbrWhite); 
      
      
         
         
      return 0;
      
      case WM\_DESTROY:
          PostQuitMessage(0);
          return 0;
      
      	
             
      case WM\_PAINT:
          {
              PAINTSTRUCT ps;
      		
              HDC hdc = BeginPaint(hwnd, &ps);
      
              
      
             
      		 SetRect(&rc,x, 20, 40, 40); 
      		FillRect(hdc, &rc, hbrGray); 
      		SetRect(&rc2,60, 60, 80, 80); 
      		FillRect(hdc, &rc2, hbrGray); 
      
              EndPaint(hwnd, &ps);
          }
          return 0;
      
      }
      return DefWindowProc(hwnd, uMsg, wParam, lParam);
      

      }
      void GameUpdate()
      {

      x = x +1;
      

      }

      [Edit] it looks like WM_PAINT isn`t triggered every frame, only if I minimize and then maximize the window a change takes place

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      I think you need an "event"; like a timer tick; in order to create a "frame loop" (update and paint).

      "Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I

      1 Reply Last reply
      0
      • C Calin Negru

        I`m trying to animate things in a basic window. The code I have should get a rectangle (rc) move to the right however that does not happen, it`s all static.

        LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
        {
        static RECT rcBmp;
        static HDC hdcCompat;
        static HBITMAP hbmp;
        static RECT rc;
        static RECT rc2;
        static RECT rc3;
        static RECT rc4;
        HBRUSH hbrWhite, hbrGray;
        const COLORREF SomeColor = RGB(255,255,255);
        const COLORREF SomeColor2 = RGB(0,0,1);
        hbrWhite = CreateSolidBrush(SomeColor);
        hbrGray = CreateSolidBrush(SomeColor2);

        int y = 0;
        HDC hdc = (HDC) wParam; 
        
        
        switch (uMsg)
        {
        	case WM\_CREATE: 
        	
        	return 0L; 
        

        case WM_ERASEBKGND:

        GetClientRect(hwnd, &rc); 
        SetMapMode(hdc, MM\_ANISOTROPIC); 
        SetWindowExtEx(hdc, 100, 100, NULL); 
        SetViewportExtEx(hdc, rc.right, rc.bottom, NULL); 
        FillRect(hdc, &rc, hbrWhite); 
        
        
           
           
        return 0;
        
        case WM\_DESTROY:
            PostQuitMessage(0);
            return 0;
        
        	
               
        case WM\_PAINT:
            {
                PAINTSTRUCT ps;
        		
                HDC hdc = BeginPaint(hwnd, &ps);
        
                
        
               
        		 SetRect(&rc,x, 20, 40, 40); 
        		FillRect(hdc, &rc, hbrGray); 
        		SetRect(&rc2,60, 60, 80, 80); 
        		FillRect(hdc, &rc2, hbrGray); 
        
                EndPaint(hwnd, &ps);
            }
            return 0;
        
        }
        return DefWindowProc(hwnd, uMsg, wParam, lParam);
        

        }
        void GameUpdate()
        {

        x = x +1;
        

        }

        [Edit] it looks like WM_PAINT isn`t triggered every frame, only if I minimize and then maximize the window a change takes place

        G Offline
        G Offline
        Graham Breach
        wrote on last edited by
        #3

        Windows is event-driven, it doesn't have regular animation frame updates. You can use SetTimer() to get WM_TIMER messages as often as you want, then invalidate your window to get the WM_PAINT message and repaint. GDI performance is not very good though, so to reduce flicker you'll probably want to draw to an offscreen compatible device context and then BitBlt() that to the screen. Or use Direct2D - but then you'll have a new problem to think about.

        1 Reply Last reply
        0
        • C Calin Negru

          I`m trying to animate things in a basic window. The code I have should get a rectangle (rc) move to the right however that does not happen, it`s all static.

          LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
          {
          static RECT rcBmp;
          static HDC hdcCompat;
          static HBITMAP hbmp;
          static RECT rc;
          static RECT rc2;
          static RECT rc3;
          static RECT rc4;
          HBRUSH hbrWhite, hbrGray;
          const COLORREF SomeColor = RGB(255,255,255);
          const COLORREF SomeColor2 = RGB(0,0,1);
          hbrWhite = CreateSolidBrush(SomeColor);
          hbrGray = CreateSolidBrush(SomeColor2);

          int y = 0;
          HDC hdc = (HDC) wParam; 
          
          
          switch (uMsg)
          {
          	case WM\_CREATE: 
          	
          	return 0L; 
          

          case WM_ERASEBKGND:

          GetClientRect(hwnd, &rc); 
          SetMapMode(hdc, MM\_ANISOTROPIC); 
          SetWindowExtEx(hdc, 100, 100, NULL); 
          SetViewportExtEx(hdc, rc.right, rc.bottom, NULL); 
          FillRect(hdc, &rc, hbrWhite); 
          
          
             
             
          return 0;
          
          case WM\_DESTROY:
              PostQuitMessage(0);
              return 0;
          
          	
                 
          case WM\_PAINT:
              {
                  PAINTSTRUCT ps;
          		
                  HDC hdc = BeginPaint(hwnd, &ps);
          
                  
          
                 
          		 SetRect(&rc,x, 20, 40, 40); 
          		FillRect(hdc, &rc, hbrGray); 
          		SetRect(&rc2,60, 60, 80, 80); 
          		FillRect(hdc, &rc2, hbrGray); 
          
                  EndPaint(hwnd, &ps);
              }
              return 0;
          
          }
          return DefWindowProc(hwnd, uMsg, wParam, lParam);
          

          }
          void GameUpdate()
          {

          x = x +1;
          

          }

          [Edit] it looks like WM_PAINT isn`t triggered every frame, only if I minimize and then maximize the window a change takes place

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          You need to call InvalidateRect function (winuser.h) - Win32 apps | Microsoft Docs[^] each time the object's position changes. The WM_PAINT message is only sent when Windows decides that the view needs repainting. Calling InvalidateRect tells it to do this.

          C 1 Reply Last reply
          0
          • L Lost User

            You need to call InvalidateRect function (winuser.h) - Win32 apps | Microsoft Docs[^] each time the object's position changes. The WM_PAINT message is only sent when Windows decides that the view needs repainting. Calling InvalidateRect tells it to do this.

            C Offline
            C Offline
            Calin Negru
            wrote on last edited by
            #5

            I had in mind switching my videogame from DirectX to GDI. After some attempts at getting a win app working the way I need it and failing I will probably give up on the idea. I should probably keep using DirectX. Thanks for your help Richard and everyone else. Sorry for taking your time, this GDI idea is just a dead end.

            L 1 Reply Last reply
            0
            • C Calin Negru

              I had in mind switching my videogame from DirectX to GDI. After some attempts at getting a win app working the way I need it and failing I will probably give up on the idea. I should probably keep using DirectX. Thanks for your help Richard and everyone else. Sorry for taking your time, this GDI idea is just a dead end.

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              I would suggest you remove that link from your message (and future ones), as the system assumes it is spam. And some people here will just flag the message which means it will not get published.

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups