也許我的解決方案將幫助你。
我的C#「應用程序」是用作WIX的CustomAction的類庫/ dll。我想要一個TaskDialog而不是MessageBox,但是我遇到了和你一樣的異常,而據我所知,清單文件對於C#類庫不起作用。我必須結合使用我的代碼才能加載正確版本的comctl32.dll。
我剛剛得到它的工作,所以我的代碼有點凌亂和脂肪。
來源:
- http://truecheaters.com/f51/%5Bc-%5D-taskdialog-9368.html
- http://support.microsoft.com/kb/830033
1)附上EnableThemingInScope類原樣從上面的第二連桿。
2)包括此改性TaskDialog其實枚舉/類:
[Flags]
public enum TaskDialogButtons {
OK = 0x0001,
Cancel = 0x0008,
Yes = 0x0002,
No = 0x0004,
Retry = 0x0010,
Close = 0x0020
}
public enum TaskDialogIcon {
Information = UInt16.MaxValue - 2,
Warning = UInt16.MaxValue,
Stop = UInt16.MaxValue - 1,
Question = 0,
SecurityWarning = UInt16.MaxValue - 5,
SecurityError = UInt16.MaxValue - 6,
SecuritySuccess = UInt16.MaxValue - 7,
SecurityShield = UInt16.MaxValue - 3,
SecurityShieldBlue = UInt16.MaxValue - 4,
SecurityShieldGray = UInt16.MaxValue - 8
}
public enum TaskDialogResult {
None,
OK,
Cancel,
Yes,
No,
Retry,
Close
}
public class StatusDialog {
#region API
[DllImport("comctl32.dll", CharSet = CharSet.Unicode)]
public static extern int TaskDialog(IntPtr hWndParent, IntPtr hInstance, string pszWindowTitle, string pszMainInstruction, string pszContent, int dwCommonButtons, IntPtr pszIzon, out int pnButton);
#endregion
#region Modal
public static TaskDialogResult Show(IWin32Window owner, string text) {
return Show(owner, text, null, null, TaskDialogButtons.OK);
}
public static TaskDialogResult Show(IWin32Window owner, string text, string instruction) {
return Show(owner, text, instruction, null, TaskDialogButtons.OK, 0);
}
public static TaskDialogResult Show(IWin32Window owner, string text, string instruction, string caption) {
return Show(owner, text, instruction, caption, TaskDialogButtons.OK, 0);
}
public static TaskDialogResult Show(IWin32Window owner, string text, string instruction, string caption, TaskDialogButtons buttons) {
return Show(owner, text, instruction, caption, buttons, 0);
}
public static TaskDialogResult Show(IWin32Window owner, string text, string instruction, string caption, TaskDialogButtons buttons, TaskDialogIcon icon) {
return ShowInternal(owner.Handle, text, instruction, caption, buttons, icon);
}
#endregion
#region Non-Modal
public static TaskDialogResult Show(string text) {
return Show(text, null, null, TaskDialogButtons.OK);
}
public static TaskDialogResult Show(string text, string instruction) {
return Show(text, instruction, null, TaskDialogButtons.OK, 0);
}
public static TaskDialogResult Show(string text, string instruction, string caption) {
return Show(text, instruction, caption, TaskDialogButtons.OK, 0);
}
public static TaskDialogResult Show(string text, string instruction, string caption, TaskDialogButtons buttons) {
return Show(text, instruction, caption, buttons, 0);
}
public static TaskDialogResult Show(string text, string instruction, string caption, TaskDialogButtons buttons, TaskDialogIcon icon) {
return ShowInternal(IntPtr.Zero, text, instruction, caption, buttons, icon);
}
#endregion
#region Core Implementation
private static TaskDialogResult ShowInternal(IntPtr owner, string text, string instruction, string caption, TaskDialogButtons buttons, TaskDialogIcon icon) {
int p;
using (new EnableThemingInScope(true)) {
int resss = TaskDialog(owner, IntPtr.Zero, caption, instruction, text, (int) buttons, new IntPtr((int) icon), out p);
if (resss != 0)
throw new InvalidOperationException("Something weird has happened." + resss.ToString());
}
switch (p) {
case 1:
return TaskDialogResult.OK;
case 2:
return TaskDialogResult.Cancel;
case 4:
return TaskDialogResult.Retry;
case 6:
return TaskDialogResult.Yes;
case 7:
return TaskDialogResult.No;
case 8:
return TaskDialogResult.Close;
default:
return TaskDialogResult.None;
}
}
#endregion
}
3.調用它,只需:
try {
StatusDialog.Show("About to test this...", "Heading I won't use.", "Dialog Title", TaskDialogButtons.OK);
} catch (Exception e) {
MessageBox.Show(e.ToString(), "Error Found", MessageBoxButtons.OK);
}
4.結果:
![enter image description here](https://i.stack.imgur.com/sFlJY.png)
你爲什麼這樣做?爲什麼不只有一個以'System.Windows.Application'開頭的常規WPF應用程序呢?我猜想winforms(或者之前加載的任何東西)正在加載一堆古老的廢話,WPF並不在意。 –
好吧,這個系統是我之前有一個bug的一個片段。我只是改變了系統。它現在通過一個真正的WPF項目開始。 它仍然沒有工作。如果我激活「系統自己的調試」,我沒有得到任何執行,並打開對話框。唯一的錯誤是:圖標丟失: -/ 這可能是MS Api Codepack中的一個問題? – SharpShade