📄 sip.cs
字号:
using System;
using System.Runtime.InteropServices;
namespace PInvokeLibrary
{
/// <summary>
/// Provides SIP manipulation methods.
/// </summary>
public class SIP
{
/// <summary>
/// The software-based input panel is off, or not visible.
/// </summary>
public const uint SIPF_OFF = 0x0000;
/// <summary>
/// The software-based input panel is on, or visible.
/// </summary>
public const uint SIPF_ON = 0x0001;
/// <summary>
/// The software-based input panel is docked, or not floating.
/// </summary>
public const uint SIPF_DOCKED = 0x00000002;
/// <summary>
/// The software-based input panel is locked, meaning that the user cannot
/// change its visible status.
/// </summary>
public const uint SIPF_LOCKED = 0x00000004;
/// <summary>
/// This function shows or hides the currently active software-based input
/// panel window. An application must call this function to display the
/// software-based input panel and its current input method.
/// </summary>
/// <param name="dwFlag">[in] Specifies whether to show or hide the current
/// software-based input panel window. It is one of the following flags.
/// Can be either SIPF_OFF or SIPF_ON.</param>
[DllImport("coredll.dll")]
public extern static void SipShowIM(uint dwFlag);
// SipStatus return values
public const uint SIP_STATUS_UNAVAILABLE = 0;
public const uint SIP_STATUS_AVAILABLE = 1;
/// <summary>
/// This function is called by an application to determine if the software-
/// based input panel environment is loaded. With this information, an
/// application can decide whether to continue interacting with the
/// software-based input panel environment.
/// </summary>
/// <returns>SIP_STATUS_AVAILABLE indicates that the software-based input
/// panel environment is installed. SIP_STATUS_UNAVAILABLE indicates that
/// the software-based input panel environment is not installed.</returns>
[DllImport("coredll.dll")]
public extern static uint SipStatus();
/// <summary>
/// Embedded in the SIPINFO class. RECT represents a rectangle.
/// </summary>
public struct RECT
{
public int left;
public int top;
public int right;
public int bottom;
}
/// <summary>
/// This structure contains information about the current state of the
/// software-based input panel, such as the software-based input panel size,
/// screen location, docked status, and visibility status.
/// </summary>
public class SIPINFO
{
/// <summary>
/// Initialize a SIPINFO instance by setting the size.
/// </summary>
public SIPINFO()
{
cbSize = (uint)Marshal.SizeOf(this);
}
/// <summary>
/// Size, in bytes, of the SIPINFO structure. This member must be filled
/// in by the application with the size of operator. Because the system
/// can check the size of the structure to determine the operating system
/// version number, this member allows for future enhancements to the
/// SIPINFO structure while maintaining backward compatibility.
/// </summary>
public uint cbSize;
/// <summary>
/// Specifies flags representing state information of the software-based
/// input panel. It is any combination of the following bit flags:
/// SIPF_DOCKED, SIPF_LOCKED, SIPF_OFF, and SIPF_ON.
/// </summary>
public uint fdwFlags;
/// <summary>
/// Rectangle, in screen coordinates, that represents the area of the
/// desktop not obscured by the software-based input panel. If the software-
/// based input panel is floating, this rectangle is equivalent to the
/// working area. Full-screen applications that respond to software-based
/// input panel size changes can set their window rectangle to this rectangle.
/// If the software-based input panel is docked but does not occupy an entire
/// edge, then this rectangle represents the largest rectangle not obscured by
/// the software-based input panel. If an application wants to use the screen
/// space around the software-based input panel, it needs to reference
/// rcSipRect.
/// </summary>
public RECT rcVisibleDesktop;
/// <summary>
/// Rectangle, in screen coordinates of the window rectangle and not the
/// client area, the represents the size and location of the software-based
/// input panel. An application does not generally use this information unless
/// it needs to wrap around a floating or a docked software-based input panel
/// that does not occupy an entire edge.
/// </summary>
public RECT rcSipRect;
/// <summary>
/// Specifies the size of the data pointed to by the pvImData member.
/// </summary>
public uint dwImDataSize;
/// <summary>
/// Void pointer to IM-defined data. The IM calls the
/// IInputMethod::GetImData and IInputMethod::SetImData methods to send
/// and receive information from this structure.
/// </summary>
public IntPtr pvImData;
}
/// <summary>
/// This function receives information including the state of the software-
/// based input panel, the area of the desktop that is not obscured by the
/// software-based input panel, the screen coordinates of the software-based
/// input panel, and information about the input method (IM) that the
/// software-based input panel is currently using.
/// </summary>
/// <param name="pSipInfo">[out] Pointer to the SIPINFO structure that contains
/// information about the current software-based input panel.</param>
/// <returns>TRUE indicates success. FALSE indicates failure. To get extended
/// error information, call GetLastError.</returns>
[DllImport("coredll.dll")]
public extern static uint SipGetInfo(SIPINFO pSipInfo);
/// <summary>
/// This function sets information including the state of the software-based
/// input panel, the area of the desktop that is not obscured by the software-
/// based input panel, the screen coordinates of the software-based input
/// panel, and application-defined information about the input method (IM)
/// that the software-based input panel is currently using.
/// </summary>
/// <param name="pSipInfo">[in] Pointer to the SIPINFO structure that contains
/// information about the current software-based input panel.</param>
/// <returns>TRUE indicates success. FALSE indicates failure. To get extended
/// error information, call GetLastError.</returns>
[DllImport("coredll.dll")]
public extern static uint SipSetInfo(SIPINFO pSipInfo);
/// <summary>
/// Displays the current status flags as string outputs to the provided
/// MainTest.DisplayLineDelegate instance.
/// This is a helper function for TestProc.
/// </summary>
/// <param name="flags">SIPINFO.fdwFlags value</param>
/// <param name="showLine">Method to use for displaying text</param>
protected static void ShowSipFlags(uint flags, MainTest.DisplayLineDelegate showLine)
{
if ((flags & SIPF_ON) == SIPF_ON)
showLine("SIP ON");
else
showLine("SIP Off");
if ((flags & SIPF_DOCKED) == SIPF_DOCKED)
showLine("SIP Docked");
if ((flags & SIPF_LOCKED) == SIPF_LOCKED)
showLine("SIP Locked");
}
/// <summary>
/// Run a test of the SIP class.
/// </summary>
/// <param name="showLine">Delegate called to show debug information</param>
public static void TestProc(MainTest.DisplayLineDelegate showLine)
{
if (SipStatus() == SIP_STATUS_UNAVAILABLE)
{
showLine("FAILURE: SIP Unavailable");
return;
}
else
{
showLine("SIP Available.");
}
SIPINFO sipInfo = new SIPINFO();
if (SipGetInfo(sipInfo) != 0)
{
ShowSipFlags(sipInfo.fdwFlags, showLine);
}
else
{
showLine("FAILURE: SipGetInfo failed");
}
showLine("Showing SIP (1 second)...");
SipShowIM(SIPF_ON);
if (SipGetInfo(sipInfo) != 0)
{
ShowSipFlags(sipInfo.fdwFlags, showLine);
}
else
{
showLine("FAILURE: SipGetInfo failed");
}
for (int i = 0; i < 100; i++)
{
System.Windows.Forms.Application.DoEvents();
System.Threading.Thread.Sleep(10);
}
showLine("Removing SIP");
SipShowIM(SIPF_OFF);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -