Hilfe bei MessageBox Klasse

  • C#
  • .NET (FX) 1.0–2.0

Es gibt 3 Antworten in diesem Thema. Der letzte Beitrag () ist von RodFromGermany.

    Hilfe bei MessageBox Klasse

    Hi,

    Also Leute mein Ziel ist es diese Klasse von "Dino Esposito" einem msdn mitglied so zu modifizieren das das icon der msgbox nicht aus der shell32.dll per index geladen wird sondern
    von einer datei abc.ico
    Kann mir jemand helfen das zu bewerkstelligen?
    Ich habe keine Idee wie ich das am besten mache da ein IntPtr für das icon erwartet wird und ich ja das ganze via image machen möchte.

    Die klasse:

    C#-Quellcode

    1. using System;
    2. using System.Text;
    3. using System.Drawing;
    4. using System.Windows.Forms;
    5. using System.Runtime.InteropServices;
    6. namespace MsdnMag
    7. {
    8. // *****************************************************************
    9. // Buttons combination
    10. public enum MyMessageBoxButtons
    11. {
    12. OK = 0,
    13. OKCancel = 1,
    14. AbortRetryIgnore = 2,
    15. YesNoCancel = 3,
    16. YesNo = 4,
    17. RetryCancel = 5,
    18. YesNoAllCancel = 6 // NEW
    19. }
    20. // *****************************************************************
    21. public class MessageBox
    22. {
    23. // *****************************************************************
    24. // Properties
    25. protected LocalCbtHook m_cbt;
    26. protected IntPtr m_handle = IntPtr.Zero;
    27. protected bool m_alreadySetup = false;
    28. // *****************************************************************
    29. // *****************************************************************
    30. public MessageBox()
    31. {
    32. m_cbt = new LocalCbtHook();
    33. m_cbt.WindowCreated += new LocalCbtHook.CbtEventHandler(WndCreated);
    34. m_cbt.WindowDestroyed += new LocalCbtHook.CbtEventHandler(WndDestroyed);
    35. m_cbt.WindowActivated += new LocalCbtHook.CbtEventHandler(WndActivated);
    36. }
    37. // *****************************************************************
    38. // *****************************************************************
    39. // PROPERTY IconFile
    40. private string m_iconFile = "";
    41. public string IconFile
    42. {
    43. get {return m_iconFile;}
    44. set {m_iconFile=value; m_iconIndex=0;}
    45. }
    46. // *****************************************************************
    47. // *****************************************************************
    48. // PROPERTY YesAllNoCancel
    49. public bool YesAllNoCancel = false;
    50. private const int IDALL = 9; // same as IDHELP in the Win32 API
    51. private const int MB_HELP = 0x00004000;
    52. // *****************************************************************
    53. // *****************************************************************
    54. // PROPERTY IconIndex
    55. private int m_iconIndex = 0;
    56. public int IconIndex
    57. {
    58. get {return m_iconIndex;}
    59. set {m_iconIndex=value;}
    60. }
    61. // *****************************************************************
    62. // *****************************************************************
    63. // Show
    64. public DialogResult Show(string text, string title, MessageBoxButtons buttons, MessageBoxIcon icon)
    65. {
    66. // If a custom icon is requested, ensure the window leaves room for
    67. // the icon... Add a fake icon
    68. if (IconFile.Length > 0 && icon == MessageBoxIcon.None)
    69. icon = MessageBoxIcon.Information;
    70. else
    71. IconFile = "";
    72. // Apply the changes needed if 4 buttons are required
    73. if (YesAllNoCancel)
    74. {
    75. m_cbt.Install();
    76. buttons = MessageBoxButtons.YesNoCancel;
    77. // Must use MessageBox API because of the 4th button
    78. int res = _MessageBox(GetActiveWindow(), text, title,
    79. (int) (buttons + (int) icon + MB_HELP));
    80. m_cbt.Uninstall();
    81. return (DialogResult) res;
    82. }
    83. // Go the default way
    84. m_cbt.Install();
    85. DialogResult dr = System.Windows.Forms.MessageBox.Show(text, title, buttons, icon);
    86. m_cbt.Uninstall();
    87. return dr;
    88. }
    89. public DialogResult Show(string text, string title, MessageBoxButtons buttons)
    90. {
    91. return Show(text, title, buttons, MessageBoxIcon.None);
    92. }
    93. public DialogResult Show(string text, string title)
    94. {
    95. return Show(text, title, MessageBoxButtons.OK, MessageBoxIcon.None);
    96. }
    97. public DialogResult Show(string text)
    98. {
    99. return Show(text, "", MessageBoxButtons.OK, MessageBoxIcon.None);
    100. }
    101. // *****************************************************************
    102. // *****************************************************************
    103. // WndCreated event handler
    104. private void WndCreated(object sender, CbtEventArgs e)
    105. {
    106. if (e.IsDialogWindow)
    107. {
    108. m_alreadySetup = false;
    109. Console.WriteLine("MessageBox created");
    110. m_handle = e.Handle;
    111. }
    112. }
    113. // *****************************************************************
    114. // *****************************************************************
    115. // WndDestroyed event handler
    116. private void WndDestroyed(object sender, CbtEventArgs e)
    117. {
    118. if (e.Handle == m_handle)
    119. {
    120. m_alreadySetup = false;
    121. m_handle = IntPtr.Zero;
    122. }
    123. }
    124. // *****************************************************************
    125. // *****************************************************************
    126. // WndActivated event handler
    127. private void WndActivated(object sender, CbtEventArgs e)
    128. {
    129. if (m_handle != e.Handle)
    130. return;
    131. // Not the first time
    132. if (m_alreadySetup)
    133. {
    134. Console.WriteLine("(Already configured. Not the first time it is activated!)");
    135. return;
    136. }
    137. else
    138. m_alreadySetup = true;
    139. //
    140. // Modify the MessageBox window
    141. //
    142. Console.WriteLine("MessageBox activated");
    143. // Replace the icon (0x0014 is the ID of the icon window)
    144. if (m_iconFile.Length >0)
    145. {
    146. IntPtr hwndIcon = GetDlgItem(m_handle, 0x0014);
    147. IntPtr hIcon = ExtractIcon(IntPtr.Zero, m_iconFile, m_iconIndex);
    148. SendMessage(hwndIcon, STM_SETICON, hIcon, IntPtr.Zero);
    149. }
    150. // Get the static window with the text and the text
    151. IntPtr hwndText = GetDlgItem(m_handle, 0xFFFF);
    152. StringBuilder sb = new StringBuilder();
    153. sb.Capacity = 256;
    154. GetWindowText(hwndText, sb, 256);
    155. string text = sb.ToString();
    156. // Get the window position
    157. RECT rc = new RECT();
    158. GetWindowRect(hwndText, rc);
    159. POINT pt = new POINT();
    160. pt.x = rc.left;
    161. pt.y = rc.top;
    162. ScreenToClient(m_handle, pt);
    163. // Create the alternate EDIT window
    164. IntPtr m_edit = CreateWindowEx(0, "edit", "", ES_READONLY|ES_MULTILINE|WS_CHILD|WS_VISIBLE,
    165. pt.x, pt.y, rc.right-rc.left, rc.bottom-rc.top,
    166. m_handle, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero);
    167. // Set font and text
    168. IntPtr hFont = SendMessage(hwndText, WM_GETFONT, IntPtr.Zero, IntPtr.Zero);
    169. SendMessage(m_edit, WM_SETFONT, hFont, new IntPtr(1));
    170. SetWindowText(m_edit, text);
    171. // Copy the displayed text to the clipboard
    172. Clipboard.SetDataObject(text);
    173. // Finalizing...
    174. DestroyWindow(hwndText);
    175. // Yes-All-No-Cancel setup
    176. // Map the YES-NO-CANCEL-HELP sequence to YES-OK-NO-CANCEL
    177. // The mapping has two steps: button text and button ID
    178. // No need to change the Yes button
    179. // Map button text
    180. SetWindowText(GetDlgItem(m_handle,(int) DialogResult.No), "OK to All");
    181. SetWindowText(GetDlgItem(m_handle,(int) DialogResult.Cancel), DialogResult.No.ToString());
    182. SetWindowText(GetDlgItem(m_handle,IDALL), DialogResult.Cancel.ToString());
    183. // Map button IDs
    184. SetWindowLong(GetDlgItem(m_handle,(int) DialogResult.No), GWL_ID, (int)DialogResult.OK);
    185. SetWindowLong(GetDlgItem(m_handle,(int) DialogResult.Cancel), GWL_ID, (int) DialogResult.No);
    186. SetWindowLong(GetDlgItem(m_handle,IDALL), GWL_ID, (int) DialogResult.Cancel);
    187. }
    188. // *****************************************************************
    189. #region Win32 Imports
    190. private const int ES_READONLY = 0x00000800;
    191. private const int ES_MULTILINE = 0x00000004;
    192. private const int WS_VISIBLE = 0x10000000;
    193. private const int WS_CHILD = 0x40000000;
    194. private const int WM_SETFONT = 0x00000030;
    195. private const int WM_GETFONT = 0x00000031;
    196. private const int STM_SETICON = 0x00000170;
    197. // ************************************************************************
    198. // Win32: GetClassName
    199. [DllImport("user32.dll")]
    200. protected static extern int GetClassName(IntPtr hwnd,
    201. StringBuilder lpClassName, int nMaxCount);
    202. // ************************************************************************
    203. // ************************************************************************
    204. // Win32: GetDlgItem
    205. [DllImport("user32.dll")]
    206. protected static extern IntPtr GetDlgItem(IntPtr hwnd, int id);
    207. // ************************************************************************
    208. // ************************************************************************
    209. // Win32: GetWindowText
    210. [DllImport("user32.dll")]
    211. protected static extern int GetWindowText(IntPtr hwnd,
    212. StringBuilder lpString, int nMaxCount);
    213. // ************************************************************************
    214. // ************************************************************************
    215. // Win32: SetWindowText
    216. [DllImport("user32.dll")]
    217. protected static extern bool SetWindowText(IntPtr hwnd, string text);
    218. // ************************************************************************
    219. // ************************************************************************
    220. // Win32: GetWindowRect
    221. [DllImport("user32.dll")]
    222. protected static extern int GetWindowRect(IntPtr hwnd, RECT rc);
    223. // ************************************************************************
    224. // ************************************************************************
    225. // Win32: ScreenToClient
    226. [DllImport("user32.dll")]
    227. protected static extern int ScreenToClient(IntPtr hwnd, POINT pt);
    228. // ************************************************************************
    229. // ************************************************************************
    230. // Win32: SetWindowLong
    231. protected const int GWL_ID = -12;
    232. [DllImport("user32.dll")]
    233. protected static extern int SetWindowLong(IntPtr hwnd,
    234. int index, int newValue);
    235. // ************************************************************************
    236. // ************************************************************************
    237. // Win32: ExtractIcon
    238. [DllImport("shell32.dll")]
    239. protected static extern IntPtr ExtractIcon(
    240. IntPtr hInst, // instance handle
    241. string lpszExeFileName, // file name
    242. int nIconIndex // icon index
    243. );
    244. // ************************************************************************
    245. // ************************************************************************
    246. // Win32: DestroyWindow
    247. [DllImport("user32.dll")]
    248. protected static extern void DestroyWindow(IntPtr hwnd);
    249. // ************************************************************************
    250. // ************************************************************************
    251. // Win32: MessageBox
    252. [DllImport("user32.dll", EntryPoint="MessageBox")]
    253. protected static extern int _MessageBox(IntPtr hwnd, string text, string caption,
    254. int options);
    255. // ************************************************************************
    256. // ************************************************************************
    257. // Win32: SendMessage
    258. [DllImport("user32.dll")]
    259. protected static extern IntPtr SendMessage(IntPtr hwnd,
    260. int msg, IntPtr wParam, IntPtr lParam);
    261. // ************************************************************************
    262. // ************************************************************************
    263. // Win32: GetActiveWindow
    264. [DllImport("user32.dll")]
    265. protected static extern IntPtr GetActiveWindow();
    266. // ************************************************************************
    267. // ************************************************************************
    268. // Win32: CreateWindowEx
    269. [DllImport("user32.dll")]
    270. protected static extern IntPtr CreateWindowEx(
    271. int dwExStyle, // extended window style
    272. string lpClassName, // registered class name
    273. string lpWindowName, // window name
    274. int dwStyle, // window style
    275. int x, // horizontal position of window
    276. int y, // vertical position of window
    277. int nWidth, // window width
    278. int nHeight, // window height
    279. IntPtr hWndParent, // handle to parent or owner window
    280. IntPtr hMenu, // menu handle or child identifier
    281. IntPtr hInstance, // handle to application instance
    282. IntPtr lpParam // window-creation data
    283. );
    284. // ************************************************************************
    285. // ************************************************************************
    286. // POINT
    287. [StructLayout(LayoutKind.Sequential)]
    288. public class POINT
    289. {
    290. public int x;
    291. public int y;
    292. }
    293. // ************************************************************************
    294. // ************************************************************************
    295. // RECT
    296. [StructLayout(LayoutKind.Sequential)]
    297. public class RECT
    298. {
    299. public int left;
    300. public int top;
    301. public int right;
    302. public int bottom;
    303. }
    304. // ************************************************************************
    305. #endregion
    306. }
    307. }


    Das Beispielprojekt kann man auch hier herunterladen: download.microsoft.com/downloa…1f1fe/CuttingEdge0211.exe
    Bzw MSDN Seite: msdn.microsoft.com/en-us/magazine/cc188920.aspx

    Bin für jede Hilfe und jeden Tipp dankbar
    C# Developer
    Learning C++

    Rikudo schrieb:

    sondern von einer datei abc.ico
    1. Mach bitte um Deinen Quellcode einen Extender.
    2. Was hat dieser ellenlange Code mit dem Problem der Herkunft der Icons zu tun?
    3. Mal Dir die Icons selber und pack sie in die Ressource Deines Programms oder
    4. Mach Dir eine eigene Ressource-DLL.
    Falls Du diesen Code kopierst, achte auf die C&P-Bremse.
    Jede einzelne Zeile Deines Programms, die Du nicht explizit getestet hast, ist falsch :!:
    Ein guter .NET-Snippetkonverter (der ist verfügbar).
    Programmierfragen über PN / Konversation werden ignoriert!

    Rikudo schrieb:

    Hier wird das icon aus der shell32.dll geladen?
    Weiß ich nicht.
    Mach bitte in Deinem 1. Post einen Spoiler um den Code.
    Mach Dir ein Programm, lade die Icons in eine Bitmap, speichere sie ab uind pack die in die internen Ressourcen Deines Programms.
    Falls Du diesen Code kopierst, achte auf die C&P-Bremse.
    Jede einzelne Zeile Deines Programms, die Du nicht explizit getestet hast, ist falsch :!:
    Ein guter .NET-Snippetkonverter (der ist verfügbar).
    Programmierfragen über PN / Konversation werden ignoriert!