[英]MFC: how to enable page change via mouse wheeling for print preview?
In print preview dialog, I would like to enable page change via mouse wheeling.在打印预览对话框中,我想通过鼠标滚动启用页面更改。 As I am still a beginner for MFC programming, I don't have code to start with.
由于我还是 MFC 编程的初学者,我没有代码可以开始。 The closest question I've found is this one (for C#) but no clear answer yet: https://www.codeproject.com/Questions/555242/5bc-23-5dplusprintpreviewdialogplusandplusmousewhe .
我发现的最接近的问题是这个(对于 C#),但还没有明确的答案: https://www.codeproject.com/Questions/555242/5bc-23-5dplusprintpreviewdialogplusandplusmousewhe 。
If you are using MFC's CPreviewView
class, then you can derive a custom class from that, in which you can override the OnMouseWheel
member.如果您使用的是 MFC 的
CPreviewView
class,那么您可以从中派生一个自定义的 class,您可以在其中覆盖OnMouseWheel
成员。 In your override, you would call the OnVScroll
handler to shift up or down, as though you had clicked on the up/down arrows of the scrollbar:在您的覆盖中,您将调用
OnVScroll
处理程序向上或向下移动,就像您单击了滚动条的向上/向下箭头一样:
BOOL MyPreviewView::OnMouseWheel(UINT /*flags*/, short delta, CPoint /*point*/)
{
OnVScroll(UINT((delta < 0) ? SB_LINEDOWN : SB_LINEUP), 0, nullptr);
return TRUE;
}
Also, you need to add ON_WM_MOUSEWHEEL()
to your derived class' message-map:此外,您需要将
ON_WM_MOUSEWHEEL()
添加到派生类的消息映射中:
BEGIN_MESSAGE_MAP(MyPreviewView, CPreviewView)
//...
ON_WM_MOUSEWHEEL()
//...
END_MESSAGE_MAP()
Feel free to ask for further clarification and/or explanation.随时要求进一步澄清和/或解释。
It is an addition to the existing answer of Adrian Mole's Answer这是对Adrian Mole's Answer现有答案的补充
If you are not using custom class already, this is the complete implementation of the class:如果您还没有使用自定义 class,这是 class 的完整实现:
MyPreviewViewEx.h我的PreviewViewEx.h
// CMyPreviewViewEx view
class CMyPreviewViewEx : public CPreviewViewEx
{
DECLARE_DYNCREATE(CMyPreviewViewEx)
protected:
CMyPreviewViewEx() {} // protected constructor used by dynamic creation
virtual ~CMyPreviewViewEx() {}
DECLARE_MESSAGE_MAP()
public:
afx_msg BOOL OnMouseWheel(UINT nFlags, short zDelta, CPoint pt);
static void MyPrintPreview(CView* pView);
};
MyPreviewViewEx.cpp我的PreviewViewEx.cpp
// CMyPreviewViewEx
IMPLEMENT_DYNCREATE(CMyPreviewViewEx, CPreviewViewEx)
BEGIN_MESSAGE_MAP(CMyPreviewViewEx, CPreviewViewEx)
ON_WM_MOUSEWHEEL()
END_MESSAGE_MAP()
// CMyPreviewViewEx message handlers
BOOL CMyPreviewViewEx::OnMouseWheel(UINT nFlags, short zDelta, CPoint pt)
{
OnVScroll(UINT((zDelta < 0) ? SB_LINEDOWN : SB_LINEUP), 0, nullptr);
return TRUE;
}
// Override AFXPrintPreview
void CMyPreviewViewEx::MyPrintPreview(CView* pView)
{
ASSERT_VALID(pView);
CPrintPreviewState *pState= new CPrintPreviewState;
if (!pView->DoPrintPreview(IDD_AFXBAR_RES_PRINT_PREVIEW, pView, RUNTIME_CLASS(CMyPreviewViewEx), pState))
{
TRACE0("Error: OnFilePrintPreview failed.\n");
AfxMessageBox(AFX_IDP_COMMAND_FAILURE);
delete pState; // preview failed to initialize, delete State now
}
}
In the view class, change as applicable在视图 class 中,根据需要进行更改
// AFXPrintPreview(pView);
CMyPreviewViewEx::MyPrintPreview(pView);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.