Examples of several API calls

xiaoxiao2021-03-06  104

When developing personal software "Windows Superman", the author applies more Win32API, and now the VB development environment is called, and experiences with you.

1. Drag no title form

The first method moves when the window does not display the window content, that is, only the virtual frame is displayed during movement.

Private Declare Function ReleaseCapture LIB "User32" () AS Long

Private Declare Function SendMessage Lib "User32" Alias ​​"SendMessagea" (Byval HWnd As Long, Byval WParam As Long, LParam As Long) As long

Private const wm_syscommand = & h112

Private const sc_move = & hf012

Private Sub Form_MouseDown (Button As Integer, Shift As Integer, x as single, y as single)

ReleaseCapture 'release mouse capture

SendMessage Hwnd, WM_SYSCOMMAND, SC_MOVE, 0 'Send Window Mobile Messages

End Sub

The second method moves the window display window content, that is, moving an entity, (such as your window style has a title bar, this code will have a deviation of the location of the window location, please adjust the local adjustment)

Set SCALEMODE to 3, using pixels as internal metric units before the LOAD event or run

Private Declare Function SetCapture LIB "User32" (Byval Hwnd As Long) AS Long

Private Declare Function ReleaseCapture LIB "User32" () AS Long

Private Declare Function GetCursorpos LIB "User32" (LPPOINT AS POINTAPI) AS Long

Private Type PointApi

X as long

Y as long

End Type

Dim MousePoint As Pointapi, MouseSize As Pointapi

Private Sub Form_MouseDown (Button As Integer, Shift As Integer, x as single, y as single)

If Button = 1 THEN

SetCapture (hwnd) 'Setting the mouse capture

Tag = "1" 'Take it as a mobile sign

MouseSize.x = x

MouseSize.y = Y

'Memory mouse Press the cursor position

END IF

End Sub

Private Sub Form_Mousemove (Button As Integer, Shift As Integer, x as single, y as single)

IF tag = "1" THEN

Call getCursorpos (MousePoint) 'Returns the location of the mouse in the screen

Left = screen.twipsperpixelx * (MousePoint.x - mouseesize.x)

TOP = Screen.twipsperpixely * (MousePoint.y - MouseSize.y)

'Setting the window position

END IF

End Sub

Private Sub Form_MouseUp (Button As Integer, Shift As Integer, X as Single, Y as Single Tag = ""

ReleaseCapture 'release mouse

End Sub

VB and Delphi have a lot of similarity. The two code can be easily converted as the other party. Now use the Delphi to establish a system bar diagram mark an example of several APIs, including custom messages and custom processing for system messages, if you want to pursue Smaller EXE and more understanding of the system rather than using others to encapsulate controls, please see the following example

2. System bar icon processing

Unit unit1;

Interface

Uses

Windows, Messages, Sysutils, Classes, Graphics, Controls, Forms, Dialogs,

Extctrls, shellapi; // Add shellapi in the reference

Const WM_MYCALL = WM_USER $ 1000; // Custom Message

Type

TFORM1 = Class (TFORM)

Procedure formcreate (Sender: TOBJECT);

Procedure WndProc (var Msg: tMessage); OVERRIDE;

Procedure formdestroy (sender: TOBJECT); // Overloaded and processes the module for custom messages

Private

{Private Declarations}

public

{Public declarations}

END;

VAR

FORM1: TFORM1;

ICO: TNOTIFYICONDATA; / / Declaration an instance

IMPLEMENTATION

{$ R * .dfm}

Procedure systemo (msgs: dword; icos: ticon = nil); // Handling a taskbar icon process

Begin

If icos = nil the // If the default null value, delete this icon from the system bar.

Shell_Notifyicon (Nim_Delete, @ iCo)

Else

Begin

With ico do // joins Trayicon

Begin

CBSIZE: = SizeOf (ICO); // Initialization size is instance size

Wnd: = form1.handle; // Valid window handle

Uid: = 0; // An application is in the system

Uflags: = nif_icon or nif_message or nif_tip; // Setting the valid domain as icons, messages, and prompts

UCALLBACKMESSAGE: = WM_MYCALL; // Corresponding message processing

Hicon: = icos.handle; // icon

LSTRCPY (SZTIP, 'Left button pop-up system menu, right-click change icon'); // Set the prompt when the mouse is passed

Shell_Notifyicon (MSGS, @ ICO);

END;

END;

END;

Procedure TFORM1.WndProc (Var Msg: TMessage); // Reserved and custom message

Var MousePoint: Tpoint; CurrentTime, Delaysecond: Really

Begin

GetCursorpos (MousePoint); // Get the current mouse position

With msg do

Begin

IF msg = wm_mycall life // is a custom message

Begin

Delaysecond: = getDoubleClickTime (); // Get the maximum time lapse of the mouse double click

Case Lparam of

WM_LBUTTONDBLCLK: / / Double click

Begin

Tag: = 1; // When you have double-click, this code is re-entered and the clicks are fake.

MessageBox (Handle, 'This is double-click processing', 'joining code ", MB_OK); sendMessage (Handle, WM_Close, 0, 0)

END;

WM_LBUTTONDOWN: / / Click

Begin

CurrentTime: = gettickcount; // Get the current boot time

While (CurrentTime Delaysecond)) and (tag = 0) DO

Application.ProcessMESSAGES;

// If you do not double click within the valid time that you double-click, you can perform the clicks as a BOOL variable that you double-click. This loop is the replacement of the clock control, or you can activate a clock when you click. When you have not triggered the event, turn off the clock, turn the clock, do double-clicking on the code, otherwise execute the clicking code when the clock is triggered, so you can avoid a double-click action to a single pair of two messages

If Tag = 0 TRACKPOPUPMENU (GetsystemMenu (Handle, False), TPM_BOTTOMALIGN OR TPM_RIGHTALIGN, MOUSEPOINT.X, MousePoint.y, 0, Handle, NIL); // Pop-up system menu, click the mouse position when you click

TAG: = 0;

END;

WM_RBUTTONDOWN: SYSTRAYICO (NIM_MODIFY, Application.icon); // Modify System Bar Icon

END;

end

Else

if wparam = sc_minimize the messageBox (Handle, 'will minimize', 'intercept system messages', MB_OK); // can use this method to intercept all messages to the window to customize the processing process

END;

Inherited WNDPROC (MSG); // Continue to have not been processed

END;

Procedure TFORM1.FormCreate (Sender: TOBJECT);

Begin

SYSTRAYICO (NIM_ADD, ICON); // Establish a system bar icon

END;

Procedure TFORM1.FORMDESTROY (Sender: TOBJECT);

Begin

SYSTRAYICO (NIM_DELETE); // Remove the icon when exiting

END;

End.

The above is a more than the code using the API, the API call is:

Shell_notifyicon (Action to the System Task Bar); getCursorpos (getting the current mouse location); getDoubleClicktime (get the maximum time interval for double-click); MessageBox; gettickcount; TRACKPOPMENU (Power-on menu) GetSystemMenu (get the system menu handle) and close the window via SendMessage

转载请注明原文地址:https://www.9cbs.com/read-124325.html

New Post(0)