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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Crect pass to CWnd, possible?

Crect pass to CWnd, possible?

Scheduled Pinned Locked Moved C / C++ / MFC
graphicshelpquestionc++
9 Posts 2 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.
  • L Offline
    L Offline
    loid grey manuel
    wrote on last edited by
    #1

    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 called

    since car is Crect i can't pass it to CWnd to set the focus, thanks ahead guys,

    C 1 Reply Last reply
    0
    • L loid grey manuel

      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 called

      since car is Crect i can't pass it to CWnd to set the focus, thanks ahead guys,

      C Offline
      C Offline
      Cool_Dev
      wrote on last edited by
      #2

      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().

      L 1 Reply Last reply
      0
      • C Cool_Dev

        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().

        L Offline
        L Offline
        loid grey manuel
        wrote on last edited by
        #3

        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?

        C 1 Reply Last reply
        0
        • L loid grey manuel

          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?

          C Offline
          C Offline
          Cool_Dev
          wrote on last edited by
          #4

          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 class

          if(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..
          

          }

          L 1 Reply Last reply
          0
          • C Cool_Dev

            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 class

            if(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..
            

            }

            L Offline
            L Offline
            loid grey manuel
            wrote on last edited by
            #5

            i just tried this code:

            BOOL CGDICarSampleDlg::PreTranslateMessage(MSG* pMsg)
            {
            // TODO: Add your specialized code here and/or call the base class

            if(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);

            C 1 Reply Last reply
            0
            • L loid grey manuel

              i just tried this code:

              BOOL CGDICarSampleDlg::PreTranslateMessage(MSG* pMsg)
              {
              // TODO: Add your specialized code here and/or call the base class

              if(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);

              C Offline
              C Offline
              Cool_Dev
              wrote on last edited by
              #6

              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..
              

              }

              L 1 Reply Last reply
              0
              • C Cool_Dev

                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..
                

                }

                L Offline
                L Offline
                loid grey manuel
                wrote on last edited by
                #7

                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

                C 1 Reply Last reply
                0
                • L loid grey manuel

                  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

                  C Offline
                  C Offline
                  Cool_Dev
                  wrote on last edited by
                  #8

                  http://hotfile.com/dl/68105506/4e54fa7/cp.rar.html[^] replace these files. :|

                  L 1 Reply Last reply
                  0
                  • C Cool_Dev

                    http://hotfile.com/dl/68105506/4e54fa7/cp.rar.html[^] replace these files. :|

                    L Offline
                    L Offline
                    loid grey manuel
                    wrote on last edited by
                    #9

                    thanks man, i got alot of hints to get my car going on track. it's working fine man. your code works well..

                    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