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

User message

Scheduled Pinned Locked Moved C / C++ / MFC
question
3 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.
  • O Offline
    O Offline
    Oliver123
    wrote on last edited by
    #1

    My existing code is below. A global function which is not a member of any class is calling a function which is a member of class CNewDlg. This code works, but I don't like it. I'm using it because it's the best I have been able to do. I think it can be done better using a user message, but I don't know how user messages work and haven't been able to find any examples. How would I code this using a user message? void WINAPI HostChange(LinkPtr data) <---global; not in any class. A callback provides ptr data. { ((CNewDlg*)AfxGetApp()->m_pMainWnd)->LogonChange(data); } void CNewDlg::LogonChange(LinkPtr data) <---function in a dialog class { if(data->SomeField == 3){Do something...} }

    R G 2 Replies Last reply
    0
    • O Oliver123

      My existing code is below. A global function which is not a member of any class is calling a function which is a member of class CNewDlg. This code works, but I don't like it. I'm using it because it's the best I have been able to do. I think it can be done better using a user message, but I don't know how user messages work and haven't been able to find any examples. How would I code this using a user message? void WINAPI HostChange(LinkPtr data) <---global; not in any class. A callback provides ptr data. { ((CNewDlg*)AfxGetApp()->m_pMainWnd)->LogonChange(data); } void CNewDlg::LogonChange(LinkPtr data) <---function in a dialog class { if(data->SomeField == 3){Do something...} }

      R Offline
      R Offline
      Ravi Bhavnani
      wrote on last edited by
      #2

      Oliver123 wrote:

      This code works, but I don't like it.

      I'm with you on this! :) Here's an ultra-easy alternative, which while not perfect, has its advantages.

      1. Add 2 buttons (IDC_HOST_CHANGE and IDC_LOGON_CHANGE) to CNewDlg and make them invisible.

      2. Write a handler for each - the one for IDC_HOST_CHANGE should call HostChange() and the one for IDC_LOGON_CHANGE should call LogonChange().

      3. HostChange() and LogonChange() should get a pointer to an (perhaps the) instance of LinkPtr via a singleton access method exposed by your app class.

      4. Your static callbacks should post the appropriate message to your app's AfxGetMainWnd(). No casting to CNewDlg is required. For example:

        AfxGetMainWnd()->PostMessage (WM_COMMAND, IDC_HOST_CHANGE);

      In lieu of a registered message, this provides a clean separation between event generation by your application's innards and event handling by your application's user interface. /ravi

      This is your brain on Celcius Home | Music | Articles | Freeware | Trips ravib(at)ravib(dot)com

      1 Reply Last reply
      0
      • O Oliver123

        My existing code is below. A global function which is not a member of any class is calling a function which is a member of class CNewDlg. This code works, but I don't like it. I'm using it because it's the best I have been able to do. I think it can be done better using a user message, but I don't know how user messages work and haven't been able to find any examples. How would I code this using a user message? void WINAPI HostChange(LinkPtr data) <---global; not in any class. A callback provides ptr data. { ((CNewDlg*)AfxGetApp()->m_pMainWnd)->LogonChange(data); } void CNewDlg::LogonChange(LinkPtr data) <---function in a dialog class { if(data->SomeField == 3){Do something...} }

        G Offline
        G Offline
        Gary R Wheeler
        wrote on last edited by
        #3

        Here's a quick, general introduction to user-defined messages. I'm assuming you're using MFC here. 1. Define a message identifier. You can base it off of WM_USER or WM_APP:

        #define WM_MY_MESSAGE (WM_APP + 1)

        2. Declare a message handler in your dialog class:

        afx_msg LRESULT OnMyMessage(WPARAM wParam,LPARAM lParam);

        3. Add an entry to the message map for your dialog class:

        BEGIN_MESSAGE_MAP(...)
        // ...
        ON_MESSAGE(WM_MY_MESSAGE,OnMyMessage)
        // ...
        END_MESSAGE_MAP()

        4. Define your message handler:

        LRESULT MyDialogClass::OnMyMessage(WPARAM wParam,LPARAM lParam)
        {
        // ...
        }

        5. Finally, send the message when needed:

        my_dialog->SendMessage(WM_MY_MESSAGE,
        (WPARAM)first_parameter,
        (LPARAM)second_parameter);

        where first_parameter and second_parameter are whatever values you want to send with the message.


        Software Zen: delete this;

        Fold With Us![^]

        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