Saturday, July 28, 2012

How to minimize a window by passing the process name

The VB.Net code posted on this page minimizes the window for the given process name.

  1. Module modMain  
  2. #Region "Declarations for Minimizing Windows"  
  3.   Private Declare Function ShowWindow Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal nCmdShow As SHOW_WINDOW) As Boolean  
  4.   
  5.   Private Enum SHOW_WINDOW As Integer  
  6.     SW_HIDE = 0  
  7.     SW_SHOWNORMAL = 1  
  8.     SW_NORMAL = 1  
  9.     SW_SHOWMINIMIZED = 2  
  10.     SW_SHOWMAXIMIZED = 3  
  11.     SW_MAXIMIZE = 3  
  12.     SW_SHOWNOACTIVATE = 4  
  13.     SW_SHOW = 5  
  14.     SW_MINIMIZE = 6  
  15.     SW_SHOWMINNOACTIVE = 7  
  16.     SW_SHOWNA = 8  
  17.     SW_RESTORE = 9  
  18.     SW_SHOWDEFAULT = 10  
  19.     SW_FORCEMINIMIZE = 11  
  20.     SW_MAX = 11  
  21.   End Enum  
  22. #End Region  
  23.   
  24.   Sub Main()  
  25.     'How to call this function  
  26.     MinimizeWindows("acrobat"'This will minimize the acrobat application  
  27.   End Sub  
  28.   
  29.   Public Sub MinimizeWindows(ByVal sProcessName As String)  
  30.     Dim p As Process  
  31.     For Each p In Process.GetProcessesByName(sProcessName)  
  32.       ShowWindow(p.MainWindowHandle, SHOW_WINDOW.SW_MINIMIZE)  
  33.     Next p  
  34.   End Sub  
  35. End Module

How to Check whether the application/process is running or not by passing the process name

Here is the awesome VB.Net code to check whether the application/process is running or not by passing the process name:

  1. Module modMain  
  2.   Sub Main()  
  3.     'Check whether acrobat is running or not  
  4.     If IsProcessRunning("acrobat"Then  
  5.       Console.WriteLine("Yes. Acrobat is running")  
  6.     Else  
  7.       Console.WriteLine("No. Acrobat is not running")  
  8.     End If  
  9.   End Sub  
  10.   Private Function IsProcessRunning(ByVal sProcess As StringAs Boolean  
  11.     For Each oProc As Process In Process.GetProcessesByName(sProcess)  
  12.       Return True  
  13.     Next  
  14.   End Function  
  15. End Module 

How to open a webpage when we click a button?

To open a webpage in your system’s default web browser, when you click a button in your form, write the following code in your button click event:


  1. Dim sURL as String = "http://www.dotnetblogger.info"  
  2. Process.Start(sURL) 
 
 

The above piece of code will lauch the web page as specified with the default browser.

How to print some data from bas Module

  1. Module modMain  
  2.   Dim sText As String  
  3.   Public Sub printRecord()  
  4.       Dim pDocument As New Printing.PrintDocument  
  5.       AddHandler pDocument.PrintPage, AddressOf pd_PrintPage  
  6.       sText = "DotNet Blogger - Your favorite Microsoft Visual Studio .Net Programming Blog."  
  7.       pDocument.Print()  
  8.   End Sub  
  9.   
  10.   Private Sub pd_PrintPage(ByVal sender As System.ObjectByVal e As System.Drawing.Printing.PrintPageEventArgs)  
  11.       'this uses a module level variable  
  12.       e.Graphics.DrawString(sText, New Font("Arial", 10, FontStyle.Regular, GraphicsUnit.Pixel), Brushes.Black, 50, 50)  
  13.   End Sub  
  14. End Module
 

How to convert HTML Color code to RGB color code

  1. Module modMain  
  2.   Public Function ConvertHTMLToRGBColor(ByVal sHtmlColor As StringAs Color  
  3.     'Remove the # if exists  
  4.     sHtmlColor = sHtmlColor.Replace("#""")  
  5.     Return Color.FromArgb(System.Convert.ToInt32(sHtmlColor.Substring(0, 2), 16), System.Convert.ToInt32(sHtmlColor.Substring(2, 2), 16), System.Convert.ToInt32(sHtmlColor.Substring(4, 2), 16))  
  6.   End Function  
  7. End Module