在Delphi放一個TMemo名為TxtBox那麼用TxtBox.Lines.GetText這一句就可以得到所有的內容.這過是PChar類的.那能不能把PChar轉成Unicode?<br><br> 在Delphi的在Help裡有以下三行:<br> A PChar is a pointer to a null-terminated string of 8-bit characters.<br> A PAnsiChar is a pointer to a null-terminated string of 8-bit characters.<br> A PWideChar is a pointer to a null-terminated string of 16-bit characters.<br> <br> 那麼PChar<->PWideChar這樣相互轉換.轉換成PWideChar後是不是就等是Unicode字符了,如果是的話,那TMemo能用Add語句加入PWideChar嗎,或加入Unicode字符嗎?<br><br> 其實說這麼多,我真正的用意是想統計TMemo裡有幾個字符.不轉為Unicode的話,用lenght算出的數不對.英文字符與中文不一樣.如果隻有英文的話能算對.有中文就不行了.<br> 像上例VB的代碼:TxtBox.Text = StrConv(InputB$(LOF(1), 1), vbUnicode)真的是簡單.VB的完整代碼(就是一個過程,過程是彈出一個打開文件的對話框,選擇一個文本文件後,把文件的內容Load到TxtBox裡,Load的時就轉換了):VB裡用'表示注釋一行就像Delphi裡的//一樣<br><br>Private Sub menuFileOpenText_Click() <br> Dim sLocation As String <br> ' Set CancelError is True<br> ComDlg.CancelError = True<br> On Error GoTo ErrHandler <br> ' Set flags<br> ComDlg.Flags = cdlOFNFileMustExist Or cdlOFNPathMustExist<br> ' Set Dialog title<br> ComDlg.DialogTitle = "Open a Text File"<br> ' Set open directory<br> sLocation = GetDirectory()<br> If Len(sLocation) <> 0 Then<br> ComDlg.InitDir = sLocation<br> End If <br> ' Set filters<br> ComDlg.Filter = "All Files (*.*)|*.*|Text, XML Files " & "(*.txt;*.xml)|*.txt;*.xml" <br> ' Specify default filter<br> ComDlg.FilterIndex = 2<br> ' Display the Open dialog box<br> ComDlg.ShowOpen <br> ' Now open the text file and open it in the text box.<br> ' We only support text files encoded with the system code page as the<br> ' binary to unicode conversion in VB is using system code page.<br> Open ComDlg.FileName For Binary Access Read As 1<br> TxtBox.Text = StrConv(InputB$(LOF(1), 1), vbUnicode)<br> Close #1 <br> Exit Sub <br>ErrHandler:<br> 'User pressed the Cancel button, do not show error<br> If Not (Err.Number = 32755) Then<br> AddDebugInfo "Open file: ", Err.Description<br> End If<br>End Sub<br><br>注在以上代碼中這一句:Open ComDlg.FileName For Binary Access Read As 1好像也很重要哦.是以2進制方式讀文件的.