Silverlight5 RC调用Win32API
没错,如你所知,silverlight5 RC已经提供 P/Invoke 调用本机函数的支持! 一时间情绪有些激动,立即写了个小Demo测试一下效果.
目前已经确认的是在silverlightOOB信任模式下,才可以正常调用!客户端提权需要在silverlight应用发布时进行证书签名,即可提权!
设置OOB模式 勾选 提升信任权限.
例子 1 调用windows系统提示框的例子.
代码如下
[DllImport("user32.dll", EntryPoint = "MessageBoxA")] static extern int MsgBox(int hWnd, string msg, string caption, int type);
private void button1_Click(object sender, System.Windows.RoutedEventArgs e) { MsgBox(0, "这就是用DllImport调用DLL弹出的提示框哦!", "提示", 0x30); }
例子2 让silverlight窗口保存置顶
[DllImport("user32.dll", CharSet = CharSet.Auto)] private static extern int SetWindowPos(IntPtr hWnd, int hWndInsertAfter, int x, int y, int Width, int Height, int flags); /// <summary> /// 得到当前活动的窗口 /// </summary> /// <returns></returns> [DllImport("user32.dll", CharSet = CharSet.Auto)] private static extern System.IntPtr GetForegroundWindow();
private void button1_Click(object sender, System.Windows.RoutedEventArgs e) { SetWindowPos(GetForegroundWindow(), -1, 0, 0, 0, 0, 1 | 2); }