A Class To Hide Show Task Bar (Works with XP)

using System;
using System.Runtime.InteropServices;


///
/// This class will show or hide windows taskbar for full screen mode.
///

internal class HandleTaskBar
{
private const int SWP_HIDEWINDOW = 0x0080;
private const int SWP_SHOWWINDOW = 0x0040;

///
/// Default Constructor.
///

public HandleTaskBar()
{
}

[DllImport("User32.dll", EntryPoint="FindWindow")]
private static extern int FindWindow(string lpClassName, string lpWindowName);

[DllImport("User32.dll")]
private static extern int SetWindowPos(int hWnd, int hWndInsertAfter, int x, int y, int cx, int cy, int wFlags);

///
/// Show the TaskBar.
///

public static void showTaskBar()
{
int hWnd = FindWindow("Shell_TrayWnd", "");
SetWindowPos(hWnd, 0, 0, 0, 0, 0, SWP_SHOWWINDOW);
}

///
/// Hide the TaskBar.
///

public static void hideTaskBar()
{
int hWnd = FindWindow("Shell_TrayWnd", "");
SetWindowPos(hWnd, 0, 0, 0, 0, 0, SWP_HIDEWINDOW);
}
}


 

Convert a File to Byte Arrary Function

  public byte[] ReadByteArrayFromFile(string fileName)
{
byte[] buff = null;
FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fs);
long numBytes = new FileInfo(fileName).Length;
buff = br.ReadBytes((int)numBytes);
return buff;
}
 

A Simple Decrypt Function

First Add This Namespaces

using System;
using System.Collections.Generic;
using System.Text;
using System.Security;
using System.Security.Cryptography;
using System.Runtime.InteropServices;
using System.Text.RegularExpressions;
using System.IO;
using System.Windows.Forms;

Now The Function:

public void Decrypt(string inputfile,string outputfile,string passPhrase,string saltValue,string hashAlgorithm,int passwordIterations,string initVector,int keySize)
{
// Convert strings defining encryption key characteristics into byte
// arrays. Let us assume that strings only contain ASCII codes.
// If strings include Unicode characters, use Unicode, UTF7, or UTF8
// encoding.
byte[] initVectorBytes = Encoding.ASCII.GetBytes(initVector);
byte[] saltValueBytes = Encoding.ASCII.GetBytes(saltValue);

// Convert our ciphertext into a byte array.
byte[] cipherTextBytes = ReadByteArrayFromFile(inputfile);

// First, we must create a password, from which the key will be
// derived. This password will be generated from the specified
// passphrase and salt value. The password will be created using
// the specified hash algorithm. Password creation can be done in
// several iterations.
PasswordDeriveBytes password = new PasswordDeriveBytes(
passPhrase,
saltValueBytes,
hashAlgorithm,
passwordIterations);

// Use the password to generate pseudo-random bytes for the encryption
// key. Specify the size of the key in bytes (instead of bits).
byte[] keyBytes = password.GetBytes(keySize / 8);

// Create uninitialized Rijndael encryption object.
RijndaelManaged symmetricKey = new RijndaelManaged();

// It is reasonable to set encryption mode to Cipher Block Chaining
// (CBC). Use default options for other symmetric key parameters.
symmetricKey.Mode = CipherMode.CBC;

// Generate decryptor from the existing key bytes and initialization
// vector. Key size will be defined based on the number of the key
// bytes.
ICryptoTransform decryptor = symmetricKey.CreateDecryptor(
keyBytes,
initVectorBytes);

// Define memory stream which will be used to hold encrypted data.
MemoryStream memoryStream = new MemoryStream(cipherTextBytes);

// Define cryptographic stream (always use Read mode for encryption).
CryptoStream cryptoStream = new CryptoStream(memoryStream,
decryptor,
CryptoStreamMode.Read);

// Since at this point we don't know what the size of decrypted data
// will be, allocate the buffer long enough to hold ciphertext;
// plaintext is never longer than ciphertext.
byte[] plainTextBytes = new byte[cipherTextBytes.Length];

// Start decrypting.
int decryptedByteCount = cryptoStream.Read(plainTextBytes,
0,
plainTextBytes.Length);

// Close both streams.
memoryStream.Close();
cryptoStream.Close();


File.WriteAllBytes(outputfile,plainTextBytes);

// Convert decrypted data into a string.
// Let us assume that the original plaintext string was UTF8-encoded.
//string plainText = Encoding.UTF8.GetString(plainTextBytes,
//0,
//decryptedByteCount);

// Return decrypted string.
//return plainText;
}
 

A Simple Encryption Function

First Add This Namespaces

using System;
using System.Collections.Generic;
using System.Text;
using System.Security;
using System.Security.Cryptography;
using System.Runtime.InteropServices;
using System.Text.RegularExpressions;
using System.IO;
using System.Windows.Forms;

Now The Function:


public void Encrypt(string inputfile,string outputfile,string passPhrase,string saltValue,string hashAlgorithm,int passwordIterations,string initVector,int keySize)
{
byte[] initVectorBytes = Encoding.ASCII.GetBytes(initVector);
byte[] saltValueBytes = Encoding.ASCII.GetBytes(saltValue);

byte[] plainTextBytes = ReadByteArrayFromFile(inputfile);

PasswordDeriveBytes password = new PasswordDeriveBytes(passPhrase,saltValueBytes,hashAlgorithm,passwordIterations);

byte[] keyBytes = password.GetBytes(keySize / 8);
RijndaelManaged symmetricKey = new RijndaelManaged();
symmetricKey.Mode = CipherMode.CBC;

ICryptoTransform encryptor = symmetricKey.CreateEncryptor(keyBytes,initVectorBytes);
MemoryStream memoryStream = new MemoryStream();
CryptoStream cryptoStream = new CryptoStream(memoryStream,encryptor,CryptoStreamMode.Write);

cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length);


cryptoStream.FlushFinalBlock();
byte[] cipherTextBytes = memoryStream.ToArray();

memoryStream.Close();
cryptoStream.Close();

File.WriteAllBytes(outputfile, cipherTextBytes);
}
 

File To Object Function


public static object FileToObject(string _FileName)
{
try
{
// Open file for reading
System.IO.FileStream _FileStream = new System.IO.FileStream(_FileName, System.IO.FileMode.Open, System.IO.FileAccess.Read);

// attach filestream to binary reader
System.IO.BinaryReader _BinaryReader = new System.IO.BinaryReader(_FileStream);

// get total byte length of the file
long _TotalBytes = new System.IO.FileInfo(_FileName).Length;

// read entire file into buffer
byte[] _ByteArray = _BinaryReader.ReadBytes((Int32)_TotalBytes);

// close file reader and do some cleanup
_FileStream.Close();
_FileStream.Dispose();
_FileStream = null;
_BinaryReader.Close();

// convert byte array to memory stream
System.IO.MemoryStream _MemoryStream = new System.IO.MemoryStream(_ByteArray);

// create new BinaryFormatter
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter _BinaryFormatter
= new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();

// set memory stream position to starting point
_MemoryStream.Position = 0;

// Deserializes a stream into an object graph and return as a object.
return _BinaryFormatter.Deserialize(_MemoryStream);
}
catch (Exception _Exception)
{
// Error
Console.WriteLine("Exception caught in process: {0}", _Exception.ToString());
}

// Error occured, return null
return null;
}

 

Object To File Function



public static bool ObjectToFile(object _Object, string _FileName)
{
try
{
// create new memory stream
System.IO.MemoryStream _MemoryStream = new System.IO.MemoryStream();

// create new BinaryFormatter
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter _BinaryFormatter
= new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();

// Serializes an object, or graph of connected objects, to the given stream.
_BinaryFormatter.Serialize(_MemoryStream, _Object);

// convert stream to byte array
byte[] _ByteArray = _MemoryStream.ToArray();

// Open file for writing
//System.IO.FileStream _FileStream = new System.IO.FileStream(_FileName, System.IO.FileMode.Create, System.IO.FileAccess.Write);
System.IO.FileStream _FileStream = new System.IO.FileStream(_FileName, FileMode.Create, FileAccess.Write);

// Writes a block of bytes to this stream using data from a byte array.
_FileStream.Write(_ByteArray, 0, _ByteArray.Length);

// close file stream
_FileStream.Close();

// cleanup
_MemoryStream.Close();
_MemoryStream.Dispose();
_MemoryStream = null;
_ByteArray = null;

return true;
}
catch (Exception _Exception)
{
// Error
MessageBox.Show(_Exception.Message);
Console.WriteLine("Exception caught in process: {0}", _Exception.ToString());
}

// Error occured, return null
return false;
}

 

DataTable To ListView

public static void DataTablToListView(DataTable dt, ListView lst)
{
for (int j = 0; j < dt.Columns.Count; j++)
{
lst.Columns.Add(dt.Columns[j].ColumnName);
}

for (int i = 0; i < dt.Rows.Count; i++)
{
DataRow dr = dt.Rows[i];
string[] rowdata = new string[dt.Columns.Count];
for (int j = 0; j < dt.Columns.Count; j++)
{
rowdata[j] = dr[j].ToString();
}


ListViewItem lvi = new ListViewItem(rowdata);
lst.Items.Add(lvi);
}
}
 

Getting The Image Thumbnail

 public static Image GetImageThumbNail(Image img,int mWidth,int mHeight)
{
Image.GetThumbnailImageAbort myCallback = new Image.GetThumbnailImageAbort(ThumbnailCallback);
img = img.GetThumbnailImage(mWidth , mHeight , myCallback, IntPtr.Zero);
return img;
}
private static bool ThumbnailCallback()
{
return false;
}

 

Getting Rounded Edged Corner Graphics Path

public static GraphicsPath GetRoundPath(Rectangle r, int depth)
{
GraphicsPath graphPath = new GraphicsPath();

graphPath.AddArc(r.X, r.Y, depth, depth, 180, 90);
graphPath.AddArc(r.X + r.Width - depth, r.Y, depth, depth, 270, 90);
graphPath.AddArc(r.X + r.Width - depth, r.Y + r.Height - depth, depth, depth, 0, 90);
graphPath.AddArc(r.X, r.Y + r.Height - depth, depth, depth, 90, 90);
graphPath.AddLine(r.X, r.Y + r.Height - depth, r.X, r.Y + depth / 2);

return graphPath;
}
 

Only First Letter Capital

public static string FirstLetterCapital(string strData)
{
string strReturn = string.Empty;
strReturn = strData[0].ToString().ToUpper();

for (int i = 1; i < strData.Length; i++)
{
strReturn += strData[i].ToString().ToLower();
if (strData[i] == ' ' && (i+1) < strData.Length ) //
{
strReturn += strData[i + 1].ToString().ToUpper();
i++;
}
}

return strReturn;
}
Input: WINDOWS FORMS PROGRAMMING
Output: Windows Forms Programming