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. Is there any way to SendMessage to a Button control

Is there any way to SendMessage to a Button control

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
11 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.
  • L Offline
    L Offline
    lamparded
    wrote on last edited by
    #1

    Just SendMessge to the Button IDC_BUTTON1 and make sure the button will be clicked what's the code ? thanks for help!

    _ 1 Reply Last reply
    0
    • L lamparded

      Just SendMessge to the Button IDC_BUTTON1 and make sure the button will be clicked what's the code ? thanks for help!

      _ Offline
      _ Offline
      _Superman_
      wrote on last edited by
      #2

      If you're not using MFC, the following would be the code, where hDlg is the handle to the dialog hosting the button -

      ::SendMessage(hDlg, WM_COMMAND, (BN_CLICKED << 16) | IDC_BUTTON1, ::GetDlgItem(hDlg, IDC_BUTTON1);

      If you're using MFC, the following would be the version if you're calling it from within the dialog class -

      SendMessage(WM_COMMAND, (BN_CLICKED << 16) | IDC_BUTTON1, GetDlgItem(IDC_BUTTON1));

      «_Superman_»  _I love work. It gives me something to do between weekends.

      _Microsoft MVP (Visual C++)

      Polymorphism in C

      L J 2 Replies Last reply
      0
      • _ _Superman_

        If you're not using MFC, the following would be the code, where hDlg is the handle to the dialog hosting the button -

        ::SendMessage(hDlg, WM_COMMAND, (BN_CLICKED << 16) | IDC_BUTTON1, ::GetDlgItem(hDlg, IDC_BUTTON1);

        If you're using MFC, the following would be the version if you're calling it from within the dialog class -

        SendMessage(WM_COMMAND, (BN_CLICKED << 16) | IDC_BUTTON1, GetDlgItem(IDC_BUTTON1));

        «_Superman_»  _I love work. It gives me something to do between weekends.

        _Microsoft MVP (Visual C++)

        Polymorphism in C

        L Offline
        L Offline
        lamparded
        wrote on last edited by
        #3

        ERROR,parameter 3... SendMessage(WM_COMMAND, ((WPARAM)BN_CLICKED)<<8|(WPARAM)IDC_BUTTON, 0L); is that right? I want the Button will be clicked down when the Message send,but there's nothing happened with the Button while the Message sent ,why

        H L 2 Replies Last reply
        0
        • L lamparded

          ERROR,parameter 3... SendMessage(WM_COMMAND, ((WPARAM)BN_CLICKED)<<8|(WPARAM)IDC_BUTTON, 0L); is that right? I want the Button will be clicked down when the Message send,but there's nothing happened with the Button while the Message sent ,why

          H Offline
          H Offline
          Hans Dietrich
          wrote on last edited by
          #4

          If you want to emulate a human pressing a button, then you will have to perform the actions a human performs: 1. Press button: send WM_LBUTTONDOWN 2. Hold button down a while: use SetTimer() or similar to implement delay. 3. Release button: send WM_LBUTTONUP.

          Best wishes, Hans


          [Hans Dietrich Software]

          1 Reply Last reply
          0
          • L lamparded

            ERROR,parameter 3... SendMessage(WM_COMMAND, ((WPARAM)BN_CLICKED)<<8|(WPARAM)IDC_BUTTON, 0L); is that right? I want the Button will be clicked down when the Message send,but there's nothing happened with the Button while the Message sent ,why

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            Why did you not use the code that Superman posted? Re-read his answer and select the appropriate one (MFC or non-MFC) and use copy & paste to add to your project.

            The best things in life are not things.

            L 1 Reply Last reply
            0
            • L Lost User

              Why did you not use the code that Superman posted? Re-read his answer and select the appropriate one (MFC or non-MFC) and use copy & paste to add to your project.

              The best things in life are not things.

              L Offline
              L Offline
              lamparded
              wrote on last edited by
              #6

              I got a error with it in my MFC app Error 1 error C2664: 'CWnd::SendMessageW' : cannot convert parameter 3 from 'CWnd *' to 'LPARAM'

              L 1 Reply Last reply
              0
              • L lamparded

                I got a error with it in my MFC app Error 1 error C2664: 'CWnd::SendMessageW' : cannot convert parameter 3 from 'CWnd *' to 'LPARAM'

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #7

                Just use a cast thus:

                SendMessage(p1, p2, (LPARAM)p3 ...

                You should really study and understand these basics of C++ before embarking on advanced projects such as you describe.

                The best things in life are not things.

                L 1 Reply Last reply
                0
                • L Lost User

                  Just use a cast thus:

                  SendMessage(p1, p2, (LPARAM)p3 ...

                  You should really study and understand these basics of C++ before embarking on advanced projects such as you describe.

                  The best things in life are not things.

                  L Offline
                  L Offline
                  lamparded
                  wrote on last edited by
                  #8

                  yea,I use

                  SendMessage(WM_COMMAND,(BN_CLICKED)<<16|IDC_BUTTON1,(LPARAM)GetDlgItem(IDC_BUTTON1));

                  it's passed in Compiling but pop up a Assertion Failded when running

                  > mfc100ud.dll!CWnd::OnCommand(unsigned int wParam, long lParam) Line 2708 + 0x27 bytes C++

                  so I changed it in Release Configuration,no Assertion Failed pop up ,why?

                  B L 2 Replies Last reply
                  0
                  • L lamparded

                    yea,I use

                    SendMessage(WM_COMMAND,(BN_CLICKED)<<16|IDC_BUTTON1,(LPARAM)GetDlgItem(IDC_BUTTON1));

                    it's passed in Compiling but pop up a Assertion Failded when running

                    > mfc100ud.dll!CWnd::OnCommand(unsigned int wParam, long lParam) Line 2708 + 0x27 bytes C++

                    so I changed it in Release Configuration,no Assertion Failed pop up ,why?

                    B Offline
                    B Offline
                    barneyman
                    wrote on last edited by
                    #9

                    asserts don't get compiled in release code - they are a debugging tool and have little value in release code - i.e. the problem is still there in release code, you just didn't get a popup In that code nugget the assert could be caused by an invalid window, are your sure that the window you are in is the immediate parent of the button you'd normally be in a CDialog... class for this to work

                    1 Reply Last reply
                    0
                    • L lamparded

                      yea,I use

                      SendMessage(WM_COMMAND,(BN_CLICKED)<<16|IDC_BUTTON1,(LPARAM)GetDlgItem(IDC_BUTTON1));

                      it's passed in Compiling but pop up a Assertion Failded when running

                      > mfc100ud.dll!CWnd::OnCommand(unsigned int wParam, long lParam) Line 2708 + 0x27 bytes C++

                      so I changed it in Release Configuration,no Assertion Failed pop up ,why?

                      L Offline
                      L Offline
                      Lost User
                      wrote on last edited by
                      #10

                      What are the values of wParam and lParam, and what assertion did you receive?

                      The best things in life are not things.

                      1 Reply Last reply
                      0
                      • _ _Superman_

                        If you're not using MFC, the following would be the code, where hDlg is the handle to the dialog hosting the button -

                        ::SendMessage(hDlg, WM_COMMAND, (BN_CLICKED << 16) | IDC_BUTTON1, ::GetDlgItem(hDlg, IDC_BUTTON1);

                        If you're using MFC, the following would be the version if you're calling it from within the dialog class -

                        SendMessage(WM_COMMAND, (BN_CLICKED << 16) | IDC_BUTTON1, GetDlgItem(IDC_BUTTON1));

                        «_Superman_»  _I love work. It gives me something to do between weekends.

                        _Microsoft MVP (Visual C++)

                        Polymorphism in C

                        J Offline
                        J Offline
                        jeff6
                        wrote on last edited by
                        #11

                        NO. Just send BM_CLICK (!)

                        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