请教一下C# win32API编程的问题?在线等待。。。急用,谢谢

fengzhengju 2010-07-12 07:28:36

class MessageBoxEx
{
//测试样例
public static void test()
{
Show("提示消息", "提示标题", MessageBoxButtons.YesNoCancel, new string[] { "按钮一(&O)", "按钮二(&T)", "按钮三(&H)" });
}

public static DialogResult Show(string text, string caption, MessageBoxButtons buttons, string[] buttonTitles)
{
DummyForm frm = new DummyForm(buttons, buttonTitles);
frm.Show();
frm.WatchForActivate = true;
DialogResult result = MessageBox.Show(frm, text, caption, buttons);
frm.Close();
return result;
}

class DummyForm : Form
{
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int x, int y, int Width, int Height, int flags);
[DllImport("user32.dll")]
public static extern IntPtr GetWindow(IntPtr hWnd, long wCmd);
[DllImport("user32.dll")]
public static extern bool SetWindowText(IntPtr hWnd, string lpString);
[DllImport("user32.dll")]
public static extern int GetClassNameW(IntPtr hWnd, [MarshalAs(UnmanagedType.LPWStr)]StringBuilder lpString, int nMaxCount);

public static string GetWindowClassName(IntPtr handle)
{
StringBuilder sb = new StringBuilder(256);

GetClassNameW(handle, sb, sb.Capacity); //得到窗口类名并保存在strClass中
return sb.ToString();
}

public const int GW_CHILD = 5;
public const int GW_HWNDNEXT = 2;

IntPtr _handle;
MessageBoxButtons _buttons;
string[] _buttonTitles = null;

bool _watchForActivate = false;

public bool WatchForActivate
{
get { return _watchForActivate; }
set { _watchForActivate = value; }
}

public DummyForm(MessageBoxButtons buttons, string[] buttonTitles)
{
_buttons = buttons;
_buttonTitles = buttonTitles;

//让自己在界面上看不到
this.Text = "";
this.StartPosition = FormStartPosition.Manual;
this.Location = new Point(-32000, -32000);
this.ShowInTaskbar = false;
}

protected override void OnShown(EventArgs e)
{
base.OnShown(e);
//把自己藏起来,在任务列表里也看不到
NativeWin32API.SetWindowPos(this.Handle, IntPtr.Zero, 0, 0, 0, 0, 659);
}

protected override void WndProc(ref System.Windows.Forms.Message m)
{
if (_watchForActivate && m.Msg == 0x0006)
{
_watchForActivate = false;
_handle = m.LParam;
CheckMsgbox();
}
base.WndProc(ref m);
}

private void CheckMsgbox()
{
if (_buttonTitles == null || _buttonTitles.Length == 0)
return;

//按钮标题的索引
int buttonTitleIndex = 0;
//获取子控件的句柄
IntPtr h = NativeWin32API.GetWindow(_handle, OCCommon.Message.GW_CHILD);
while (h != IntPtr.Zero)
{
//按顺序把按钮标题赋上
if (NativeWin32API.GetWindowClassName(h).Equals("Button"))
{
if (_buttonTitles.Length > buttonTitleIndex)
{
NativeWin32API.SetWindowText(h, _buttonTitles[buttonTitleIndex]);
buttonTitleIndex++;
}
}
h = NativeWin32API.GetWindow(h, OCCommon.Message.GW_HWNDNEXT);
}
}
}
}
请问一下NativeWin32API和OCCommon这两个是怎么来的啊?
怎样调用win32API编程呢?
...全文
180 2 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
wuyq11 2010-07-12
  • 打赏
  • 举报
回复
自定义的类
看看相关DLL和命名空间
兔子-顾问 2010-07-12
  • 打赏
  • 举报
回复
自己定义的。封装了api函数的静态类。
OCCommon也是,一个静态类。

111,094

社区成员

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

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

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