|
-
Aug 27th, 2000, 07:11 PM
#1
Thread Starter
Frenzied Member
My Favourite Functions Part #4
Since I really enjoyed the "My Favorite Functions" on Vb-World and learned a lot of it, I thought it would be cool to make a fourth part, so guys/girls, go ahead and post your favorite functions here! (API's are welcome too!)
Jop - validweb.nl
Alcohol doesn't solve any problems, but then again, neither does milk.
-
Aug 27th, 2000, 08:09 PM
#2
One of my favorite API Functions is FindWindow
well, actually its a tie between FindWindow, CallWindowProc, and SetWindowLong
Code:
Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Public Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hWnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Public Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
-
Aug 27th, 2000, 08:11 PM
#3
Thread Starter
Frenzied Member
Jop - validweb.nl
Alcohol doesn't solve any problems, but then again, neither does milk.
-
Aug 27th, 2000, 08:20 PM
#4
They are used for subclassing,
SetWindowLong sets which function is used in a window's procedure, it has to be a public function inside a module, to get the address of this function you use, the AddressOf Operator, CallWindowProc is used so when you subclass the window still does what it is supposed to do, I have found if I dont use CallWindowProc VB Freezes.
-
Aug 27th, 2000, 08:34 PM
#5
Thread Starter
Frenzied Member
Ok thanx,
can someone please explain what the
- Like operator exactely does? (and give me an example please)
- Mod operator is used for? (it seems to be some mathematical thingy, and I hate maths )
Thanx 
BTW, one of my favorite VB functions is still IsNummeric, many programmers aren't aware of this handy function, you could do some complicated loop thingy's to check for the ASCII nr's, but this one is easier.
example:
Code:
Dim MyVar As String, MyVar2 As String
MyVar = 129937
MyVar2 = "hello"
'IsNummeric Returns true if the passed variable is Nummeric
MsgBox IsNummeric(MyVar) 'True
MsgBox IsNummeric(MyVar2) 'False
Jop - validweb.nl
Alcohol doesn't solve any problems, but then again, neither does milk.
-
Aug 27th, 2000, 08:44 PM
#6
Some developers suggest putting DoEvents in long loops to keep your application responsive.
This is never a good idea because you take an (in my opinion) unacceptable performance hit.
If your users must be able to click on a cancel button while your loop is executing use the GetInputState function instead.
This API function returns 1 if the user has pressed a mouse button or hit a key on the keyboard.
The overhead for GetInputState is much lower than for DoEvents.
If a keyboard or a mouse event occurs, then and only then you call DoEvents.
Code:
Private Declare Function GetInputState _
Lib "user32" () As Long
Private m_blnCancel As Boolean
Private Sub cmdCancel_Click()
m_blnCancel = True
End Sub
Private Sub cmdStartLoop_Click()
Dim lngCounter As Long
m_blnCancel = False
For lngCounter = 0 To 10000000
'do a lot of stuff here
If lngCounter Mod 100 = 0 Then
If GetInputState <> 0 Then
'a mouse or keyboard event is in
'the message queue so we call DoEvents
DoEvents
If m_blnCancel = True Then
Exit Sub
End If
End If
End If
Next
End Sub
Best regards
-
Aug 27th, 2000, 08:55 PM
#7
The Mod operator perform a modulus operation.
It returns the remainder when you devide two integers.
5 Mod 2 = 1 (5/2=2, 2*2=4, 5-4=1)
6 Mod 2 = 0 (6/2=3, 3*2=6, 6-6=0)
The Like operator is used to compare two strings.
string Like pattern
In the pattern string you can have wildcard characters like this:
Code:
If "Joacim Andersson" Like "*ss*" Then
The above code compares my name with *ss* which means that there can be any number of characters before and after the two s characters.
In this case the line would return True.
-
Aug 27th, 2000, 09:12 PM
#8
Fanatic Member
The mod operator is useful for performing a task every x number of loops (like mentioned above).
One place I often use it is to update a progress bar. On an intensive loop, having a graphic control updated is often a performance drag and unnecessary. so to have it update only every 50 loops you can add...
Code:
For X = 1 to 10000
' Do normal loop code
If X Mod 50 = 0 Then
ProgressBar1.Value = X
End If
Next
It's like having "Step 50" on the loop for the progress bar update but not for the rest of the loop.
I also used it in a database call in an ASP page to change the background colour of every 4th row to light blue so that the dynamic table was easier to reference.
Very useful
Paul Dwyer 
Network Engineer
Aussie In Tokyo
Using Powerbasic 6 & VB6 SP4 (Please also add your VB Version to your signature!)
-
Aug 28th, 2000, 03:52 AM
#9
transcendental analytic
With mod operator you can split up values in factors
?3 mod 2
1
?int(3/2) mod 2
1
so 3 is binary 11. You'll find mod's in all base converters - splitting up a color long into red green and blue is the same technique. 256 is the base
Also for counters you could use mod's to reset to 0 after a certain value
n=(n+1) mod 10
For Like, check out vb-help, the comparations are much like the file matching done in dos like "*.*" you remember, but like has more features
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Aug 28th, 2000, 04:15 AM
#10
Fanatic Member
Originally posted by Jop
BTW, one of my favorite VB functions is still IsNummeric
It's spelt:
Shish. Typical.
Anyway, my favourite APIs follow:
Code:
'Makes any app look really Microsofty:
Public Declare Function DrawEdge Lib "user32" Alias "DrawEdge" (ByVal hdc As Long, qrc As RECT, ByVal edge As Long, ByVal grfFlags As Long) As Long
'Gets your own back on VB's attempt to make ActiveX controls useless:
Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
'Nice for tooblars
Public Declare Function DrawIconEx Lib "user32" Alias "DrawIconEx" (ByVal hdc As Long, ByVal xLeft As Long, ByVal yTop As Long, ByVal hIcon As Long, ByVal cxWidth As Long, ByVal cyWidth As Long, ByVal istepIfAniCur As Long, ByVal hbrFlickerFreeDraw As Long, ByVal diFlags As Long) As Long
'Basic move memory with VB:
Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
'These go together (for creating special windows):
Public Declare Function SetWindowRgn Lib "user32" Alias "SetWindowRgn" (ByVal hWnd As Long, ByVal hRgn As Long, ByVal bRedraw As Boolean) As Long
Public Declare Function CreateRectRgn Lib "gdi32" Alias "CreateRectRgn" (ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long) As Long
Non API:
Code:
'Pointers do exist in VB :)
VarPtr()
'Good for strings
Like
Oh, well.
They're my favourite functions. (Well, CopyMemory's a Sub, and Like is an more of an operator)
Enough of this gay banter.
Au revoir (as Ms Le Pont would have us say)
[Edited by V(ery) Basic on 08-28-2000 at 05:19 AM]
-
Aug 28th, 2000, 06:54 AM
#11
Thread Starter
Frenzied Member
Oops :) Shame on me!
Let's say I was tired 
hehe anyway, anyone thanks for the info about mod.
More favourite functions, operators, api-calls?
Jop - validweb.nl
Alcohol doesn't solve any problems, but then again, neither does milk.
-
Aug 28th, 2000, 10:51 AM
#12
V(ery):
how do you use DrawEdge? I tried once, but it didnt work.
-
Aug 28th, 2000, 01:52 PM
#13
my fave functions are PARAMARRAY, REPLACE, LIKE, ISMISSING and in joint 5th place: SPLIT and JOIN
(Check out Paramarray especially)
-
Aug 28th, 2000, 02:02 PM
#14
Monday Morning Lunatic
V(ery) - you can also use StrPtr() for string pointers.
Favourite API function, glutSolidSphere.
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Aug 28th, 2000, 02:13 PM
#15
after you get the pointer to a string or variable, what do you do with it?
how do you use it? in C++ I know, but not in VB.
-
Aug 28th, 2000, 02:16 PM
#16
Monday Morning Lunatic
Some API functions require it, like OpenGL. Also, you can use it with other API functions to do optimised string handling functions, which require an absolute memory address.
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Aug 29th, 2000, 12:22 AM
#17
Thread Starter
Frenzied Member
What are ParamArray and IsMissing for?
Sounds quite interesting!
Jop - validweb.nl
Alcohol doesn't solve any problems, but then again, neither does milk.
-
Aug 29th, 2000, 05:03 AM
#18
transcendental analytic
ParamArray is a keyword you place in a function delcaration before the last argument, which should be an array.
Then you can pass as many arguments as you like, for instance like array() function, and the arguments will be in the last array.
Ismissing is a function that returns true if a variable, an optional argument passed to a procedure is omited.
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Aug 29th, 2000, 05:55 AM
#19
IsMissing can only be used with Variants so in my humble opinion I think it’s a bit obsolete.
When the Optional keyword was first introduced you could only use it with Variants but today you can use it with any data type.
Code:
Private Sub AnExample(A As Integer, Optional B As Integer)
'do some stuff
End Sub
If you should use IsMissing on the optional argument B it would always return False, because if you omit that argument B would be zero.
-
Aug 29th, 2000, 01:59 PM
#20
V(ery) I am gonna look at it, just havent needed menu's for a while 
-
Aug 29th, 2000, 05:02 PM
#21
Hyperactive Member
V(ery),
I used your DrawEdge rutine to get that 'Microsofty' look. I set a menu at design time and called your rutine. The rectangle looks good but it's drawn below my menu. How can I use it to get the menu on top of the rectangle?
Thanks
-
Aug 29th, 2000, 06:43 PM
#22
Guru
Originally posted by V(ery) Basic
parksie, ObjPtr gets the pointer of an object. I'm one up on you now boy and since there aren't any more, I've won.
V(ery): You lose.
Code:
Declare Function ArrPtr Lib "msvbvm50.dll" Alias "VarPtr" (Arr() As Any) As Long
Well, it is an expansion of VarPtr but I think it still counts.
-
Aug 29th, 2000, 06:46 PM
#23
Monday Morning Lunatic
Yonatan, you're too sneaky at this. BTW - do you have any info on asm and functions?
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Aug 30th, 2000, 01:09 PM
#24
Thread Starter
Frenzied Member
Any more functions? I'm still hungry!
Jop - validweb.nl
Alcohol doesn't solve any problems, but then again, neither does milk.
-
Aug 30th, 2000, 01:32 PM
#25
Fanatic Member
Originally posted by Yonatan
Originally posted by V(ery) Basic
parksie, ObjPtr gets the pointer of an object. I'm one up on you now boy and since there aren't any more, I've won.
V(ery): You lose.
Code:
Declare Function ArrPtr Lib "msvbvm50.dll" Alias "VarPtr" (Arr() As Any) As Long
Well, it is an expansion of VarPtr but I think it still counts.
Bugger.
I won't comment on the fact that the Alias is actually VarPtr.
Anyway (sees an opportunity to give his app to another innocent victim ) :
Tomexx: Windows changes the coordinate system when it adds a menu. I have
(turns to his oven) made one earlier and I'm gonna e-mail it to you whether
you like it or not
Probably not, but I don't care.
-
Aug 30th, 2000, 01:34 PM
#26
I like DrawFocusRect
Code:
Public Declare Function DrawFocusRect Lib "user32" Alias "DrawFocusRect" (ByVal hdc As Long, lpRect As RECT) As Long
PS:
Does anybody know how to draw the focus thing, but not in a rectangle shape(for example, like a circle or something)??
-
Aug 31st, 2000, 06:48 AM
#27
transcendental analytic
Maybe you just draw two circles on each other, which starts from 7/4 pi radians and goes to 3/4 pi radians + vice versa in the right system colors..
couldn't be hard
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Aug 31st, 2000, 07:49 AM
#28
Member
I kinda found this to be very usefull when you have people using a common machine.
Public Declare Function SwapMouseButton Lib "user32" Alias "SwapMouseButton" (ByVal bSwap As Long) As Long
VB 6, SQL, Java, AutoLISP, Avenue and on a good day AML
-
Aug 31st, 2000, 08:25 AM
#29
Fanatic Member
Well, well well, aren't we all in love with the API.
I personally am rubbish at the API, so my favourite
functions are standard VB ones.
My favourite functions then, as no one else has mentioned
them.
InStr The basis for all string manipulation.
Mid$ Goes with InStr, you can forget about Left$()
and Right$(), you dont need them
Get #1, , variable Wonderful for fast file access.
You can also control the file in manageable chunks. If you
get the chunk sizes right, you can manipulate files much
quicker than with the FSO, and slightly faster than the
Input functions.
Put #1, , variable Obviously paired with Get
Iain, thats with an i by the way!
-
Aug 31st, 2000, 08:30 AM
#30
Hyperactive Member
Hey Very!!
I am getting a bit jealous here. Can I get a look at this app of yours?
[email protected]
Thanks
-
Aug 31st, 2000, 08:55 AM
#31
Fanatic Member
*Smiles smugly*
Certainly.
-
Aug 31st, 2000, 11:40 AM
#32
transcendental analytic
My favorite must be Rnd, or maybe Int, but i think i use Int most oftenly, hehe why do we love API? 
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Sep 1st, 2000, 04:39 AM
#33
-
Sep 1st, 2000, 06:43 AM
#34
Thread Starter
Frenzied Member
What does GetWindowThreadProcessId do then?
Never used it before!
Jop - validweb.nl
Alcohol doesn't solve any problems, but then again, neither does milk.
-
Sep 1st, 2000, 07:04 AM
#35
Guru
-
Sep 1st, 2000, 08:16 AM
#36
Fanatic Member
I use a lot of functions and subs in my own functions
Code:
'-- Useful --
Private Function Random(ByVal Min as Integer, ByVal Max as Integer) as Integer
Randomize Timer
Random = Int(Val(Rnd * Max) + Min)
End Function
Private Function IsOdd(ByVal n as Long) as Boolean
IsOdd = True
If n mod 2 = 0 then
IsOdd = False
End If
End Function
'-- Fun --
Private Function Leeto(ByVal txt as string) as string
Dim tmpTxt as string
Dim i as Integer
tmpTxt = ""
For i = 1 to Len(txt)
If IsOdd(i) then
tmpTxt = tmpTxt & UCase(Mid(txt, i, 1))
Else
tmpTxt = tmpTxt & LCase(Mid(txt, i, 1))
End If
Next
Leeto = tmpTxt
End Function
r0ach™
Don't forget to rate the post
-
Sep 1st, 2000, 08:43 AM
#37
Lively Member
My fav. functions are:
split ->Turns a string into an array
Ubound ->Returns the number of posts in an array
Len ->Return the lenght of a string
FileLen ->Returns the lenght of a file
Do/Loop
Select case
fav. APIs:
Public Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, ByVal X As Long, ByVal Y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long
Declare Function Shell_NotifyIcon Lib "shell32.dll" (ByVal dwMessage As Long, lpDate As NOTIFYICONDATA) As Long
Public Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long
They will try to steal everything you own,
It goes on and on and on...
Pain - On and On
-
Sep 1st, 2000, 08:59 AM
#38
Thread Starter
Frenzied Member
I use this Function from the book SAMS Teach Yourself Internet Programming with Visual Basic 6 in 21 days so all credits go to them, they rule I don't and I really recommend this book. (Ok enough commercial brabbling)
Code:
Option Explicit
Public Function GetLinks(s As String, baseUrl As String)
Dim pos As Long, pos1 As Long, pos2 As Long
Dim buf As String, temp As String
Dim sq As String, dq As String
Dim qc As String, Start As Long
buf = ""
'Make sure a nonempty string has been passed.
If s = Null Or Len(s) = 0 Then
GetLinks = buf
Exit Function
End If
'Make sure there is at least one link
Start = InStr(1, s, "<a href=", vbTextCompare)
If Start = 0 Then
GetLinks = buf
Exit Function
End If
'Define the single
dq = Chr$(34)
sq = Chr$(39)
Do
'get the first Dq or Sq
pos = InStr(Start, s, dq, vbTextCompare)
pos2 = InStr(Start, s, sq, vbTextCompare)
If pos = 0 And pos2 = 0 Then Exit Do 'Nothing found
If pos > 0 And pos2 > 0 Then
If pos < pos2 Then 'It's a Dq
qc = dq
Else
qc = sq
pos = pos2
End If
ElseIf pos = 0 Then 'Only Signle
qc = sq
pos = pos2
ElseIf pos2 = 0 Then
qc = dq
End If
pos1 = InStr(pos + 1, s, qc, vbTextCompare)
If pos1 = 0 Then Exit Do
temp = Mid$(s, pos + 1, pos1 - pos - 1)
'Forget about FTP and Mailto links
If LCase(Left(temp, 7)) = "mailto:" Or LCase(Left(temp, 3)) = "ftp" Then
GoTo DoNotAdd
End If
'See if it's a full URL, if not add the base Url
If LCase(Left(temp, 7)) <> "http://" Then
temp = baseUrl & temp
End If
'Strip off anything following a # or ?
pos = InStr(1, temp, "#")
If pos > 0 Then
temp = Left(temp, pos - 1)
End If
pos = InStr(1, temp, "?")
If pos > 0 Then
temp = Left(temp, pos - 1)
End If
buf = buf & temp & "|"
DoNotAdd:
'Locate the next link
pos = InStr(pos1, s, "<a href=", vbTextCompare)
Start = pos
'If there a no more links then quit
If pos = 0 Then Exit Do
DoEvents
Loop While True
'Strip off the trailing |
GetLinks = Left(buf, Len(buf) - 1)
'MsgBox buf
End Function
Hope it's of use for someone.
Again, all respect, flowers, presents, money, kisses, pies and ofcourse the new ferrari goes to the great [/i]Peter Aitken[/i], the author of the book Sams Teach Yourself Internet Programming with VB6 in 21 days! BUY IT!!!
Jop - validweb.nl
Alcohol doesn't solve any problems, but then again, neither does milk.
-
Sep 1st, 2000, 09:00 AM
#39
Fanatic Member
BINARY SEARCHING OF SORTED LISTS
will search a sorted list of 1,000,000 in 20 jumps max (instead of 1,000,000)
It's set up for LONG data types but it'll work for strings etc with little modification, it's the idea of the search technique rather than this code per se which is clever
Code:
Public Function BinarySearch(target As Long, List() As Long, NumItems As Long) As Long
Dim min As Long
Dim max As Long
Dim middle As Long
NumSearches = 0
' During the search the target's index will be
' between Min and Max: Min <= target index <= Max
min = 1
max = NumItems
Do While min <= max
NumSearches = NumSearches + 1
middle = (max + min) / 2
If target = List(middle) Then ' We have found it!
BinarySearch = middle
Exit Function
ElseIf target < List(middle) Then ' Search the left half.
max = middle - 1
Else ' Search the right half.
min = middle + 1
End If
Loop
' If we get here the target is not in the list.
BinarySearch = 0
End Function
Can you use "As Any" in this sort of function or should you use Variant to make it generic?
[Edited by Paul282 on 09-01-2000 at 10:07 AM]
Paul Dwyer 
Network Engineer
Aussie In Tokyo
Using Powerbasic 6 & VB6 SP4 (Please also add your VB Version to your signature!)
-
Sep 1st, 2000, 05:13 PM
#40
Hyperactive Member
DirectX
I Need to use DirectX anyway for what I'm doing. I need Direct Draw to create the app in fullscreen and then use Direct 3D. I Initialize Direct 3D and make it run in Direct 3D HAL(Hardware Abstract Layer for those who don't know what HAL is). Then, In the D3D Initialization sub, nefore I Init the device, I add a Z-Buffer. I won't bother posting these HUGE functions here, but, If you want to see them and other 3D related things, got to my Homepage and go to the I3D section. I3D is a graphics engine that I made myself and Comtech is my company(So far, I'm the only one in it, heh heh heh). In the I3D section you'll find new stuff and the I3D Features page. Don't forget to download the engine(what price is better than FREE!!!). P.S. I would like some feedback on I3D.
Designer/Programmer of the Comtech Operating System(CTOS)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|