请教一下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编程呢?
...全文
109 2 点赞 打赏 收藏 举报
写回复
2 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
wuyq11 2010-07-12
自定义的类
看看相关DLL和命名空间
  • 打赏
  • 举报
回复
兔子-顾问 2010-07-12
自己定义的。封装了api函数的静态类。
OCCommon也是,一个静态类。
  • 打赏
  • 举报
回复
相关推荐
发帖
C#
创建于2007-09-28

10.6w+

社区成员

.NET技术 C#
申请成为版主
帖子事件
创建了帖子
2010-07-12 07:28
社区公告

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