Solved

MFC! Insert BMP and JPEG window in dialog and how to resize SDI window?

Posted on 2003-12-05
6
1,100 Views
Last Modified: 2013-11-20
Urgent!!! i need to put bitmap and jpeg into my visual C++ program, into a dialog when i start running my program.
but i don't know how. Can somebody explain step by step to me?

another question is how can i resize my window when I start running my program? which class should I place my code in?(I am working with SDI)

thanx in advance(hope to receive reply asap)



0
Comment
Question by:dixon_lee
6 Comments
 
LVL 48

Accepted Solution

by:
AlexFM earned 43 total points
ID: 9887590
0
 
LVL 44

Assisted Solution

by:AndyAinscow
AndyAinscow earned 41 total points
ID: 9887639
To put a bitmap (256 colour) onto a dialog you can use the resource editor.

To resize your window, one method would be (eg in the OnInitialUpdate of the view, OnInitDialog for a dialog):
AfxGetMainWnd()->MoveWindow(left, top, width, height);

more flexibility is provided with the SetWindowPos fn.
0
 

Assisted Solution

by:andyinlondon
andyinlondon earned 41 total points
ID: 9892631
1. Edit your dialog.rc file using Visual Studio's dialogue editor (as though you want to add buttons etc.).
2. Add a "Picture Control" from the "toolbox". Make it the size that you want your final image to be.
3. In the "properties" view of the picture control, change its ID to "IDC_PICTURE_PLACEHOLDER".
4. Add the following member variables to your dialog class:
   CRect m_PictureHolderRect;
   CImage m_bmpBitmap;
5. Add ON_WM_PAINT() to your dialogue box message map (it's probably already there).
6. Add the following methods (assuming your dialog is called CMyDlg).

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

  // get the screen dimensions of the PictureControl - it was only put there for this purpose
  CWnd* pWnd = GetDlgItem(IDC_PICTURE_PLACEHOLDER);
  pWnd->GetWindowRect(&m_PictureHolderRect);
  // convert to client coordinates
  ScreenToClient(&m_PictureHolderRect);
  LoadBitmap("C:\\My Pictures\\Somewhere\\Nice Picture.JPG");
  return TRUE;
}

void CMyDlg::LoadBitmap(CString str)
{ if (str.GetLength() > 0)
  { // Delete the current image
    m_bmpBitmap.Destroy();
    // Load the new image
    m_bmpBitmap.Load(str);
    // Invalidate the dialog
    Invalidate();
  }
}

void CMyDlg::OnPaint()
{ // get the device context
  CPaintDC dc(this);

  // get the rectangle containing the picture place-holder
  CRect rect(m_PictureHolderRect);

  // retain the correct aspect ratio
  int spaceWidth = rect.Width();
  int spaceHeight = rect.Height();
  int bmpWidth = m_bmpBitmap.GetWidth();
  int bmpHeight = m_bmpBitmap.GetHeight();
  if ((double)bmpWidth / bmpHeight > (double)spaceWidth / spaceHeight)
  { spaceHeight = (int)((double)spaceWidth / bmpWidth * bmpHeight);
  }
  else
  { spaceWidth = (int)((double)spaceHeight / bmpHeight * bmpWidth);
  }
  rect.right = rect.left + spaceWidth;
  rect.bottom = rect.top + spaceHeight;

  // re-position to the centre of the place-holder position
  LONG offsetX = (m_PictureHolderRect.right - rect.right) / 2;
  LONG offsetY = (m_PictureHolderRect.bottom - rect.bottom) / 2;
  rect.left += offsetX;   rect.right += offsetX;
  rect.top += offsetY;    rect.bottom += offsetY;

  // resize the picture placeholder
  CWnd* pWnd = GetDlgItem(IDC_PICTURE_POS);
  pWnd->SetWindowPos(NULL,
                     rect.left - 4,
                     rect.top - 4,
                     rect.Width() + 8,
                     rect.Height() + 8,
                     SWP_NOOWNERZORDER | SWP_NOSENDCHANGING | SWP_NOZORDER /* | SWP_NOMOVE */);

  // set the bitmap stretch mode to half-tone
  dc.SetStretchBltMode(HALFTONE);

  // display the bitmap
  m_bmpBitmap.Draw(dc.m_hDC, rect);
}

Not only will this display your picture within your dialogue box, it will also retain the correct aspect ratio.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now