Solved

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

Posted on 2003-12-05
6
1,098 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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
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.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

757 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

20 Experts available now in Live!

Get 1:1 Help Now