110,567
社区成员
发帖
与我相关
我的任务
分享
From2 fm = new Form2();
Point point = PointToScreen(this.Location);
fm.StartPosition = FormStartPosition.Manual;
fm.Location = new Point(point.X + (this.Size.Width - fm.Size.Width) / 2, point.Y + (this.Size.Height - fm.Size.Height) / 2);
fm.Show();
public class MessageBox
{
public MessageBox()
{
}
public static DialogResult Show()
{
Form2 frm = new Form2();//Form2是自己新建的form,外观做的和MessageBox.show出来的效果一样。设置窗体的StartPosition为CenterParent.
return frm.ShowDialog();
}
}
public class MessageBox
{
public MessageBox()
{
}
public static DialogResult Show()
{
Form2 frm = new Form2();//Form2是自己新建的form,外观做的和MessageBox.show出来的效果一样。设置窗体的StartPosition为CenterParent.
return frm.ShowDialog();
}
}
using System.Runtime.InteropServices;
public delegate IntPtr HookProc(int nCode, IntPtr wParam, IntPtr lParam);
[DllImport("user32.dll")]
public static extern IntPtr SetWindowsHookEx(int hookid,
HookProc pfnhook, IntPtr hinst, int threadid);
[DllImport("user32.dll")]
public static extern IntPtr CallNextHookEx(IntPtr hhook,
int code, IntPtr wparam, IntPtr lparam);
[DllImport("kernel32.dll")]
public static extern IntPtr GetModuleHandle(string modName);
[DllImport("user32.dll")]
public static extern bool UnhookWindowsHookEx(IntPtr hhook);
[DllImport("user32.dll")]
public static extern bool GetWindowRect(IntPtr hWnd, ref Rectangle rect);
[DllImport("user32.dll")]
public static extern bool MoveWindow(
IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);
public const int WH_CBT = 5;
public const int HCBT_ACTIVATE = 5;
IntPtr hookHandle = IntPtr.Zero;
private IntPtr CBTHookCallback(int nCode, IntPtr wParam, IntPtr lParam)
{
switch(nCode)
{
case HCBT_ACTIVATE:
Rectangle vRectangle = new Rectangle();
GetWindowRect(wParam, ref vRectangle);
vRectangle.Width = vRectangle.Width - vRectangle.Left;
vRectangle.Height = vRectangle.Height - vRectangle.Top;
MoveWindow(wParam, // 右下
Screen.GetWorkingArea(this).Width - vRectangle.Width,
Screen.GetWorkingArea(this).Height - vRectangle.Height,
vRectangle.Width, vRectangle.Height, false);
UnhookWindowsHookEx(hookHandle);
break;
}
return CallNextHookEx(hookHandle, nCode, wParam, lParam);
}
private void button1_Click(object sender, EventArgs e)
{
hookHandle = SetWindowsHookEx(WH_CBT, new HookProc(CBTHookCallback),
GetModuleHandle(null), 0);
MessageBox.Show("Zswang 路过");
}
居中就是计算一下出现的位置,交给楼主自己。[img=http://p.blog.csdn.net/images/p_blog_csdn_net/zswang/reply.jpg" alt="" />