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. Problem in redrawing Rectangle on "Up Arrow Key Down" in MFC Dialog Based application

Problem in redrawing Rectangle on "Up Arrow Key Down" in MFC Dialog Based application

Scheduled Pinned Locked Moved C / C++ / MFC
helpc++
5 Posts 3 Posters 0 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.
  • A Offline
    A Offline
    Arindam Tewary
    wrote on last edited by
    #1

    Hi, I am writing a small program in MFC using Dialog based project. What I want to achive is : On pressing "Up Arrow Key" I want to redraw a rectangle which was already drawn.( want to draw the rectangle in other point). Can you please help me what I am doing wrong here. I am setting a variable "iKeyPressed" to "1" "PreTranslateMessage" method when Up Key is pressed.

    BOOL CKeyDownTestDlg::PreTranslateMessage(MSG* pMsg)
    {
    // TODO: Add your specialized code here and/or call the base class
    iKeyPressed = 0;
    if (pMsg->message == WM_KEYDOWN)
    {
    if(pMsg->wParam==VK_UP)
    {
    iKeyPressed = 1;
    CKeyDownTestDlg::OnPaint();
    //::AfxMessageBox(_T("Inside"));
    }
    }
    return CDialog::PreTranslateMessage(pMsg);
    }

    Then in OnPaint() method I am checking the value of "iKeyPressed". If that is "1", then I intend to "Redraw" (erasing out the previously drawn rectangle)rectangle which was drawn earlier.

    void CKeyDownTestDlg::OnPaint()
    {
    if (IsIconic())
    {
    //These code is not executed.
    }
    else
    {
    char c[10];

    	CPaintDC dc(this); // device context for painting
    	CString MFCString; 
    	MFCString = c;
    	if(iKeyPressed==0)
    	{
    		dc.Rectangle(5,5,100,20);
    		AfxMessageBox(\_T("iKeyPressed = 0"));     
    		/\*dc.SelectObject(old\_brush);\*/
    	}
    
    	if(iKeyPressed==1)
    	{
    		CPaintDC dc(this); // device context for painting
    		AfxMessageBox(\_T("iKeyPressed = 1"));      
    		dc.Rectangle(5,5,20,100);			
    	}
    	CDialog::OnPaint();
    }
    

    }

    Interestingly, while executing I could see Message "iKeyPressed = 1". Any help is appreciated.

    Thanks, Arindam D Tewary

    A CPalliniC 2 Replies Last reply
    0
    • A Arindam Tewary

      Hi, I am writing a small program in MFC using Dialog based project. What I want to achive is : On pressing "Up Arrow Key" I want to redraw a rectangle which was already drawn.( want to draw the rectangle in other point). Can you please help me what I am doing wrong here. I am setting a variable "iKeyPressed" to "1" "PreTranslateMessage" method when Up Key is pressed.

      BOOL CKeyDownTestDlg::PreTranslateMessage(MSG* pMsg)
      {
      // TODO: Add your specialized code here and/or call the base class
      iKeyPressed = 0;
      if (pMsg->message == WM_KEYDOWN)
      {
      if(pMsg->wParam==VK_UP)
      {
      iKeyPressed = 1;
      CKeyDownTestDlg::OnPaint();
      //::AfxMessageBox(_T("Inside"));
      }
      }
      return CDialog::PreTranslateMessage(pMsg);
      }

      Then in OnPaint() method I am checking the value of "iKeyPressed". If that is "1", then I intend to "Redraw" (erasing out the previously drawn rectangle)rectangle which was drawn earlier.

      void CKeyDownTestDlg::OnPaint()
      {
      if (IsIconic())
      {
      //These code is not executed.
      }
      else
      {
      char c[10];

      	CPaintDC dc(this); // device context for painting
      	CString MFCString; 
      	MFCString = c;
      	if(iKeyPressed==0)
      	{
      		dc.Rectangle(5,5,100,20);
      		AfxMessageBox(\_T("iKeyPressed = 0"));     
      		/\*dc.SelectObject(old\_brush);\*/
      	}
      
      	if(iKeyPressed==1)
      	{
      		CPaintDC dc(this); // device context for painting
      		AfxMessageBox(\_T("iKeyPressed = 1"));      
      		dc.Rectangle(5,5,20,100);			
      	}
      	CDialog::OnPaint();
      }
      

      }

      Interestingly, while executing I could see Message "iKeyPressed = 1". Any help is appreciated.

      Thanks, Arindam D Tewary

      A Offline
      A Offline
      Aescleal
      wrote on last edited by
      #2

      You need to understand how WM_PAINT and OnPaint works. If you call your OnPaint at an arbitrary time what happens is it creates a CPaintDC which won't (or rather probably won't) have any invalid region in it. So all your drawing is clipped to a non-existent region and nothing appears on the screen. Try calling Invalidate() instead of OnPaint() and see if that improves the situation. Can't remember the parameters off the top of me head so you'll have to look them up! This signals that you want to paint the whole window and will cause a redraw (i.e. your OnPaint()) will get called indirectly. When that happens CPaintDC will be set up properly. Oh, and only have one object of type CPaintDC in a given OnPaint method, you might get some strangeness otherwise. Cheers, Ash

      1 Reply Last reply
      0
      • A Arindam Tewary

        Hi, I am writing a small program in MFC using Dialog based project. What I want to achive is : On pressing "Up Arrow Key" I want to redraw a rectangle which was already drawn.( want to draw the rectangle in other point). Can you please help me what I am doing wrong here. I am setting a variable "iKeyPressed" to "1" "PreTranslateMessage" method when Up Key is pressed.

        BOOL CKeyDownTestDlg::PreTranslateMessage(MSG* pMsg)
        {
        // TODO: Add your specialized code here and/or call the base class
        iKeyPressed = 0;
        if (pMsg->message == WM_KEYDOWN)
        {
        if(pMsg->wParam==VK_UP)
        {
        iKeyPressed = 1;
        CKeyDownTestDlg::OnPaint();
        //::AfxMessageBox(_T("Inside"));
        }
        }
        return CDialog::PreTranslateMessage(pMsg);
        }

        Then in OnPaint() method I am checking the value of "iKeyPressed". If that is "1", then I intend to "Redraw" (erasing out the previously drawn rectangle)rectangle which was drawn earlier.

        void CKeyDownTestDlg::OnPaint()
        {
        if (IsIconic())
        {
        //These code is not executed.
        }
        else
        {
        char c[10];

        	CPaintDC dc(this); // device context for painting
        	CString MFCString; 
        	MFCString = c;
        	if(iKeyPressed==0)
        	{
        		dc.Rectangle(5,5,100,20);
        		AfxMessageBox(\_T("iKeyPressed = 0"));     
        		/\*dc.SelectObject(old\_brush);\*/
        	}
        
        	if(iKeyPressed==1)
        	{
        		CPaintDC dc(this); // device context for painting
        		AfxMessageBox(\_T("iKeyPressed = 1"));      
        		dc.Rectangle(5,5,20,100);			
        	}
        	CDialog::OnPaint();
        }
        

        }

        Interestingly, while executing I could see Message "iKeyPressed = 1". Any help is appreciated.

        Thanks, Arindam D Tewary

        CPalliniC Offline
        CPalliniC Offline
        CPallini
        wrote on last edited by
        #3

        Arindam Tewary wrote:

        BOOL CKeyDownTestDlg::PreTranslateMessage(MSG* pMsg) { // TODO: Add your specialized code here and/or call the base class iKeyPressed = 0;

        I think the iKeyPressed = 0 in the above code is you problem. Remove it and change the following piece of code

        Arindam Tewary wrote:

        if(iKeyPressed==1) { CPaintDC dc(this); // device context for painting AfxMessageBox(_T("iKeyPressed = 1")); dc.Rectangle(5,5,20,100); }

        This way

        if(iKeyPressed==1)
        {
        iKeyPressed = 0
        CPaintDC dc(this); // device context for painting
        AfxMessageBox(_T("iKeyPressed = 1"));
        dc.Rectangle(5,5,20,100);
        }

        BTW calling OnPaint directly is a bad idea: you should replace it with

        InvalidateRect(...);
        UpdateWindow();

        sequence. :)

        If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
        This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
        [My articles]

        In testa che avete, signor di Ceprano?

        A 1 Reply Last reply
        0
        • CPalliniC CPallini

          Arindam Tewary wrote:

          BOOL CKeyDownTestDlg::PreTranslateMessage(MSG* pMsg) { // TODO: Add your specialized code here and/or call the base class iKeyPressed = 0;

          I think the iKeyPressed = 0 in the above code is you problem. Remove it and change the following piece of code

          Arindam Tewary wrote:

          if(iKeyPressed==1) { CPaintDC dc(this); // device context for painting AfxMessageBox(_T("iKeyPressed = 1")); dc.Rectangle(5,5,20,100); }

          This way

          if(iKeyPressed==1)
          {
          iKeyPressed = 0
          CPaintDC dc(this); // device context for painting
          AfxMessageBox(_T("iKeyPressed = 1"));
          dc.Rectangle(5,5,20,100);
          }

          BTW calling OnPaint directly is a bad idea: you should replace it with

          InvalidateRect(...);
          UpdateWindow();

          sequence. :)

          If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
          This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
          [My articles]

          A Offline
          A Offline
          Arindam Tewary
          wrote on last edited by
          #4

          Thanks Mr. Palini for reply. I just started VC++ MFC couple of days back. I am still not very much comfortable in it after comming from C# background. So I directly called that OnPaint method. I would keep learning about it in comming days for sure. I used your suggestion but still it does not repaint it. :( . Any other idea sir?

          Thanks, Arindam D Tewary

          CPalliniC 1 Reply Last reply
          0
          • A Arindam Tewary

            Thanks Mr. Palini for reply. I just started VC++ MFC couple of days back. I am still not very much comfortable in it after comming from C# background. So I directly called that OnPaint method. I would keep learning about it in comming days for sure. I used your suggestion but still it does not repaint it. :( . Any other idea sir?

            Thanks, Arindam D Tewary

            CPalliniC Offline
            CPalliniC Offline
            CPallini
            wrote on last edited by
            #5

            You should remove the Message Boxes calls to see it. Also you're using too many device contexts in OnPaint. :)

            If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
            This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
            [My articles]

            In testa che avete, signor di Ceprano?

            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