using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace SmartDeviceProjectWcfTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.Load += new EventHandler(MainForm_Load);
}
private const int GWL_WNDPROC = -4;
private const int WM_SETTINGCHANGE = 0x1A;
private const int SPI_SETSIPINFO = 0xE0;
private enum SIPStatus
{
SIPF_OFF = 0,
SIPF_ON
}
[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
public Int32 left;
public Int32 top;
public Int32 right;
public Int32 bottom;
}
[StructLayout(LayoutKind.Sequential)]
public struct IMINFO
{
public Int32 cbSize;
public IntPtr hImageNarrow;
public IntPtr hImageWide;
public Int32 iNarrow;
public Int32 iWide;
public Int32 fdwFlags;
public RECT rcSipRect;
}
[StructLayout(LayoutKind.Sequential)]
public struct SIPINFO
{
public Int32 cbSize;
public Int32 fdwFlags;
public RECT rcVisibleDesktop;
public RECT rcSipRect;
public Int32 dwImDataSize;
public IntPtr pvImData;
}
delegate IntPtr WndProcDelegate(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam);
[DllImport ("coredll.dll", EntryPoint = "GetWindowLong")]
private static extern IntPtr GetWindowLong (IntPtr hWnd, int nIndex);
[DllImport ("coredll.dll")]
static extern int SetWindowLong (IntPtr hWnd, int nIndex, IntPtr newWndProc);
[DllImport ("coredll.dll")]
static extern IntPtr CallWindowProc (IntPtr lpPrevWndFunc, IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
[DllImport ("coredll")]
static extern bool SipGetInfo (ref SIPINFO si);
[DllImport("coredll.dll")]
private static extern bool SipSetInfo(ref SIPINFO pSipInfo);
private static IntPtr oldWndProc = IntPtr.Zero;
private static WndProcDelegate newWndProc;
protected virtual void SipPannelShow(){}
protected virtual void SipPannelHide() { }
private void MainForm_Load (object sender, EventArgs e)
{
newWndProc = new WndProcDelegate (WndProc);
oldWndProc = GetWindowLong (this.Handle, GWL_WNDPROC);
int success = SetWindowLong (this.Handle, GWL_WNDPROC, Marshal.GetFunctionPointerForDelegate (newWndProc));
}
public IntPtr WndProc (IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam)
{
if (msg == WM_SETTINGCHANGE && wParam.ToInt32() == SPI_SETSIPINFO)
{
SIPINFO sipInfo = new SIPINFO();
sipInfo.cbSize = Marshal.SizeOf (typeof(SIPINFO));
SipGetInfo(ref sipInfo);
if ((sipInfo.fdwFlags & (int)SIPStatus.SIPF_ON) == (int)SIPStatus.SIPF_ON)
{
// Do something if the SIP is showing
SipPannelShow();
}
else
{
// Do something if the SIP is NOT showing
SipPannelHide();
}
}
return CallWindowProc (oldWndProc, this.Handle, msg, wParam, lParam);
}
public static bool SipSetRect(int x, int y, int width, int height)
{
SIPINFO sipinfo = new SIPINFO();
if (!SipGetInfo(ref sipinfo))
return false;
IMINFO iminfo = (IMINFO)Marshal.PtrToStructure(sipinfo.pvImData, typeof(IMINFO));
iminfo.rcSipRect.left = x;
iminfo.rcSipRect.right = x + width - 1;
iminfo.rcSipRect.top = y;
iminfo.rcSipRect.bottom = y + height - 1;
Marshal.StructureToPtr(iminfo, sipinfo.pvImData, true);
return SipSetInfo(ref sipinfo);
}
}
}
子窗体
override protected void SipPannelShow()
{
this.panelMain.Height = this.panelMain.Height + 180;
this.panelMain.Location = new Point(0, -180);
}
override void SipPannelHide()
{
base.SipPannelHide();
this.panelMain.Height = this.panelMain.Height - 180;
this.panelMain.Location = new Point(0, 0);
}