Crect pass to CWnd, possible?
-
hi guys, i have problem on my GDI drawing (car) when i try to add a button control or any other control, i lose the focus of my drawing. here is the scenario, when i press SPACE my car moves forward frm left to right. but when i tried adding a button for colorPicker purpose, mar car won't move when i try to press SPACE on my ketborad.. tried to remove the button it worked fine. how do i set the focus to my GDI car drawing after selecting a color of the car? or should i say what code to use? i tried to use
CWnd* pWnd = myCar.rec;
pWnd->SetFocus();obviously it's error
\lg.manuel\daily quests\gdicarsample\gdicarsample\gdicarsampledlg.cpp(115) : error C2440: 'initializing' : cannot convert from 'CRect' to 'CWnd *'
1> No user-defined-conversion operator available that can perform this conversion, or the operator cannot be calledsince car is Crect i can't pass it to CWnd to set the focus, thanks ahead guys,
-
hi guys, i have problem on my GDI drawing (car) when i try to add a button control or any other control, i lose the focus of my drawing. here is the scenario, when i press SPACE my car moves forward frm left to right. but when i tried adding a button for colorPicker purpose, mar car won't move when i try to press SPACE on my ketborad.. tried to remove the button it worked fine. how do i set the focus to my GDI car drawing after selecting a color of the car? or should i say what code to use? i tried to use
CWnd* pWnd = myCar.rec;
pWnd->SetFocus();obviously it's error
\lg.manuel\daily quests\gdicarsample\gdicarsample\gdicarsampledlg.cpp(115) : error C2440: 'initializing' : cannot convert from 'CRect' to 'CWnd *'
1> No user-defined-conversion operator available that can perform this conversion, or the operator cannot be calledsince car is Crect i can't pass it to CWnd to set the focus, thanks ahead guys,
-
How did you handle SPACE key press? The focus may be on button and abviously it will take the SPACe key input. In OnButtonClick handler, To kill focus from button, call SetFocus() on main parent window. You may use like this->SetFocus().
here is my code for SPACE bar hanlder CGDICarSampleDlg.cpp
void CGDICarSampleDlg::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
// TODO: Add your message handler code here and/or call default
if (nChar == 8) {
myCar.Backward();
// force a repaint of the canvass
Invalidate();} else if (nChar == 32) { myCar.Forward(); // force a repaint of the canvass Invalidate(); } else if (nChar == 80){ CColorDialog dlg; if (dlg.DoModal() == IDOK) { color = dlg.GetColor(); } red = GetRValue(color); green = GetGValue(color); blue = GetBValue(color); char rr\[32\]; itoa(red, rr, 10); r = rr; char gg\[32\]; itoa(green, gg, 10); g = gg; char bb\[32\]; itoa(blue, bb, 10); b = bb; myCar.m\_brCar.Detach(); myCar.m\_brCar.CreateSolidBrush(RGB(red, green, blue)); WritePrivateProfileString("car", "red", r, /\*D:\\lg.manuel\\daily quests\\GDICarSample\\GDICarSample\\\*/"car.ini"); WritePrivateProfileString("car", "green", g, /\*D:\\lg.manuel\\daily quests\\GDICarSample\\GDICarSample\\\*/"car.ini"); WritePrivateProfileString("car", "blue", b, /\*D:\\lg.manuel\\daily quests\\GDICarSample\\GDICarSample\\\*/"car.ini"); myCar.PickColor(); //force a repaint of the canvass Invalidate(); } else { TRACE( "others" ); } CDialog::OnKeyDown(nChar, nRepCnt, nFlags);
}
and here is the command triggered Car.cpp
void CCar::Forward()
{
m_nCarX += 5;
angleFlag = angleFlag + 1;}
void CCar::Backward()
{
m_nCarX -= 3;
angleFlag = angleFlag - 1;
}if i SetFocus on the Main Dlg Window, will it kill the focus of the button?
-
here is my code for SPACE bar hanlder CGDICarSampleDlg.cpp
void CGDICarSampleDlg::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
// TODO: Add your message handler code here and/or call default
if (nChar == 8) {
myCar.Backward();
// force a repaint of the canvass
Invalidate();} else if (nChar == 32) { myCar.Forward(); // force a repaint of the canvass Invalidate(); } else if (nChar == 80){ CColorDialog dlg; if (dlg.DoModal() == IDOK) { color = dlg.GetColor(); } red = GetRValue(color); green = GetGValue(color); blue = GetBValue(color); char rr\[32\]; itoa(red, rr, 10); r = rr; char gg\[32\]; itoa(green, gg, 10); g = gg; char bb\[32\]; itoa(blue, bb, 10); b = bb; myCar.m\_brCar.Detach(); myCar.m\_brCar.CreateSolidBrush(RGB(red, green, blue)); WritePrivateProfileString("car", "red", r, /\*D:\\lg.manuel\\daily quests\\GDICarSample\\GDICarSample\\\*/"car.ini"); WritePrivateProfileString("car", "green", g, /\*D:\\lg.manuel\\daily quests\\GDICarSample\\GDICarSample\\\*/"car.ini"); WritePrivateProfileString("car", "blue", b, /\*D:\\lg.manuel\\daily quests\\GDICarSample\\GDICarSample\\\*/"car.ini"); myCar.PickColor(); //force a repaint of the canvass Invalidate(); } else { TRACE( "others" ); } CDialog::OnKeyDown(nChar, nRepCnt, nFlags);
}
and here is the command triggered Car.cpp
void CCar::Forward()
{
m_nCarX += 5;
angleFlag = angleFlag + 1;}
void CCar::Backward()
{
m_nCarX -= 3;
angleFlag = angleFlag - 1;
}if i SetFocus on the Main Dlg Window, will it kill the focus of the button?
One option is to allow the user to trigger the colour pick button by mouse click only. On Key down events, process the movements of your car. Override PreTranslateMessage() of your window to handle all key down events.
//button click handler
void CGDICarSampleDlg::OnBnClickedButtonColour()
{
// colour picker code here
}BOOL CGDICarSampleDlg::PreTranslateMessage(MSG* pMsg)
{
// TODO: Add your specialized code here and/or call the base classif(WM\_KEYDOWN == pMsg->message) { KeyDownHandler(pMsg->wParam); return TRUE; } return CDialog::PreTranslateMessage(pMsg);
}
void CGDICarSampleDlg::KeyDownHandler(UINT nChar)
{
if (nChar == 8)
{
//move car backward
}
else if (nChar == 32)
{
//move car backward
}//other cases..
}
-
One option is to allow the user to trigger the colour pick button by mouse click only. On Key down events, process the movements of your car. Override PreTranslateMessage() of your window to handle all key down events.
//button click handler
void CGDICarSampleDlg::OnBnClickedButtonColour()
{
// colour picker code here
}BOOL CGDICarSampleDlg::PreTranslateMessage(MSG* pMsg)
{
// TODO: Add your specialized code here and/or call the base classif(WM\_KEYDOWN == pMsg->message) { KeyDownHandler(pMsg->wParam); return TRUE; } return CDialog::PreTranslateMessage(pMsg);
}
void CGDICarSampleDlg::KeyDownHandler(UINT nChar)
{
if (nChar == 8)
{
//move car backward
}
else if (nChar == 32)
{
//move car backward
}//other cases..
}
i just tried this code:
BOOL CGDICarSampleDlg::PreTranslateMessage(MSG* pMsg)
{
// TODO: Add your specialized code here and/or call the base classif(WM\_KEYDOWN == pMsg->message) { OnKeyDown(pMsg->wParam); return TRUE; } return CDialog::PreTranslateMessage(pMsg);
}
got an error saying
error C2660: 'CGDICarSampleDlg::OnKeyDown' : function does not take 1 arguments
maybe because OnKeyDown needs three not just one, on my code below notice that the OnKeyDown event has 3 return values,. CGDICarSampleDlg.cpp
void CGDICarSampleDlg::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
// TODO: Add your message handler code here and/or call default
if (nChar == 8) {
myCar.Backward();
// force a repaint of the canvass
Invalidate();} else if (nChar == 32) { myCar.Forward(); // force a repaint of the canvass Invalidate(); } else { TRACE( "others" ); } CDialog::OnKeyDown(nChar, nRepCnt, nFlags);
}
declaration of OnKeyDown ... CGDICarSampleDlg.h
afx_msg void OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags);
-
i just tried this code:
BOOL CGDICarSampleDlg::PreTranslateMessage(MSG* pMsg)
{
// TODO: Add your specialized code here and/or call the base classif(WM\_KEYDOWN == pMsg->message) { OnKeyDown(pMsg->wParam); return TRUE; } return CDialog::PreTranslateMessage(pMsg);
}
got an error saying
error C2660: 'CGDICarSampleDlg::OnKeyDown' : function does not take 1 arguments
maybe because OnKeyDown needs three not just one, on my code below notice that the OnKeyDown event has 3 return values,. CGDICarSampleDlg.cpp
void CGDICarSampleDlg::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
// TODO: Add your message handler code here and/or call default
if (nChar == 8) {
myCar.Backward();
// force a repaint of the canvass
Invalidate();} else if (nChar == 32) { myCar.Forward(); // force a repaint of the canvass Invalidate(); } else { TRACE( "others" ); } CDialog::OnKeyDown(nChar, nRepCnt, nFlags);
}
declaration of OnKeyDown ... CGDICarSampleDlg.h
afx_msg void OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags);
hey that is not the WM_KEYDOWN message handler. You don't need it anymore. :doh:
BOOL CGDICarSampleDlg::PreTranslateMessage(MSG* pMsg)
{
if(WM_KEYDOWN == pMsg->message)
{
//OnKeyDown(pMsg->wParam); //???
KeyDownHandler(pMsg->wParam); //call my KeyDownHandler
return TRUE;
}return CDialog::PreTranslateMessage(pMsg);
}
//This is my KeyDownHandler, add this function to your class.
void CGDICarSampleDlg::KeyDownHandler(UINT nChar)
{
if (nChar == 8)
{
//move car backward
}
else if (nChar == 32)
{
//move car backward
}//other cases..
}
-
hey that is not the WM_KEYDOWN message handler. You don't need it anymore. :doh:
BOOL CGDICarSampleDlg::PreTranslateMessage(MSG* pMsg)
{
if(WM_KEYDOWN == pMsg->message)
{
//OnKeyDown(pMsg->wParam); //???
KeyDownHandler(pMsg->wParam); //call my KeyDownHandler
return TRUE;
}return CDialog::PreTranslateMessage(pMsg);
}
//This is my KeyDownHandler, add this function to your class.
void CGDICarSampleDlg::KeyDownHandler(UINT nChar)
{
if (nChar == 8)
{
//move car backward
}
else if (nChar == 32)
{
//move car backward
}//other cases..
}
yup i did that already but it seems that i still get the same error.. KeyDownHandler does not take 1 argument... you might want to check the sample project, here's the link and take a look... i did not include the button yet.. run and see.. http://hotfile.com/dl/68100441/675ae33/GDICarSample.rar.html[^] meybe am just too dumb to get your point, haha, sory.. take a peek at my code to understand what i want do do.. thanks for help man, appreciate it.
modified on Monday, September 13, 2010 11:56 PM
-
yup i did that already but it seems that i still get the same error.. KeyDownHandler does not take 1 argument... you might want to check the sample project, here's the link and take a look... i did not include the button yet.. run and see.. http://hotfile.com/dl/68100441/675ae33/GDICarSample.rar.html[^] meybe am just too dumb to get your point, haha, sory.. take a peek at my code to understand what i want do do.. thanks for help man, appreciate it.
modified on Monday, September 13, 2010 11:56 PM
-
http://hotfile.com/dl/68105506/4e54fa7/cp.rar.html[^] replace these files. :|
thanks man, i got alot of hints to get my car going on track. it's working fine man. your code works well..