添加引用和代码, using System.Runtime.InteropServices; [DllImport("user32.dll", EntryPoint = "SetWindowPos")] public static extern int SetWindowPos( int hwnd, int hWndInsertAfter, int x, int y, int cx, int cy, int wFlags ); [DllImport("user32.dll", EntryPoint = "FindWindow")] public static extern int FindWindow( string lpClassName, string lpWindowName );然后在 Form_Load中加入如下语句即可: retValue = SetWindowPos(Me.hwnd, HWND_TOPMOST, Me.CurrentX, Me.CurrentY, 300, 300, SWP_SHOWWINDOW) 这样窗体就能保持在所有窗体的前面了。 C#窗口前置 前端显示 .NET技术 作者:王铭 出处:夜来居 2009-6-12 22:27:10
刚才有个参数错了,using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Runtime.InteropServices;namespace text { public partial class Form1 : Form { [DllImport("user32.dll", EntryPoint = "SetWindowPos")] public static extern int SetWindowPos( IntPtr hwnd, int hWndInsertAfter, int x, int y, int cx, int cy, int wFlags ); public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { int a = SetWindowPos(this.Handle, -1, Screen.PrimaryScreen.Bounds.Width / 2, Screen.PrimaryScreen.Bounds.Height / 2, this.Width, this.Height, 0); } } }新建了一个窗体,完整代码,测试可用。
using System.Runtime.InteropServices; [DllImport("user32.dll", EntryPoint = "SetWindowPos")] public static extern int SetWindowPos( int hwnd, int hWndInsertAfter, int x, int y, int cx, int cy, int wFlags ); [DllImport("user32.dll", EntryPoint = "FindWindow")] public static extern int FindWindow( string lpClassName, string lpWindowName );然后在
Form_Load中加入如下语句即可: retValue = SetWindowPos(Me.hwnd, HWND_TOPMOST, Me.CurrentX, Me.CurrentY, 300, 300, SWP_SHOWWINDOW) 这样窗体就能保持在所有窗体的前面了。
C#窗口前置 前端显示 .NET技术
作者:王铭 出处:夜来居 2009-6-12 22:27:10
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;namespace text
{
public partial class Form1 : Form
{
[DllImport("user32.dll", EntryPoint = "SetWindowPos")]
public static extern int SetWindowPos(
IntPtr hwnd,
int hWndInsertAfter,
int x,
int y,
int cx,
int cy,
int wFlags
);
public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{
int a = SetWindowPos(this.Handle, -1, Screen.PrimaryScreen.Bounds.Width / 2, Screen.PrimaryScreen.Bounds.Height / 2, this.Width, this.Height, 0);
}
}
}新建了一个窗体,完整代码,测试可用。
知道这代码什么意思比“完整代码,测试可用”有效多了。这个“作者:王铭”似乎有了大发现,可喜可贺。可是人家微软早就把SetWindowPos(SWP_TOPMOST)封装到了Form的TopMost属性中啦。
惊现版主!感谢指点~
虽然没查过内人是谁,但是当初靠这方法解决了很多问题~
作为伸手党,直接copy了网页代码留档,
如今有人问了就发出来交流交流~
嘛~只是现在才知道form自带顶置~ 囧~~~
另外,电脑只有一个显示器接口的话,只能插一个显示器,换个带双接口的显卡吧~