-
Oct 21st, 2006, 09:42 AM
#41
Re: Visual Basic API FAQs
Originally Posted by VBDie
The problem with this code is that it will change the size of the form to 0,0,0,0. This is what I have added:
I'm afraid you're wrong - the original code works fine. The problem you are having is due to the final parameter, the wFlags value.
In your example you have it set to &H50 - this value indicates that the 5th and 7th bits are turned on (&H10 and &H40 respectively). If you look at the constants for SetWindowPos you'll see that you have the following set:
Private Const SWP_NOACTIVATE = &H10
Private Const SWP_SHOWWINDOW = &H40
in the original code example two further bits are set, the first and second (&H1 and &H2):
Private Const SWP_NOSIZE = &H1
Private Const SWP_NOMOVE = &H2
as the name of these constants suggests they prevent the window being affected by the X, Y, cx and cy parameters.
change the last parameter to &H53 and you'll find that it'll all works fine.
the moral of the story is to not use magic numbers.
-
Oct 21st, 2006, 10:43 AM
#42
New Member
Re: Visual Basic API FAQs
Originally Posted by bushmobile
I'm afraid you're wrong - the original code works fine. The problem you are having is due to the final parameter, the wFlags value.
In your example you have it set to &H50 - this value indicates that the 5th and 7th bits are turned on (&H10 and &H40 respectively). If you look at the constants for SetWindowPos you'll see that you have the following set:
Private Const SWP_NOACTIVATE = &H10
Private Const SWP_SHOWWINDOW = &H40
in the original code example two further bits are set, the first and second (&H1 and &H2):
Private Const SWP_NOSIZE = &H1
Private Const SWP_NOMOVE = &H2
as the name of these constants suggests they prevent the window being affected by the X, Y, cx and cy parameters.
change the last parameter to &H53 and you'll find that it'll all works fine.
the moral of the story is to not use magic numbers.
Sometimes we need someone to look over our shoulder to point out our mistakes.
Thanks!
-
Jan 19th, 2008, 05:23 AM
#43
New Member
retrieve and set mouse Position
-
Aug 8th, 2008, 01:28 PM
#44
Re: Visual Basic API FAQs
Is it worth adding code to this thread of should I place it in the codebank?
-
Aug 8th, 2008, 01:30 PM
#45
Re: Visual Basic API FAQs
-
Aug 8th, 2008, 03:39 PM
#46
Re: Visual Basic API FAQs
Just post your code and this thread should be cleaned up too so its only code entries and not dicsussions. My post should be deleted too.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum.
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it!
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6
-
Aug 9th, 2008, 06:55 AM
#47
Re: Visual Basic API FAQs
Originally Posted by Hack
What do you have?
Just a couple of small things that I've made over the last week when trying to help people on here... I thought it might be a good idea to post them here in case people want to do the same thing in future - but if they are not the kind of thing that should go in here then thats fine.
Both of them are in recent threads in this API forum, so you might of already seen them but one was how to use the RegisterHotKey function (and unregisterhotkey) to register a hotkey that will minimise or maximise the current window. The other one was how to process the DEVICECHANGE message that gets sent to all top level windows when a USB device is connected or disconnected.
-
Aug 9th, 2008, 07:01 AM
#48
Re: Visual Basic API FAQs
Originally Posted by RobDog888
Just post your code and this thread should be cleaned up too so its only code entries and not dicsussions. My post should be deleted too.
Would it not maybe be a good idea to just have an API section in the codebank? Seems odd that if anyone wants to submit a tutorial or example of something useful in VB6 or VB.NET then it goes in the codebank but for APIs it goes in a sticky in the API forum.
-
Aug 9th, 2008, 09:00 AM
#49
Re: Visual Basic API FAQs
While the forums for questions are split into subject areas (such as API/Database/Networking/etc), the CodeBank forums are not - and I don't think they should be, for a few reasons:
- many people who look for code don't necessarily understand what subject area(s) something falls into (especially if it covers more than one), so it would be harder to find what they want.
- similar applies to some of the people who post code, who could get confused in the same kind of way (and then perhaps post multiple copies, with only one being kept up to date).
- another issue is that API code varies a bit by language, so if there was an API CodeBank, there would need to be language specific sub-forums (or another method of showing the language)
As a general rule, API examples should be posted in the CodeBank of the language they are written in.
Note that there is a big difference between a code sample (that would go into the CodeBank), and an FAQ - not every code sample answers a question that is frequently asked. Tutorials don't really belong in either, but in our UtilityBank-Tutorials forum.
For Classic VB (and Databases), I already keep track of what gets posted to the CodeBank and UtilityBank forums - and if apt add a link from the relevant FAQ index.
Originally Posted by RobDog888
this thread should be cleaned up too so its only code entries and not dicsussions. My post should be deleted too.
We should do something.. perhaps unsticky/lock this thread, and create a new index page based on the API section of the Classic VB FAQs (which already links to individual posts in this thread), maybe separated by language?
-
Aug 9th, 2008, 02:48 PM
#50
Re: Visual Basic API FAQs
I see what you mean but apart from the first couple of posts in this thread, the APIs posted dont seem like Frequently Asked Questions to me so I figured this was maybe the place to just post any useful API examples.
In my opinion the only problem with placing API code in the codebank section that relates to the language it was written in is that I dont think people would look in there for API examples (I know I wouldnt... but maybe thats just me )
I think it would be a good idea to do what you suggest and create an index page that links to various API samples seperated into language groups
-
Aug 10th, 2008, 04:43 PM
#51
Re: Visual Basic API FAQs
I disagree as APIs are not VB6 specific. So a specific API index in the API forum may be a better idea.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum.
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it!
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6
-
Aug 10th, 2008, 05:00 PM
#52
Re: Visual Basic API FAQs
Originally Posted by RobDog888
I disagree as APIs are not VB6 specific. So a specific API index in the API forum may be a better idea.
I thought that was what Si ment - have a similar thing to what is setup for VB6 at the moment with the API index, but instead of it just being for VB6 it would be divided into languages.
-
Aug 11th, 2008, 03:24 PM
#53
Re: Visual Basic API FAQs
Pretty much.. what I was thinking is use the current list of items in this thread from the C.VB FAQs (which points to the posts in this thread) as a basis for it, but instead of the questions being links directly to the posts here, there would be language specific links after them (almost like the O.D. FAQs), eg:
- Forms/Windows:
- Set Window Parent (put one form inside another) - [Classic VB example] [VB.Net example not available yet]
The problem with following our usual standard of linking directly is that some posts here contain more than one example, so creating one thread per question (with a Classic VB example and a VB.Net example) would take lots of effort duplicating/splitting/editing posts... but I guess it would be better to do it that way.
-
Aug 11th, 2008, 04:08 PM
#54
Re: Visual Basic API FAQs
But then you are limiting the FAQ to VB6 only. The API FAQ should be more generic as you can use it in C++, VB6, any of the .NET languages etc.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum.
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it!
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6
-
Aug 11th, 2008, 04:16 PM
#55
Re: Visual Basic API FAQs
There is no intention to limit it at all - only to start with the examples we already have in this thread (which happen to be VB6).
Ideally we should copy/split the posts here into individual threads, so that examples for VB.Net etc can be added to those threads, rather than added as separate ones.
-
Aug 11th, 2008, 04:21 PM
#56
Re: Visual Basic API FAQs
Thats fine but they shouldnt be listing multiple language examples on the VB6 FAQ unless they are linking to only these VB6 code examples blah, blah, blah ...
But I dont see why we cant have an API FAQ which would have example code in multiple languages. That is how MSDN and AllAPI do it. Could be a better structure to follow.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum.
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it!
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6
-
Aug 11th, 2008, 04:42 PM
#57
Re: Visual Basic API FAQs
Yep, I haven't decided what changes to make to the Classic VB FAQs, but to be honest it isn't really relevant (but something will be needed after we've finished a new version of this thread).
I agree that a single thread per API (with examples in multiple languages, rather than separate post/thread per example) would be the best option.. but setting it up by splitting the posts out will take much more work, so will take a bit longer before I find the time to do it.
-
Aug 11th, 2008, 04:44 PM
#58
Re: Visual Basic API FAQs
You dont have to remove the current links or threads, just any expansion should be done under a new organization so we can build it forwards. then if you are board you can always work backwards to clean them all up that point to this thread or ??
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum.
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it!
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6
-
Aug 11th, 2008, 04:50 PM
#59
Re: Visual Basic API FAQs
So how's this going to start then? I mean for example with the two API examples that I was going to add to this thread, should I just create a new thread for each of them and then when this new API index thread is setup we would link to the 2 threads? Or - because both of the API examples are on this forum (in a rough form) in my other threads then would you just link to those existing threads instead of me creating two new ones?
(hope I didnt loose anyone there )
-
Aug 11th, 2008, 04:57 PM
#60
Re: Visual Basic API FAQs
It would be best to wait and see what we come up with.. I'll try to add things myself tho (by duplicating or linking to existing threads/posts).
Originally Posted by RobDog888
You dont have to remove the current links or threads, just any expansion should be done under a new organization so we can build it forwards. then if you are board you can always work backwards to clean them all up that point to this thread or ??
That's probably the best way - it would (in the short term at least) save some effort!
-
Aug 11th, 2008, 05:06 PM
#61
Re: Visual Basic API FAQs
Alright cool well let me know when its open for business
-
May 7th, 2009, 03:53 AM
#62
Member
Re: this is a big one
Originally Posted by The Hobo
System Tray and Title Bar Button
This (really long) code is something I wrote awhile ago to demonstrate how to 1) add a button to the title bar, and 2) minimize the form to the system tray.
VB Code:
'form code:
Option Explicit
Private Sub Form_Load()
Init
End Sub
Private Sub Form_Unload(Cancel As Integer)
RemoveIcon
Terminate
End Sub
Private Sub Form_MouseMove(Button As Integer, Shift As Integer, x As Single, y As Single)
Dim msg As Long
msg = x / Screen.TwipsPerPixelX
Select Case msg
'Case WM_LBUTTONDOWN
'Case WM_LBUTTONUP
Case WM_LBUTTONDBLCLK
Me.Visible = True
Me.WindowState = 0
'Case WM_RBUTTONDOWN
'Case WM_RBUTTONUP
'Case WM_RBUTTONDBLCLK
End Select
End Sub
Public Sub ButtonPressed()
AddIcon Me, "test"
End Sub
'module code:
Option Explicit
Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" _
(ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Private Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, _
lpRect As Rect) As Long
Private Declare Function GetParent Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function SetParent Lib "user32" (ByVal hWndChild As Long, _
ByVal hWndNewParent As Long) As Long
Private 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
Private Declare Function SetWindowsHookEx Lib "user32" Alias _
"SetWindowsHookExA" (ByVal idHook&, ByVal lpfn&, ByVal hmod&, ByVal _
dwThreadId&) As Long
Private Declare Function UnhookWindowsHookEx Lib "user32" _
(ByVal hHook&) As Long
Private Declare Function CreateWindowEx Lib "user32" Alias _
"CreateWindowExA" (ByVal dwExStyle As Long, ByVal lpClassName As String, _
ByVal lpWindowName As String, ByVal dwStyle As Long, ByVal x As Long, _
ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal _
hWndParent As Long, ByVal hMenu As Long, ByVal hInstance As Long, _
lpParam As Any) As Long
Private Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, _
ByVal nCmdShow As Long) As Long
Private Declare Function Shell_NotifyIcon Lib "shell32" Alias _
"Shell_NotifyIconA" (ByVal dwMessage As Long, pnid As NOTIFYICONDATA) _
As Boolean
Private Type Rect
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Private Type CWPSTRUCT
lParam As Long
wParam As Long
Message As Long
hwnd As Long
End Type
Public Type NOTIFYICONDATA
cbSize As Long
hwnd As Long
uid As Long
uFlags As Long
uCallBackMessage As Long
hIcon As Long
szTip As String * 64
End Type
Private Const NIM_ADD = &H0
Private Const NIM_MODIFY = &H1
Private Const NIM_DELETE = &H2
Private Const WM_MOUSEMOVE = &H200
Private Const NIF_MESSAGE = &H1
Private Const NIF_ICON = &H2
Private Const NIF_TIP = &H4
Public Const WM_LBUTTONDBLCLK = &H203
Public Const WM_LBUTTONDOWN = &H201
Public Const WM_LBUTTONUP = &H202
Public Const WM_RBUTTONDBLCLK = &H206
Public Const WM_RBUTTONDOWN = &H204
Public Const WM_RBUTTONUP = &H205
Private NID As NOTIFYICONDATA
Const WM_MOVE = &H3
Const WM_SETCURSOR = &H20
Const WM_NCPAINT = &H85
Const WM_COMMAND = &H111
Const SWP_FRAMECHANGED = &H20
Const GWL_EXSTYLE = -20
Private WHook&
Private ButtonHwnd As Long
Public Sub Init()
'Create the button that is going to be placed in the Titlebar
ButtonHwnd& = CreateWindowEx(0&, "Button", "-", &H40000000, 50, 50, 14, 14, frmMain.hwnd, 0&, App.hInstance, 0&)
'Show the button cause it´s invisible
Call ShowWindow(ButtonHwnd&, 1)
'Initialize the window hooking for the button
WHook = SetWindowsHookEx(4, AddressOf HookProc, 0, App.ThreadID)
Call SetWindowLong(ButtonHwnd&, GWL_EXSTYLE, &H80)
Call SetParent(ButtonHwnd&, GetParent(frmMain.hwnd))
End Sub
Public Sub Terminate()
'Terminate the window hooking
Call UnhookWindowsHookEx(WHook)
Call SetParent(ButtonHwnd&, frmMain.hwnd)
End Sub
Public Function HookProc&(ByVal nCode&, ByVal wParam&, Inf As CWPSTRUCT)
Dim FormRect As Rect
Static LastParam&
If Inf.hwnd = GetParent(ButtonHwnd&) Then
If Inf.Message = WM_COMMAND Then
Select Case LastParam
'If the LastParam is cmdInTitlebar call the Click-Procedure
'of the button
Case ButtonHwnd&: frmMain.ButtonPressed
End Select
ElseIf Inf.Message = WM_SETCURSOR Then
LastParam = Inf.wParam
End If
ElseIf Inf.hwnd = frmMain.hwnd Then
If Inf.Message = WM_NCPAINT Or Inf.Message = WM_MOVE Then
'Get the size of the Form
Call GetWindowRect(frmMain.hwnd, FormRect)
'Place the button int the Titlebar
Call SetWindowPos(ButtonHwnd&, 0, FormRect.Right - 75, FormRect.Top + 6, 17, 14, SWP_FRAMECHANGED)
End If
End If
End Function
Public Sub AddIcon(TheForm As Form, strT As String)
NID.cbSize = Len(NID)
NID.hwnd = TheForm.hwnd
NID.uid = vbNull
NID.uFlags = NIF_ICON Or NIF_TIP Or NIF_MESSAGE
NID.uCallBackMessage = WM_MOUSEMOVE
NID.hIcon = TheForm.Icon
NID.szTip = strT & vbNullChar
Shell_NotifyIcon NIM_ADD, NID
TheForm.WindowState = vbMinimized
TheForm.Hide
End Sub
Public Sub RemoveIcon()
Shell_NotifyIcon NIM_DELETE, NID
End Sub
Enjoy!
It's nice, The button appears on the title bar but without any action when I click it.
-
Nov 24th, 2009, 01:25 AM
#63
Frenzied Member
Re: Visual Basic API FAQs
Originally Posted by The Hobo
I figured since this forum didn't have a FAQ part to it, we should make one, since a lot of questions asked are constantly asked. So here are a few things I came up with. Please feel free to add your own API code that you think would benifit someone else.
How to get a windows HWND
Since many tasks through API can be used on other windows, such as changing a caption or getting a caption, it'd
be important to first now how to obtain the HWND (which is used in most API) of another window. There are two methods
explored below:
Get HWND from caption
This example requires that you know the exact caption of the window, such as 'Untitled' with Notepad.
< snip >
Cool, but how can I list all open windows with API calls? Task Manager can do that, and it can even show all running EXE files for programs without windows. It also can close any window/EXE by just selecting it and pushing end task or end process. How can I make a program like that in VB6 using API calls?
Last edited by si_the_geek; Nov 24th, 2009 at 03:57 AM.
Reason: removed large amount of unnecessary quoted text
-
Nov 24th, 2009, 03:56 AM
#64
Re: Visual Basic API FAQs
Ben321, this is an FAQ thread containing API code snippets - it is not a place to have all of your questions related to API's answered.
Please post your questions (and you have 3 or 4 different ones in that post) as new threads in apt forums. If there is a reason to refer to code in an existing thread (there is not in this case) link to it rather than posting in it.
-
Jul 4th, 2010, 04:35 PM
#65
Re: Visual Basic API FAQs
Originally Posted by si_the_geek
It would be best to wait and see what we come up with.. I'll try to add things myself tho (by duplicating or linking to existing threads/posts).
That's probably the best way - it would (in the short term at least) save some effort!
Well... its been two years, I think its time for a bump on this idea
Anyway, the main reason I was posting in this thread is to mention that I've made a class library that provides .NET definitions for over 50 Windows APIs as well as about 30 managed .NET methods that use these Windows APIs to do specific tasks (examples include: enumerating all open windows, installing a window service, joining a computer to a domain, extracting icons from a DLL). So if anyone is interested, here's some more info and a download link: http://cjwdev.wordpress.com/2010/07/...pack-released/
Last edited by chris128; Jul 4th, 2010 at 05:03 PM.
-
Apr 11th, 2012, 11:02 AM
#66
Lively Member
Re: Visual Basic API FAQs
Originally Posted by crptcblade
Get the name of the current user, and the computer
VB Code:
Option Explicit
Private Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" (ByVal lpBuffer As String, _
nSize As Long) _
As Long
Private Declare Function GetComputerName Lib "kernel32" Alias "GetComputerNameA" (ByVal lpBuffer As String, _
nSize As Long) _
As Long
Private Function ReturnUserName() As String
ReturnUserName = Space$(255)
Call GetUserName(ReturnUserName, Len(ReturnUserName))
End Function
Private Function ReturnComputerName() As String
ReturnComputerName = Space$(255)
Call GetComputerName(ReturnComputerName, Len(ReturnComputerName))
End Function
Private Sub Command1_Click()
MsgBox ReturnUserName
MsgBox ReturnComputerName
End Sub
environ("computername")
and
environ("username")
Was that so hard xD
No need of api's and stuff :P
NiTrOwow
-
Apr 11th, 2012, 11:20 AM
#67
Re: Visual Basic API FAQs
While using Environ is slightly simpler, it is also far less reliable - the values can be changed by users or other programs, and the item names can vary between versions of Windows.
-
Aug 12th, 2012, 07:05 AM
#68
Lively Member
Re: Visual Basic API FAQs
Thats true. But i mean those values can be changed anyway. So it doesn't matter.
-
Aug 12th, 2012, 08:41 AM
#69
Re: Visual Basic API FAQs
That was a rather slow response!
It does matter, because they are not the same values - the ones that you get from Environ are just copies, which (in some circumstances at least) can be easily changed without changing the "originals", while the API gets the actual value.
Even without that, the issue of Environ item names changing still applies, and thus makes it unsafe.
-
Aug 12th, 2012, 10:44 AM
#70
Lively Member
Re: Visual Basic API FAQs
Alright, i am not that much on the computer anymore. i am busy in real life. And also have a life i guess haha
Later mate and thanks for the info i'lll keep it in mind if i ever make a program again i'll use the api's for the safety
Regards,
NiTrOwow
-
Jun 16th, 2013, 06:17 PM
#71
Junior Member
Re: Visual Basic API FAQs
Click a button
Code:
Option Explicit
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As long, ByVal lParam As Any) As Long
Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
Const WM_SETTEXT As Long = &HC
Private Declare Function SetActiveWindow Lib "user32.dll" (ByVal hwnd As Long) As Long
Private Const BM_CLICK = &HF5
Sub press_button()
Dim hwnd As Long
Dim OK_button As Long
hwnd = FindWindow("#32770", "Export")
OK_button = FindWindowEx(hwnd, 0&, "Button", "Ok")
Call SendMessage(OK_button, BM_CLICK, 0, ByVal 0&)
End Sub
Last edited by tahi.laci; Jun 18th, 2013 at 04:34 AM.
-
Jun 16th, 2013, 06:19 PM
#72
Junior Member
Re: Visual Basic API FAQs
Close window
Code:
Option Explicit
Private Declare Function PostMessage Lib "user32" _
Alias "PostMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Declare Function FindWindow Lib "user32" _
Alias "FindWindowA" (ByVal szClass$, ByVal szTitle$) As Long
Private Const WM_CLOSE = &H10
Private Sub Close()
Dim hWnd, retval As Long
Dim WinTitle As String
WinTitle = "Reports list" '<- Title of Window
hWnd = FindWindow(vbNullString, WinTitle)
retval = PostMessage(hWnd, WM_CLOSE, 0&, 0&)
End Sub
-
Jun 16th, 2013, 06:22 PM
#73
Junior Member
Re: Visual Basic API FAQs
Write into notepad
Note: in theory this works with other editable fields like textboxes for passwords.
Code:
Option Explicit
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
'Finds a window with the name, returns the handle.
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As long, ByVal lParam As Any) As Long
'Sends a specified message to the window handle that you give it.
Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
'Gets a controls window handle. The form window handle must be specified to get a decent control.
Const WM_SETTEXT As Long = &HC
'Sets the text of this control.
Sub Write2Notepad()
Dim hWnd As Long
Dim chWnd As Long
'start Notepad
Call Shell("NOTEPAD.EXE", vbNormalFocus)
hWnd = FindWindow("Notepad", vbNullString)
'Find a control window handle... in Notepad, there's the main Textbox that you want.
chWnd = FindWindowEx(hWnd, 0&, vbNullString, vbNullString)
SendMessage chWnd, WM_SETTEXT, ByVal 0&, "text"
End Sub
Last edited by tahi.laci; Jun 18th, 2013 at 04:35 AM.
-
Jun 16th, 2013, 06:28 PM
#74
Junior Member
Re: Visual Basic API FAQs
Enumerate windows
Only use it on an empty worksheet. It gives you classnames, windowtitles, subwindows and handles in a format where the hierarchy is clear. I know spy++ can do this and more, but in my workplace I'm not allowed to install anything, plus I find it easier to search windows in this format.
Code:
Option Explicit
Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" _
(ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" _
(ByVal hWnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" _
(ByVal hWnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Private x As Integer
'Used a user defined type here rather than Enum so that it works on 97
Private Type winEnum
winHandle As Integer
winClass As Integer
winTitle As Integer
winHandleClass As Integer
winHandleTitle As Integer
winHandleClassTitle As Integer
End Type
Dim winOutputType As winEnum
Public Sub GetWindows()
x = 0
winOutputType.winHandle = 0
winOutputType.winClass = 1
winOutputType.winTitle = 2
winOutputType.winHandleClass = 3
winOutputType.winHandleTitle = 4
winOutputType.winHandleClassTitle = 5
GetWinInfo 0&, 0, winOutputType.winHandleClassTitle
End Sub
Private Sub GetWinInfo(hParent As Long, intOffset As Integer, OutputType As Integer)
'Sub to recursively obtain window handles, classes and text
'given a parent window to search
'Written by Mark Rowlinson
'www.markrowlinson.co.uk - The Programming Emporium
Dim hWnd As Long, lngRet As Long, y As Integer
Dim strText As String
hWnd = FindWindowEx(hParent, 0&, vbNullString, vbNullString)
While hWnd <> 0
Select Case OutputType
Case winOutputType.winClass
strText = String$(100, Chr$(0))
lngRet = GetClassName(hWnd, strText, 100)
Range("a1").Offset(x, intOffset) = Left$(strText, lngRet)
Case winOutputType.winHandle
Range("a1").Offset(x, intOffset) = hWnd
Case winOutputType.winTitle
strText = String$(100, Chr$(0))
lngRet = GetWindowText(hWnd, strText, 100)
If lngRet > 0 Then
Range("a1").Offset(x, intOffset) = Left$(strText, lngRet)
Else
Range("a1").Offset(x, intOffset) = "N/A"
End If
Case winOutputType.winHandleClass
Range("a1").Offset(x, intOffset) = hWnd
strText = String$(100, Chr$(0))
lngRet = GetClassName(hWnd, strText, 100)
Range("a1").Offset(x, intOffset + 1) = Left$(strText, lngRet)
Case winOutputType.winHandleTitle
Range("a1").Offset(x, intOffset) = hWnd
strText = String$(100, Chr$(0))
lngRet = GetWindowText(hWnd, strText, 100)
If lngRet > 0 Then
Range("a1").Offset(x, intOffset + 1) = Left$(strText, lngRet)
Else
Range("a1").Offset(x, intOffset + 1) = "N/A"
End If
Case winOutputType.winHandleClassTitle
Range("a1").Offset(x, intOffset) = hWnd
strText = String$(100, Chr$(0))
lngRet = GetClassName(hWnd, strText, 100)
Range("a1").Offset(x, intOffset + 1) = Left$(strText, lngRet)
strText = String$(100, Chr$(0))
lngRet = GetWindowText(hWnd, strText, 100)
If lngRet > 0 Then
Range("a1").Offset(x, intOffset + 2) = Left$(strText, lngRet)
Else
Range("a1").Offset(x, intOffset + 2) = "N/A"
End If
End Select
'check for children
y = x
Select Case OutputType
Case Is > 4
GetWinInfo hWnd, intOffset + 3, OutputType
Case Is > 2
GetWinInfo hWnd, intOffset + 2, OutputType
Case Else
GetWinInfo hWnd, intOffset + 1, OutputType
End Select
'increment by 1 row if no children found
If y = x Then
x = x + 1
End If
'now get next window
hWnd = FindWindowEx(hParent, hWnd, vbNullString, vbNullString)
Wend
End Sub
-
Jun 17th, 2013, 10:02 AM
#75
Re: Visual Basic API FAQs
Originally Posted by tahi.laci
Click a button
Code:
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Integer, ByVal lParam As Any) As Long
Should be:
Code:
ByVal wParam As Long
Originally Posted by tahi.laci
Close window
Code:
Dim hWnd, retval As Long
hWnd was declared implicitly as a Variant.
Originally Posted by tahi.laci
Write into notepad
Code:
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Integer, ByVal lParam As Any) As Long
. . .
SendMessage chWnd, WM_SETTEXT, ByVal 0&, "text"
Again, wParam is of wrong type.
On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)
-
Jun 18th, 2013, 04:40 AM
#76
Junior Member
Re: Visual Basic API FAQs
Thanks for mentioning that. I usually just search these declarations on the internet and I never really go through them. If the incorrect declaration ever caused a problem I'd never find it. I changed them in my posts.
-
Jun 20th, 2013, 01:37 PM
#77
Junior Member
Re: Visual Basic API FAQs
Select menubar item with sendmessage
Assuming you have a "New text document.txt" open this program will choose the "File" -> "Save As" menu. Please note: Instead of giving the name of the menu you have to specify it's zero-based position. (Eg.: File = 0, Edit = 1, Format = 2 etc.) For example if you wanted to get "Format" -> "Font" you would change the code "Call RunMenu(hwnd, 0, 3)" to "Call RunMenu(hwnd, 2, 1)". This code is only made for when the menu item to be selected is a submenu of the topmenu. In notepad all the menus are like that, but if you need to reach a menu item which is located in a submenu of a submenu you would have to make small changes. Unfortunately this code didn't work for the "Help" menu for me. I don't understand why.
Code:
Private Declare Function GetMenu Lib "user32.dll" (ByVal hwnd As Long) As Long
Private Declare Function GetSubMenu Lib "user32.dll" (ByVal hMenu As Long, ByVal nPos As Long) As Long
Private Declare Function GetMenuItemID Lib "user32.dll" (ByVal hMenu As Long, ByVal nPos As Long) As Long
Private Declare Function SendMessage Lib "user32.dll" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByRef lParam As Any) As Long
Private Declare Function FindWindow Lib "user32.dll" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Const WM_COMMAND As Long = &H111
Sub test()
Dim hwnd As Long
hwnd = FindWindow(vbNullString, "New text document.txt - Notepad")
Call RunMenu(hwnd, 0, 3)
End Sub
Public Sub RunMenu(ByVal parentWindow As Long, ByVal rtopMenu As Long, ByVal rsubMenu As Long)
Dim rmenu, rmenu2, rthemenu As Long
rmenu = GetMenu(parentWindow)
rmenu2 = GetSubMenu(rmenu, rtopMenu)
rthemenu = GetMenuItemID(rmenu2, rsubMenu)
Call SendMessage(parentWindow, WM_COMMAND, rthemenu, 0&)
End Sub
-
Jan 5th, 2024, 07:38 AM
#78
New Member
Re: Another biggie...
I tried this and it works great! I know it's a very old post but that's irrelevant to a question I have.
While the routine works great, it does seem very sensitive to crashing and burning with news at 11. I've found that while the hook is set the form will respond nicely when the edges are sized via dragging. However, any attempt to resize the window with standard vb code results in a crash. No problem, I just stay away from doing that.
However, when I change font size of some text on the form, I need to change the minimum window size and resize the window if it's below the new minimum. But pray tell, how? Calling Form_Resize does not trigger the NewWndProc function which enforces the minimum. The only way I've found is to drag the edge of the window. Surely there must be a way to do it through code and I'm hoping an expert or two here might have an answer.
BTW, one thing I tried upon changing font size was to Call Unhook, then attempt to resize with standard vb, then Call Hook again, but no good - crash and burn!
Any help would be appreciated!
-
Jan 5th, 2024, 07:43 AM
#79
New Member
-
Jan 5th, 2024, 01:28 PM
#80
Re: Visual Basic API FAQs
To set min/max height, you should use the WM_GETMINMAXINFO mesage instead of WM_SIZING.
Code:
Public Type POINTAPI
X As Long
Y As Long
End Type
Public Type MINMAXINFO
ptReserved As POINTAPI
ptMaxSize As POINTAPI
ptMaxPosition As POINTAPI
ptMinTrackSize As POINTAPI
ptMaxTrackSize As POINTAPI
End Type
Public Const WM_GETMINMAXINFO As Long = &H24
Dim mmi As MINMAXINFO
Select Case uMsg
Case WM_GETMINMAXINFO
' Snatch copy of current minmax.
Call CopyMemory(mmi, ByVal lParam, Len(mmi))
mmi.ptMinTrackSize.X = MinWidthInPixels
mmi.ptMinTrackSize.Y = MinHeightInPixels
' Make sure maximum really is maximum, to avoid flash.
mmi.ptMaxSize = mmi.ptMaxTrackSize
' Send altered values back to Windows.
Call CopyMemory(ByVal lParam, mmi, Len(mmi))
Exit Function
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
|