Problem in redrawing Rectangle on "Up Arrow Key Down" in MFC Dialog Based application
-
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
-
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
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
-
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
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 codeArindam 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 withInvalidateRect(...);
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] -
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 codeArindam 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 withInvalidateRect(...);
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]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
-
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
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]