|
DWORD GetModuleFileName( HMODULE hModule, // handle to module。將要得到的模塊的句柄。如果是當前模塊,NULL LPTSTR lpFilename, // path buffer 得到的文件名。 DWORD nSize // size of buffer 一般MAX_PATH就可以了); 得到全路徑TCHAR exeFullPath[MAX_PATH]; // MAX_PATHGetModuleFileName(NULL,exeFullPath,MAX_PATH);//得到程序模塊名稱,全路徑也就是當前運行程序的全路徑 例子: GetModuleFileName(NULL,strFullPath.GetBuffer(MAX_PATH),MAX_PATH); // 使用全路徑 避免因當前目錄改變而找不到文件
FillRect(&fillrect,&colorbrush) colorbrush 是畫刷 的 id. 它是 CBrush 類(class)的 一個 對象,實際上它定義了 用什么顏色 和 “花紋”來 “涂”一個 區域。它自身 并無 幾何形狀和大小的限制。 fillrect 定義 了一個 矩形區域范圍 的 坐標。 FillRect 就是 “用colorbrush 規定的顏色和花紋來 涂 滿 fillrect 定義的矩形區域”。
Create | Creates a tool tip control and attaches it to a CToolTipCtrl object. | CreateEx | Creates a tool tip control with the specified Windows extended styles and attaches it to a CToolTipCtrl object. | CToolTipCtrl | Constructs a CToolTipCtrl object. | GetBubbleSize | Retrieves the size of the tool tip. | GetCurrentTool | Retrieves information, such as the size, position, and text, of the tooltip window that the current tooltip control displays. | GetDelayTime | Retrieves the initial, pop-up, and reshow durations that are currently set for a tool tip control. | GetMargin | Retrieves the top, left, bottom, and right margins that are set for a tool tip window. | GetMaxTipWidth | Retrieves the maximum width for a tool tip window. | GetText | Retrieves the text that a tool tip control maintains for a tool. | GetTipBkColor | Retrieves the background color in a tool tip window. | GetTipTextColor | Retrieves the text color in a tool tip window. | GetTitle | Retrieves the title of the current tooltip control. | GetToolCount | Retrieves a count of the tools maintained by a tool tip control. | GetToolInfo | Retrieves the information that a tool tip control maintains about a tool. | SetDelayTime | Sets the initial, pop-up, and reshow durations for a tool tip control. | SetMargin | Sets the top, left, bottom, and right margins for a tool tip window. | SetMaxTipWidth | Sets the maximum width for a tool tip window. | SetTipBkColor | Sets the background color in a tool tip window. | SetTipTextColor | Sets the text color in a tool tip window. | SetToolInfo | Sets the information that a tool tip maintains for a tool. | SetWindowTheme | Sets the visual style of the tool tip window. | Activate | Activates and deactivates the tool tip control. | AddTool | Registers a tool with the tool tip control. | AdjustRect | Converts between a tool tip control's text display rectangle and its window rectangle. | DelTool | Removes a tool from the tool tip control. | HitTest | Tests a point to determine whether it is within the bounding rectangle of the given tool. If so, retrieves information about the tool. | Pop | Removes a displayed tool tip window from view. | Popup | Causes the current ToolTip control to display at the coordinates of the last mouse message. | RelayEvent | Passes a mouse message to a tool tip control for processing. | SetTitle | Adds a standard icon and title string to a tool tip. | SetToolRect | Sets a new bounding rectangle for a tool. | Update | Forces the current tool to be redrawn. | UpdateTipText | Sets the tool tip text for a tool. |
為了避免同一個文件被include多次 1 #ifndef方式 2 #pragma once方式 在能夠支持這兩種方式的編譯器上,二者并沒有太大的區別,但是兩者仍然還是有一些細微的區別。 方式一: #ifndef __SOMEFILE_H__ #define __SOMEFILE_H__ ... ... // 一些聲明語句 #endif 方式二: #pragma once ... ... // 一些聲明語句 #ifndef的方式依賴于宏名字不能沖突,這不光可以保證同一個文件不會被包含多次,也能保證內容完全相同的兩個文件不會被不小心同時包含。當然,缺點就是如果不同頭文件的宏名不小心“撞車”,可能就會導致頭文件明明存在,編譯器卻硬說找不到聲明的狀況 #pragma once則由編譯器提供保證:同一個文件不會被包含多次。注意這里所說的“同一個文件”是指物理上的一個文件,而不是指內容相同的兩個文件。帶來的好處是,你不必再費勁想個宏名了,當然也就不會出現宏名碰撞引發的奇怪問題。對應的缺點就是如果某個頭文件有多份拷貝,本方法不能保證他們不被重復包含。當然,相比宏名碰撞引發的“找不到聲明”的問題,重復包含更容易被發現并修正。 方式一由語言支持所以移植性好,方式二 可以避免名字沖突
#include<iostream.h> class point { public: int x; int y; point() { x=0; y=0; } point(int a,int b) { x=a; y=b; } void output() { cout<<x<<endl<<y<<endl; } void input(int x,int y) { this->x=x; this->y=y; }
}
void main() { point pt(5,5); pt.input(10,10); pt.output();
} 輸出結果為10 10
如果把this指針去掉則為5 5
在使用構造函數時注意是否需要釋放內存。~構造函數名()利用析構函數解決內存泄漏問題。
MessageBox(hwnd,szChar,"char",0);
MessageBox 在2008中定義為 MessageBoxW W指的是寬字節(也叫UNICODE),有3種方法可解決 ①用函數MessageBoxA ②在內容前加上TEXT(對變量無效),如MessageBox(hwnd,szChar,TEXT("char"),0);
③在項目屬性->常規中,把Uicode改成多字符段。
PS:在2008中,很多函數的返回值都是寬字節的,所以不一定要用MessageBoxA 在MSDN上可以查到用寬字節的函數和同樣功能普通函數的名稱。 在6.0中沒用寬字節
TCHAR字符串操作函數: _tcslen(str) 獲得字符串長度 _tcsrchr(str, L'\\') 反向搜索獲得最后一個TCHAR的位置 _stprintf(TCHAR *buffer,const TCHAR *format [,argument] ... )獲得一個格式化字符串 _tcsdup 給一個指針分配源字符串大小的內存并從源字符串copy值 _tcstok 按標記將字符串拆分 tcscpy 拷貝字符串
CString的構造函數 CString( ); 例:CString csStr;
CString( const CString& stringSrc ); 例:CString csStr("ABCDEF中文123456"); CString csStr2(csStr);
CString( TCHAR ch, int nRepeat = 1 ); 例:CString csStr('a',5); //csStr="aaaaa"
CString( LPCTSTR lpch, int nLength ); 例:CString csStr("abcdef",3); //csStr="abc"
CString( LPCWSTR lpsz ); 例:wchar_t s[]=L"abcdef"; CString csStr(s); //csStr=L"abcdef"
CString( const unsigned char* psz ); 例:const unsigned char s[]="abcdef"; const unsigned char* sp=s; CString csStr(sp); //csStr="abcdef"
CString( LPCSTR lpsz ); 例:CString csStr("abcdef"); //csStr="abcdef"
int GetLength( ) const; 返回字符串的長度,不包含結尾的空字符。 例:csStr="ABCDEF中文123456"; printf("%d",csStr.GetLength()); //16
void MakeReverse( ); 顛倒字符串的順序 例:csStr="ABCDEF中文123456"; csStr.MakeReverse(); cout<<csStr; //654321文中FEDCBA
void MakeUpper( ); 將小寫字母轉換為大寫字母 例:csStr="abcdef中文123456"; csStr.MakeUpper(); cout<<csStr; //ABCDEF中文123456
void MakeLower( ); 將大寫字母轉換為小寫字母 例:csStr="ABCDEF中文123456"; csStr.MakeLower(); cout<<csStr; //abcdef中文123456
int Compare( LPCTSTR lpsz ) const; 區分大小寫比較兩個字符串,相等時返回0,大于時返回1,小于時返回-1 例:csStr="abcdef中文123456"; csStr2="ABCDEF中文123456"; cout<<csStr.CompareNoCase(csStr2); //0
int CompareNoCase( LPCTSTR lpsz ) const; 不區分大小寫比較兩個字符串,相等時返回0,大于時返回1,小于時返回-1 例:csStr="abcdef中文123456"; csStr2="ABCDEF中文123456"; cout<<csStr.CompareNoCase(csStr2); //-1
int Delete( int nIndex, int nCount = 1 ) 刪除字符,刪除從下標nIndex開始的nCount個字符 例:csStr="ABCDEF"; csStr.Delete(2,3); cout<<csStr; // ABF //當nIndex過大,超出對像所在內存區域時,函數沒有任何操作。 //當nIndex為負數時,從第一個字符開始刪除。 //當nCount過大,導致刪除字符超出對像所在內存區域時,會發生無法預料的結果。 //當nCount為負數時,函數沒有任何操作。
int Insert( int nIndex, TCHAR ch ) int Insert( int nIndex, LPCTSTR pstr ) 在下標為nIndex的位置,插入字符或字符串。返回插入后對象的長度 例:csStr="abc"; csStr.Insert(2,'x'); cout<<csStr; //abxc csStr="abc"; csStr.Insert(2,"xyz"); cout<<csStr; //abxyzc //當nIndex為負數時,插入在對象開頭 //當nIndex超出對象末尾時,插入在對象末尾
int Remove( TCHAR ch ); 移除對象內的指定字符。返回移除的數目 例:csStr="aabbaacc"; csStr.Remove('a'); cout<<csStr; //bbcc
int Replace( TCHAR chOld, TCHAR chNew ); int Replace( LPCTSTR lpszOld, LPCTSTR lpszNew ); 替換字串 例:csStr="abcdef"; csStr.Replace('a','x'); cout<<csStr; //xbcdef csStr="abcdef"; csStr.Replace("abc","xyz"); cout<<csStr; //xyzdef
void TrimLeft( ); void TrimLeft( TCHAR chTarget ); void TrimLeft( LPCTSTR lpszTargets ); 從左刪除字符,被刪的字符與chTarget或lpszTargets匹配,一直刪到第一個不匹配的字符為止 例:csStr="aaabaacdef"; csStr.TrimLeft('a'); cout<<csStr; //baacdef csStr="aaabaacdef"; csStr.TrimLeft("ab"); cout<<csStr; //cdef //無參數時刪除空格
void TrimRight( ); void TrimRight( TCHAR chTarget ); void TrimRight( LPCTSTR lpszTargets ); 從右刪除字符,被刪的字符與chTarget或lpszTargets匹配,一直刪到第一個不匹配的字符為止 例:csStr="abcdeaafaaa"; csStr.TrimRight('a'); cout<<csStr; //abcdeaaf csStr="abcdeaafaaa"; csStr.TrimRight("fa"); cout<<csStr; //abcde //無參數時刪除空格
void Empty( ); 清空 例:csStr="abcdef"; csStr.Empty(); printf("%d",csStr.GetLength()); //0
BOOL IsEmpty( ) const; 測試對象是否為空,為空時返回零,不為空時返回非零 例:csStr="abc"; cout<<csStr.IsEmpty(); //0; csStr.Empty(); cout<<csStr.IsEmpty(); //1;
int Find( TCHAR ch ) const; int Find( LPCTSTR lpszSub ) const; int Find( TCHAR ch, int nStart ) const; int Find( LPCTSTR pstr, int nStart ) const; 查找字串,nStart為開始查找的位置。未找到匹配時返回-1,否則返回字串的開始位置 例:csStr="abcdef"; cout<<csStr.Find('b'); //1 cout<<csStr.Find("de"); //3 cout<<csStr.Find('b',3); //-1 cout<<csStr.Find('b',0); //1 cout<<csStr.Find("de",4); //-1 cout<<csStr.Find("de",0); //3 //當nStart超出對象末尾時,返回-1。 //當nStart為負數時,返回-1。
int FindOneOf( LPCTSTR lpszCharSet ) const; 查找lpszCharSet中任意一個字符在CString對象中的匹配位置。未找到時返回-1,否則返回字串的開始位置 例:csStr="abcdef"; cout<<csStr.FindOneOf("cxy"); //2
CString SpanExcluding( LPCTSTR lpszCharSet ) const; 返回對象中與lpszCharSet中任意匹配的第一個字符之前的子串 例:csStr="abcdef"; cout<<csStr.SpanExcluding("cf"); //ab
CString SpanIncluding( LPCTSTR lpszCharSet ) const; 從對象中查找與lpszCharSe中任意字符不匹配的字符,并返回第一個不匹配字符之前的字串 例:csStr="abcdef"; cout<<csStr.SpanIncluding("fdcba"); //abcd
int ReverseFind( TCHAR ch ) const; 從后向前查找第一個匹配,找到時返回下標。沒找到時返回-1 例:csStr="abba"; cout<<csStr.ReverseFind('a'); //3
void Format( LPCTSTR lpszFormat, ... ); void Format( UINT nFormatID, ... ); 格式化對象,與C語言的sprintf函數用法相同 例:csStr.Format("%d",13); cout<<csStr; //13
TCHAR GetAt( int nIndex ) const; 返回下標為nIndex的字符,與字符串的[]用法相同 例:csStr="abcdef"; cout<<csStr.GetAt(2); //c //當nIndex為負數或超出對象末尾時,會發生無法預料的結果。
void SetAt( int nIndex, TCHAR ch ); 給下標為nIndex的字符重新賦值 例:csStr="abcdef"; csStr.SetAt(2,'x'); cout<<csStr; //abxdef //當nIndex為負數或超出對象末尾時,會發生無法預料的結果。
CString Left( int nCount ) const; 從左取字串 例:csStr="abcdef"; cout<<csStr.Left(3); //abc //當nCount等于0時,返回空。 //當nCount為負數時,返回空。 //當nCount大于對象長度時,返回值與對象相同。
CString Right( int nCount ) const; 從右取字串 例:csStr="abcdef"; cout<<csStr.Right(3); //def //當nCount等于0時,返回空。 //當nCount為負數時,返回空。 //當nCount大于對象長度時,返回值與對象相同。
CString Mid( int nFirst ) const; CString Mid( int nFirst, int nCount ) const; 從中間開始取字串 例:csStr="abcdef"; cout<<csStr.Mid(2); //cdef csStr="abcdef"; cout<<csStr.Mid(2,3); //cde //當nFirst為0和為負數時,從第一個字符開始取。 //當nFirst等于對象末尾時,返回空字串。 //當nFirst超出對象末尾時,會發生無法預料的結果。 //當nCount超出對象末尾時,返回從nFirst開始一直到對象末尾的字串 //當nCount為0和為負數時,返回空字串。
LPTSTR GetBuffer( int nMinBufLength ); 申請新的空間,并返回指針 例:csStr="abcde"; LPTSTR pStr=csStr.GetBuffer(10); strcpy(pStr,"12345"); csStr.ReleaseBuffer(); pStr=NULL; cout<<csStr //12345 //使用完GetBuffer后,必須使用ReleaseBuffer以更新對象內部數據,否則會發生無法預料的結果。
void ReleaseBuffer( int nNewLength = -1 ); 使用GetBuffer后,必須使用ReleaseBuffer以更新對象內部數據 例:csStr="abc"; LPTSTR pStr=csStr.GetBuffer(10); strcpy(pStr,"12345"); cout<<csStr.GetLength(); //3(錯誤的用法) csStr.ReleaseBuffer(); cout<<csStr.GetLength(); //5(正確) pStr=NULL; //CString對象的任何方法都應在ReleaseBuffer之后調用
LPTSTR GetBufferSetLength( int nNewLength ); 申請新的空間,并返回指針 例:csStr="abc"; csStr.GetBufferSetLength(20); cout<<csStr; //abc count<<csStr.GetLength(); //20; csStr.ReleaseBuffer(); count<<csStr.GetLength(); //3; //使用GetBufferSetLength后可以不必使用ReleaseBuffer。
CString ImagePath = "d:/abc/dd.avi";
int i = ImagePath.ReverseFind(_T('/')); CString strtest = ImagePath.Left(i+1);
pasting
int _stricmp( const char *string1, const char *string2 ); strcmp 用來比較ANSI字符串,而_tcscmp用 來比較UNICODE(寬字符)的字符串。ANSI字符串中,1個英文字母為1個字節,1個中文字符為2個字節,遇到0字符表示字符串結束。而在 UNICODE(寬字符)中,所有的字符都為2個字節,此時字符串中間的字節,可能含有0字符,此時就不能用strcmp比較了。strcmp(char *s1, char *s2) : 按照各個字符(ascii)比較s1和s2,相等則返回0,否則返回ascii相減的結果。
int _stricmp(
const char *string1,
const char *string2
);
Return value |
Description |
< 0 |
string1 less than string2 |
0 |
string1 identical to string2 |
> 0 |
string1 greater than string2 |
CFileDialog 文件選擇對話框的使用:首先構造一個對象并提供相應的參數,構造函數原型如下: CFileDialog::CFileDialog( BOOL bOpenFileDialog, LPCTSTR lpszDefExt = NULL, LPCTSTR lpszFileName = NULL, DWORD dwFlags = OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, LPCTSTR lpszFilter = NULL, CWnd* pParentWnd = NULL );
例子:CFileDialog mFileDlg(FALSE, NULL, NULL, OFN_ALLOWMULTISELECT, _T("所有文件 (*.*)|*.*||"), this);
參數意義如下:
bOpenFileDialog 為TRUE則顯示打開對話框,為FALSE則顯示保存對話文件對話框。 lpszDefExt 指定默認的文件擴展名。 lpszFileName 指定默認的文件名。 dwFlags 指明一些特定風格。 lpszFilter 是最重要的一個參數,它指明可供選擇的文件類型和相應的擴展名。參數格式如: "Chart Files (*.xlc)|*.xlc|Worksheet Files (*.xls)|*.xls|Data Files (*.xlc;*.xls)|*.xlc; *.xls|All Files (*.*)|*.*||";文件類型說明和擴展名間用 | 分隔,同種類型文件的擴展名間可以用 ; 分割,每種文件類型間用 | 分隔,末尾用 || 指明。 pParentWnd 為父窗口指針。 創建文件對話框可以使用DoModal(),在返回后可以利用下面的函數得到用戶選擇: CString CFileDialog::GetPathName( ) 得到完整的文件名,包括目錄名和擴展名如:c: est est1.txt CString CFileDialog::GetFileName( ) 得到完整的文件名,包括擴展名如:test1.txt CString CFileDialog::GetExtName( ) 得到完整的文件擴展名,如:txt CString CFileDialog::GetFileTitle ( ) 得到完整的文件名,不包括目錄名和擴展名如:test1 POSITION CFileDialog::GetStartPosition( ) 對于選擇了多個文件的情況得到第一個文件位置。 CString CFileDialog::GetNextPathName( POSITION& pos ) 對于選擇了多個文件的情況得到下一個文件位置,并同時返回當前文件名。但必須已經調用過POSITION CFileDialog::GetStartPosition( )來得到最初的POSITION變量。
|