Introduction
You may find yourself in a situation that asks for a simple application, but there is no suitable software on the system and you aren't allowed to install it. Luckily, a .NET framework with its compiler is always there for you. All you need is Notepad, compiler and little know-how from this article.
Background
An application compiled from command line is by default a command line app. To show a Form, you need to write a class and create an instance of if. By default, the command line window remains displayed. Although it may be useful for debugging purposes, for ordinary use, you'll likely want to hide it.
What's demonstrated in the template:
- Definition of the
Form
's class and its call
- Enabling or disabling of single instance feature
- Hiding the command line window. By default, it remains show
- Adding controls to the form and assigning their event procedures
- A feature to help you with controls. You need to define the controls position and size numerically. This template helps you get the coordinates by clicking on the form of your running app.
- Closing your application from the
Form
Using the Code
Copy it to Notepad and save it as plain text (.txt extension).
Imports System.Drawing
Imports System.Windows.Forms
Namespace MyNamespace
Public Class MyFormClass
Inherits Form
private singleInstance as boolean = false
private hideCommandlineWindow as boolean = false
Private objMutex As System.Threading.Mutex
Private Declare Auto Function GetConsoleWindow Lib "kernel32.dll" () As IntPtr
Private Declare Auto Function ShowWindow Lib "user32.dll" _
(ByVal hWnd As IntPtr, ByVal nCmdShow As Integer) As Boolean
Private Const SW_HIDE as Integer = 0
Private Const SW_SHOW as Integer = 5
private hWndConsole As Integer
Private XPreviousClick, YPreviousClick as integer
Private tmr1 As Timer
Private btn1, btn2 As Button
Private lbl1 as Label
Private txb1 as textbox
Public Sub New()
if singleinstance then objMutex = New System.Threading.Mutex(False, "Name of your program")
If objMutex.WaitOne(0, False) = False Then
objMutex.Close()
objMutex = Nothing
MessageBox.Show("Another instance is already running!")
End
End If
End if
hWndConsole = GetConsoleWindow() if hideCommandlineWindow then
If hWndConsole <> IntPtr.Zero Then ShowWindow(hWndConsole, SW_HIDE)
End If
DisplayTheForm()
End Sub
Private Sub DisplayTheForm()
Me.Name = "Windows Form" Me.Text = "Here's your windows header text."
Me.Size = New Size(706, 250)
Me.StartPosition = FormStartPosition.CenterScreen
tmr1 = new timer()
AddHandler tmr1.Tick, AddressOf Tmr1Tick
btn1 = New Button()
btn1.Name = "button"
btn1.Text = "Hide"
btn1.Location = New Point(10, 5)
btn1.Size = New Size(80, 35)
Me.Controls.Add(btn1)
AddHandler btn1.Click, AddressOf btn1Click
btn2 = New Button()
btn2.Name = "button"
btn2.Text = "Start timer1"
btn2.Location = New Point(300, 5)
btn2.Size = New Size(80, 35)
me.controls.add(btn2)
AddHandler btn2.Click, AddressOf btn2Click
lbl1 = new Label()
lbl1.text = "Left-click on left top corner of intended control to see its coordinates" & vbcrlf & "Right-click on right bottom corner of intended control to see it's size."
lbl1.Location = New Point(11, 45)
lbl1.Size = New Size(370, 37)
me.controls.add(lbl1 )
txb1 = New textbox()
txb1.text = " "
txb1.Location = New Point(95, 6)
txb1.Size = New Size(200, 25)
me.controls.add(txb1)
Addhandler me.mousedown ,Addressof MeMousedown
End Sub
Private Sub FormcloseClick(source As Object, e As EventArgs)
application.exit()
end
End Sub
Private Sub Tmr1Tick(source As Object, e As EventArgs)
tmr1.stop()
MessageBox.Show("Timer 1 has elapsed !")
End Sub
Private Sub btn1Click(source As Object, e As EventArgs)
if btn1.text = "Hide" then
ShowWindow(hWndConsole, SW_HIDE)
btn1.text = "Show"
else
ShowWindow(hWndConsole, SW_SHOW)
btn1.text = "Hide"
end if
End Sub
Private Sub btn2Click(source As Object, e As EventArgs)
tmr1.interval = 5000 tmr1.start()
End Sub
Private Sub MeMousedown(source As Object, e As MouseEventArgs)
If (e.Button = MouseButtons.Left) then
lbl1 .text = "New Point(" & convert.tostring(e.x) & "," & convert.tostring(e.y) & ")"
XPreviousClick = e.x
YPreviousClick = e.y
end if
If (e.Button = MouseButtons.Right) then
lbl1 .text = "New Size(" & convert.tostring(e.x - XPreviousClick ) & "," & convert.tostring(e.y - YPreviousClick ) & ")"
end if
txb1.text = lbl1.text
End Sub
Public Shared Sub Main(args As [String]())
Application.Run(New MyFormClass())
End Sub
End Class
End Namespace
Once the code is saved, you need to compile it. The hard way from command-line. To save the hassle, copy the code below to Notepad and save as text with .bat extension, e.g., compile.bat. The compilation becomes a matter of dragging and dropping your .txt file on the compile.bat file.
@rem remove the temp file should it remain in place from previous attempt
@del %1_temp.vb
@rem the compiler requires .vb extension, so we'll have to copy our source to a temporary file
copy %1 %1_temp.vb /y
@rem prepare string without the .txt extension
set str=%1
set str=%str:.txt=%
@rem the actual build. check/update the path to your compiler according it's location on your system
@rem if you imported more namespaces in you source filem don't forget to add corresponding libraries
@rem
C:\Windows\Microsoft.NET\Framework\v2.0.50727\vbc.exe /platform:AnyCPU /r:Microsoft.VisualBasic.dll, system.management.dll,system.data.dll,mscorlib.dll,system.windows.forms.dll /target:exe %1_temp.vb /out:%str%.exe
@rem compiler will print out syntax errors it may have found in your code
@rem a pause is added to keep compiler messages on screen, useful for debugging
pause
@rem clean up the temporary file
del %1_temp.vb
Here you are, there's nothing to stop you from coding.
History
- 13th August, 2016: Initial version