[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2003-12-05
6
Medium Priority
?
1,136 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
3 Comments
 
LVL 48

Accepted Solution

by:
AlexFM earned 172 total points
ID: 9887590
0
 
LVL 45

Assisted Solution

by:AndyAinscow
AndyAinscow earned 164 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 164 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
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.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Suggested Courses

873 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