VB《葵花宝典》--指针技术,快写完,看看先。

老熊宝宝 2002-02-27 11:30:07
这几天没来CSDN,我没闲着,我在深入研究VB指针技术。
有这个想法,是因为在写《API/COM调用,VB文档里没有东西》
时出了问题,我写了一万四千多字,连API调用的问题都没有谈
完,而且由于写之前没有计划,结构很乱,于是我决定完全重写
并把这些内容分成三篇文章来谈,《VB《葵花宝典》--指针技术》
已经基本写完。这篇文章的目的,就是要在VB里尽可能引入指针
技术。
为了写好这篇文章,这五天来我进行了深入的研究,跟踪、
测试、改进代码、研究可行性。
我的结论是:在VB里有很多地方使用指针技术可以大大提高
程序性能,不需要写用别的语写DLL,就在VB里我们也可以。
为了研究可行性,我甚至完全用指针技术实现了动态存储分配的
带头结点的单向链表。为比较性能,我在VB里分别用堆栈HeapAlloc、
虚拟内存VirtualAlloc,和原来的内存映射文件技术实现Malloc/Free
的内存动态分配功能,最终的决定还是使用和C库一样堆栈技术来实现
Malloc/Free。
虽然最终的程序需要反复用CopyMemory在动态申请的内存和
VB能直接使用的自定义类型变量之间拷贝数据,但另人吃惊的是,
最后实现的指针链表性能和用对象实现的链表基本一样,在节点
数小于10000时,指针链表甚至比对象链表还要快一点点。
当然,这一点点的提高不能说服我们去使用动态内存分配,虽然
可行性上失败了,但是我证明了可能性。在使用有些需要调用程序提
供缓冲区的API时,我们除了Byte Array和String外我们又多了一种
选择,我的实验证明用堆栈HeapAlloc还是有价值的。
无论如何,我的这篇文章敢称《葵花宝典》,肯定得拿出些更
有说服力更有实用价值的东西。在VB里指针我已证明至少有两点应用
还是非常有价值的,一是交换指针而不交换数据的技术,下面的SwapPtr
比SwapStr快1倍,这是很可观的;二是直接在数组里搬数据而不用VB的
数组下标来做,在数组较大时这种做法在效率上的提高非常可观。

'用指针的SwapPtr
Private Declare Sub CopyMemory Lib "kernel32" Alias _

"RtlMoveMemory" (Destination As Any, Source As Any, _
ByVal Length As Long)

Sub SwapPtr(sA As String, sB As String)
Dim lTmp As Long
CopyMemory lTmp, ByVal VarPtr(sA), 4
CopyMemory ByVal VarPtr(sA), ByVal VarPtr(sB), 4
CopyMemory ByVal VarPtr(sB), lTmp, 4
End Sub
'用StrPtr上面的程序还可以更快。

'标准的做法SwapStr
Sub SwapStr(sA As String, sB As String)
Dim sTmp As String
sTmp = sA: sA = sB: sB = sTmp
End Sub
在我的机器上:
运行100000次,SwapStr需要170毫秒,SwapPtr需要90毫秒。
运行200000次,SwapStr需要340毫秒,SwapPtr需要170毫秒。
运行2000000次,SwapStr需要3300毫秒,SwapPtr需要1500毫秒

'搬数组:
Dim value(20) as MyType
假设数组已经有数据,现要从第1个元素开始的10个元素需要顺序
后移到第11个元素开始的10个元素。
'用指针两句话:
CopyMemory ByVal VarPtr(value(11)), _
ByVal VarPtr(value(1)), _
10 * itemLen
ZeroMemory ByVal VarPtr(value(1)), 10*itemLen

以上简单谈了一下这几天的成果,只是要告诉大家我没来CSDN也没闲着

,如果有VB里比较有挑战性的问题,请给我来信,
我的地址:xcbear@netease.com
需要说明,不是我不愿意回答VB文档里就有的问题,因为如果我
这次回答了,你就失去了一次自己解决问题的机会。
比如关于API的问题实际上有很多在不停的重复,问人之前请查一
查先。


-微软非官方技术研究自由人,VB非官方技术支持

本贴子仅供参考。本人不提供数据库、VB文档已有说明的和任何未经思

考的技术问题之支持。若你有百思不得其解的问题,请给本人发短消息

或写信。
我的地址:xcbear@netease.com



...全文
124 20 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
hand2001 2002-09-06
  • 打赏
  • 举报
回复
up
maoqingfeng 2002-09-06
  • 打赏
  • 举报
回复
up
cwbboy 2002-03-13
  • 打赏
  • 举报
回复
你真棒!!!
老熊宝宝 2002-03-04
  • 打赏
  • 举报
回复
第二篇文章已经出来了,不过不知怎么到了VC版里了。
快到
http://www.csdn.net/develop/list_article.asp?author=AdamBear
去看。
老熊宝宝 2002-03-04
  • 打赏
  • 举报
回复
完整写的文章都在:
http://www.csdn.net/develop/list_article.asp?author=AdamBear
在贴子里写的,那都不是文章,只是讨论。
lihanbing 2002-03-03
  • 打赏
  • 举报
回复
有多大用处呢?
DullMan 2002-03-03
  • 打赏
  • 举报
回复
gz
Bardo 2002-03-03
  • 打赏
  • 举报
回复
我觉得这个问题不需要劳很多神!
因为MSDN:
Partial books
Win32 API programing With Visual Basic
这里只列了一个章节
这篇实际上已经将字串指针讲得非常清楚了!!
iwzw 2002-03-03
  • 打赏
  • 举报
回复
ok
wgku 2002-03-03
  • 打赏
  • 举报
回复
TO AdamBear (学习再学习)
你的文章真不错,不过我看过的不全,问一下你能不能把你的文章地址都贴一下
这样就不要从一篇串连到另一篇又跳到这一篇的
直接把地址贴在一个地方让大家方便一下如何???就贴在这如何??谢谢,很佩服你的钻研精神。
老熊宝宝 2002-03-03
  • 打赏
  • 举报
回复
to shawls:
你说的FlatScollBar的问题,是VB的一个大BUG,下面的文章可能对
你有用:
就是应该在 Private Sub Form_Activate()用代码设置:
ListView1.FlatScrollBar = True
下面的东西是MSDN里的:
SYMPTOMS
When using the FlatScrollBar in the ListView control you may run into one of the following problems:


If you check the FlatScrollBar property of the ListView in the IDE, you will not see a ScrollBar when you run the form.

-or-


The FlatScrollBar does not appear to track properly on the ListView control. The control scrolls the columns properly, but the ScrollBar "thumb" position does not match.





RESOLUTION
For problem number 1, set the FlatScrollBar property for the ListView control in the form Activate instead of the IDE.

There is no resolution for problem number 2 at this time.



STATUS
Microsoft has confirmed this to be a bug in the Microsoft products listed at the beginning of this article. We are researching this bug and will post new information here in the Microsoft Knowledge Base as it becomes available.



MORE INFORMATION

Steps to Reproduce Behavior
Create a new Standard EXE project. Form1 is created by default.


Using the Projects menu, select Components to bring up the Components dialog box. On the Controls Tab, select "Microsoft Windows Common Controls 6.0," and then click OK.


Place a ListView and ImageList control on Form1.


Add four Images to the ImageList Control using the Images Tab of the ImageList control's Property Page.


In the Properties window, for the ListView control set the FlatScrollBar property to True.


Add the following code to Form1's code window:

Option Explicit

Private Sub Form_Activate()
' Uncomment the next line to show the scroll bar as Flat.
' For this to work you must make sure that you don't have
' FlatScrollBar set to True in the properties window of the ListView.
'ListView1.FlatScrollBar = True
End Sub

Private Sub Form_Load()
Dim i As Integer
Dim y As Integer
y = 1

' Add some items to the ListView control
For i = 1 To 40
ListView1.ListItems.Add Text:="test" & Str(i), Icon:=y
y = y + 1
If y = 5 Then y = 1
Next i
End Sub




Save and run the project. You will not have a ScrollBar on the ListView control.


In the Properties window for the ListView control, set the FlatScrollBar property to False, and then uncomment the following line of code in the Form_Activate:

ListView1.FlatScrollBar = True




Save and run the project. You now have a flat ScrollBar on the ListView control.


Try using the ScrollBar to scroll to the bottom of the ListView control. You will see the control scroll past the last items in the ListView and the ScrollBar will go to the bottom. But after you release the ScrollBar it will jump towards the top of the control, but you will still see the last items in the ListView control.



Additional query words: kbDSupport kbDSD kbCtrl kbVBp kbVBp600bug

Keywords : kbGrpVB
Version :
Platform : WINDOWS
Issue type : kbbug


Last Reviewed: January 5, 2000
? 2000 Microsoft Corporation. All rights reserved. Terms of Use.

zyl910 2002-03-03
  • 打赏
  • 举报
回复
我最近想出来了一个速度比较快的16进制察看的算法(贴在http://www.csdn.net/expert/topic/550/550567.xml)。
有谁能够再改改,使运算速度更快。
dsclub 2002-03-03
  • 打赏
  • 举报
回复
权力支持!!!
老熊宝宝 2002-03-03
  • 打赏
  • 举报
回复
我是在文章写到一半的时候看到这个第六章的,不错。
关于字符串的讨论,在下面的贴子里我写了很多,正确的、错误的都是自己的
研究:
http://www.csdn.net/expert/topic/533/533904.xml
我也在考虑有没有必要再写我的第三篇《VB字符串全攻略》,不过我的第
二篇文章《指针葵花宝典》没有谈字串指针,而是专注于通用的指针问题。
第二篇文章,已经提交给CSDN审核,应该可以很快和大家见面。
可以关注下面:
http://www.csdn.net/develop/list_article.asp?author=AdamBear
希望能得到大家的支持。
visualbaby 2002-03-03
  • 打赏
  • 举报
回复
而且,msdn上只给出了全书的第六章 字符串!!!
但是,后面的章节,windows操作系统,Win32 API programing With Visual Basic这本书讲得很粗糙,读完后有一种不解渴的感觉,希望谁有好的资料公布出来,供大家欣赏!!!
visualbaby 2002-03-03
  • 打赏
  • 举报
回复
Bardo(巴顿 所讲的Win32 API programing With Visual Basic
已经有中文译本,中国电力出版社,英文原版为O'REILLY(奥莱利)出版社出版
我感觉此书讲得确实不错,封面是一只猴子,相信大家都见过,不过我觉得 AdamBear (学习再学习) 的精神可嘉,大家应给支持。
况且,许多问题经过讨论之后,你会发现,你又有了新的理解(A further step)
shawls 2002-02-28
  • 打赏
  • 举报
回复


问一个问题:

如何使listview treeview 的scrollbar变成平面?


用sendmessage吗?

可是,我不知道消息的id

谁有详细资料??
shawls 2002-02-28
  • 打赏
  • 举报
回复


关注,不错,我会一直关注,mail联系!
yangzhaoyu 2002-02-28
  • 打赏
  • 举报
回复
支持中
Mike_sun 2002-02-27
  • 打赏
  • 举报
回复
GZ

743

社区成员

发帖
与我相关
我的任务
社区描述
VB 版八卦、闲侃,联络感情地盘,禁广告帖、作业帖
社区管理员
  • 非技术类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧