CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 19
  1. #1
    Join Date
    Jan 2002
    Location
    United Kingdom
    Posts
    491

    Quick Button Clicks

    Hi,

    I have a button (MFC CButton object) which would very possibly want to be clicked repetively (quite fast) and produce its result on all clicks. By default clicking the button once will do its event, the second click does nothing, then the third click does, etc. I think that the double-click message might be causing problems, does anybody know of a fix?

    Regards,
    Lea Hayes

  2. #2
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656
    If you do not want Double clicks, remove CS_DBLCLKS style from your Window class.
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinWindows - replacement windows manager for Visual Studio, and more...

  3. #3
    Join Date
    Jan 2002
    Location
    United Kingdom
    Posts
    491
    Hi,

    Thanks for your help, would you be able to show me how to do this please?

    Best Regards,
    Lea Hayes

  4. #4
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656
    You'd have to derive your own class for that control (was it a button?), and override PreCreateWindow() function like that:
    Code:
    BOOL CYourWnd::PreCreateWindow(CREATESTRUCT& cs) 
    {
    	if (!CWnd::PreCreateWindow(cs))
    		return FALSE;
    
    	cs.dwExStyle |= WS_EX_CLIENTEDGE;
    	cs.style &= ~WS_BORDER;
    	cs.lpszClass = AfxRegisterWndClass(CS_HREDRAW|CS_VREDRAW, 
    		::LoadCursor(NULL, IDC_ARROW), reinterpret_cast<HBRUSH>(COLOR_WINDOW+1), NULL);
    
    	return TRUE;
    }
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinWindows - replacement windows manager for Visual Studio, and more...

  5. #5
    Join Date
    Jan 2002
    Location
    United Kingdom
    Posts
    491
    Hi,

    Thanks for your help!

    Regards,
    Lea Hayes

  6. #6
    Join Date
    Dec 2001
    Location
    Bremen, Germany
    Posts
    314
    Hi VladimirF

    Just had the same problem, thank you for the tip of CS_DBLCLKS style. I think that I found a solution without deriving my own class:
    DWORD dwStyle = ::GetClassLong(m_hwndButton, GCL_STYLE);
    dwStyle &= ~CS_DBLCLKS;
    ::SetClassLong(m_hwndButton, GCL_STYLE, dwStyle);
    That's better for my case; maybe someone else needs it too.

    Oliver

  7. #7
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656
    Hi Oliver,
    your solution will work ONLY if you want to change double-click handling for all windows of that class. For example, if you want to disable it for one button, all buttons will be affected.
    If this works for your particular situation - than your way is simpler.
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinWindows - replacement windows manager for Visual Studio, and more...

  8. #8
    Join Date
    Dec 2001
    Location
    Bremen, Germany
    Posts
    314
    Hi VladimirF

    Yes, you're right... it works well for my situation, but it is not generally desireable. Thanks for this note, I'll better keep it in mind!

    Oliver.

  9. #9
    Join Date
    Jan 2004
    Posts
    97
    Hi VladimirF. I also tried your code for my button on status bar but my button seems like a white edit box and I cannot change its text. the button has the main dialog as parent window(I could not attach it to statusbar window since I could not get CWnd of status bar). How can I make the button work. It is only to show a popup menu. Thanks

  10. #10
    Join Date
    Jan 2002
    Location
    United Kingdom
    Posts
    491
    Hi,

    I have found another solution for those interested; if you create a custom button (derived from CButton) and override the DefWindowProc virtual function, and place the following in its body; then that also fixes the problem:

    Code:
    	if(message == WM_LBUTTONDBLCLK)
    		message = WM_LBUTTONDOWN;
    	
    	return CButton::DefWindowProc(message, wParam, lParam);
    Best Regards,
    Lea Hayes

  11. #11
    Join Date
    Dec 2001
    Location
    Bremen, Germany
    Posts
    314
    Hi lhayes00

    Funny, I just found a quite similar solution and wanted to post it. My attempt was to handle WM_LBUTTONDBLCLK
    Code:
    void CMyButton::OnLButtonDblClk(UINT nFlags, CPoint point)
    {
    	// send normal left-click message
    	SendMessage(WM_LBUTTONDOWN, nFlags, MAKELPARAM(point.x, point.y));
    }
    Oliver.

  12. #12
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656
    Hi Lea and Oliver,
    although what you did might work in your particular case, you are playing dangerous game by replacing one window's message with another.
    The double click could have been generated as a result of BOTH WM_LBUTTONDOWN and WM_LBUTTONUP processing, and you are only forwarding button down.
    If a client is relying on getting button up for each button down, he is going to wait for a long time...
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinWindows - replacement windows manager for Visual Studio, and more...

  13. #13
    Join Date
    Dec 2001
    Location
    Bremen, Germany
    Posts
    314
    Hi VladimirF

    Right. So can we "fix" this by sending WM_LBUTTONDOWN and WM_LBUTTONUP?

    Oliver.

  14. #14
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656
    Originally posted by Oliver Twesten
    Right. So can we "fix" this by sending WM_LBUTTONDOWN and WM_LBUTTONUP?
    Not really. It wasn't my point. I said "could have been".
    Your first suggestion will work. If you read remarks from MSDN:
    Only windows that have the CS_DBLCLKS style can receive WM_LBUTTONDBLCLK messages, which the system generates whenever the user presses, releases, and again presses the left mouse button within the system's double-click time limit. Double-clicking the left mouse button actually generates a sequence of four messages: WM_LBUTTONDOWN, WM_LBUTTONUP, WM_LBUTTONDBLCLK, and WM_LBUTTONUP.
    All I am saying is that you should not substitute one message with another, especially if you are not sure.
    My point was that double click messages are controlled by documented style, and if you do not want to receive them - do not set that style.
    Of course, you could find a hack that will work...
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinWindows - replacement windows manager for Visual Studio, and more...

  15. #15
    Join Date
    Dec 2001
    Location
    Bremen, Germany
    Posts
    314
    Hi VladimirF

    Sorry if this sounds as if I just do not want to try your solution. The point is, that I can not get it to work for my special case. Maybe you can help me and I'll surely use the better / cleaner approach!

    I have already derived my own button class that does a lot of painting things with my buttons. I use this ownerdrawn button on almost all dialog templates in my project and need to disable the double-clicks for only 2 buttons.
    And I thought - as long as I find a hack that can solve this situation for the 2 buttons - I do not want to implement many new stuff.

    Would you mind telling me exactly what to do in my situation? OnCreate will not be called for the button class - I think I'm missing something here. Do I have to re-create the whole button functionality and derive from CWnd?

    Thank you!

    Oliver.

Page 1 of 2 12 LastLast

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