Click here to Skip to main content
15,920,005 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Need help on how to create image on child window and have to move along with vertical scroll bar to my dialog box Pin
Stuart Dootson6-Feb-09 8:03
professionalStuart Dootson6-Feb-09 8:03 
GeneralRe: Need help on how to create image on child window and have to move along with vertical scroll bar to my dialog box Pin
John5028-Feb-09 4:14
John5028-Feb-09 4:14 
GeneralRe: Need help on how to create image on child window and have to move along with vertical scroll bar to my dialog box Pin
Stuart Dootson8-Feb-09 6:38
professionalStuart Dootson8-Feb-09 6:38 
GeneralRe: Need help on how to create image on child window and have to move along with vertical scroll bar to my dialog box Pin
John5028-Feb-09 7:05
John5028-Feb-09 7:05 
GeneralRe: Need help on how to create image on child window and have to move along with vertical scroll bar to my dialog box Pin
Stuart Dootson8-Feb-09 8:01
professionalStuart Dootson8-Feb-09 8:01 
GeneralRe: Need help on how to create image on child window and have to move along with vertical scroll bar to my dialog box Pin
John5028-Feb-09 9:59
John5028-Feb-09 9:59 
GeneralRe: Need help on how to create image on child window and have to move along with vertical scroll bar to my dialog box Pin
Stuart Dootson8-Feb-09 10:28
professionalStuart Dootson8-Feb-09 10:28 
GeneralRe: Need help on how to create image on child window and have to move along with vertical scroll bar to my dialog box Pin
Stuart Dootson8-Feb-09 22:07
professionalStuart Dootson8-Feb-09 22:07 
OK - as I promised, here's my code (or at least the relevant bits of it).

Here's the relevant bits of the dialog class declaration:

// CaaaDlg dialog
class CaaaDlg : public CDialog
{
public:
// Public methods
protected:
// Implementation
private:
// Private stuff I've added
   void OnResized();
   CImage image1_;
   CImage image2_;
   CRect baseSize_;
   CPoint offset_;
};


And the relevant (bits of) the method implementations:

CaaaDlg::CaaaDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CaaaDlg::IDD, pParent),
     offset_(0, 0)
{
   // Load the icons in the constructor, so we only do it once.
   image1_.Load(path to image 1);
   image2_.Load(path to image 1);
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CaaaDlg::OnPaint()
{
   if (IsIconic())
   {
      // Set the dialog's icon
   }
   else
   {
      // Get a DC
      PAINTSTRUCT ps;
      CDC* paintDC = BeginPaint(&ps);
      // Get a 'top of the dialog' rectangle into rcImage1
      CRect rcImage1(baseSize_);
      rcImage1.bottom /= 2;
      // Get a 'bottom of the dialog' rectangle into rcImage2
      CRect rcImage2;
      rcImage2.SubtractRect(baseSize_, rcImage1);
      // Offset the images by the scroll position
      rcImage1.OffsetRect(-offset_);
      rcImage2.OffsetRect(-offset_);
      // Draw the images
      image1_.StretchBlt(*paintDC, rcImage1, SRCCOPY);
      image2_.StretchBlt(*paintDC, rcImage2, SRCCOPY);
      EndPaint(&ps);
   }
}

void CaaaDlg::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
   SCROLLINFO si = { sizeof(si), 0 };
   GetScrollInfo(SB_VERT, &si, SIF_ALL);
   // We'll get the new scroll position in newOffset
   CPoint newOffset(offset_);
   switch (nSBCode)
   {
   case SB_LINEDOWN:
      newOffset.y += si.nPage / 10;
      break;

   case SB_LINEUP:
      newOffset.y -= si.nPage / 10;
      break;
   case SB_PAGEDOWN:
      newOffset.y += si.nPage;
      break;

   case SB_THUMBTRACK:
   case SB_THUMBPOSITION:
      newOffset.y = nPos;
      break;

   case SB_PAGEUP:
      newOffset.y -= si.nPage;
      break;

   default:
      return;
   }

   // Limit the new scroll position according to the min 
   // and max possible values of the scroll position
   CRect currSize;
   GetClientRect(&currSize);
   const int maxOffset = baseSize_.Height() - currSize.Height();
   newOffset.y = max(min(newOffset.y, maxOffset), 0);

   // Scroll the window appropriately
   SetScrollPos(SB_VERT,newOffset.y,TRUE);
   ScrollWindow(0,offset_.y-newOffset.y);
   offset_ = newOffset;

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

// Call this from OnInitDialong and the WM_SIZE handler
void CaaaDlg::OnResized()
{
   // Adjust the scrollbar according to the current size of the dialog
   CRect currSize;
   GetClientRect(&currSize);
   SCROLLINFO si;
   si.cbSize = sizeof(SCROLLINFO);
   si.fMask = SIF_DISABLENOSCROLL|SIF_ALL;
   si.nMin = 0;
   si.nMax = baseSize_.Height()-1;
   si.nPage = currSize.Height();
   CPoint oldOffset(offset_);
   // This is the important bit - if the dialog is made bigger 
   // when the scroll bar is all the way down, we will likely
   // need to alter the current scroll position.
   if(offset_.y + si.nPage > baseSize_.Height()) offset_.y = baseSize_.Height() - si.nPage;
   if(offset_.y < 0) offset_.y = 0;
   si.nTrackPos = si.nPos = offset_.y;
   SetScrollInfo(SB_VERT, &si, TRUE); 
   // If we did need to alter the current scroll position, scroll the window.
   if (oldOffset != offset_) 
   {
      SetScrollPos(SB_VERT,offset_.y,TRUE);
      ScrollWindow(0,oldOffset.y-offset_.y);
   }
}

GeneralRe: Need help on how to create image on child window and have to move along with vertical scroll bar to my dialog box Pin
John5029-Feb-09 3:05
John5029-Feb-09 3:05 
GeneralRe: Need help on how to create image on child window and have to move along with vertical scroll bar to my dialog box Pin
John50210-Feb-09 7:00
John50210-Feb-09 7:00 
GeneralRe: Need help on how to create image on child window and have to move along with vertical scroll bar to my dialog box Pin
Stuart Dootson10-Feb-09 8:17
professionalStuart Dootson10-Feb-09 8:17 
GeneralRe: Need help on how to create image on child window and have to move along with vertical scroll bar to my dialog box Pin
John50210-Feb-09 20:45
John50210-Feb-09 20:45 
GeneralRe: Need help on how to create image on child window and have to move along with vertical scroll bar to my dialog box Pin
Stuart Dootson10-Feb-09 21:01
professionalStuart Dootson10-Feb-09 21:01 
GeneralRe: Need help on how to create image on child window and have to move along with vertical scroll bar to my dialog box Pin
John50211-Feb-09 1:56
John50211-Feb-09 1:56 
GeneralRe: Need help on how to create image on child window and have to move along with vertical scroll bar to my dialog box Pin
Stuart Dootson11-Feb-09 2:12
professionalStuart Dootson11-Feb-09 2:12 
GeneralRe: Need help on how to create image on child window and have to move along with vertical scroll bar to my dialog box Pin
John50212-Feb-09 1:01
John50212-Feb-09 1:01 
GeneralRe: Need help on how to create image on child window and have to move along with vertical scroll bar to my dialog box Pin
Stuart Dootson12-Feb-09 1:02
professionalStuart Dootson12-Feb-09 1:02 
GeneralRe: Need help on how to create image on child window and have to move along with vertical scroll bar to my dialog box Pin
John50215-Feb-09 23:51
John50215-Feb-09 23:51 
GeneralRe: Need help on how to create image on child window and have to move along with vertical scroll bar to my dialog box Pin
Stuart Dootson16-Feb-09 0:04
professionalStuart Dootson16-Feb-09 0:04 
GeneralRe: Need help on how to create image on child window and have to move along with vertical scroll bar to my dialog box Pin
John50216-Feb-09 0:45
John50216-Feb-09 0:45 
GeneralRe: Need help on how to create image on child window and have to move along with vertical scroll bar to my dialog box Pin
Stuart Dootson16-Feb-09 0:55
professionalStuart Dootson16-Feb-09 0:55 
GeneralRe: Need help on how to create image on child window and have to move along with vertical scroll bar to my dialog box Pin
John50216-Feb-09 5:01
John50216-Feb-09 5:01 
GeneralRe: Need help on how to create image on child window and have to move along with vertical scroll bar to my dialog box Pin
Stuart Dootson16-Feb-09 6:49
professionalStuart Dootson16-Feb-09 6:49 
GeneralRe: Need help on how to create image on child window and have to move along with vertical scroll bar to my dialog box Pin
John50216-Feb-09 7:16
John50216-Feb-09 7:16 
GeneralRe: Need help on how to create image on child window and have to move along with vertical scroll bar to my dialog box Pin
Stuart Dootson16-Feb-09 8:12
professionalStuart Dootson16-Feb-09 8:12 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.