Solved

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

Posted on 2003-12-05
6
1,113 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Expand macro to ask for filename column 8 37
pre4 challenge 19 111
fix34  challenge 9 167
How to convert MFC::CString to UTF8 wchar_t* 10 388
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

730 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