【发布时间】:2010-06-19 21:57:28
【问题描述】:
在我的 C# 应用程序中,我尝试使用 TDM_CLICK_BUTTON 消息单击 TaskDialog 中的按钮。这基本上工作得很好。我期望发生的事情,发生了。我什至收到了TDN_BUTTON_CLICKED 通知。
但documentation 表示,如果调用成功,SendMessage 的返回值将非零。但它总是对我返回零。
这是我的代码:
public void ClickButton( int buttonId ) {
bool success = UnsafeNativeMethods.SendMessage(
WindowHandle,
(uint)UnsafeNativeMethods.TASKDIALOG_MESSAGES.TDM_CLICK_BUTTON,
(IntPtr)buttonId,
IntPtr.Zero ) != IntPtr.Zero;
if( !success ) {
int lastWin32Error = Marshal.GetLastWin32Error();
throw new Win32Exception( lastWin32Error, "SendMessage for TDM_CLICK_BUTTON failed." );
}
}
lastWin32Error 在抛出异常时始终为零。这将是一切都很好的另一个指标。
SendMessage 在我的代码中声明如下:
[DllImport( "user32.dll", SetLastError = true )]
internal static extern IntPtr SendMessage( IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam );
是文档不正确还是我使用的消息不正确?
【问题讨论】:
-
不是答案,但您需要将
SetLastError=true添加到您的 DllImport 以使Marshal.GetLastWin32Error()正常工作。 msdn.microsoft.com/en-us/library/… -
谢谢。我添加了,但 lastWin32Error 仍然为 0。
-
如果我要查看文档,不,它不会。返回值为零。但是我打算对调用执行的操作(调用单击某个按钮时会发生的操作)成功了。我什至收到了 TDN_BUTTON_CLICKED 通知。这是我整个困惑的基础。
标签: c# winapi sendmessage taskdialog