最近对特殊字符处理比较头疼,请各位教小弟我
最近对特殊字符处理比较头疼,请各位教我。
例如,®字符
我想在textbox输入框中显示,结果不行,显示?
想将这个字符写道txt文件中,也不行,怎么办?
------解决方案--------------------
1,到word中,使用插入符号,将你兴趣的符号插入到word文档中
2,word中选择该符号
3,在工具菜单中,点开Vb编辑器,在立即窗口中输入 ?hex(ascw(selection.Text))获得符号16进制的unicode编码
4,在VBForm中,autoredraw设置为true
5,在Form代码中,
例如,®字符
我想在textbox输入框中显示,结果不行,显示?
想将这个字符写道txt文件中,也不行,怎么办?
------解决方案--------------------
1,到word中,使用插入符号,将你兴趣的符号插入到word文档中
2,word中选择该符号
3,在工具菜单中,点开Vb编辑器,在立即窗口中输入 ?hex(ascw(selection.Text))获得符号16进制的unicode编码
4,在VBForm中,autoredraw设置为true
5,在Form代码中,
- VB code
Option Explicit Private Declare Function TextOutW Lib "gdi32" (ByVal hdc As Long, ByVal X As Long, ByVal Y As Long, ByVal lpString As Long, ByVal nCount As Long) As Long Private Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long Private Declare Function ReleaseDC Lib "user32" (ByVal hwnd As Long, ByVal hdc As Long) As Long ’演示特殊符号输出到Text中,该方式只是演示,非常不好控制 Private Sub Form_Click() Dim s(1) As Byte s(0) = &HAE s(1) = 0 Dim hdc As Long hdc = GetDC(Text1.hwnd) TextOutW hdc, 10, 10, VarPtr(s(0)), 1 'draw the text ReleaseDC Text1.hwnd, hdc End Sub '演示特殊符号输出到窗口中 Private Sub Form_Load() Dim s(1) As Byte s(0) = &HAE s(1) = 0 FontSize = 20 TextOutW Me.hdc, 10, 10, VarPtr(s(0)), 1 'draw the text End Sub
------解决方案--------------------
你可以看看我的这篇文章,里面有几个函数,可以了解到字符串处理的一些知识,另外再给你看看
几个例子,应该可以帮你具体了解字符串处理方面的方法。
以下例子虽然是C++写的,不过也大同小异:
- C/C++ code
//---------------------------------------------------------------- // Function Name: LenB(W/A) // Return Value: (BOOL) - return string byte size. // Description : Account string byte size. //---------------------------------------------------------------- // LenB unicode edition DWORD LenBW(LPWSTR lpExpression) // { DWORD i = 0; if(lpExpression==NULL) return 0; try{ while(lpExpression[i]!=0) { i++; } i=i*2; }catch(...){ i = 0; } return i; } // LenB ascii edition DWORD LenBA(LPSTR lpExpression) // { DWORD i = 0; if(lpExpression==NULL) return 0; try{ while(lpExpression[i]!=0) { i++; } }catch(...){ i = 0; } return i; } //---------------------------------------------------------------- // Function Name: Len(W/A) // Return Value: (BOOL) - return string's word count. // Description : Account string's word count. //---------------------------------------------------------------- // Len unicode edition DWORD LenW(LPWSTR lpExpression) // { DWORD i = 0; if(lpExpression==NULL) return 0; try{ while(lpExpression[i]!=0) { i++; } }catch(...){ i = 0; } return i; } // Len ascii edition DWORD LenA(LPSTR lpExpression) // { DWORD i = 0; DWORD WordCount = 0; if(lpExpression==NULL) return 0; try{ while(lpExpression[i]!=0) { if((BYTE)lpExpression[i]>=0xA0){ WordCount++; i++; if(lpExpression[i]==0){ WordCount--; break; } }else WordCount++; i++; } }catch(...){ WordCount = 0; } return WordCount; } //---------------------------------------------------------------- // Function Name: InStrB(W/A) // Return Value: (DWORD) - // Description : //---------------------------------------------------------------- // InStrB unicode edition DWORD InStrBW(DWORD dwStart, // LPWSTR lpString1, // LPWSTR lpString2) // { DWORD i = 0; DWORD j = 0; DWORD isret = 0; DWORD PrimaryLenB = LenW(lpString1); DWORD FindLenB = LenW(lpString2); for(i=dwStart;i<PrimaryLenB;i++){ isret=1; for(j=0;j<FindLenB;j++){ if(lpString1[i+j]!=lpString2[j]){ isret=0; break; } } if(isret==1){ i=i*2; return i; break; } } return 0; } // InStrB ascii edition DWORD InStrBA(DWORD dwStart, // LPSTR lpString1, // LPSTR lpString2) // { DWORD i = 0; DWORD j = 0; DWORD isret = 0; DWORD PrimaryLenB = LenB(lpString1); DWORD FindLenB = LenB(lpString2); for(i=dwStart;i<PrimaryLenB;i++){ isret=1; for(j=0;j<FindLenB;j++){ if(lpString1[i+j]!=lpString2[j]){ isret=0; break; } } if(isret==1){ return i; break; } } return 0; } //---------------------------------------------------------------- // Function Name: InStr(W/A) // Return Value: (DWORD) - // Description : //---------------------------------------------------------------- // InStr unicode edition DWORD InStrW(DWORD dwStart, // LPWSTR lpString1, // LPWSTR lpString2) // { DWORD i = 0; DWORD j = 0; DWORD isret = 0; DWORD PrimaryLenB = LenW(lpString1); DWORD FindLenB = LenW(lpString2); for(i=dwStart;i<PrimaryLenB;i++){ isret=1; for(j=0;j<FindLenB;j++){ if(lpString1[i+j]!=lpString2[j]){ isret=0; break; } } if(isret==1){ return i; break; } } return 0; } // InStr ascii edition DWORD InStrA(DWORD dwStart, // LPSTR lpString1, // LPSTR lpString2) // { DWORD i = 0; DWORD j = 0; DWORD isret = 0; DWORD PrimaryLenB = LenB(lpString1); DWORD FindLenB = LenB(lpString2); DWORD WordCount = 0; DWORD IsStart = 0; for(i=0;i<PrimaryLenB;i++){ if(dwStart==WordCount){ IsStart = 1; } if(IsStart==1){ isret=1; for(j=0;j<FindLenB;j++){ if(lpString1[i+j]!=lpString2[j]){ isret=0; break; } } if(isret==1){ WordCount++; return WordCount; break; } } WordCount++; if((BYTE)lpString1[i]>=0xA0){ //属于中文字符 i++; } } return 0; } //---------------------------------------------------------------- // Function Name: Left(W/A) // Return Value: (LPWSTR) - // Description : //---------------------------------------------------------------- // Left unicode edition LPWSTR LeftW(LPWSTR lpString, // DWORD dwLength) // { DWORD PrimaryLen = LenW(lpString); if(dwLength>PrimaryLen) return NULL; WCHAR *tmpValue; tmpValue = (WCHAR *)GlobalAlloc(GMEM_ZEROINIT, dwLength*sizeof(WCHAR) + sizeof(WCHAR)); for(DWORD i=0;i<dwLength;i++){ tmpValue[i]=lpString[i]; } tmpValue[dwLength] = 0; return tmpValue; } // Left ascii edition LPSTR LeftA(LPSTR lpString, // DWORD dwLength) // { char *tmpValue; DWORD i; DWORD dwTextCount; DWORD dwByteCount; DWORD PrimaryLen = Len(lpString); DWORD PrimaryLenB = LenB(lpString); DWORD dwNewLenB; if(dwLength>PrimaryLen) return NULL; // 先数要提取的 ASCII 字符所占用的字节数 dwTextCount = 0; dwByteCount = 0; for(i=0;dwTextCount<dwLength;i++){ dwTextCount++; dwByteCount++; if((BYTE)lpString[i]>=0xA0){ // 中文字符 dwByteCount++; i++; } } // 开始提取字符数据 dwNewLenB = dwByteCount; tmpValue = (char *)GlobalAlloc(GMEM_ZEROINIT, dwNewLenB + 1); for(i=0;i<dwNewLenB;i++){ tmpValue[i] = lpString[i]; } tmpValue[dwNewLenB] = 0; return tmpValue; }