⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bmpviewdlg.cpp

📁 画图程序,非常好,好好啊哈
💻 CPP
字号:
// bmpviewDlg.cpp : implementation file
//

#include "stdafx.h"
#include "bmpview.h"
#include "bmpviewDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CBmpviewDlg dialog

CBmpviewDlg::CBmpviewDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CBmpviewDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CBmpviewDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CBmpviewDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CBmpviewDlg)
	DDX_Control(pDX, IDC_IMAGE, m_image);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CBmpviewDlg, CDialog)
	//{{AFX_MSG_MAP(CBmpviewDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_COMMAND(ID_EXIT, OnExit)
	ON_COMMAND(ID_BABY1, OnBaby1)
	ON_COMMAND(ID_BABY2, OnBaby2)
	ON_COMMAND(ID_BABY3, OnBaby3)
	ON_COMMAND(ID_OPEN, OnOpen)
	ON_COMMAND(ID_ABOUT, OnAbout)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CBmpviewDlg message handlers

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

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	//获得指向主菜单的指针。
	m_pMainMenu=GetMenu();
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CBmpviewDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CBmpviewDlg::OnPaint() 
{
/*	if (IsIconic())
	{
	//	CPaintDC dc(this); // device context for painting

	//	SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
	*/
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CBmpviewDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CBmpviewDlg::OnExit() 
{
	// TODO: Add your command handler code here
	CDialog::OnOK(); //直接调用CDialog::OnOK()完成退出程序
}

//我们添加的成员函数,用来显示位图资源
//两个参数:
//nBitmapID, 位图资源的ID;
//nMenuID,菜单项的ID
void CBmpviewDlg::OnSelectBitmap(UINT nBitmapID, UINT nMenuID)
{
	m_pSubMenu=m_pMainMenu->GetSubMenu(0);//
	m_pSubMenu->CheckMenuItem(ID_BABY1,MF_UNCHECKED);//设成未选中
	m_pSubMenu->CheckMenuItem(ID_BABY2,MF_UNCHECKED);//同上
	m_pSubMenu->CheckMenuItem(ID_BABY3,MF_UNCHECKED);//同上
	m_pSubMenu->CheckMenuItem(nMenuID,MF_CHECKED);//将传过来的菜单项设成选中

	m_bitmap.DeleteObject(); //调用CGdiObject的DeleteObject函数将以前的BITMAP对象删除。
	m_bitmap.LoadBitmap(nBitmapID);//载入指定ID的图象资源

	//执行显示函数
	ShowBitmap();
}

void CBmpviewDlg::OnBaby1() 
{
	// TODO: Add your command handler code here
	OnSelectBitmap(IDB_BITMAP1, ID_BABY1);
}

void CBmpviewDlg::OnBaby2() 
{
	// TODO: Add your command handler code here
	OnSelectBitmap(IDB_BITMAP2, ID_BABY2);
}

void CBmpviewDlg::OnBaby3() 
{
	// TODO: Add your command handler code here
	OnSelectBitmap(IDB_BITMAP3, ID_BABY3);
}

void CBmpviewDlg::OnOpen() 
{
	// TODO: Add your command handler code here
	CFileDialog dlg(TRUE,NULL,NULL,NULL,"位图文件(*.bmp)|*.bmp");
	INT Result=dlg.DoModal();
	if(Result==IDOK)
	{
		HBITMAP hbitmap;
		hbitmap=(HBITMAP)::LoadImage(NULL,dlg.GetFileName(),IMAGE_BITMAP,0,0,LR_LOADFROMFILE);
		BITMAP bm;
		::GetObject(hbitmap,sizeof(BITMAP),&bm);
		m_bitmap.DeleteObject(); //调用CGdiObject的DeleteObject函数将以前的BITMAP对象删除。
		m_bitmap.Attach(hbitmap);
		ShowBitmap();
		}
	else if(Result==IDCANCEL)
		return;
}

void CBmpviewDlg::ShowBitmap()
{
	BITMAP bm;
	m_bitmap.GetObject(sizeof(BITMAP),&bm);
	CDC *pDC=new CDC;
	CClientDC dc(&m_image);
	//CClientDC dc(NULL);
	pDC->CreateCompatibleDC(&dc);
	pDC->SelectObject(m_bitmap);
	//将图片伸缩成我们设定的大小。
	dc.StretchBlt(0,0,320,240,pDC,0,0,bm.bmWidth,bm.bmHeight,SRCCOPY);
	//如果不准备进行缩放可以用BITBLT。	
	//dc.BitBlt(0,0,bm.bmWidth,bm.bmHeight,pDC,0,0,SRCCOPY);

	delete pDC;
}

void CBmpviewDlg::OnAbout() 
{
	// TODO: Add your command handler code here
	CAboutDlg dlgAbout;
	dlgAbout.DoModal();
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -