图形处理---位图、图标和光标

图形处理---位图、图标和光标

关键字: cbitmap, icon, cursor

      1.位图

     例1 编写一个应用程序MyBMP,程序运行后在客户区显示一幅BMP位图

     [编程说明与实现]

     利用MFC AppWizard应用程序向导创建一个单文档应用程序MyBMP。执行Insert|Resource命令,插入一个BMP位图资源。利用资源编辑器位图进行编辑,并将其ID改为IDB_MYBITMAP。在重绘函数OnDraw()中添加如下代码。

     

Cpp代码 复制代码
  1. void CMyBMPView::OnDraw(CDC* pDC)   
  2. {   
  3.     CMyBMPDoc* pDoc = GetDocument();   
  4.     ASSERT_VALID(pDoc);   
  5.     // TODO: add draw code for native data here   
  6.     CDC MemDC;   
  7.     MemDC.CreateCompatibleDC(pDC);          //创建一个内存设备环境   
  8.     CBitmap Bitmap;   
  9.     Bitmap.LoadBitmap(IDB_MYBITMAP);        //装入BMP格式的位图资源   
  10.     CBitmap* pOldBitmap=MemDC.SelectObject(&Bitmap);    //将位图对象选入设备环境   
  11.     BITMAP bm;   
  12.     Bitmap.GetObject(sizeof(BITMAP),&bm);       //读取位图信息   
  13.     //将内存中位图复制到屏幕上   
  14.     pDC->BitBlt(0,0,bm.bmWidth,bm.bmHeight,&MemDC,0,0,SRCCOPY);   
  15. }  
void CMyBMPView::OnDraw(CDC* pDC)
{
	CMyBMPDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	// TODO: add draw code for native data here
	CDC MemDC;
	MemDC.CreateCompatibleDC(pDC);			//创建一个内存设备环境
	CBitmap Bitmap;
	Bitmap.LoadBitmap(IDB_MYBITMAP);		//装入BMP格式的位图资源
	CBitmap* pOldBitmap=MemDC.SelectObject(&Bitmap);	//将位图对象选入设备环境
	BITMAP bm;
	Bitmap.GetObject(sizeof(BITMAP),&bm);		//读取位图信息
	//将内存中位图复制到屏幕上
	pDC->BitBlt(0,0,bm.bmWidth,bm.bmHeight,&MemDC,0,0,SRCCOPY);
}

      例2 编写一个应用程序MyIcon,程序运行后在标题栏不显示原来默认的图标,而显示自已创建的新图标,并在客户区显示该图标和一个Windows预定义图标。

      [编程说明与实现]

      (1)首先利用MFC AppWizard应用程序向导创建一个SDI应用程序MyIcon,然后执行Insert|Resource命令,要创建的资源选择Icon(图标),单击New按钮,打开图形编辑器,可以开始创建图标,新创建图标的ID为IDI_ICON1。也可通过Import命令导入一个图标文件。

      (2)为了在标题栏显示创建的图标IDI_ICON1,在初始化函数InitInstance()中添加如下粗体代码:

Cpp代码 复制代码
  1. HICON hIcon=AfxGetApp()->LoadIcon(IDI_ICON1);        //加载自定义图标   
  2. m_pMainWnd->SetIcon(hIcon,TRUE);                 //设置32*32图标   
  3. m_pMainWnd->SetIcon(hIcon,FALSE);                    //设置16*16图标   
  4.   
  5. // The one and only window has been initialized, so show and update it.   
  6. m_pMainWnd->ShowWindow(SW_SHOW);   
  7. m_pMainWnd->UpdateWindow();  
	HICON hIcon=AfxGetApp()->LoadIcon(IDI_ICON1);		//加载自定义图标
	m_pMainWnd->SetIcon(hIcon,TRUE);					//设置32*32图标
	m_pMainWnd->SetIcon(hIcon,FALSE);					//设置16*16图标

	// The one and only window has been initialized, so show and update it.
	m_pMainWnd->ShowWindow(SW_SHOW);
	m_pMainWnd->UpdateWindow();

     (3)在函数OnDraw()中使用图标,需要编写代码加载和显示图标,如下所示:

Cpp代码 复制代码
  1. void CMyIconView::OnDraw(CDC* pDC)   
  2. {   
  3.     CMyIconDoc* pDoc = GetDocument();   
  4.     ASSERT_VALID(pDoc);   
  5.     // TODO: add draw code for native data here   
  6.     HICON hIcon=AfxGetApp()->LoadIcon(IDI_ICON1);        //加载自定义图标   
  7.     pDC->DrawIcon(0,0,hIcon);                            //显示图标   
  8.     DestroyIcon(hIcon);                                 //释放图标资源   
  9.     hIcon=AfxGetApp()->LoadStandardIcon(IDI_EXCLAMATION);//加载系统预定义图标   
  10.     pDC->DrawIcon(50,0,hIcon);   
  11.     DestroyIcon(hIcon);   
  12. }  
void CMyIconView::OnDraw(CDC* pDC)
{
	CMyIconDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	// TODO: add draw code for native data here
	HICON hIcon=AfxGetApp()->LoadIcon(IDI_ICON1);		//加载自定义图标
	pDC->DrawIcon(0,0,hIcon);							//显示图标
	DestroyIcon(hIcon);									//释放图标资源
	hIcon=AfxGetApp()->LoadStandardIcon(IDI_EXCLAMATION);//加载系统预定义图标
	pDC->DrawIcon(50,0,hIcon);
	DestroyIcon(hIcon);
}

    3.光标


 

 

      例3 编写一个应用程序WaitCursor,程序运行后单击鼠标光标处理沙漏状,2秒钟光标恢复正常状态。

      [编程说明与实现]

      (1)利用MFC AppWizard向导创建一个SDI应用程序WaitCursor,利用ClassWizard为类CWaitCurWait添加消息WM_LBUTTONDOWN的处理函数,并添加如下代码。

Cpp代码 复制代码
  1. void CWaitCurView::OnLButtonUp(UINT nFlags, CPoint point)    
  2. {   
  3.     // TODO: Add your message handler code here and/or call default   
  4.     SetCapture();   
  5.     BeginWaitCursor();          //显示沙漏光标   
  6.     SetTimer(1,2000,NULL);      //启动一个2秒的计时器   
  7.   
  8.     CView::OnLButtonUp(nFlags, point);   
  9. }  
void CWaitCurView::OnLButtonUp(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	SetCapture();
	BeginWaitCursor();			//显示沙漏光标
	SetTimer(1,2000,NULL);		//启动一个2秒的计时器

	CView::OnLButtonUp(nFlags, point);
}

    (2)利用ClassWizard类向导为类CWaitCurView添加消息WM_TIMER的处理函数,在函数中添加如下代码。

 

Cpp代码 复制代码
  1. void CWaitCurView::OnTimer(UINT nIDEvent)    
  2. {   
  3.     // TODO: Add your message handler code here and/or call default   
  4.     ReleaseCapture();   
  5.     EndWaitCursor();        //2秒后恢复光标原来的形状   
  6.     KillTimer(1);           //删除计时器   
  7.     CView::OnTimer(nIDEvent);   
  8. }  
void CWaitCurView::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
	ReleaseCapture();
	EndWaitCursor();		//2秒后恢复光标原来的形状
	KillTimer(1);			//删除计时器
	CView::OnTimer(nIDEvent);
}

      例4 编写一个应用程序MyCursor,程序运行后,当光标移动客户区时变为自定义的形状。执行"查看|系统光标"命令打开一个对话框,当光标移到对话框时,光标变为Windows预定义的4个箭头光标。

      [编程说明与说明]

      (1)首先利用MFC AppWizard向导创建一个SDI应用程序MyCursor,然后执行Insert|Resource命令插入一个Cursor资源。利用光标资源编辑器绘制一个手形光标,设置光标热点,将光标的ID改为IDC_HAND。利用ClassWizard为CMyCursorView类添加消息WM_SETCURSOR的处理函数,并添加如下代码。

Cpp代码 复制代码
  1. BOOL CMyCursorView::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message)    
  2. {   
  3.     // TODO: Add your message handler code here and/or call default   
  4.     HCURSOR hcursor;   
  5.     hcursor=AfxGetApp()->LoadCursor(IDC_HAND);       //加载自定义光标   
  6.     SetCursor(hcursor);                             //设置光标   
  7.     return TRUE;   
  8.     //return CView::OnSetCursor(pWnd, nHitTest, message);   
  9. }  
BOOL CMyCursorView::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message) 
{
	// TODO: Add your message handler code here and/or call default
	HCURSOR hcursor;
	hcursor=AfxGetApp()->LoadCursor(IDC_HAND);		//加载自定义光标
	SetCursor(hcursor);								//设置光标
	return TRUE;
	//return CView::OnSetCursor(pWnd, nHitTest, message);
}

    (2)向项目添加一个ID为IDD_MYDLG、标题为"使用系统光标"的对话框资源,双击对话框资,利用ClassWizard类向导创建对话框类CMyDlg。利用ClassWizard为对话框类添加消息WM_SETCURSOR的处理函数,加载Windows系统光标。

Cpp代码 复制代码
  1. BOOL CMyDlg::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message)    
  2. {   
  3.     // TODO: Add your message handler code here and/or call default   
  4.     HCURSOR hcursor;   
  5.     hcursor=AfxGetApp()->LoadStandardCursor(IDC_SIZEALL);    //加载系统光标   
  6.     SetCursor(hcursor);   
  7.     return TRUE;   
  8.     //return CDialog::OnSetCursor(pWnd, nHitTest, message);   
  9. }  
BOOL CMyDlg::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message) 
{
	// TODO: Add your message handler code here and/or call default
	HCURSOR hcursor;
	hcursor=AfxGetApp()->LoadStandardCursor(IDC_SIZEALL);	//加载系统光标
	SetCursor(hcursor);
	return TRUE;
	//return CDialog::OnSetCursor(pWnd, nHitTest, message);
}

 

  (3)在"查看"主菜单中增加"系统光标"菜单项,其ID为ID_VIEW_SYSCUR。利用ClassWizard类向导在CMyCursorView类中添加该菜单项的命令处理函数。

Cpp代码 复制代码
  1. void CMainFrame::OnViewSyscur()    
  2. {   
  3.     // TODO: Add your command handler code here   
  4.     CMyDlg dlg;   
  5.     dlg.DoModal();   
  6. }  
void CMainFrame::OnViewSyscur() 
{
	// TODO: Add your command handler code here
	CMyDlg dlg;
	dlg.DoModal();
}

 

    在MyCursorView.h文件的开头位置用#include指令包含对话框类CMyDlg定义的头文件MyDlg.h。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值