VBGood网站全文搜索 Google

搜索VBGood全站网页(全文搜索)

VB爱好者乐园(VBGood)

 找回密码
 立即注册
搜索
查看: 2136|回复: 2

[转帖] 也许你不知道,NT系统下TextBox文本框是可以支持超过64K文本滴!

[复制链接]
发表于 2013-6-15 13:01:17 | 显示全部楼层 |阅读模式
只需是用API设置文本即可,一句搞定!

看原文:
Load Large File Into a Textbox
Q. I thought that most 64K limits were a thing of the past--notably that psuedo-32bit OS, Windows 9x. However, when I put a multiline textbox on a form (under NT4) and tried to load a large file into it, the file was truncated, and Len(txtBox.Text) returned 65,535. Can you provide an example of how to load more than 64K into a VB textbox?

A. Microsoft designed all the intrinsic controls to lowest-common-denominator specs. Because much of Windows 9x is still 16-bit, Microsoft made compromises "for your protection."

Under NT, the underlying edit class is fully 32-bit, but you must go entirely to the API if you want to exceed the built-in 64K limits. The most reliable method to load large amounts of text into a VB textbox is to use SendMessage with WM_SETTEXT (see Listing 3). The SetWindowText API wraps this message, but has the drawback that it doesn't return the proper result code when it fails under Windows 9x, should your code also find itself running there.




VB4/32, VB5, VB6  Defy 16-Bit Limits
Option Explicit

Private Declare Function SendMessage Lib "user32" Alias _
   "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As _
   Long, ByVal wParam As Long, lParam As Any) As Long
Private Declare Function SetWindowText Lib "user32" _
   Alias "SetWindowTextA" (ByVal hWnd As Long, ByVal _
   lpString As String) As Long
Private Declare Function GetWindowTextLength Lib _
   "user32" Alias "GetWindowTextLengthA" (ByVal hWnd As _
   Long) As Long

Private Const WM_SETTEXT = &HC
Private Const WM_GETTEXT = &HD
Private Const WM_GETTEXTLENGTH = &HE

Private Sub Form_Load()
   Dim nRet As Long
   Dim s As String

   ' Create string with >64K chars.
   s = String(66000, "X")
   Me.Show

   ' Try setting large amount of text.
   nRet = SendMessage(Text1.hWnd, WM_SETTEXT, 0&, _
      ByVal s)
   Debug.Print "WM_SETTEXT: " & nRet

   ' See how much made it.
   nRet = SendMessage(Text1.hWnd, WM_GETTEXTLENGTH, _
      0&, ByVal 0&)
   Debug.Print "WM_GETTEXTLENGTH: " & nRet

   ' This call doesn't report failure
   ' under Windows 9x!
   nRet = SetWindowText(Text1.hWnd, s)
   Debug.Print "SetWindowText: " & nRet

   ' Just for kicks...
   nRet = GetWindowTextLength(Text1.hWnd)
   Debug.Print "GetWindowTextLength: " & nRet
End Sub

Private Sub Form_Resize()
   Text1.Move 0, 0, Me.ScaleWidth, Me.ScaleHeight
End Sub



Listing 3 IEither of these two methods can load more than 64K of text into a standard VB textbox. Both work only in Windows NT/2000. Adding to the Windows 9x frustration is the failure of the SetWindowText API to report its own failure in that environment. Stick with SendMessage for the most reliable results, and insist on NT if you need full 32-bit capabilities.

点评

没死,要卡十几秒而已  发表于 2013-6-15 19:42
奇怪,我这里执行这句代码后程序就死了  发表于 2013-6-15 17:56
火星人,Text1.SelText=String(1048576,"A")就解决问题  发表于 2013-6-15 16:28
 楼主| 发表于 2013-6-15 13:20:28 | 显示全部楼层
再转:关于 SendMessage 使用的问题(在Text1中,转到指定行、设置光标位置、返回光标位置、获得光标所在行的位置)
http://bbs.csdn.net/topics/300217271
回复 支持 反对

使用道具 举报

头像被屏蔽
发表于 2013-6-15 13:40:03 | 显示全部楼层
提示: 作者被禁止或删除 内容自动屏蔽
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

文字版|手机版|小黑屋|VBGood  

GMT+8, 2022-7-3 01:46

VB爱好者乐园(VBGood)
快速回复 返回顶部 返回列表