Visual Basic API FAQs - Page 2-VBForums
Page 2 of 2 FirstFirst 12
Results 41 to 77 of 77

Thread: Visual Basic API FAQs

  1. #41
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,590

    Re: Visual Basic API FAQs

    Quote 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.

  2. #42
    New Member
    Join Date
    Oct 2006
    Location
    South Africa
    Posts
    8

    Re: Visual Basic API FAQs

    Quote 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!

  3. #43
    New Member
    Join Date
    Jan 2008
    Posts
    1

    Resolved retrieve and set mouse Position

    i know u are pro and iam Amateur ..
    but

    #############################


    Code:
    Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
      Private Type POINTAPI
              x As Long
              y As Long
      End Type
      
      Private Declare Function SetCursorPos Lib "user32" (ByVal x As Long, ByVal y As Long) As Long
      Private Declare Function GetKeyState Lib "user32" (ByVal nVirtKey As Long) As Integer
      Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
      
      Dim x As Long, y As Long, inc As Integer
        
    
    Private Sub Command1_Click()
    ' if Text1.text<> number then nothing :)
    On Error Resume Next
    SetCursorPos Me.Text1, Me.Text2
    End Sub
    
    
    Private Sub Form_Load()
    Me.Timer1.Interval = 1
    End Sub
    
      Private Sub Timer1_Timer()
        Dim xy As POINTAPI
        Call GetCursorPos&(xy)
        
        With Me
              .Text3 = xy.x
              .Text4 = xy.x
              End With
          
      End Sub

    now add timer "timer1" , Command Button "Command1" and 4 X textbox "text1" , "text2" , "text3" , "text4"

  4. #44
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,595

    Re: Visual Basic API FAQs

    Is it worth adding code to this thread of should I place it in the codebank?
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  5. #45
    Super Moderator Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,334

    Re: Visual Basic API FAQs

    What do you have?
    Please use [Code]your code goes in here[/Code] tags when posting code.
    When you have received an answer to your question, please mark it as resolved using the Thread Tools menu.
    Before posting your question, did you look here?
    Got a question on Linux? Visit our Linux sister site.
    I dont answer coding questions via PM or EMail. Please post a thread in the appropriate forum section.

    Creating A Wizard In VB.NET
    Paging A Recordset
    What is wrong with using On Error Resume Next
    Good Article: Language Enhancements In Visual Basic 2010
    Upgrading VB6 Code To VB.NET
    Microsoft MVP 2005/2006/2007/2008/2009/2010/2011/2012/Defrocked

  6. #46
    Super Moderator RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,074

    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!
    Star Wars Gangsta Rap Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel Core 2 Extreme Ed., 2 WD Raptor 10K RPM 300 GB HDs, 2 GBs DDR2 667 MHz RAM, 2 Viewsonic 24" LCDs, Windows 7 SP-1/Windows 8.1, Office 2010, VS 2013

  7. #47
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,595

    Re: Visual Basic API FAQs

    Quote 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.
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  8. #48
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,595

    Re: Visual Basic API FAQs

    Quote 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.
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  9. #49
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    37,083

    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.

    Quote 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?

  10. #50
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,595

    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
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  11. #51
    Super Moderator RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,074

    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!
    Star Wars Gangsta Rap Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel Core 2 Extreme Ed., 2 WD Raptor 10K RPM 300 GB HDs, 2 GBs DDR2 667 MHz RAM, 2 Viewsonic 24" LCDs, Windows 7 SP-1/Windows 8.1, Office 2010, VS 2013

  12. #52
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,595

    Re: Visual Basic API FAQs

    Quote 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.
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  13. #53
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    37,083

    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.

  14. #54
    Super Moderator RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,074

    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!
    Star Wars Gangsta Rap Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel Core 2 Extreme Ed., 2 WD Raptor 10K RPM 300 GB HDs, 2 GBs DDR2 667 MHz RAM, 2 Viewsonic 24" LCDs, Windows 7 SP-1/Windows 8.1, Office 2010, VS 2013

  15. #55
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    37,083

    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.

  16. #56
    Super Moderator RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,074

    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!
    Star Wars Gangsta Rap Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel Core 2 Extreme Ed., 2 WD Raptor 10K RPM 300 GB HDs, 2 GBs DDR2 667 MHz RAM, 2 Viewsonic 24" LCDs, Windows 7 SP-1/Windows 8.1, Office 2010, VS 2013

  17. #57
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    37,083

    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.

  18. #58
    Super Moderator RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,074

    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!
    Star Wars Gangsta Rap Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel Core 2 Extreme Ed., 2 WD Raptor 10K RPM 300 GB HDs, 2 GBs DDR2 667 MHz RAM, 2 Viewsonic 24" LCDs, Windows 7 SP-1/Windows 8.1, Office 2010, VS 2013

  19. #59
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,595

    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 )
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  20. #60
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    37,083

    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).
    Quote 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!

  21. #61
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,595

    Re: Visual Basic API FAQs

    Alright cool well let me know when its open for business
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  22. #62
    Member
    Join Date
    May 2009
    Posts
    56

    Re: this is a big one

    Quote Originally Posted by The Hobo View Post
    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:
    1. 'form code:
    2. Option Explicit
    3.  
    4. Private Sub Form_Load()
    5.   Init
    6. End Sub
    7.  
    8. Private Sub Form_Unload(Cancel As Integer)
    9.   RemoveIcon
    10.   Terminate
    11. End Sub
    12.  
    13. Private Sub Form_MouseMove(Button As Integer, Shift As Integer, x As Single, y As Single)
    14. Dim msg As Long
    15.  
    16.   msg = x / Screen.TwipsPerPixelX
    17.   Select Case msg
    18.     'Case WM_LBUTTONDOWN
    19.  
    20.     'Case WM_LBUTTONUP
    21.  
    22.     Case WM_LBUTTONDBLCLK
    23.       Me.Visible = True
    24.       Me.WindowState = 0
    25.     'Case WM_RBUTTONDOWN
    26.            
    27.     'Case WM_RBUTTONUP
    28.  
    29.     'Case WM_RBUTTONDBLCLK
    30.  
    31.   End Select
    32.  
    33. End Sub
    34.  
    35. Public Sub ButtonPressed()
    36.   AddIcon Me, "test"
    37. End Sub
    38.  
    39. 'module code:
    40. Option Explicit
    41.  
    42. Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" _
    43.   (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
    44. Private Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, _
    45.   lpRect As Rect) As Long
    46. Private Declare Function GetParent Lib "user32" (ByVal hwnd As Long) As Long
    47. Private Declare Function SetParent Lib "user32" (ByVal hWndChild As Long, _
    48.   ByVal hWndNewParent As Long) As Long
    49. Private Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Long, _
    50.   ByVal hWndInsertAfter As Long, ByVal x As Long, ByVal y As Long, ByVal cx _
    51.   As Long, ByVal cy As Long, ByVal wFlags As Long) As Long
    52. Private Declare Function SetWindowsHookEx Lib "user32" Alias _
    53.   "SetWindowsHookExA" (ByVal idHook&, ByVal lpfn&, ByVal hmod&, ByVal _
    54.   dwThreadId&) As Long
    55. Private Declare Function UnhookWindowsHookEx Lib "user32" _
    56.   (ByVal hHook&) As Long
    57. Private Declare Function CreateWindowEx Lib "user32" Alias _
    58.   "CreateWindowExA" (ByVal dwExStyle As Long, ByVal lpClassName As String, _
    59.   ByVal lpWindowName As String, ByVal dwStyle As Long, ByVal x As Long, _
    60.   ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal _
    61.   hWndParent As Long, ByVal hMenu As Long, ByVal hInstance As Long, _
    62.   lpParam As Any) As Long
    63. Private Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, _
    64.   ByVal nCmdShow As Long) As Long
    65.  
    66. Private Declare Function Shell_NotifyIcon Lib "shell32" Alias _
    67.   "Shell_NotifyIconA" (ByVal dwMessage As Long, pnid As NOTIFYICONDATA) _
    68.   As Boolean
    69.  
    70.  
    71. Private Type Rect
    72.     Left As Long
    73.     Top As Long
    74.     Right As Long
    75.     Bottom As Long
    76. End Type
    77.  
    78. Private Type CWPSTRUCT
    79.     lParam As Long
    80.     wParam As Long
    81.     Message As Long
    82.     hwnd As Long
    83. End Type
    84.  
    85. Public Type NOTIFYICONDATA
    86.    cbSize As Long
    87.    hwnd As Long
    88.    uid As Long
    89.    uFlags As Long
    90.    uCallBackMessage As Long
    91.    hIcon As Long
    92.    szTip As String * 64
    93. End Type
    94.  
    95. Private Const NIM_ADD = &H0
    96. Private Const NIM_MODIFY = &H1
    97. Private Const NIM_DELETE = &H2
    98. Private Const WM_MOUSEMOVE = &H200
    99. Private Const NIF_MESSAGE = &H1
    100. Private Const NIF_ICON = &H2
    101. Private Const NIF_TIP = &H4
    102.  
    103. Public Const WM_LBUTTONDBLCLK = &H203
    104. Public Const WM_LBUTTONDOWN = &H201
    105. Public Const WM_LBUTTONUP = &H202
    106. Public Const WM_RBUTTONDBLCLK = &H206
    107. Public Const WM_RBUTTONDOWN = &H204
    108. Public Const WM_RBUTTONUP = &H205
    109.  
    110. Private NID As NOTIFYICONDATA
    111.  
    112. Const WM_MOVE = &H3
    113. Const WM_SETCURSOR = &H20
    114. Const WM_NCPAINT = &H85
    115. Const WM_COMMAND = &H111
    116.  
    117. Const SWP_FRAMECHANGED = &H20
    118. Const GWL_EXSTYLE = -20
    119.  
    120. Private WHook&
    121. Private ButtonHwnd As Long
    122.  
    123. Public Sub Init()
    124.     'Create the button that is going to be placed in the Titlebar
    125.     ButtonHwnd& = CreateWindowEx(0&, "Button", "-", &H40000000, 50, 50, 14, 14, frmMain.hwnd, 0&, App.hInstance, 0&)
    126.     'Show the button cause it´s invisible
    127.     Call ShowWindow(ButtonHwnd&, 1)
    128.     'Initialize the window hooking for the button
    129.     WHook = SetWindowsHookEx(4, AddressOf HookProc, 0, App.ThreadID)
    130.     Call SetWindowLong(ButtonHwnd&, GWL_EXSTYLE, &H80)
    131.     Call SetParent(ButtonHwnd&, GetParent(frmMain.hwnd))
    132. End Sub
    133.  
    134. Public Sub Terminate()
    135.     'Terminate the window hooking
    136.     Call UnhookWindowsHookEx(WHook)
    137.     Call SetParent(ButtonHwnd&, frmMain.hwnd)
    138. End Sub
    139.  
    140. Public Function HookProc&(ByVal nCode&, ByVal wParam&, Inf As CWPSTRUCT)
    141.     Dim FormRect As Rect
    142.     Static LastParam&
    143.     If Inf.hwnd = GetParent(ButtonHwnd&) Then
    144.         If Inf.Message = WM_COMMAND Then
    145.             Select Case LastParam
    146.                 'If the LastParam is cmdInTitlebar call the Click-Procedure
    147.                 'of the button
    148.                 Case ButtonHwnd&: frmMain.ButtonPressed
    149.             End Select
    150.         ElseIf Inf.Message = WM_SETCURSOR Then
    151.             LastParam = Inf.wParam
    152.         End If
    153.         ElseIf Inf.hwnd = frmMain.hwnd Then
    154.         If Inf.Message = WM_NCPAINT Or Inf.Message = WM_MOVE Then
    155.             'Get the size of the Form
    156.             Call GetWindowRect(frmMain.hwnd, FormRect)
    157.             'Place the button int the Titlebar
    158.             Call SetWindowPos(ButtonHwnd&, 0, FormRect.Right - 75, FormRect.Top + 6, 17, 14, SWP_FRAMECHANGED)
    159.         End If
    160.     End If
    161. End Function
    162.  
    163. Public Sub AddIcon(TheForm As Form, strT As String)
    164.     NID.cbSize = Len(NID)
    165.     NID.hwnd = TheForm.hwnd
    166.     NID.uid = vbNull
    167.     NID.uFlags = NIF_ICON Or NIF_TIP Or NIF_MESSAGE
    168.     NID.uCallBackMessage = WM_MOUSEMOVE
    169.     NID.hIcon = TheForm.Icon
    170.     NID.szTip = strT & vbNullChar
    171.     Shell_NotifyIcon NIM_ADD, NID
    172.    
    173.     TheForm.WindowState = vbMinimized
    174.     TheForm.Hide
    175. End Sub
    176.  
    177. Public Sub RemoveIcon()
    178.   Shell_NotifyIcon NIM_DELETE, NID
    179. End Sub

    Enjoy!
    It's nice, The button appears on the title bar but without any action when I click it.

  23. #63
    Hyperactive Member
    Join Date
    Oct 2008
    Posts
    467

    Re: Visual Basic API FAQs

    Quote Originally Posted by The Hobo View Post
    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 02:57 AM. Reason: removed large amount of unnecessary quoted text

  24. #64
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    37,083

    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.

  25. #65
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,595

    Re: Visual Basic API FAQs

    Quote Originally Posted by si_the_geek View Post
    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.
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  26. #66
    Lively Member
    Join Date
    Oct 2011
    Posts
    79

    Re: Visual Basic API FAQs

    Quote Originally Posted by crptcblade View Post
    Get the name of the current user, and the computer
    VB Code:
    1. Option Explicit
    2.  
    3. Private Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" (ByVal lpBuffer As String, _
    4.                                                                               nSize As Long) _
    5.                                                                               As Long
    6. Private Declare Function GetComputerName Lib "kernel32" Alias "GetComputerNameA" (ByVal lpBuffer As String, _
    7.                                                                                   nSize As Long) _
    8.                                                                                   As Long
    9.  
    10.  
    11. Private Function ReturnUserName() As String
    12.  
    13.     ReturnUserName = Space$(255)
    14.     Call GetUserName(ReturnUserName, Len(ReturnUserName))
    15.  
    16. End Function
    17.  
    18. Private Function ReturnComputerName() As String
    19.  
    20.     ReturnComputerName = Space$(255)
    21.     Call GetComputerName(ReturnComputerName, Len(ReturnComputerName))
    22.  
    23. End Function
    24.  
    25. Private Sub Command1_Click()
    26.  
    27.     MsgBox ReturnUserName
    28.     MsgBox ReturnComputerName
    29.    
    30. End Sub

    environ("computername")
    and
    environ("username")

    Was that so hard xD

    No need of api's and stuff :P

    NiTrOwow
    Best Regards

  27. #67
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    37,083

    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.

  28. #68
    Lively Member
    Join Date
    Oct 2011
    Posts
    79

    Re: Visual Basic API FAQs

    Thats true. But i mean those values can be changed anyway. So it doesn't matter.
    Best Regards

  29. #69
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    37,083

    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.

  30. #70
    Lively Member
    Join Date
    Oct 2011
    Posts
    79

    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
    Best Regards

  31. #71
    Junior Member
    Join Date
    Jun 2013
    Posts
    24

    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.

  32. #72
    Junior Member
    Join Date
    Jun 2013
    Posts
    24

    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

  33. #73
    Junior Member
    Join Date
    Jun 2013
    Posts
    24

    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.

  34. #74
    Junior Member
    Join Date
    Jun 2013
    Posts
    24

    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

  35. #75
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    3,132

    Re: Visual Basic API FAQs

    Quote Originally Posted by tahi.laci View Post
    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
    Quote Originally Posted by tahi.laci View Post
    Close window

    Code:
    Dim hWnd, retval As Long
    hWnd was declared implicitly as a Variant.

    Quote Originally Posted by tahi.laci View Post
    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)

  36. #76
    Junior Member
    Join Date
    Jun 2013
    Posts
    24

    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.

  37. #77
    Junior Member
    Join Date
    Jun 2013
    Posts
    24

    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

Page 2 of 2 FirstFirst 12

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.