Created
June 20, 2018 06:16
-
-
Save gsw945/b1b948325891f7b8d919f54a57b70fd9 to your computer and use it in GitHub Desktop.
single cs file for winfrom by csc.exe from cli
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; // Console | |
using System.Runtime.InteropServices; // DllImport、StructLayout | |
using System.Windows.Forms; // Screen、Form | |
using System.Drawing; // Rectangle | |
using System.ComponentModel; | |
using System.Diagnostics; // Process | |
/** | |
* https://docs.microsoft.com/en-us/dotnet/framework/app-domains/how-to-build-a-single-file-assembly | |
* https://docs.microsoft.com/zh-cn/dotnet/csharp/language-reference/compiler-options/command-line-building-with-csc-exe | |
* https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/inside-a-program/hello-world-your-first-program | |
* https://docs.microsoft.com/en-us/dotnet/framework/winforms/how-to-create-a-windows-forms-application-from-the-command-line | |
* https://stackoverflow.com/a/24406318/6528523 | |
* https://stackoverflow.com/questions/5282588/how-can-i-bring-my-application-window-to-the-front | |
*/ | |
namespace TopWin | |
{ | |
public class Form1 : Form | |
{ | |
[DllImport("user32.dll")] | |
private static extern bool GetWindowRect(HandleRef hWnd, [In, Out] ref RECT rect); | |
[DllImport("user32.dll")] | |
private static extern IntPtr GetForegroundWindow(); | |
[StructLayout(LayoutKind.Sequential)] | |
private struct RECT | |
{ | |
public int left; | |
public int top; | |
public int right; | |
public int bottom; | |
} | |
public static bool IsAnyWindowFullScreen() | |
{ | |
RECT rect = new RECT(); | |
GetWindowRect(new HandleRef(null, GetForegroundWindow()), ref rect); | |
return new Rectangle(rect.left, | |
rect.top, | |
rect.right - rect.left, | |
rect.bottom - rect.top | |
).Contains(Screen.PrimaryScreen.Bounds); | |
} | |
[DllImport("User32.dll", SetLastError = true)] | |
private static extern void SwitchToThisWindow(IntPtr hWnd, bool fAltTab); | |
[DllImport("User32.dll")] | |
public static extern Int32 SetForegroundWindow(int hWnd); | |
////////////////////////////////////////// | |
public Button button1; | |
private System.Timers.Timer timer; | |
private void button1_Click(object sender, EventArgs e) | |
{ | |
this.WindowState = FormWindowState.Minimized; | |
this.BringToFront(); | |
this.Show(); | |
this.WindowState = FormWindowState.Normal; | |
MessageBox.Show("Hello World"); | |
} | |
private void OnTimedEvent(Object source, System.Timers.ElapsedEventArgs e) | |
{ | |
Process currentProcess = Process.GetCurrentProcess(); | |
// SwitchToThisWindow(currentProcess.MainWindowHandle, true); | |
SetForegroundWindow(currentProcess.MainWindowHandle.ToInt32()); | |
} | |
public Form1() | |
{ | |
button1 = new Button(); | |
button1.Size = new Size(40, 40); | |
button1.Location = new Point(30, 30); | |
button1.Text = "Click me"; | |
this.Controls.Add(button1); | |
button1.Click += new EventHandler(button1_Click); | |
// Create a timer with a 1 millisecond interval. | |
timer = new System.Timers.Timer(1000 * 1); // 1 second | |
timer.Elapsed += this.OnTimedEvent; | |
// Hook up the Elapsed event for the timer. | |
timer.AutoReset = true; | |
Console.WriteLine("The timer should fire every {0} * 1000 milliseconds.", timer.Interval); | |
timer.Enabled = true; | |
} | |
[STAThread] | |
static void Main(string[] args) | |
{ | |
string csc_exe = @"C:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe"; | |
string cs_file = @"C:\Users\312\Desktop\top-win.cs"; | |
Console.WriteLine(csc_exe + " " + cs_file); | |
// Keep the console window open in debug mode. | |
Console.WriteLine("Press any key to show window."); | |
Console.ReadKey(); | |
Application.EnableVisualStyles(); | |
Application.SetCompatibleTextRenderingDefault(false); | |
Form1 form = new Form1(); | |
form.StartPosition = FormStartPosition.CenterScreen; | |
if(!IsAnyWindowFullScreen()) | |
{ | |
Console.WriteLine("IsAnyWindowFullScreen() == false"); | |
// form.Hide(); | |
form.TopMost = true; | |
} | |
// form.Show(); | |
Application.Run(form); | |
// https://pypi.org/project/websockets/ | |
// https://github.com/aaugustin/websockets | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
build with command line(maybe need to change v4.0.30319 to another version number):