Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 30th, 2012  |  syntax: None  |  size: 1.30 KB  |  hits: 19  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Behavior in WinForm/Console Hybrid Application
  2. [System.Runtime.InteropServices.DllImport("kernel32.dll")]
  3. private static extern bool AllocConsole();
  4.  
  5. [System.Runtime.InteropServices.DllImport("kernel32.dll")]
  6. private static extern bool AttachConsole(int pid);
  7.  
  8. ...
  9.  
  10. if (!AttachConsole(-1))
  11. {
  12.    AllocConsole();
  13. }
  14.        
  15. // nowin is a bool that is set based on a parameter
  16. if (nowin)
  17. {
  18.     if (!AttachConsole(-1))
  19.     {
  20.         AllocConsole();
  21.     }
  22.     //... Console mode code here...
  23. }
  24. else
  25. {
  26.     // run in window
  27.     Application.EnableVisualStyles();
  28.     Application.SetCompatibleTextRenderingDefault(false);
  29.     Application.Run(new Form1(argDict));
  30. }
  31.        
  32. [STAThread]
  33. public static int Main(string[] args)
  34. {
  35.    if (args.Length > 0)
  36.    {
  37.       // run console code here
  38.    }
  39.    else
  40.    {
  41.       // start up the win form app
  42.       Application.EnableVisualStyles();
  43.       Application.SetCompatibleTextRenderingDefault(false);
  44.       Application.Run(new MainForm());
  45.    }
  46.  
  47.    return 0;
  48. }
  49.        
  50. static void Main(params string[] args)
  51.     {
  52.         if (args.Length > 0 && args[0] == "consolemode")
  53.         {
  54.             // do stuff
  55.         }
  56.         else
  57.         {
  58.             Application.EnableVisualStyles();
  59.             Application.SetCompatibleTextRenderingDefault(false);
  60.             Application.Run(new MainForm());
  61.         }
  62.     }