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. HELP! Anyone know MFC here?

HELP! Anyone know MFC here?

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++help
13 Posts 6 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.
  • N Nitron

    You need to handle the ON_BN_CLICKED message generated by the button. The easiest way to do this is to use the class wizard to automagically generate the code for you. Then in your message handler do

    AfxMessageBox(_T("Your Message here."));

    - Nitron


    "Those that say a task is impossible shouldn't interrupt the ones who are doing it." - Chinese Proverb

    B Offline
    B Offline
    Bigsteiny
    wrote on last edited by
    #3

    Thanks, but i'm not sure that helps. The classwizard doesn't offer any mapping abilities for my object... In the View.h i have... CButton myButton; then in the cpp i have myButton.Create("Test", WS_CHILD, CRect(0,0,50,50), AfxGetMainWnd(), WM_USER+1); myButton.ShowWindow(SW_SHOW); If i go to the classwizard, under the "Message Maps" tab, i select the View as the Class Name, but then what? Theres no reference to my button anywhere. :confused: Thanks for the help though mate, i appreciate it. any more thoughts? -BigSteiny

    D 1 Reply Last reply
    0
    • B Bigsteiny

      Thanks, but i'm not sure that helps. The classwizard doesn't offer any mapping abilities for my object... In the View.h i have... CButton myButton; then in the cpp i have myButton.Create("Test", WS_CHILD, CRect(0,0,50,50), AfxGetMainWnd(), WM_USER+1); myButton.ShowWindow(SW_SHOW); If i go to the classwizard, under the "Message Maps" tab, i select the View as the Class Name, but then what? Theres no reference to my button anywhere. :confused: Thanks for the help though mate, i appreciate it. any more thoughts? -BigSteiny

      D Offline
      D Offline
      David Crow
      wrote on last edited by
      #4

      Bigsteiny wrote: If i go to the classwizard, under the "Message Maps" tab, i select the View as the Class Name, but then what? Theres no reference to my button anywhere. Nor should there be. ClassWizard is obviously not going to know about a control that is created at run-time. What is your view based on?

      B 1 Reply Last reply
      0
      • B Bigsteiny

        I need help. i must be such a retard i dont understand. I'm making an MFC SDI app, with a CButton object in the view. in OnInitialUpdate i am ::Create() ing the object fine, and ShowWindow() ing fine. The button appears... now how do i simply make an AfxMessageBox for when i click on this button!? I've read about subclassing and it seems like it might be the way to go but i just *cant* get it to work. Any help would be appreciated...

        T Offline
        T Offline
        Toni78
        wrote on last edited by
        #5

        The best way to do this is to create a class derived from CButton. class CMyButton : public CButton { // class members go here }; // end class And then in your MFC application you declare a member variable: CMyButton *pButton; Then you procceed by creating the button and showing it. To handle messages simply go to classwizard and select CMyButton. You will be able to see the list of messages that your class can handle. // Afterall I realized that even my comment lines have bugs

        B 1 Reply Last reply
        0
        • T Toni78

          The best way to do this is to create a class derived from CButton. class CMyButton : public CButton { // class members go here }; // end class And then in your MFC application you declare a member variable: CMyButton *pButton; Then you procceed by creating the button and showing it. To handle messages simply go to classwizard and select CMyButton. You will be able to see the list of messages that your class can handle. // Afterall I realized that even my comment lines have bugs

          B Offline
          B Offline
          Bigsteiny
          wrote on last edited by
          #6

          Arrggh! No good i'm afraid. I made a class called MyButton which inherits from CButton fine. made it ok. Create-ed it fine, ShowWindow-ed fine.... i even got the class wizard to apparently map the OnLButtonDown.. but when i click on it - it does nothing. The code never executes?! Help! :rolleyes: BEGIN_MESSAGE_MAP(MyButton, CButton) //{{AFX_MSG_MAP(MyButton) ON_WM_LBUTTONDOWN() //}}AFX_MSG_MAP END_MESSAGE_MAP() // Generated message map functions protected: //{{AFX_MSG(MyButton) afx_msg void OnLButtonDown(UINT nFlags, CPoint point); //}}AFX_MSG void MyButton::OnLButtonDown(UINT nFlags, CPoint point) { AfxMessageBox("You Clicked LButton on MyButton class"); CButton::OnLButtonDown(nFlags, point); } That's basically whats been added... but it doesn't work. Any thoughts? Thanks for the help.

          1 Reply Last reply
          0
          • D David Crow

            Bigsteiny wrote: If i go to the classwizard, under the "Message Maps" tab, i select the View as the Class Name, but then what? Theres no reference to my button anywhere. Nor should there be. ClassWizard is obviously not going to know about a control that is created at run-time. What is your view based on?

            B Offline
            B Offline
            Bigsteiny
            wrote on last edited by
            #7

            Based on? Um. It just used the AppWizard and made an SDI application. The control is created at run time, but it's declared at compile time... not sure if this makes a difference though... Thanks. -Az.;)

            D 1 Reply Last reply
            0
            • B Bigsteiny

              I need help. i must be such a retard i dont understand. I'm making an MFC SDI app, with a CButton object in the view. in OnInitialUpdate i am ::Create() ing the object fine, and ShowWindow() ing fine. The button appears... now how do i simply make an AfxMessageBox for when i click on this button!? I've read about subclassing and it seems like it might be the way to go but i just *cant* get it to work. Any help would be appreciated...

              R Offline
              R Offline
              Roger Allen
              wrote on last edited by
              #8

              Because you are creating the control using Create(), you are going to have to add the MESSAGE_MAP entries manually to get this to work. // in header file afx_msg void OnMyButton() in the MESSAGE_MAP ON_BN_CLICKED(ID_OF_CREATED_BUTTON, OnMyButton) void CYourClass::OnMyButton() { AfxMessageBox("I hope this works!"); } Roger Allen Sonork 100.10016 Were you different as a kid? Did you ever say "Ooohhh, shiny red" even once? - Paul Watson 11-February-2003

              B 1 Reply Last reply
              0
              • B Bigsteiny

                Based on? Um. It just used the AppWizard and made an SDI application. The control is created at run time, but it's declared at compile time... not sure if this makes a difference though... Thanks. -Az.;)

                D Offline
                D Offline
                David Crow
                wrote on last edited by
                #9

                Bigsteiny wrote: Based on? Um. It just used the AppWizard and made an SDI application. Yes, but what base class was the view derived from? If you did not change any of the defaults, it's probably going to be CView. Other types are CFormView, CListView, CEditView, CHtmlView, CRichEditView, CScrollView, and CTreeView. Besides the list control, are you going to want other controls displayed on the view?

                1 Reply Last reply
                0
                • R Roger Allen

                  Because you are creating the control using Create(), you are going to have to add the MESSAGE_MAP entries manually to get this to work. // in header file afx_msg void OnMyButton() in the MESSAGE_MAP ON_BN_CLICKED(ID_OF_CREATED_BUTTON, OnMyButton) void CYourClass::OnMyButton() { AfxMessageBox("I hope this works!"); } Roger Allen Sonork 100.10016 Were you different as a kid? Did you ever say "Ooohhh, shiny red" even once? - Paul Watson 11-February-2003

                  B Offline
                  B Offline
                  Bigsteiny
                  wrote on last edited by
                  #10

                  I must be doing something wrong but i don't know what. So here's all of it.. Thanks for the patience. ////////////////////////////////////////////////////////////////// // tstButtonView.h : interface of the CTstButtonView class #include "MyButton.h" class CTstButtonView : public CView { protected: // create from serialization only CTstButtonView(); DECLARE_DYNCREATE(CTstButtonView) // Attributes public: CTstButtonDoc* GetDocument(); MyButton* myButton; // Overrides // ClassWizard generated virtual function overrides //{{AFX_VIRTUAL(CTstButtonView) public: virtual void OnDraw(CDC* pDC); // overridden to draw this view virtual BOOL PreCreateWindow(CREATESTRUCT& cs); virtual void OnInitialUpdate(); //}}AFX_VIRTUAL // Implementation public: virtual ~CTstButtonView(); #ifdef _DEBUG virtual void AssertValid() const; virtual void Dump(CDumpContext& dc) const; #endif protected: // Generated message map functions protected: //{{AFX_MSG(CTstButtonView) // NOTE - the ClassWizard will add and remove member functions here. // DO NOT EDIT what you see in these blocks of generated code ! //}}AFX_MSG DECLARE_MESSAGE_MAP() }; //////////////////////////////////////////////////////////// // and in the view's cpp... void CTstButtonView::OnInitialUpdate() { CView::OnInitialUpdate(); myButton = new MyButton; myButton->Create("Test", WS_CHILD, CRect(0,0,50,50), AfxGetMainWnd(), WM_USER+1); myButton->ShowWindow(SW_SHOW); } ////////////////////////////////////////////////////////////////// // MyButton.h : header file // MyButton window class MyButton : public CButton { // Construction public: MyButton(); // Overrides // ClassWizard generated virtual function overrides //{{AFX_VIRTUAL(MyButton) //}}AFX_VIRTUAL // Implementation public: virtual ~MyButton(); // Generated message map functions protected: //{{AFX_MSG(MyButton) afx_msg void OnMyButton(); //}}AFX_MSG DECLARE_MESSAGE_MAP() }; ///////////////////////////////////////////////////////////////////////////// // MyButton.cpp file. MyButton::MyButton() { } MyButton::~MyButton() { } BEGIN_MESSAGE_MAP(MyButton, CButton) //{{AFX_MSG_MAP(MyButton) ON_BN_CLICKED(WM_USER+1, OnMyButton) //}}AFX_MSG_MAP END_MESSAGE_MAP() void MyButton::OnMyButton() { AfxMessageBox("My Function!"); }

                  M 2 Replies Last reply
                  0
                  • B Bigsteiny

                    I must be doing something wrong but i don't know what. So here's all of it.. Thanks for the patience. ////////////////////////////////////////////////////////////////// // tstButtonView.h : interface of the CTstButtonView class #include "MyButton.h" class CTstButtonView : public CView { protected: // create from serialization only CTstButtonView(); DECLARE_DYNCREATE(CTstButtonView) // Attributes public: CTstButtonDoc* GetDocument(); MyButton* myButton; // Overrides // ClassWizard generated virtual function overrides //{{AFX_VIRTUAL(CTstButtonView) public: virtual void OnDraw(CDC* pDC); // overridden to draw this view virtual BOOL PreCreateWindow(CREATESTRUCT& cs); virtual void OnInitialUpdate(); //}}AFX_VIRTUAL // Implementation public: virtual ~CTstButtonView(); #ifdef _DEBUG virtual void AssertValid() const; virtual void Dump(CDumpContext& dc) const; #endif protected: // Generated message map functions protected: //{{AFX_MSG(CTstButtonView) // NOTE - the ClassWizard will add and remove member functions here. // DO NOT EDIT what you see in these blocks of generated code ! //}}AFX_MSG DECLARE_MESSAGE_MAP() }; //////////////////////////////////////////////////////////// // and in the view's cpp... void CTstButtonView::OnInitialUpdate() { CView::OnInitialUpdate(); myButton = new MyButton; myButton->Create("Test", WS_CHILD, CRect(0,0,50,50), AfxGetMainWnd(), WM_USER+1); myButton->ShowWindow(SW_SHOW); } ////////////////////////////////////////////////////////////////// // MyButton.h : header file // MyButton window class MyButton : public CButton { // Construction public: MyButton(); // Overrides // ClassWizard generated virtual function overrides //{{AFX_VIRTUAL(MyButton) //}}AFX_VIRTUAL // Implementation public: virtual ~MyButton(); // Generated message map functions protected: //{{AFX_MSG(MyButton) afx_msg void OnMyButton(); //}}AFX_MSG DECLARE_MESSAGE_MAP() }; ///////////////////////////////////////////////////////////////////////////// // MyButton.cpp file. MyButton::MyButton() { } MyButton::~MyButton() { } BEGIN_MESSAGE_MAP(MyButton, CButton) //{{AFX_MSG_MAP(MyButton) ON_BN_CLICKED(WM_USER+1, OnMyButton) //}}AFX_MSG_MAP END_MESSAGE_MAP() void MyButton::OnMyButton() { AfxMessageBox("My Function!"); }

                    M Offline
                    M Offline
                    MAAK
                    wrote on last edited by
                    #11

                    You dont need to inherit from CButton to handle the click event of your button All you have to do is to add a button notification handler in the view class for the id of your button, and ensure that the button's id is unique in order not to conflict with other controls. This code should explain it:

                    //in the class declaration
                    class CTstButtonView : public CView
                    {
                    	.
                    	.
                    	CButton m_btn;
                    	afx_msg void MyButtonHandler();
                    	.
                    	.
                    };
                    
                    //this code is in the message map
                    BEGIN_MESSAGE_MAP(CTstButtonView , CView)
                    	.
                    	.
                    	ON_BN_CLICKED(ID_MYBUTTON, MyButtonHandler)
                    	.
                    	.
                    END_MESSAGE_MAP()
                    
                    //implementation of the OnInitialUpdate
                    void CTstButtonView::OnInitialUpdate() 
                    {
                    	CView::OnInitialUpdate();
                    	//if the button is not already created
                    	if(!m_btn.GetSafeHwnd())
                    		//create the button, WS_VISIBLE can be ised instead of using
                    		//ShowWindow
                    		m_btn.Create("TEST", WS_CHILD|WS_VISIBLE, CRect(0,0, 200, 100), this, ID_MYBUTTON);
                    }
                    //implementation of the button's handler funciton
                    void MyButtonHandler()
                    {
                    	AfxMessageBox("My Function!");
                    }
                    

                    note that in your code you made the parent window of the button is the main window which is the CMainFrame class, if you want it this way you may need to move the handler function and handler message map to the CMainFrame instead of the CView

                    1 Reply Last reply
                    0
                    • B Bigsteiny

                      I must be doing something wrong but i don't know what. So here's all of it.. Thanks for the patience. ////////////////////////////////////////////////////////////////// // tstButtonView.h : interface of the CTstButtonView class #include "MyButton.h" class CTstButtonView : public CView { protected: // create from serialization only CTstButtonView(); DECLARE_DYNCREATE(CTstButtonView) // Attributes public: CTstButtonDoc* GetDocument(); MyButton* myButton; // Overrides // ClassWizard generated virtual function overrides //{{AFX_VIRTUAL(CTstButtonView) public: virtual void OnDraw(CDC* pDC); // overridden to draw this view virtual BOOL PreCreateWindow(CREATESTRUCT& cs); virtual void OnInitialUpdate(); //}}AFX_VIRTUAL // Implementation public: virtual ~CTstButtonView(); #ifdef _DEBUG virtual void AssertValid() const; virtual void Dump(CDumpContext& dc) const; #endif protected: // Generated message map functions protected: //{{AFX_MSG(CTstButtonView) // NOTE - the ClassWizard will add and remove member functions here. // DO NOT EDIT what you see in these blocks of generated code ! //}}AFX_MSG DECLARE_MESSAGE_MAP() }; //////////////////////////////////////////////////////////// // and in the view's cpp... void CTstButtonView::OnInitialUpdate() { CView::OnInitialUpdate(); myButton = new MyButton; myButton->Create("Test", WS_CHILD, CRect(0,0,50,50), AfxGetMainWnd(), WM_USER+1); myButton->ShowWindow(SW_SHOW); } ////////////////////////////////////////////////////////////////// // MyButton.h : header file // MyButton window class MyButton : public CButton { // Construction public: MyButton(); // Overrides // ClassWizard generated virtual function overrides //{{AFX_VIRTUAL(MyButton) //}}AFX_VIRTUAL // Implementation public: virtual ~MyButton(); // Generated message map functions protected: //{{AFX_MSG(MyButton) afx_msg void OnMyButton(); //}}AFX_MSG DECLARE_MESSAGE_MAP() }; ///////////////////////////////////////////////////////////////////////////// // MyButton.cpp file. MyButton::MyButton() { } MyButton::~MyButton() { } BEGIN_MESSAGE_MAP(MyButton, CButton) //{{AFX_MSG_MAP(MyButton) ON_BN_CLICKED(WM_USER+1, OnMyButton) //}}AFX_MSG_MAP END_MESSAGE_MAP() void MyButton::OnMyButton() { AfxMessageBox("My Function!"); }

                      M Offline
                      M Offline
                      MAAK
                      wrote on last edited by
                      #12

                      Another note concerning the code of you derived CButton class. The code did not work becuase the BN_CLICKED message is not sent to the button, this message is sent to the parent of the button. Therefore the message is not sent to the button in the first place.

                      B 1 Reply Last reply
                      0
                      • M MAAK

                        Another note concerning the code of you derived CButton class. The code did not work becuase the BN_CLICKED message is not sent to the button, this message is sent to the parent of the button. Therefore the message is not sent to the button in the first place.

                        B Offline
                        B Offline
                        Bigsteiny
                        wrote on last edited by
                        #13

                        Right, i'd actually alread tried that method too... and it didn't work. I tried swapping the mapping from my custom class, to the view and still didn't work. The problem was... m_btn.Create("TEST", WS_CHILD|WS_VISIBLE, CRect(0,0, 200, 100), this, ID_MYBUTTON) as opposed to my m_btn.Create("TEST", WS_CHILD|WS_VISIBLE, CRect(0,0, 200, 100), AfxGetMainWnd(), ID_MYBUTTON) should have seen this. Thanks for the help everyone.:-D:-D:-D

                        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