请问怎么控制MessageBox()弹出的对话框的位置?

liyuhui2007 2007-12-29 01:03:02
例如:我想让一个MessageBox()弹出的对话框在窗体的左下角显示,或右下角,或是窗体的某一个位置
...全文
1269 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
shinaterry 2008-02-26
  • 打赏
  • 举报
回复
-_-!!!
北京的雾霾天 2007-12-29
  • 打赏
  • 举报
回复
哎,真是的,这样的:

我也写了一个,秀一下哈,要不白费了,好在和上面的都不太一样:
[code=C#]
private NW nw = null;
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
nw = new NW();

}
protected override void WndProc(ref Message m)
{
if (m.Msg == 0x86 && m.LParam!=IntPtr.Zero)
{
if (nw.Handle != null)
{
nw.ReleaseHandle();
}
nw.AssignHandle(m.LParam);
nw.SetPoint(this.Left, this.Top);//显示在窗体的左上角了
}
base.WndProc(ref m);
}
protected override void OnClosed(EventArgs e)
{
base.OnClosed(e);
nw.ReleaseHandle();
}
class NW : NativeWindow
{
[StructLayout(LayoutKind.Sequential)]
public struct WINDOWPOS
{
public IntPtr hwnd;
public IntPtr hwndInsertAfter;
public int x;
public int y;
public int cx;
public int cy;
public int flags;
public override string ToString()
{
return string.Format("{0} {1}, {2}, {3}", x, y, cx, cy);
}
}

[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int x, int y, int cx, int cy, int flags);

private int m_x;
private int m_y;
protected override void WndProc(ref Message m)
{
if (m.Msg == 0x6)
{
SetWindowPos(this.Handle, IntPtr.Zero, this.m_x, this.m_y, 0, 0, 0x1);
}
base.WndProc(ref m);
}

public void SetPoint(int x, int y)
{
this.m_x = x;
this.m_y = y;
}
public void SetPoint(Point point)
{
this.m_x = point.X;
this.m_y = point.Y;
}
}

private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("aaaaa");
}code]
北京的雾霾天 2007-12-29
  • 打赏
  • 举报
回复
我也写了一个,秀一下哈,要不白费了,好在和上面的都不太一样:
[code=C#]
private NW nw = null;
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
nw = new NW();

}
protected override void WndProc(ref Message m)
{
if (m.Msg == 0x86 && m.LParam!=IntPtr.Zero)
{
if (nw.Handle != null)
{
nw.ReleaseHandle();
}
nw.AssignHandle(m.LParam);
nw.SetPoint(this.Left, this.Top);//显示在窗体的左上角了
}
base.WndProc(ref m);
}
protected override void OnClosed(EventArgs e)
{
base.OnClosed(e);
nw.ReleaseHandle();
}
class NW : NativeWindow
{
[StructLayout(LayoutKind.Sequential)]
public struct WINDOWPOS
{
public IntPtr hwnd;
public IntPtr hwndInsertAfter;
public int x;
public int y;
public int cx;
public int cy;
public int flags;
public override string ToString()
{
return string.Format("{0} {1}, {2}, {3}", x, y, cx, cy);
}
}

[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int x, int y, int cx, int cy, int flags);

private int m_x;
private int m_y;
protected override void WndProc(ref Message m)
{
if (m.Msg == 0x6)
{
SetWindowPos(this.Handle, IntPtr.Zero, this.m_x, this.m_y, 0, 0, 0x1);
}
base.WndProc(ref m);
}

public void SetPoint(int x, int y)
{
this.m_x = x;
this.m_y = y;
}
public void SetPoint(Point point)
{
this.m_x = point.X;
this.m_y = point.Y;
}
}

private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("aaaaa");
}code]
jx0797 2007-12-29
  • 打赏
  • 举报
回复

struct RECT
{
public int left;
public int top;
public int right;
public int bottm;
}

class State
{
public string caption;
public int x;
public int y;

public State(string c, int x1, int y1)
{
caption = c;
x = x1;
y = y1;
}
}

class MyMessageBox
{
#region Api

[DllImport("User32")]
static extern bool MoveWindow(uint hwnd, int x, int y, int width, int height, bool rePaint);
[DllImport("User32")]
static extern uint FindWindow(string className, string windowName);
[DllImport("User32")]
static extern bool GetWindowRect(uint hwnd, ref RECT rect);

#endregion

public static DialogResult ShowMessageBox(string text, string caption, int x, int y)
{
ThreadPool.QueueUserWorkItem(new WaitCallback(ChangeWindowPoint), new State(caption, x, y));
return MessageBox.Show(text, caption);
}

static void ChangeWindowPoint(object state)
{
State temp = state as State;
RECT rect = new RECT();
uint hwnd = 0;

while (hwnd == uint.MinValue)
{
hwnd = FindWindow("#32770", temp.caption);
}
GetWindowRect(hwnd, ref rect);
MoveWindow(hwnd, temp.x, temp.y, rect.right - rect.left, rect.bottm - rect.top, true);
}
}



private void button1_Click(object sender, EventArgs e)
{
MyMessageBox.ShowMessageBox("自定义坐标消息框。", "消息框", 400, 400);
}
王集鹄 2007-12-29
  • 打赏
  • 举报
回复
实现起来是比较麻烦,参考如下代码:
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 路过");
}
赵凯~ 2007-12-29
  • 打赏
  • 举报
回复
与其调用API还不如自己写个FORM呢~~~~~~~~
lovefootball 2007-12-29
  • 打赏
  • 举报
回复
lovefootball 2007-12-29
  • 打赏
  • 举报
回复
API可以实现
首先用FindWindow找到对话框然后用MoveWindow

[DllImport("user32", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern int MoveWindow(IntPtr hWnd, int x, int y, int w, int h, int repaint);
[DllImport("user32.dll", EntryPoint = "FindWindow", CharSet = CharSet.Auto)]
private extern static IntPtr FindWindow(string lpClassName, string lpWindowName);
jlbaowei 2007-12-29
  • 打赏
  • 举报
回复
应该不可以
GhostAdai 2007-12-29
  • 打赏
  • 举报
回复
不可以,自己重写一个。
jupiter911 2007-12-29
  • 打赏
  • 举报
回复
因为MessageBox本身并不是Form,所以你无法控制他的位置,只能自己写一个窗体了
liyuhui2007 2007-12-29
  • 打赏
  • 举报
回复
自己写一个form怎么弄呀?请给个例子看看呀
wzytiger 2007-12-29
  • 打赏
  • 举报
回复
没用过,好象不可以的,
taiyangyu119 2007-12-29
  • 打赏
  • 举报
回复
不可以,自己写一个吧
peterb 2007-12-29
  • 打赏
  • 举报
回复
自己做个form实现吧 好像MessageBox()不可以

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧