CDialog上的可滚动CStatic

Scrollable CStatic on CDialog

本文关键字:滚动 CStatic CDialog      更新时间:2023-10-16

我有一个CStatic图片控件在一个CDialog,我用它来绘制内容:

CMyDrawingControl.h

CMyDrawingControl : CStatic
{
   //Constructor, Destructor, other items
   //End Constructor, Destructor, other items
public:
   void DrawStuff(CDC *dc);
protected:
   afx_msg void OnPaint();
   afx_msg void OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
}

CMyDrawingControl.cpp

CMyDrawingControl::CMyDrawingControl
{
}
BEGIN_MESSAGE_MAP(CMyDrawingControl, CStatic)
//{{AFX_MSG_MAP(CMyDrawingControl)
ON_WM_VSCROLL()
ON_WM_PAINT()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
void CMyDrawingControl::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
    //determine delta
    ScrollWindow(0, -delta);
    Invalidate();
    UpdateWindow();
}
void CMyDrawingControl::OnPaint()
{
    CPaint dc(this);
    DrawStuff(&dc);
}
void CMyDrawingControl::DrawStuff(CDC *dc)
{
    dc->SetMapMode(MM_LOMETRIC);
    //draw on dc
    //text, lines, shapes, etc
}

然而,内容通常比控件大,所以我需要能够滚动内容。CScrollView通过在OnDraw中绘制视图自动处理,但我似乎无法让它在OnPaint()中工作。滚动时,控件要么绘制空白,要么有很多重复的内容。

我基本上试图复制CScrollView对CDialog的确切行为;我看到过一些帖子接近这个,但我不想实现一个CDocument和一个CView。

我认为使用只读编辑控件要简单得多。

除了滚动,这也使得用户可以选择和复制部分文本。