ChangeBmp.cpp
上传用户:do_tie
上传日期:2007-11-03
资源大小:1095k
文件大小:3k
- // ChangeBmp.cpp : 实现文件
- //
- #include "stdafx.h"
- #include "GandyDraw.h"
- #include "ChangeBmp.h"
- // CChangeBmp 对话框
- IMPLEMENT_DYNAMIC(CChangeBmp, CDialog)
- CChangeBmp::CChangeBmp(CWnd* pParent /*=NULL*/)
- : CDialog(CChangeBmp::IDD, pParent)
- , m_iShowWidth(0)
- , m_iShowHeight(0)
- , m_BmpPathName(_T(""))
- {
- m_FileBitmap.LoadBitmapW(IDB_BITMAP_MM);
- }
- CChangeBmp::~CChangeBmp()
- {
- }
- void CChangeBmp::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
- DDX_Text(pDX, IDC_SHOW_WIDTH, m_iShowWidth);
- DDX_Text(pDX, IDC_SHOW_HEIGHT, m_iShowHeight);
- }
- BEGIN_MESSAGE_MAP(CChangeBmp, CDialog)
- ON_BN_CLICKED(IDC_LOADBMP, &CChangeBmp::OnBnClickedLoadbmp)
- ON_WM_PAINT()
- END_MESSAGE_MAP()
- // CChangeBmp 消息处理程序
- void CChangeBmp::OnBnClickedLoadbmp()
- {
- wchar_t filters[] = L"位图文件(*.bmp)|*.bmp|所有文件(*.*)|*.*||";
- CFileDialog fileDlg(TRUE, NULL, NULL, OFN_HIDEREADONLY, filters);
- if (fileDlg.DoModal() == IDOK)
- {
- HBITMAP hBmp = (HBITMAP)LoadImage(NULL, fileDlg.GetPathName(),
- IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE |
- LR_DEFAULTCOLOR | LR_DEFAULTSIZE);
- if (hBmp != NULL) {
- BITMAP bm;
- GetObject(hBmp, sizeof(bm), &bm);
- long cb = bm.bmBitsPixel * bm.bmHeight * bm.bmWidthBytes;
- bm.bmBits = new char[cb];
- GetBitmapBits(hBmp, cb, bm.bmBits);
- m_FileBitmap.CreateBitmapIndirect(&bm);
- m_BmpPathName = fileDlg.GetPathName();
- //m_bBitmapLoaded = true;
- }
- }
- //BITMAP bs;
- //m_FileBitmap.GetBitmap(&bs);
- //m_iShowWidth = bs.bmWidth;
- //m_iShowHeight = bs.bmHeight;
- //UpdateData(false);
- }
- void CChangeBmp::ShowImg(UINT ID, HBITMAP hBmp)
- {
- CWnd* pWnd = GetDlgItem(ID);
- CDC* pDC = pWnd->GetDC();
- CRect rect;
- pWnd->GetClientRect(&rect);
- pDC->FillSolidRect(&rect,RGB(128,128,128));
- pWnd->Invalidate();
- pWnd->UpdateWindow();
- BITMAP bs;
- GetObject(hBmp, sizeof(bs), &bs);
- CDC dc;
- if(dc.CreateCompatibleDC(pDC)) {
- int x0, y0, w, h;
- float rx = (float)bs.bmWidth / rect.right,
- ry = (float)bs.bmHeight / rect.bottom;
- if (rx >= ry) {
- x0 = 0; w = rect.right;
- h = (int)(bs.bmHeight / rx + 0.5);
- y0 = (rect.bottom - h) / 2;
- }
- else {
- y0 = 0; h = rect.bottom;
- w = (int)(bs.bmWidth / ry + 0.5);
- x0 = (rect.right - w) / 2;
- }
- ::SelectObject(dc.GetSafeHdc(), hBmp);
- pDC->SetStretchBltMode(HALFTONE);
- pDC->StretchBlt(x0, y0, w, h, &dc, 0, 0, bs.bmWidth, bs.bmHeight, SRCCOPY);
- SetDlgItemInt(IDC_SHOW_WIDTH, bs.bmWidth);
- SetDlgItemInt(IDC_SHOW_HEIGHT, bs.bmHeight);
- }
- }
- void CChangeBmp::OnPaint()
- {
- CPaintDC dc(this); // device context for painting
- // TODO: 在此处添加消息处理程序代码
- // 不为绘图消息调用 CDialog::OnPaint()
- HBITMAP hBmp = (HBITMAP)LoadImage(NULL, m_BmpPathName,
- IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE |
- LR_DEFAULTCOLOR | LR_DEFAULTSIZE);
- if (hBmp != NULL) {
- BITMAP bm;
- GetObject(hBmp, sizeof(bm), &bm);
- long cb = bm.bmBitsPixel * bm.bmHeight * bm.bmWidthBytes;
- bm.bmBits = new char[cb];
- GetBitmapBits(hBmp, cb, bm.bmBits);
- m_FileBitmap.CreateBitmapIndirect(&bm);
- }
- ShowImg(IDC_SHOWBMP,m_FileBitmap);
- }