Call Win32 API

xiaoxiao2021-03-06  79

Call WIN32 APIusing System.Runtime.InteropServices; DllImport ( "user32.dll")] public static extern IntPtr FindWindow (string lpClassName, string lpWindowName); [DllImport ( "user32.dll")] public static extern IntPtr FindWindowEx (IntPtr hwndParent, INTPTR HWNDCHILDAFTER, STRING LPSZCLASS, STRING LPSZWINDOW

Function: HWnd FindWindow (LPCTSTR IPClassName, LPCTSTR IPWindownAme); Function Function: This function gets a handle of a top window, the class name and window name match the given string. This function does not look for the sub-window. Not case sensitive when looking up. Parameters: IPClassName: Points to a null end string specified by the class name, or a pointer to a member identifying a class string. If this parameter is a member, it must be a global member that is previously called the GlobafadDatom function. The member is 16 bits and must be located at the low of IPClassName, and the high position must be 0. IPWindowname: Points an empty end string specified by a window name (window title). If this parameter is empty, you all match all windows. Return Value: If the function is successful, the return value is a window handle with the specified class name and window name; if the function fails, the return value is null.

Function prototype: HWnd FindWindowEx (HWND HWndParent, HWND HWNDILDAFTER, LPCTSTSTSZCLASS, LPCTSTSTSTSZWINDOW); function function: This function gets a handle of a window, the class name and window name matches the given string. This function looks for the sub-window, starting from the next sub-window behind a given sub-window. Not case sensitive when looking up. Parameter;

HWndParent: To find the parent window handle of the child window. If hwnjparent is NULL, the function is used as the parent window at the desktop window, and looks for all sub-windows of the desktop window. Windows NT5.0 and later: If hwndparent is hwnd_message, the function only looks for all message windows. HWNDCHILDAFTER: Sub window handle. Find starting from the next sub-window in the z-order. The sub-window must be a direct sub-window of the HWndParert window instead of the post-generation window. If hwndchildafter is NULL, look up starting from the first sub-window from HWNDPART. If hwndparent and hwndchildafter are simultaneously NULL, the function looks for all top-level windows and message windows. LPSZCLASS: Points to a null end string specified by the class name, or a pointer to a member identifying a class string. If this parameter is a member, it must be a global member generated by the THEGLOBAIADDATOM function before. The member is 16 bits and must be 16 bits of LPClassName, and the high position must be 0. LPSZWindow: Points an empty end string specified by the window name (window title). If this parameter is null, all windows are all matched. Return Value: If the function is successful, the return value is a window handle with the specified class name and window name. If the function fails, the return value is NULL. Function prototype: BOOL SETWINDOWTEXT (HWND HWND, LPCTSTR LPSTRJNG); function function: This function changes the text content of the title bar of the specified window (if the window has a title bar). If the specified window is a control, change the text content of the control. However, the SETWINDOWTEXT function does not change the text content of the control in other applications. Parameters: hwnd: To change the handle of the window or control of the text content. LPSTRING: Points to a pointer to an empty string that will be a new text as a window or control. Return Value: If the function is successful, the return value is non-zero; if the function fails, the return value is zero. To get more error messages, call the getLastError function. Note: If the target window belongs to the current process, the SETWINDOWTEXT function will send the WM_SETTEXT message to the specified window or control. However, if the control is controlled by the list box created in the WS_CAPTION style, the SetWindowText function will set text for the control settings instead of setting the text for the list item. The SetWindowText function does not extend the Tab character (ASCII code 09), the Tab character is displayed in character '}'. Function prototype: BOOL ShowWindow (HWND HWND, INT NCMDSHOW); Function Function: This function sets the display status of the specified window. Parameters: hwnd: Window handle.

NCMDSHOW: How to specify how the window is displayed. If the program is sent to the STARTUPINFO structure, the application is ignored when the application calls ShowWindow for the first time. Otherwise, when the ShowWindow function is called for the first time, the value should be the NCMDSHOW parameter in the function WinMain. In the subsequent call, this parameter can be one of the following values:

SW_FORCEMINIMIZE: Minimizes the window in Windownt 5.0, even if the thread with the window is hanging, it will minimize. This parameter is used when minimizing the window from other threads. SW_MIOE: Hide the window and activate other windows.

SW_MAXIMIZE: Maximize the specified window.

SW_MINIMIZE: Minimizes the specified window and activate the next top window in the z-order.

SW_RESTORE: Activate and displays the window. If the window minimizes or maximizes, the system returns the window to the original size and location. When the minimum window is restored, the application should specify this flag.

SW_SHOW: The original location is activated and displayed in the original size.

SW_SHOWDEFAULT: The STARTUPINFO structure is passed to the CreateProcess function by the SW_FLAG flag specified in the StartupInfo structure.

SW_SHOWMAXIMIZED: Activate the window and maximizes it.

SW_SHOWMINIMIZED: Activate the window and minimizes it.

SW_SHOWMINNOACTIVATE: The window minimizes the activation window.

SW_SHOWNA: Displays the window with the original status of the window. The activation window still maintains an activation state.

SW_SHOWNOACTIVATE: The window has the most recent size and status display window. The activation window still maintains an activation state.

SW_SHOWNOMAL: Activate and displays a window. If the window is minimized or maximized, the system returns to the original size and size. The application should specify this flag when the application is displayed for the first time.

Return value: If the window is visible prior to the window, the return value is non-zero. If the window is previously hidden, the return value is zero.

Remarks: When the application calls showWindow for the first time, the NCMDSHOW parameter of the Winmain function should be used as its ncmdshow parameter. When you then call the showWindow function, you must use a given value in the list, not the value specified by the NCMDSHOW parameter of the WinMain function.

As declared in the ncmdshow parameter, if the application is called, the information specified in the StartupInfo structure is used to display the window, and the NCMDSHOW parameter is ignored when the ShowWindow function is called for the first time. In this case, the ShowWindow function uses the information in the StartupInfo structure to display the window. In the subsequent call, the application must call the showWindow function (set the ncmdshow parameter to sw_showdefault) to use the startup information provided by the program to call the application. This process occurs in the following cases:

The application creates their main window functions by calling functions with the WS_Visible flag;

The application creates the main window function by calling the CTEATEWINDOW function of the WS_Visible flag, and then calls the showWindow function with the sw_show flag to display the window; ==================== ==================================================================================================================================================================== ... [DllImport ( "user32.dll")] public static extern IntPtr FindWindow (string lpClassName, string lpWindowName); [DllImport ( "user32.dll")] public static extern IntPtr FindWindowEx (IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass String lpszwindow; [DLLIMPORT ("User32.dll", Charset = Charset.Unicode)] Public Static Extern INTPTR PostMessage (INTPTR HWND, INT WMSG, INTPTR WPARAM, INTPTR LPARAM);

[DLLIMPORT ("User32.dll")] Public Static Extern INTPTR SENDMESSAGE (INTPTR HWND, INT MSG, INTPTR WPARAM, INTPTR LPARAM);

Use these API to operate ... hwnd_win = findwindow (null, "User Login"); if (hwnd_win == INTPTR.ZERO) {MessageBox.show ("Warning: Can't find the target window"); Return;} .. .

public void input (IntPtr mptr, string mstr) {Message msg; UnicodeEncoding encode = new UnicodeEncoding (); char [] chars = encode.GetChars (encode.GetBytes (mstr)); foreach (char c in chars) {msg = Message .Create (MPTR, WM_CHAR, NEW INTPTR (C), New INTPTR (0)); PostMessage (msg.hwnd, msg.msg, msg.wparam, msg.lparam);}}

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

New Post(0)