CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Apr 2008
    Posts
    8

    sendmessage on button click

    I have a button which has dual functionality based on whether it is clicked for a short duration or long duration. I have subclassed the button (MYbutton.cpp)
    where i have a ON_WM_LBUTTONDOWN() and ON_WM_LBUTTONUP(). Here are the codes:

    void CMyButton::OnLButtonDown(UINT nFlags, CPoint point)
    {
    dwTickCount1= GetTickCount();
    }
    void CMyButton::OnLButtonUp(UINT nFlags, CPoint point)
    {

    dwTickCount2= GetTickCount();
    if ((dwTickCount2 - dwTickCount1)>500)
    {
    // send message to button that it was long pressed
    }
    else

    //Send message to button that it was short pressed
    }


    Then I have a main dialogue box called Qphonedialog.cpp where there are several buttons of the type Mybutton.cpp with the functions ON_BN_CLICKED for each of the buttons of the type Mybutton.cpp

    The Question is How do i send short clicked or long clicked from Mybutton.cpp to the respective onbutton clicked function?

    I hope I made my problem clear,
    _____________________________________________
    My idea was to :
    1. Getfocus to optain handle of the button that was recently clicked.
    2. getctrlId to get the ID of the button tht was clicked.
    3. Sendmessage to the respective button
    I wonder if there is a way to do this?
    _____________________________________________________
    Thanks in advance

    swapna

  2. #2
    Join Date
    Feb 2005
    Posts
    2,160

    Re: sendmessage on button click

    It seems to me that you should just send a custom message to the parent dialog and handle that rather than using the ON_BN_CLICKED notification. You even went so far as to leave a placeholder for the message: "//Send message to button..."

    Replace that comment with:
    Code:
    GetParent()->SendMessage(WM_USER+1,0,0);  //short click
    GetParent()->SendMessage(WM_USER+1,1,0);  //long click
    then in your dialog, add a message handler for WM_USER+1:

    Code:
    BEGIN_MESSAGE_MAP(/*whatever goes here*/)
      ON_MESSAGE(WM_USER+1,MyClickHandler)
    END_MESSAGE_MAP
    
    AfxMsg LRESULT MyClickHandler(WPARAM wParam,LPARAM lParam);
    
    LRESULT MyClickHandler(WPARAM wParam,LPARAM lParam)
    {
      if(wParam==0){
        //do something for short click
        return TRUE;
      }
      if(wParam==1){
        //do something for long click
        return TRUE;
      }
      return FALSE;
    }

  3. #3
    Join Date
    Apr 2008
    Posts
    725

    Re: sendmessage on button click

    may I mini-hijack and ask what might very well be a stupid question: what is a long-click and a short-click?

  4. #4
    Join Date
    Nov 2007
    Posts
    613

    Re: sendmessage on button click

    Quote Originally Posted by swapnacasa
    I hope I made my problem clear
    Sadly I find your explanations in the best case very foggy.

    Quote Originally Posted by swapnacasa
    I have subclassed the button (MYbutton.cpp)
    My understanding is that you instantiated your button using a class derived from CButton. Subclassing is something else.

    Quote Originally Posted by swapnacasa
    The Question is How do i send short clicked or long clicked from Mybutton.cpp to the respective onbutton clicked function?
    You cannot send messages to functions, you can send them only to windows.

    What I understand (I'm not sure, maybe I'm wrong) is that you have in the dialog box parent a function named OnButton you want to be called after a click is made.

    You don't need messages to do that. From the CMyButton::OnLButtonUp function obtain a pointer to the parent (let's name it pParentDlg), then simply call the function using:
    pParentDlg->OnButton();
    Make sure the OnButton function is declared public.

    BTW, the long and short click seem to me a very bad design choice. Whenever is possible use a standard behaviour for the user interface. Most users will not read your help file before using your application and will be frustrated to not be able to get what they want. But even after reading the help some may be still confused. As you can see from this thread some people are still asking what short and long clicks are even after you explained that.
    Not to mention that usually the users handle the UI hastily, it's painful for them to wait with the mouse button pressed.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured