Archive for Desktop applications

Transparent Splash screen in .Net

To give a nice look at your applications there are a lot of little thing that makes the difference. One of that is a nice Splash screen like the one of Photoshop or other famous software. Making it in VB.Net is really simple, all that you need is:

  • A nice image to use as splash screen
    In our case we want a transparent image so we’ll save it in PNG format to take transparency.
    We’ll use the image as embedded resource in our project
  • A form that paints the image on it’s background
  • A timer to close the splash screen after 4 seconds

In this demo project we’ll create a main form that acts this way:

  • When the form loads it hides itself
    Simply use Me.Visible=false
  • Then it creates the SplashScreen and displays it as modal form
  • When the splash screen closes just show itself again

Here is the image we’ll use as splash screen (made with photoshop)

Splash screen made with Photoshop

Here is the code to write into the Splash screen Form

Private imgSplash As System.Drawing.Image = _
     System.Drawing.Image.FromStream( _
          GetResource("Splash Screen.png"))
 
Private Shared m_Assembly As System.Reflection.Assembly =_
     System.Reflection.Assembly.GetExecutingAssembly()
Private Shared m_AssemblyPath As String = _
     m_Assembly.GetName().Name().Replace(" ", "_")
 
Public Shared Function GetResource(ByVal FileName As String) _
       As System.IO.Stream
    Return m_Assembly.GetManifestResourceStream(m_AssemblyPath _
       & "." & FileName)
End Function
 
Protected Overrides Sub OnPaintBackground(ByVal pevent As _
          System.Windows.Forms.PaintEventArgs)
    Dim gfx As dra.Graphics = pevent.Graphics
    gfx.DrawImage(imgSplash, New dra.Rectangle(0, 0, _
        Me.Width, Me.Height))
End Sub
 
Private Sub SplashScreen_Load(ByVal sender As System.Object, _
               ByVal e As System.EventArgs) Handles MyBase.Load
    T1.Enabled = True
    T1.Start()
End Sub
 
Private Sub T1_Tick(ByVal sender As System.Object, _
         ByVal e As System.EventArgs) Handles T1.Tick
    T1.Stop()
    T1.Dispose()
    Me.Close()
End Sub
 
Protected Overrides Sub OnClick(ByVal e As System.EventArgs)
    T1.Stop()
    T1.Dispose()
    Me.Close()
End Sub

Download this code: Splashscreen.vb.txt

And this is the final result

Splash screen in action
Download the demo application source code here
Splash Screen demo application


TAGS: Asp.net, dotnet, code snippet, seo, search engine optimization, visual studio.net, sample code, c#, vb.net

Compile and execute code at RunTime

Last week i needed to compile and run scripts at runtime. The idea was to add code snippets into a DB and load and execute them at runtime. My first idea was to write a scripting language but it was an hard work and i had no time for this.
Searching around i found a very interesting thing about the Framework. It’s possible to load, compile and run .Net code from script file or simple string. Everything happens in memory so you don’t need to generate an output file :)

Here is the solution:

Needed:
Imports System
Imports System.Reflection
Imports System.Reflection.Emit

Assume we have a TextBox named TextBox1 where we’ll write the source code we want to compile and run on the fly.

 Try
    If TextBox1.Text.Trim <> "" Then
        Dim VBP As New VBCodeProvider
        Dim CVB As System.CodeDom.Compiler.ICodeCompiler
        CVB = VBP.CreateCompiler
        Dim PM As New System.CodeDom.Compiler.CompilerParameters
        PM.GenerateInMemory = True
        '  PM.GenerateExecutable = True
        PM.OutputAssembly = "Generated.dll"
        PM.MainClass = "ClassMain"
        PM.IncludeDebugInformation = True
 
        Dim ASM As System.Reflection.Assembly
        For Each ASM In AppDomain.CurrentDomain.GetAssemblies()
            PM.ReferencedAssemblies.Add(ASM.Location)
        Next

        'Get compilation results
        Dim Results As System.CodeDom.Compiler.CompilerResults
        Results = CVB.CompileAssemblyFromSource(PM, TextBox1.Text)
 
        'Show possible compilation errors
        Dim Err As System.CodeDom.Compiler.CompilerError
        For Each err In Results.Errors
            ListBox1.Items.Add("Error N. " & err.ErrorNumber &_
            " Message: " & err.ErrorText & " Line " & err.Line)
        Next
 
        'Use the compiled assembly
        Dim RunObj As New Object
        Dim vArgs() As Object
 
        RunObj = Results.CompiledAssembly.CreateInstance(
        	 "Generated.ClassMain", False,
        	 Reflection.BindingFlags.CreateInstance, Nothing,
        	 vArgs, Nothing, Nothing)

        If Not RunObj Is Nothing Then
            RunObj.Esegui()
        Else
            MsgBox.Show("Compile Error")
        End If
 
    Else
        MsgBox.Show("Write VB.Net code to compile")
    End If
Catch err As Exception
    MsgBox.Show(err.Message & " " & err.StackTrace)
End Try

Download this code: RuntimeCode.vb.txt

Here is a sample vb file that you can load and compile at runtime

Imports System.Windows.Forms
Imports Microsoft.VisualBasic
 
namespace Generated
   public class ClassMain
 
       public sub Esegui()
           messagebox.show("Ciao come va?")
       end sub
 
   end class
end namespace

Download this code: RuntimeCodeTest.vb.txt

I hope it could be useful for your tasks :)


TAGS: Asp.net, dotnet, code snippet, seo, search engine optimization, visual studio.net, sample code, c#, vb.net

Why Microsoft .Net Framework

I think that when you have to develop a software you must consider a lot of things before making the final choice about the tools/languages to use. I often talk with Windows/.Net and Linux/PHP fans and everyone things that his choice is the right one. I think that this way of seeing things is extremely wrong, each solution has it’s strenghts and it’s weakness, the right choice only depends on your goals. The main example of my point of view is that i use .Net to develop my applications but i’m using a PHP Open Source blog. I’m using WordPress because i think it’s one of the best products i’ve seen around and i needed something to use immediatly, my goal was to have a blog in 10 minutes, and here it is.

When i had to choose between .Net and PHP i had to make myself a question:
What do i have to do with the language i’ll choose?

The answer was:
I have to develop web applications, webservices and sometimes desktop applications. I do it as job so i need rapid development to earn more money. I need to sell my software/websites without source code, it’s commercial software.

Microsoft.Net was the right choice for me, it covered all my requirements.
The only problem was SQL Server, it’s price was extremely high so my solutions could be very difficult to sell if the customer had to pay for the licenses, so i opted for .Net & MySQL

So what i want to say is that the infinite “war” between Linux/PHP and Windows/.net people is a really stupid thing, just choose what is more convenient for your business/goals.
I always use .Net (C# or VB) but somethimes i code in PHP too when i think it’s more convenient.


TAGS: Asp.net, dotnet, code snippet, seo, search engine optimization, visual studio.net, sample code, c#, vb.net