MenuTest.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. using System;
  2. using System.Drawing;
  3. using System.Windows.Forms;
  4. using System.Collections;
  5. using System.Runtime.InteropServices;
  6. class MyMenuItem : MenuItem
  7. {
  8. public MyMenuItem( string s) : base(s)
  9. {
  10. }
  11. public int GetID()
  12. {
  13. return MenuID;
  14. }
  15. public new IntPtr Handle
  16. {
  17. get
  18. {
  19. return IntPtr.Zero;
  20. }
  21. }
  22. }
  23. // Test basic functionality of the Application and Form class
  24. class MenuTest : Form
  25. {
  26. Button button;
  27. MainMenu testMenu_ = null;
  28. public MenuTest () : base ()
  29. {
  30. ClientSize = new Size(300, 250);
  31. CreateMyMainMenu();
  32. button = new Button ();
  33. button.Top = 20;
  34. button.Left = 20;
  35. button.Width = 50;
  36. button.Height = 50;
  37. //button.Parent = this;
  38. button.Text = "Click Me!";
  39. button.Click += new EventHandler(OnMenuButtonClick);
  40. this.Controls.AddRange(new System.Windows.Forms.Control[] {button});
  41. }
  42. MenuItem[] RecentFilesMenu()
  43. {
  44. MenuItem menuItem1 = new MenuItem("MenuTest.cs");
  45. MenuItem menuItem2 = new MenuItem("/etc/passwd");
  46. MenuItem menuItem3 = new MenuItem("~/.wine/config");
  47. return new MenuItem[3] {menuItem1, menuItem2, menuItem3};
  48. }
  49. // Doesn't gets called, waiting for Button implementation
  50. void OnMenuButtonClick( object c, EventArgs e)
  51. {
  52. if( Menu != null)
  53. {
  54. Menu = null;
  55. }
  56. else
  57. {
  58. Menu = testMenu_;
  59. }
  60. }
  61. MenuItem menuItem4 = null;
  62. public void CreateMyMainMenu()
  63. {
  64. testMenu_ = new MainMenu();
  65. MyMenuItem myMI = new MyMenuItem("2");
  66. MenuItem refMi = myMI;
  67. IntPtr ip = refMi.Handle;
  68. ip = myMI.Handle;
  69. System.Console.WriteLine("My menu ID {0}", myMI.GetID());
  70. MenuItem menuItem1 = new MenuItem("&New", new System.EventHandler(this.OnFileNew));
  71. MenuItem menuItem2 = new MenuItem("&Open...", new System.EventHandler(this.OnFileOpen));
  72. MenuItem menuItem3 = new MenuItem("&Quit", new System.EventHandler(this.OnFileQuit));
  73. MenuItem menuItem4 = new MenuItem("Test &Controls", new System.EventHandler(this.OnTestControlMethods));
  74. MenuItem recentFiles = new MenuItem("Recent files", RecentFilesMenu());
  75. MenuItem FileMenu = new MenuItem("File", new MenuItem[]{menuItem1, menuItem2,recentFiles, menuItem3, menuItem4});
  76. myMI = new MyMenuItem("2");
  77. System.Console.WriteLine("My menu ID {0}", myMI.GetID());
  78. menuItem1.Text = "&File";
  79. menuItem2.Text = "&Edit";
  80. menuItem3.Text = "E&xit";
  81. MenuItem mi10 = new MenuItem("Dos");
  82. MenuItem mi11 = new MenuItem("Unix");
  83. menuItem4 = new MenuItem("&Save As...", new MenuItem[]{mi10, mi11});
  84. FileMenu.MenuItems.Add(2, menuItem4);
  85. int pos = testMenu_.MenuItems.Add(FileMenu);
  86. System.Console.WriteLine("Menu File added at position {0}", pos);
  87. MenuItem menuTest1 = new MenuItem("&Test properties", new System.EventHandler(this.OnTestProperties));
  88. MenuItem TestMenu = new MenuItem("Test", new MenuItem[]{menuTest1});
  89. testMenu_.MenuItems.Add(TestMenu);
  90. Menu = testMenu_;
  91. myMI = new MyMenuItem("2");
  92. System.Console.WriteLine("My menu ID {0}", myMI.GetID());
  93. }
  94. protected void OnFileNew( object sender, System.EventArgs e)
  95. {
  96. MessageBox.Show(this, "The File->New command selected", "MenuTest");
  97. menuItem4.Click += new System.EventHandler( this.OnFileSaveAs);
  98. menuItem4.MenuItems.Clear();
  99. }
  100. protected void OnFileOpen( object sender, System.EventArgs e)
  101. {
  102. MessageBox.Show(this, "A file-open dialog will appear soon", "MenuTest");
  103. }
  104. protected void OnFileQuit( object sender, System.EventArgs e)
  105. {
  106. System.Console.WriteLine("The Exit command selected");
  107. Application.Exit();
  108. }
  109. protected void OnFileSaveAs( object sender, System.EventArgs e)
  110. {
  111. MessageBox.Show("OnFileSaveAs");
  112. menuItem4.Index = 0;
  113. }
  114. protected void OnTestProperties( object sender, System.EventArgs e)
  115. {
  116. MenuItem send = sender as MenuItem;
  117. if( send != null)
  118. {
  119. Menu parent = send.Parent;
  120. if( parent != null){
  121. MenuItem mi1 = new MenuItem("BarBreak");
  122. mi1.BarBreak = true;
  123. MenuItem mi2 = new MenuItem("Break");
  124. mi2.Break = true;
  125. MenuItem mi3 = new MenuItem("Checked");
  126. mi3.Checked = true;
  127. MenuItem mi4 = new MenuItem("Disabled");
  128. mi4.Enabled = false;
  129. MenuItem mi5 = new MenuItem("DefaultItem");
  130. mi5.DefaultItem = true;
  131. MenuItem mi6 = new MenuItem("RadioCheck");
  132. mi6.RadioCheck = true;
  133. mi6.Checked = true;
  134. MenuItem mi7 = new MenuItem("-");
  135. mi7.RadioCheck = true;
  136. MenuItem SubMenu = new MenuItem("SubMenu", new MenuItem[]{mi1, mi2, mi3, mi4, mi5, mi6, mi7});
  137. parent.MenuItems.Add(SubMenu);
  138. }
  139. }
  140. }
  141. [DllImport ("user32.dll",
  142. CallingConvention = CallingConvention.StdCall,
  143. CharSet = CharSet.Ansi, EntryPoint = "CreateWindowExA")]
  144. internal static extern IntPtr CreateWindowExEx (
  145. uint dwExStyle, string lpClassName,
  146. string lpWindowName, uint dwStyle,
  147. int x, int y, int nWidth, int nHeight,
  148. IntPtr hWndParent, IntPtr hMenu, IntPtr hInstance,
  149. ref object lpParam);
  150. protected void OnTestControlMethods( object sender, System.EventArgs e)
  151. {
  152. int WS_CHILD = 0x40000000;
  153. int WS_VISIBLE = 0x10000000;
  154. object abc = null;
  155. IntPtr hwnd = CreateWindowExEx(0, "BUTTON", "WindowName",
  156. (uint)( WS_CHILD | WS_VISIBLE), 10, 10,
  157. 100, 50, Handle, (IntPtr)100, (IntPtr)0, ref abc);
  158. Control cc = Control.FromChildHandle(hwnd);
  159. if( cc == this) {
  160. Console.WriteLine("FromChildHandle: The frame was found by BUTTON");
  161. }
  162. else if( cc == null) {
  163. Console.WriteLine("FromChildHandle: Nothing was found by BUTTON");
  164. }
  165. else {
  166. Console.WriteLine("FromChildHandle: Some control was found by BUTTON");
  167. }
  168. Control c1 = Control.FromHandle(hwnd);
  169. if( c1 == this) {
  170. Console.WriteLine("FromHandle: The frame was found by BUTTON");
  171. }
  172. else if( c1 == null) {
  173. Console.WriteLine("FromHandle: Nothing was found by BUTTON");
  174. }
  175. else {
  176. Console.WriteLine("FromHandle: Some control was found by BUTTON");
  177. }
  178. Control cntr = Control.FromChildHandle(button.Handle);
  179. if( cntr == button) {
  180. Console.WriteLine("FromChildHandle are the same");
  181. }
  182. else {
  183. Console.WriteLine("FromChildHandle are NOT the same");
  184. }
  185. cntr = Control.FromHandle(button.Handle);
  186. if( cntr == button) {
  187. Console.WriteLine("FromHandle are the same");
  188. }
  189. else {
  190. Console.WriteLine("FromHandle are NOT the same");
  191. }
  192. }
  193. // - verifies the WndProc can be overridden propery
  194. // - verifies the Application.MessageLoop is working properly
  195. protected override void WndProc (ref Message m)
  196. {
  197. base.WndProc (ref m);
  198. // should be true after the Run command is reached
  199. //Console.WriteLine ("Application.MessageLoop: " +
  200. //Application.MessageLoop);
  201. }
  202. static public void Test1 ()
  203. {
  204. MenuTest form = new MenuTest ();
  205. //should be false
  206. Console.WriteLine ("Application.MessageLoop: " +
  207. Application.MessageLoop);
  208. Application.Run (form);
  209. }
  210. static public void Test2()
  211. {
  212. MenuItem mi = new MyMenuItem("123");
  213. MenuItem mp = new MenuItem("PPP", new MenuItem[] { mi });
  214. MenuItem mc = mi.CloneMenu();
  215. System.Console.WriteLine("Clone equals to original {0}", mc.Equals(mi));
  216. System.Console.WriteLine("Original Parent {0}", mi.Parent.ToString());
  217. System.Console.WriteLine("Clone Parent {0}", mc.Parent != null ? mc.Parent.ToString() : "<null>");
  218. System.Console.WriteLine("Clone Parent is the same {0}", mc.Parent == mi.Parent);
  219. }
  220. static public void Test3()
  221. {
  222. MenuItem mi1 = new MenuItem("123");
  223. MenuItem mi2 = new MenuItem("234");
  224. MenuItem parent = new MenuItem( "parent", new MenuItem[] { mi1, mi2});
  225. IList il = (IList)parent.MenuItems;
  226. System.Console.WriteLine("List of menu items IsReadOnly {0}, IsFixedSize {1}", il.IsReadOnly, il.IsFixedSize);
  227. il.Add(new MenuItem("This must be inside"));
  228. //il.Add( new ArrayList());
  229. //il[1] = new MenuItem("345");
  230. //parent.MenuItems[1] = new MenuItem("asd");
  231. }
  232. static public int Main (String[] args)
  233. {
  234. Test3();
  235. Test2();
  236. Test1();
  237. return 0;
  238. }
  239. }