I've faced with strange behaviour of a home ribbon button.
I've created standard MFC application in Visual Studio 2010 with Office template that has a ribbon control. But if I double click on the Home ribbon button at the upper position the application is closed.
Could you please tell me if it is standard MFC application handlers behaviour and how I can change it?
I've looked at Prevent double click on MFC-Dialog button but couldn't apply it to my case (more clearly - I don't know how to add double click handler to a ribbon home button).
CMFCRibbonApplicationButton is not derived from CWnd so cannot handle WM_LBUTTONDBLCLK message.
One solution is to derive from CMFCRibbonBar.
class CCustomRibbonBar : public CMFCRibbonBar
{
// ...
protected:
DECLARE_MESSAGE_MAP()
afx_msg void OnLButtonDblClk(UINT nFlags, CPoint point);
};
BEGIN_MESSAGE_MAP(CCustomRibbonBar, CMFCRibbonBar)
ON_WM_LBUTTONDBLCLK()
END_MESSAGE_MAP()
void CCustomRibbonBar::OnLButtonDblClk(UINT nFlags, CPoint point)
{
CMFCRibbonBaseElement* pHit = HitTest(point);
if (pHit->IsKindOf(RUNTIME_CLASS(CMFCRibbonApplicationButton)))
{
// the user double-clicked in the application button
// do what you want here but do not call CMFCRibbonBar::OnLButtonDblClk
return;
}
CMFCRibbonBar::OnLButtonDblClk(nFlags, point);
}
Another solution: override PreTranslateMessage in CMainFrame class;
BOOL CMainFrame::PreTranslateMessage(MSG* pMsg)
{
if ((WM_LBUTTONDBLCLK == pMsg->message) && (pMsg->hwnd == m_wndRibbonBar))
{
CPoint point(pMsg->pt);
m_wndRibbonBar.ScreenToClient(&point);
CMFCRibbonBaseElement* pHit = m_wndRibbonBar.HitTest(point);
if (pHit && pHit->IsKindOf(RUNTIME_CLASS(CMFCRibbonApplicationButton)))
{
// do what you want but do not call CMDIFrameWndEx::PreTranslateMessage
return TRUE; // no further dispatch
}
}
return CMDIFrameWndEx::PreTranslateMessage(pMsg);
}
Derive your own derived class of CMFCRibbonApplicationButton.
Create a message handler for CMFCRibbonApplicationButton::OnLButtonDblClk
Provide your own implementation of what you want to do on the double click. If nothing should happen, just leave the body empty.
In your CMainFrame you find a definition of CMFCRibbonApplicationButton m_MainButton. Replace the class name with your implementation.
Related
I am trying to add a picture control in a mfc dialog. I want to get the x,y coordinates of the pixel when I click on the picture control. I am using this code as a reference code and following this discussion to detect the click on the picture control.
A separate class has been derived for the picture control and PreTranslateMessage has been implemented.
BOOL CPictureCtrl::PreTranslateMessage(MSG* pMsg)
{
// TODO: Add your specialized code here and/or call the base class
if (pMsg->message == WM_LBUTTONDOWN && GetDlgItem(IDC_STATIC_BITMAP)->GetSafeHwnd() == pMsg->hwnd)
{
CPoint point(pMsg->pt);
ScreenToClient(&point);
OnLButtonDown(pMsg->wParam, point); //passes the coordinates of the clicked Point in the dialog box
}
return CStatic::PreTranslateMessage(pMsg);
}
In the main Dialog class I am calling the OnLButtonDown as follows
void CCPictureCtrlDemoDlg::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CRect rect;
m_picCtrl.GetWindowRect(&rect);
ScreenToClient(&rect);
if (rect.PtInRect(point))
{
// Do something
}
CDialog::OnLButtonDown(nFlags, point);
}
PreTranslateMessage is only triggered when i hover the mouse over the picture control, it does not trigger when i click on the picture. and the OnLButtonDown of the main dialog is does not trigger when
I click on the picture control, it only triggers when I click anywhere other than the picture control.
Thanks.
BCN_HOTITEMCHANGE notification code is not getting called with CMFCButton class.
I'm using Visual Studio 2010
Steps performed:
1) Add a CButton to my dialog from the resource editor.
2) Use my own derived class CMFCButtonEx derived from CMFCButton class (which in turn is derived from CButton class).
3) Declare CMFCButtonEx 'm_btn_ex' instance variable to my dialog.
4) Add DDX_Control(pDX, IDC_BUTTON_EX, m_btn_ex);
5) When I click this button all my message handlers in the derived class are called except BCN_HOTITEMCHANGE notification associated with OnButtonItemChange function. The breakpoint in that function never hits. All the other message handlers (OnMouseMove, OnMouseLeave, and OnButtonClicked) are called correctly, and the breakpoints in them get hit.
As a note if I derive by CMFCButtonEx class from CButton class, then I can hit the breakpoint in OnButtonItemChange function i.e. BCN_HOTITEMCHANGE notification code is handled correctly
For some reason my BCN_HOTITEMCHANGE notification code is not being handled correctly by CMFCButton class. I can't understand what am I doing wrong here. I have also tried to use TBN_HOTITEMCHANGE notification code instead of BCN_HOTITEMCHANGE but the breakpoint in the OnButtonItemChange function never hits.
Thanks!
CMFCButtonEx class code:
IMPLEMENT_DYNAMIC(CMFCButtonEx, CMFCButton)
CMFCButtonEx::CMFCButtonEx()
{
m_bMouseTracking = FALSE
}
CMFCButtonEx::~CMFCButtonEx()
{
}
BEGIN_MESSAGE_MAP(CMFCButtonEx, CMFCButton)
//{{AFX_MSG_MAP(CMFCButtonEx)
ON_WM_MOUSEMOVE()
ON_WM_MOUSELEAVE()
ON_NOTIFY_REFLECT_EX(BCN_HOTITEMCHANGE, OnButtonItemChange)
//ON_NOTIFY_REFLECT_EX(TBN_HOTITEMCHANGE, OnButtonItemChange)
ON_CONTROL_REFLECT_EX(BN_CLICKED, OnButtonClicked)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
BOOL CMFCButtonEx::OnButtonClicked()
{
AfxMessageBox("MFC Button Clicked");
return FALSE;
}
BOOL CMFCButtonEx::OnButtonItemChange(NMHDR* pNMHDR, LRESULT* pResult)
{
NMBCHOTITEM* pnmbchotitem = (NMBCHOTITEM*)pNMHDR;
AfxMessageBox("MFC Button Item Change");
*pResult = 0;
return FALSE;
}
void CMFCButtonEx::OnMouseMove(UINT nFlags, CPoint point)
{
if (!m_bMouseTracking)
{
TRACKMOUSEEVENT tme;
tme.cbSize = sizeof(TRACKMOUSEEVENT);
tme.dwFlags = TME_LEAVE;
tme.hwndTrack = this->m_hWnd;
if (::_TrackMouseEvent(&tme))
{
m_bMouseTracking = TRUE;
AfxMessageBox("MFC Button Mouse Move");
}
}
CMFCButton::OnMouseMove(nFlags, point);
}
void CMFCButtonEx::OnMouseLeave()
{
m_bMouseTracking = FALSE;
AfxMessageBox("MFC Button Mouse Move");
CMFCButton::OnMouseLeave();
}
BCN_HOTITEMCHANGE is not triggered for CMFCButton, or any classes derived from CMFCButton
As stated in documentation, Visual Style must be enabled otherwise BCN_HOTITEMCHANGE is not triggered. CMFCButton relies on owner draw method, so visual style is disabled for this button.
You could remove BS_OWNERDRAW from your CMFCButtonEx class. But this is not recommended because CMFCButtonEx relies on BS_OWNERDRAW.
A better solution is derive your class from CButton (and make sure Visual Styles is enabled), use custom draw instead of owner draw. You may not need OnMouseMove and OnMouseLeave.
class CMyButton : public CButton {}
...
BOOL CMyButton::OnButtonItemChange(NMHDR* pNMHDR, LRESULT* pResult)
{
auto ptr = (NMBCHOTITEM*)pNMHDR;
bool enter = ptr->dwFlags & HICF_ENTERING;
if (enter)
TRACE(L"CMyButton... enter\n");
else
TRACE(L"CMyButton... exit\n");
*pResult = 0;
return FALSE;
}
I am currently working on finishing some code handed off to me. It was written in MFC in Visual Studio 2005 years ago, was put on hold, and now is brought to me.
While I know C++, I have spent the last ~2 months studying the code and learning MFC and it's starting to come together.
The GUI for the code is an SWF flash file embedded in an invisible dialog window. I do not have the source code for the SWF file so will probably, in the future, redo it in WPF or something. I have the WMMode set to "Window" because in Transparent/Opaque mode it doesn't display properly, where it flashes/blinks everytime a mouse event is captured.
Anyhow, in Win XP/Vista, clicking and dragging the flash control works. In windows 7/8.1, it won't move.
Happy to provide any and all info needed. I'm still a little overwhelmed by MFC dialogs so unsure what you'd all like to see.
I found this question: Moving window by click-drag on a control
Which looks like it solves a lot of the issue. However, I don't want the whole control to be clickable like this, only the top part. Unfortunately, in the MS Resource view, the ActiveX control is blank as the SWF is only loaded at runtime; I've tried to find resources for this kind of thing but it's very difficult as I am unsure of the technical terms to use.
EDIT
I have attempted this by creating a very simple MFC app that has a Static Text control and nothing else. I am trying to get it to work by clicking on the static text (though I may be painting myself into a corner as it does not have a built-in lButtonDown event).
Here is the relevant code:
class MyDialog : public CDialog
{
public:
MyDialog(CWnd* pParent = NULL) : CDialog(MyDialog::IDD, pParent)
{ }
// Dialog Data, name of dialog form
enum{ IDD = INTERFACE1 };
protected:
virtual void DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); }
//Called right after constructor. Initialize things here.
virtual BOOL OnInitDialog()
{
CDialog::OnInitDialog();
pText = (CStatic *)GetDlgItem(ID_TEXT);
pText->SetWindowTextW(_T("Hello World!"));
return true;
}
afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
private:
CStatic * pText;
public:
DECLARE_MESSAGE_MAP()
};
BEGIN_MESSAGE_MAP(MyDialog, CDialog)
ON_WM_LBUTTONDOWN()
END_MESSAGE_MAP()
Overridden Method:
afx_msg void MyDialog::OnLButtonDown(UINT nFlags, CPoint point)
{
CWnd::OnNcLButtonDown(HTCAPTION, point);
}
I have also tried setting nFlags to 0x2, calling OnLButtonDown (as opposed to onNcLButtonDown), various other things. The message fires but the window does not move (it does move from the title bar, as normal). What am I missing?
Actually lets try this code instead with ON_WM_NCHITTEST(). This will drag the dialog if you click the mouse anywhere in client area (client area acts as caption). There is a line rc.bottom = rc.top + 100 if you uncomment it then it will only drag if you click the top section (I picked the number 100 at random).
//declare:
afx_msg LRESULT OnNcHitTest(CPoint point);
BEGIN_MESSAGE_MAP(MyDialog, CDialog)
ON_WM_NCHITTEST()
END_MESSAGE_MAP()
LRESULT MyDialog::OnNcHitTest(CPoint point)
{
ScreenToClient(&point);
CRect rc;
GetClientRect(&rc);
//rc.bottom = rc.top + 100;
if (rc.PtInRect(point))
return HTCAPTION;
return CDialog::OnNcHitTest(point);
}
Second option:
If we want to move the dialog by clicking on a child control, and if that control captures the mouse, then try this method instead. ***Note, test to make sure the control works properly after it is moved.
BOOL MyDialog::PreTranslateMessage(MSG *msg)
{
if (msg->message == WM_MOUSEMOVE && (msg->wParam & MK_LBUTTON))
{
CPoint p;
GetCursorPos(&p);
CRect r;
ActiveX->GetWindowRect(&r);
if (r.PtInRect(p))
{
ReleaseCapture();
SendMessage(WM_NCLBUTTONDOWN, HTCAPTION, 0);
SendMessage(WM_NCLBUTTONUP, HTCAPTION, 0);
return 1;
}
}
return CDialogEx::PreTranslateMessage(msg);
}
My application is a VC6 MFC dialog based application with multiple property pages.
I have to capture a mousemove event over a control, for example Checkbox.
How can I capture the mousemove events over a checkbox in MFC?
A checkbox is a button control (eg. CWnd). Derive your own class from CCheckBox and handle the OnMouseMove event.
Per request...assuming a class derived from CButton...
BEGIN_MESSAGE_MAP(CMyCheckBox, CButton)
ON_WM_MOUSEMOVE()
END_MESSAGE_MAP()
void CMyCheckBox::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CButton::OnMouseMove(nFlags, point);
}
Thanks for your replies.. I found a way to get the mousemove event for my app.
WM_SETCURSOR windows message gets the mouse move. It returns the Cwnd pointer for a control and the dialog.
Find my code below.
BOOL CMyDialog::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message)
{
CWnd* pWndtooltip = GetDlgItem(IDC_STATIC_TOOLTIP);
if (pWnd != this)
{
if (IDC_SN_START_ON == pWnd->GetDlgCtrlID())
pWndtooltip->ShowWindow(SW_SHOW);
}
else
pWndtooltip->ShowWindow(SW_HIDE);
SetCursor(AfxGetApp()->LoadStandardCursor(IDC_ARROW));
return true;
}
I found in #raj's OnSetCursor() code, that the associated Member variable for IDC_STATIC_TOOLTIP is that variable to which you assign the desired tool tip text. For example, if the associated variable is m_strToolTip, assign the desired text to display during the hovering event as follows:
m_strToolTip.Format("%s", "Tool tip text goes here");
I also found that UpdateData() was required upon entry into the event handler and UpdateData(FALSE) was required before the return. The SetCursor() call seems to have no effect when commented.
You can also override CDialog::PreTranslateMessage:
BOOL CSomeDlg::PreTranslateMessage(MSG* pMsg)
{
if (pMsg->message == WM_MOUSEMOVE && pMsg->hwnd == m_checkBox->m_hWnd)
{
...
}
return CDialog::PreTranslateMessage(pMsg);
}
I am using a MFC link control in my dialog based application,and I add an event handler of BN_CLICKED for it, hoping that it could do something for me, however, when i click on it, it just does nothing at all(when i don't set the url of it, when i set the url, it will jump to that url), my event handler is not triggered. So, how to disable its default "jump" behaviour and trigger my handler?
Subclassing CMFCLinkCtrl and adding an ON_WM_LBUTTONDOWN event handler seems to work.
You can then choose whether or not to call CMFCLinkCtrl::OnLButtonDown.
class CMyLinkCtrl : public CMFCLinkCtrl {
DECLARE_DYNAMIC(CMyLinkCtrl)
public:
CMyLinkCtrl();
virtual ~CMyLinkCtrl();
afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
protected:
DECLARE_MESSAGE_MAP()
};
IMPLEMENT_DYNAMIC(CMyLinkCtrl, CMFCLinkCtrl)
CMyLinkCtrl::CMyLinkCtrl() {
}
CMyLinkCtrl::~CMyLinkCtrl() {
}
BEGIN_MESSAGE_MAP(CMyLinkCtrl, CMFCLinkCtrl)
ON_WM_LBUTTONDOWN()
END_MESSAGE_MAP()
void CMyLinkCtrl::OnLButtonDown(UINT nFlags, CPoint point) {
static bool bDisabled = false;
if(bDisabled) {
MessageBox(_T("Link is disabled"));
} else {
CMFCLinkCtrl::OnLButtonDown(nFlags, point);
}
bDisabled = !bDisabled;
}