Advertisement
6_2008-2009 Windows API Call/ Explanation #195790

A list of Api calls

A list a usefull api calls that i thought people might be able to use.

AI

Ringkasan AI: This codebase represents a historical implementation of the logic described in the metadata. Our preservation engine analyzes the structure to provide context for modern developers.

Kode Sumber
original-source
How do I change the Double click time of the mouse?
The double click time is the time between two consecutive mouse clicks that will cause a double click event. You can change the time from your VB Application by calling the SetDoubleClickTime API function. It has only one parameter. This is the new DoubleClick time delay in milliseconds.
Declare Function SetDoubleClickTime Lib "user32" Alias _
"SetDoubleClickTime" (ByVal wCount As Long) As Long
N.B. These changes affect the entire system.

----------------------------------------------------------------------

How can I hide the cursor?
You can use the API function Showcursor, that allows you to control the visibility of the cursor. The declaration for this function is:
Declare Function ShowCursor& Lib "user32" _
(ByVal bShow As Long)
The Parameter bShow is set to True (non-zero) to display the cursor, False to hide it.

----------------------------------------------------------------------

How do I swap the mouse buttons?
Use the API Function SwapMouseButton to swap the functions of the Left and Right mouse buttons. The declare for this function is:
Declare Function SwapMouseButton& Lib "user32" _
(ByVal bSwap as long)
To swap the mouse buttons, call this function with the variable bSwap = True. Set bSwap to False to restore normal operation.

----------------------------------------------------------------------

How can I move the mouse cursor?
You can use the SetCursorPos Api function. It accepts two parameters. These are the x position and the y position in screen pixel coordinates. You can get the size of the screen by calling GetSystemMetrics function with the correct constants. This example puts the mouse cursor in the top left hand corner.
t& = SetCursorPos(0,0)
This will only work if the formula has bee declared in the declarations section:
Declare Function SetCursorPosition& Lib "user32" _
(ByVal x as long, ByVal y as long)

----------------------------------------------------------------------

How do I find out how much disk space is occupied?
Use the function GetDiskFreeSpace. The declaration for this API function is:
Declare Function GetDiskFreeSpace Lib "kernel32" Alias _
"GetDiskFreeSpaceA" (ByVal lpRootPathName As String, _
lpSectorsPerCluster As Long, lpBytesPerSector As Long, _
lpNumberOfFreeClusters As Long, lpTotalNumberOfClusters _
As Long) As Long
Here is an example of how to find out how much free space a drive has:
Dim SectorsPerCluster&
Dim BytesPerSector&
Dim NumberOfFreeClusters&
Dim TotalNumberOfClusters&
Dim FreeBytes&
dummy& = GetDiskFreeSpace("c:\", SectorsPerCluster, _
BytesPerSector, NumberOfFreeClusters, TotalNumberOfClusters)
FreeBytes = NumberOfFreeClusters * SectorsPerCluster * _
BytesPerSector
The Long FreeBytes contains the number of free bytes on the drive.

----------------------------------------------------------------------

Changing the screen resolution
A big problem for many vb-programmers is how to change the screen resolution, also because in the Api-viewer the variable for EnumDisplaySettings and ChangeDisplaySettings is missing!
1. Code for the basic-module
Declare Function EnumDisplaySettings Lib "user32" _
Alias "EnumDisplaySettingsA" _
(ByVal lpszDeviceName As Long, _
ByVal iModeNum As Long, _
lpDevMode As Any) As BooleanDeclare Function ChangeDisplaySettings Lib "user32" _
Alias "ChangeDisplaySettingsA" _
(lpDevMode As Any, ByVal dwFlags As Long) As Long
Declare Function ExitWindowsEx Lib "user32" _
(ByVal uFlags As Long, ByVal dwReserved As Long) As LongPublic Const EWX_LOGOFF = 0
Public Const EWX_SHUTDOWN = 1
Public Const EWX_REBOOT = 2
Public Const EWX_FORCE = 4
Public Const CCDEVICENAME = 32
Public Const CCFORMNAME = 32
Public Const DM_BITSPERPEL = &H40000
Public Const DM_PELSWIDTH = &H80000
Public Const DM_PELSHEIGHT = &H100000
Public Const CDS_UPDATEREGISTRY = &H1
Public Const CDS_TEST = &H4
Public Const DISP_CHANGE_SUCCESSFUL = 0
Public Const DISP_CHANGE_RESTART = 1Type DEVMODE
  dmDeviceName As String * CCDEVICENAME
  dmSpecVersion As Integer
  dmDriverVersion As Integer
  dmSize As Integer
  dmDriverExtra As Integer
  dmFields As Long
  dmOrientation As Integer
  dmPaperSize As Integer
  dmPaperLength As Integer
  dmPaperWidth As Integer
  dmScale As Integer
  dmCopies As Integer
  dmDefaultSource As Integer
  dmPrintQuality As Integer
  dmColor As Integer
  dmDuplex As Integer
  dmYResolution As Integer
  dmTTOption As Integer
  dmCollate As Integer
  dmFormName As String * CCFORMNAME
  dmUnusedPadding As Integer
  dmBitsPerPel As Integer
  dmPelsWidth As Long
  dmPelsHeight As Long
  dmDisplayFlags As Long
  dmDisplayFrequency As Long
End Type
Example
Changes the resolution to 640x480 with the current colordepth.
Dim DevM As DEVMODE
'Get the info into DevM
erg& = EnumDisplaySettings(0&, 0&, DevM)
'We don't change the colordepth, because a
'rebot will be necessary
DevM.dmFields = DM_PELSWIDTH Or DM_PELSHEIGHT 'Or DM_BITSPERPEL
DevM.dmPelsWidth = 640 'ScreenWidth
DevM.dmPelsHeight = 480 'ScreenHeight
'DevM.dmBitsPerPel = 32 (could be 8, 16, 32 or even 4)
'Now change the display and check if possibleerg& = ChangeDisplaySettings(DevM, CDS_TEST)
'Check if succesfullSelect Case erg&
Case DISP_CHANGE_RESTART
  an = MsgBox("You've to reboot", vbYesNo + vbSystemModal, "Info")
  If an = vbYes Then
    erg& = ExitWindowsEx(EWX_REBOOT, 0&)
  End If
Case DISP_CHANGE_SUCCESSFUL
  erg& = ChangeDisplaySettings(DevM, CDS_UPDATEREGISTRY)
  MsgBox "Everything's ok", vbOKOnly + vbSystemModal, "It worked!"
Case Else
  MsgBox "Mode not supported", vbOKOnly + vbSystemModal, "Error"
End SelectEnd Sub

----------------------------------------------------------------------

How to display the item which the mouse is over in a list box
I have had many letters which have asked me how to you display in a tooltip or some other means, such as a text box, the current item's text in a list box which the mouse pointer is hovering over. I now have the answer which uses the SendMessage API.
Start A new Standard-EXE project, form1 is created by default. 
Add a list box and a text box to form1. 
Open up the code window for Form1 and type the following 
Option Explicit
Private Declare Function SendMessage Lib _
"user32" Alias "SendMessageA" (ByVal hwnd _
As Long, ByVal wMsg As Long, ByVal wParam _
As Long, lParam As Any) As Long
Private Const LB_ITEMFROMPOINT = &H1A9
Private Sub Form_Load()
With List1
  .AddItem "Visit"
  .AddItem "Steve Anderson Web Site AT"
  .AddItem "http://www.microweird.demon.co.uk"
End With
End Sub
Private Sub List1_MouseMove(Button _
As Integer, Shift As Integer, X As _
Single, Y As Single)
Dim lXPoint As Long
Dim lYPoint As Long
Dim lIndex As Long
If Button = 0 Then ' if no button was pressed
  lXPoint = CLng(X / Screen.TwipsPerPixelX)
  lYPoint = CLng(Y / Screen.TwipsPerPixelY)
  With List1
    ' get selected item from list
    lIndex = SendMessage(.hwnd, _
    LB_ITEMFROMPOINT, 0, ByVal _
    ((lYPoint * 65536) + lXPoint))
    ' show tip or clear last one
    If (lIndex >= 0) And _
    (lIndex <= .ListCount) Then
      .ToolTipText = .List(lIndex)
      Text1.Text = .List(lIndex)
    Else
      .ToolTipText = ""
    End If
  End With
End If
End Sub
Run the project(F5) and hover your cursor over different items in the list box and they will be displayed in a tooltip and in Text1. 

----------------------------------------------------------------------

Finding out the amount of free memory
It is easy to return the amount of free memory in windows, using the GlobalMemoryStatus API call. Insert the following into a module's declarations section:
Public Type MEMORYSTATUS 
dwLength As Long 
dwMemoryLoad As Long 
dwTotalPhys As Long 
dwAvailPhys As Long 
dwTotalPageFile As Long 
dwAvailPageFile As Long 
dwTotalVirtual As Long 
dwAvailVirtual As Long
End TypePublic Declare Sub GlobalMemoryStatus _
Lib "kernel32" (lpBuffer As MEMORYSTATUS)
Now, add this code to get the values:
Dim MS As MEMORYSTATUS 
MS.dwLength = Len(MS) 
GlobalMemoryStatus MS
' MS.dwMemoryLoad contains percentage memory used
' MS.dwTotalPhys contains total amount of physical memory in bytes
' MS.dwAvailPhys contains available physical memory
' MS.dwTotalPageFile contains total amount of memory in the page file
' MS.dwAvailPageFile contains available amount of memory in the page file
' MS.dwTotalVirtual contains total amount of virtual memory
' MS.dwAvailVirtual contains available virtual memory
You could use this in about boxes or making a memory monitoring system

----------------------------------------------------------------------


/*
Name: Manish soni.
E-Mail: manish_99us@yahoo.com
Language: C++
Category: Miscellaneous.
Description: Its a simple program which calculates the day of the week for any given date & month of the year 2002.
*/
#include<stdio.h>     //include files.
#include<conio.h> 
#include<iostream.h> 
void main()       //main function definition.
{ 
int opt,date,day;    //variable declaration.
char b; 
cout<<"****A program to find out the day of the week for any date of the year 2002****\n\n"; 
cout<<"1.January\n2.February\n3.March\n4.April\n5.May\n6.June\n7.july\n8.August\n9.September\n10.October\n11.November\n12.December\n"; 
a: cout<<"\nEnter the value of the month:"; 
cin>>opt; 
cout<<"\nEnter the date of the month:"; 
cin>>date; 
/*Calculates the value of the variable 'day'.
 This will be the final result based on which the "day of the week" is calculated.
 day= ((month number + date of the month)%number of days in a week);
 month number is as follows:
    "6" if the 1st day of the month is "sunday".
    "7" if the 1st day of the month is "monday".
    "8" if the 1st day of the month is "tuesday".
    "9" if the 1st day of the month is "wednesday".
    "10" if the 1st day of the month is "thursday".
    "11" if the 1st day of the month is "friday".
    "12" if the 1st day of the month is "saturday".
*/
switch(opt) 
{ 
case 1: if(date<=31) 
{day=(8+date)%7;} 
else
cout<<"\nFor u'r kind information january has only 31 days.\n"; 
break; 
case 2: if(date<=28) 
{day=(11+date)%7;} 
else 
cout<<"\nFor u'r kind information february has only 28 days.\n"; 
break; 
case 3: if(date<=31) 
{day=(11+date)%7;} 
else 
cout<<"\nFor u'r kind information march has only 31 days.\n"; 
break; 
case 4: if(date<=30) 
{day=(7+date)%7;} 
else 
cout<<"\nFor u'r kind information april has only 30 days.\n"; 
break; 
case 5: if(date<=31) 
{day=(9+date)%7;} 
else 
cout<<"\nFor u'r kind information may has only 31 days.\n"; 
break; 
case 6: if(date<=30) 
{day=(12+date)%7;} 
else 
cout<<"\nFor u'r kind information june has only 30 days.\n"; 
break; 
case 7: if(date<=31) 
{day=(7+date)%7;} 
else 
cout<<"\nFor u'r kind information july has only 31 days.\n"; 
break; 
case 8: if(date<=31) 
{day=(10+date)%7;} 
else 
cout<<"\nFor u'r kind information august has only 31 days.\n"; 
break;
case 9: if(date<=30) 
{day=(6+date)%7;} 
else 
cout<<"\nFor u'r kind information september has only 30 days.\n"; 
break; 
case 10: if(date<=31) 
{day=(8+date)%7;} 
else 
cout<<"\nFor u'r kind information october has only 31 days.\n"; 
break; 
case 11: if(date<=30) 
{day=(11+date)%7;} 
else 
cout<<"\nFor u'r kind information november has only 30 days.\n"; 
break; 
case 12: if(date<=31) 
{day=(6+date)%7;} 
else 
cout<<"\nFor u'r kind information december has only 31 days.\n"; 
break; 
} 
/* The value of the variable 'day' calculated above is checked for the conditions:
  if (day==0) then the day of the week is "sunday".
  if (day==1) then the day of the week is "monday".
  if (day==2) then the day of the week is "tuesday".
  if (day==3) then the day of the week is "wednesday".
  if (day==4) then the day of the week is "thursday".
  if (day==5) then the day of the week is "friday".
  if (day==6) then the day of the week is "saturday".
*/
if(day==0) 
{cout<<"\nIts SUNDAY.\n";} 
if(day==1) 
{cout<<"\nIts MONDAY.\n";} 
if(day==2) 
{cout<<"\nIts TUESDAY.\n";} 
if(day==3) 
{cout<<"\nIts WEDNESDAY.\n";} 
if(day==4) 
{cout<<"\nIts THURSDAY.\n";} 
if(day==5) 
{cout<<"\nIts FRIDAY.\n";} 
if(day==6) 
{cout<<"\nIts SATURDAY.\n";} 
cout<<"\n****************************************************\n"; 
cout<<"\nWould u like to continue?[y/n]:"; 
cin>>b; 
if(b=='y'||b== 'Y') 
goto a; 
else 
getch(); 
getch(); 
}
Komentar Asli (3)
Dipulihkan dari Wayback Machine