scrolling in Cwnd-derived class

Hello,

I'm having a problem with the implementation of scrollbars in a custom class, derived from CWnd.

I created an object from the class in the OnInitDialog() member function :

BOOL CMyOwnDlg::OnInitDialog()
{
CDialog::OnInitDialog();

CRect w;
m_placeholder.GetWindowRect(&w);
ScreenToClient(&w);

m_filter_wnd.Create(NULL, "", WS_VISIBLE | WS_CHILD | WS_HSCROLL | WS_VSCROLL, w, this, ID_WND);
m_filter_wnd.SetScrollRange(SB_HORZ, 0, 1000);
m_filter_wnd.SetScrollRange(SB_VERT, 0, 400);

return TRUE;
}

The actual size of that window is +/- 300(width) x 150(height), but the scrollable area is set to 1000x400.

Positioning the scrollbar isn't the problem, that is done by the code below :

void CMyWnd::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
int minpos;
int maxpos;
GetScrollRange(SB_VERT, &minpos, &maxpos);
int curpos = GetScrollPos(SB_VERT);

switch (nSBCode)
{
case (SB_LINEDOWN) :
{
if (curpos+1 < maxpos)
curpos = curpos+1;
break;
}
case (SB_LINEUP) :
{
if (curpos-1 > minpos)
curpos = curpos-1;
break;
}
case (SB_THUMBPOSITION) :
{
curpos = nPos;
break;
}
case (SB_THUMBTRACK) :
{
curpos = nPos;
break;
}
}

SetScrollPos(SB_VERT, curpos);

CWnd::OnVScroll(nSBCode, nPos, pScrollBar);
}

As a test, I simply wanna draw a line on a background color.

void CMyWnd::OnPaint()
{
CPaintDC dc(this);

HDC hMemDC = CreateCompatibleDC((HDC)dc.GetSafeHdc());
CDC* pMemDC = CDC::FromHandle(hMemDC);

// Fill the window with background color
RECT rect;
GetClientRect(&rect);
int width = rect.right;
int height = rect.bottom;
HBITMAP hBitmap = CreateCompatibleBitmap((HDC)dc.GetSafeHdc(), width, height);
HBITMAP hBitmap_old = (HBITMAP)SelectObject(hMemDC, hBitmap);
pMemDC->FillSolidRect(&rect, RGB(200,200,200));

// Draw a line
pMemDC->MoveTo(10,10);
pMemDC->LineTo(100,100);

BitBlt((HDC)dc.GetSafeHdc(), rect.left, rect.top, width, height, hMemDC, 0, 0, SRCCOPY);

DeleteObject(SelectObject(hMemDC, hBitmap_old));
DeleteDC(hMemDC);
}

But then I'm having some problems : I would like the 'image' to be scrolled as soon as the scrollbars are moved,
and only the part of the image that is currently displayed in the custom window needs to be redrawn.

How can do it, what do I have to change ?
[2946 byte] By [jvr2] at [2007-11-18 20:31:34]
# 1 Re: scrolling in Cwnd-derived class
The following code does exactly what I want, but I'm not sure it's the best way to achieve it ...

void CMyWnd::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
int curpos = GetScrollPos(SB_VERT);

switch (nSBCode)
{
case (SB_LINEDOWN) :
{
if ((curpos+1) < m_scrollsize_y)
curpos = curpos+1;
break;
}
case (SB_LINEUP) :
{
if ((curpos-1) >= 0)
curpos = curpos-1;
break;
}
case (SB_THUMBTRACK) :
case (SB_THUMBPOSITION) :
{
curpos = nPos;
break;
}
}

SetScrollPos(SB_VERT, curpos);
RedrawWindow();

CWnd::OnVScroll(nSBCode, nPos, pScrollBar);
}

void CMyWnd::OnPaint()
{
CPaintDC dc(this);

HDC hMemDC = CreateCompatibleDC((HDC)dc.GetSafeHdc());
CDC* pMemDC = CDC::FromHandle(hMemDC);

int xpos = GetScrollPos(SB_HORZ);
int ypos = GetScrollPos(SB_VERT);

HBITMAP hBitmap = CreateCompatibleBitmap((HDC)dc.GetSafeHdc(), m_scrollsize_x, m_scrollsize_y);
HBITMAP hBitmap_old = (HBITMAP)SelectObject(hMemDC, hBitmap);
pMemDC->FillSolidRect(0, 0, m_scrollsize_x, m_scrollsize_y, RGB(200,200,200));

RECT rect;
GetClientRect(&rect);
int width = rect.right;
int height = rect.bottom;

// Draw a line
pMemDC->MoveTo(10,10);
pMemDC->LineTo(100,100);

BitBlt((HDC)dc.GetSafeHdc(), 0, 0, width, height, hMemDC, xpos, ypos, SRCCOPY);

DeleteObject(SelectObject(hMemDC, hBitmap_old));
DeleteDC(hMemDC);
}
jvr2 at 2007-11-11 1:12:37 >