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. How to create CButton dynamically in MFC?

How to create CButton dynamically in MFC?

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++tutorial
4 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.
  • X Offline
    X Offline
    xo3000
    wrote on last edited by
    #1

    I am used to create objects in MFC by draging icons and connect to MessageMap by suing Class Wizard. Now I want to create CButton object dynamically as when I type "20" in a Edit then click a button to create 20 buttons in my dialog. But....how(where) do I put the point of these 20 buttons ? And .. If I've created these 20 buttons, how do I connect these buttons to Message Map ? I have no idea about these question ... Gave me a hand , please .... :)

    D 1 Reply Last reply
    0
    • X xo3000

      I am used to create objects in MFC by draging icons and connect to MessageMap by suing Class Wizard. Now I want to create CButton object dynamically as when I type "20" in a Edit then click a button to create 20 buttons in my dialog. But....how(where) do I put the point of these 20 buttons ? And .. If I've created these 20 buttons, how do I connect these buttons to Message Map ? I have no idea about these question ... Gave me a hand , please .... :)

      D Offline
      D Offline
      DasdaDAS
      wrote on last edited by
      #2

      Hi, U can use the following method to create the buttons dynamically. In the following statement "but" is the CButton object which is declared as public class variable. if(!but.Create("click me",WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,CRect(10,10,100,30),this,ID_MYBUTTON)) AfxMessageBox("Button Not Created"); else AfxMessageBox("Button Created"); The argument ID_MYBUTTON is a macro and should be replaced with some integer values like below. #define ID_MYBUTTON 100 Then the message handling mechanism. First declare the member function in the class like as below. void MyMessage(); In the message map macro add the following step. This should be in the message map of the class in which u r going to create the button. ON_BN_CLICKED(ID_MYBUTTON,MyMessage) and define the function, i.e., MyMessage(). finally make sure that, ur member function and data members are displayed in the class view. Hit the execution icon, and check the result. Hope this will be little drilling for the people who doesn't have much knowledge about mfc and its message hook mechanism. Let me know if u have more doubts. thanks, Zxczc

      X 1 Reply Last reply
      0
      • D DasdaDAS

        Hi, U can use the following method to create the buttons dynamically. In the following statement "but" is the CButton object which is declared as public class variable. if(!but.Create("click me",WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,CRect(10,10,100,30),this,ID_MYBUTTON)) AfxMessageBox("Button Not Created"); else AfxMessageBox("Button Created"); The argument ID_MYBUTTON is a macro and should be replaced with some integer values like below. #define ID_MYBUTTON 100 Then the message handling mechanism. First declare the member function in the class like as below. void MyMessage(); In the message map macro add the following step. This should be in the message map of the class in which u r going to create the button. ON_BN_CLICKED(ID_MYBUTTON,MyMessage) and define the function, i.e., MyMessage(). finally make sure that, ur member function and data members are displayed in the class view. Hit the execution icon, and check the result. Hope this will be little drilling for the people who doesn't have much knowledge about mfc and its message hook mechanism. Let me know if u have more doubts. thanks, Zxczc

        X Offline
        X Offline
        xo3000
        wrote on last edited by
        #3

        Hello ,Zxczc Thank you for your reply I think I forgot to express an important factor. Before I input a number in the Edit and click the create Button ,the AP doesn't know how many Button it need to create. I don't have a good sense, so that it is what I mean "dynamically". Dose it possible to do? Or I must to decide the maximum number of the button in source code, then hide the useless button? Is that the only way ? or there is another better method? Thank you :-D

        J 1 Reply Last reply
        0
        • X xo3000

          Hello ,Zxczc Thank you for your reply I think I forgot to express an important factor. Before I input a number in the Edit and click the create Button ,the AP doesn't know how many Button it need to create. I don't have a good sense, so that it is what I mean "dynamically". Dose it possible to do? Or I must to decide the maximum number of the button in source code, then hide the useless button? Is that the only way ? or there is another better method? Thank you :-D

          J Offline
          J Offline
          jhwurmbach
          wrote on last edited by
          #4

          xo3000 wrote: Dose it possible to do? Sure it is possible! But you are very much on your own here, without the Wizard and the MFC macros. MFC may good enough for your little sister, but Win32 is for real men, you know? :rolleyes: Create the Buttons you need, and catch their BN_CLICKED notification. That is done by overriding PreTranslateMessage and waiting for a WM_COMMAND message with a HIWORD(wParam) being BN_CLICKED. The LOWORD(wParam) is the identifier (the last number of the CREATE-statement) and the lParam the buttons handle. Now you have full control over the button.


          "We trained hard, but it seemed that every time we were beginning to form up into teams we would be reorganised. I was to learn later in life that we tend to meet any new situation by reorganising: and a wonderful method it can be for creating the illusion of progress, while producing confusion, inefficiency and demoralisation." -- Caius Petronius, Roman Consul, 66 A.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