在我的C#應用程序中,我試圖使用TDM_CLICK_BUTTON
消息來單擊TaskDialog中的按鈕。這基本上工作得很好。我期望發生的事情發生。我甚至收到TDN_BUTTON_CLICKED
通知。帶TDM_CLICK_BUTTON的SendMessage返回值始終爲零
但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);
是文檔不正確或我使用不正確的信息?
不是答案,但是您需要爲''Marshal.GetLastWin32Error()''的DllImport添加'SetLastError = true'才能正常工作。 http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.marshal.getlastwin32error.aspx – shf301 2010-06-19 23:37:40
謝謝。我補充說,但lastWin32Error仍然爲0. – 2010-06-19 23:45:58
這個調用是否成功? – 2010-06-20 00:04:09