GDI+ 笔记

来源:互联网 发布:简单的数据库设计 编辑:程序博客网 时间:2024/05/29 18:07






一、CBitmap与BITMAP的区别


CBitmap是MFC中的一个类,该类提供了各种操纵位图的功能(载入,销毁,得到位图信息等)包含着位图的属性及对位图的操作,是对HBITMAP的封装;


而BITMAP是SDK中的一个结构,包含一个位图的各种信息,BITMAP是一个结构体,封装着位图的一些信息;
 
HBITMAP是位图的句柄。
关联的方式是:
CBitmap bitmap;
HBITMAP hBitmap;
bitmap.LoadBitmap(ID);
bitmap.Attach(hBitmap);
BITMAP bm;
bitmap.GetBitmap(&bm);        //此函数获得位图的一些信息,并赋给bm结构体



二、GetEncoderClsid函数,方便的获得编码器的CLSID。


int GetEncoderClsid(const WCHAR* format, CLSID* pClsid){UINT num= 0;UINT size= 0;ImageCodecInfo* pImageCodecInfo= NULL;GetImageEncodersSize(&num, &size);if(size== 0){return -1;}pImageCodecInfo= (ImageCodecInfo*)(malloc(size));if(pImageCodecInfo== NULL){return -1;}GetImageEncoders(num, size, pImageCodecInfo);for(UINT j=0; j< num; ++j){if(wcscmp(pImageCodecInfo[j].MimeType, format)== 0){*pClsid= pImageCodecInfo[j].Clsid;free(pImageCodecInfo);return j;}}free(pImageCodecInfo);return -1;}

CLSID encoderClsid

    GetEncoderClsid(L"image/png", &encoderClsid);    //png

    GetEncoderClsid(L"image/bmp", &encoderClsid);

    GetEncoderClsid(L"image/gif", &encoderClsid);

    GetEncoderClsid(L"image/jpeg", &encoderClsid);

    GetEncoderClsid(L"image/tiff", &encoderClsid);


三、MFC CFileDialog 用法详解

CFileDialog::CFileDialog
CFileDialog( BOOL bOpenFileDialog, LPCTSTR lpszDefExt = NULL, LPCTSTR lpszFileName =
NULL, DWORD dwFlags = OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, LPCTSTR lpszFilter =
NULL, CWnd* pParentWnd = NULL );
BOOL bOpenFileDialog
该参数为指定true是Open,false是Save as 对话框。
LPCTSTR lpszDefExt
添加到没有扩展名文件上的扩展名
LPCTSTR lpszFileName
应该初始选定的文件名
DWORD dwFlags 
自定义标志,当要进行多选的时候添加OFN_ALLOWMULTISELECT
LPCTSTR lpszFilter
File of type中的过滤参数
例如: 
CString lpszFilter="位图(*.bmp)|*.bmp|\
   全部文件(*.*)|*.*||";  
"Chart Files (*.xlc)|*.xlc|Worksheet Files (*.xls)|*.xls|Data Files (*.xlc;*.xls)|*.xlc; *.xls|All Files (*.*)|*.*||";文件类型说明和扩展名间用 | 分隔,同种类型文件的扩展名间可以用 ; 分割,每种文件类型间用 | 分隔,末尾用 || 指明。
CWnd* pParentWnd
父窗口的指针。


CFileDialog::GetFileName 得到打开文件的名字
For example, GetFileName will return "TEXT.DAT" for the file C:\FILES\TEXT.DAT.


CFileDialog::GetFileTitle 得到打开文件的标题,不含扩展名。
For example, GetFileTitle will return "TEXT" for the file C:\FILES\TEXT.DAT.


CFileDialog::GetFileExt 得到打开文件的扩展名。
For example, if the name of the file entered is DATA.TXT, GetFileExt returns "TXT".


CFileDialog::GetPathName 得到打开文件的路径名


For example, GetPathName will return "C:\FILES\TEXT.DAT" for the file
C:\FILES\TEXT.DAT.
CFileDialog::GetStartPosition得到列表中第一个文件路径的位置。
该函数使用时候,构造函数中的dwFlags必须选定OFN_ALLOWMULTISELECT


CFileDialog::GetNextPathName根据选择的位置返回该文件的路径名
For example, GetNextPathName will return "C:\FILES\TEXT.DAT" for the file
C:\FILES\TEXT.DAT.


CFileDialog::OnShareViolation当用户发生共享时,该函数返回共享警告或提示。
一般系统会自动提示
virtual UINT OnShareViolation( LPCTSTR lpszPathName );
If you want to disable share violation checking, use the bitwise OR operator to combine
the flag OFN_SHAREAWARE with m_ofn.Flags.


CFileDialog::OnFileNameOK使得在对话框输入的文件名生效。
一般系统会自动提示
简单的打开文件对话框
CFileDialog hFileDlg(TRUE,NULL,NULL,OFN_FILEMUSTEXIST|OFN_PATHMUSTEXIST|OFN_READONLY,
TEXT("所有支持的图像文件 (*.jpg;*.gif;*.bmp;...)|*.jpg;*.gif;*.bmp|Tiff图像文件(*.tiff;*.tif)|*.tiff;*.tif||"),NULL);
hFileDlg.m_ofn.nFilterIndex=2;
hFileDlg.m_ofn.hwndOwner=m_hWnd;
hFileDlg.m_ofn.lStructSize=sizeof(OPENFILENAME);
hFileDlg.m_ofn.lpstrTitle=TEXT("打开图像文件...\0");
hFileDlg.m_ofn.nMaxFile=MAX_PATH;
if(hFileDlg.DoModal() == IDOK)
{
m_path=_T("");
m_path = hFileDlg.GetPathName();
AfxMessageBox(m_path);
}
///////////////////////////////////////另存为对话框
CString filename;//保存路径
//图像文件 (*.jpg;*.gif;*.bmp;...)|*.jpg;*.gif;*.bmp|所有文件(*.*;)|*.*||
   CFileDialog opendlg (FALSE,_T("*"),_T("1.jk"),OFN_OVERWRITEPROMPT, _T("所有文件(*.*;)|*.*||"),NULL);   
if (opendlg.DoModal()==IDOK)
{
   filename=opendlg.GetPathName();   

/////////
AfxMessageBox(filename);