Wednesday, April 15, 2009

Point Struct

 

Ok System.drawing contains a point struct it has the basic functionality,

Can be initialized by a int32, a point ,a size.When you initialize with a int 32 the doublewords(yes your int) hi word is x and lo word is y

ex: if you want a point (12,12) 786444 is the magic number as you know 12’s hex code is C  000C000C is the double word.

   1: private static int HIWORD(int n)
   2:       {
   3:           return ((n >> 0x10) & 0xffff);
   4:       }
   5:  
   6:       private static int LOWORD(int n)
   7:       {
   8:           return (n & 0xffff);
   9:       }

Also it is interesting microsoft does not allow us to add or substract points, i think it is forgotten or somethink like that.Because you can add points, basicly a point can be thought as a vector whose starting point is the origin, so as you can add 2 vectors you can add points you can add points and sizes and can convert sizes to point so we must have the ability to add points.Because Micosoft defined the point as struct you can’t inherit a new class too.

Here is the source of the point which i added implicit convervsion to system.drawing.point and added add operator to add 2 points.

   1: namespace SystemX.DrawingX
   2: {
   3:     using System;
   4:     using System.Drawing;
   5:     using System.ComponentModel;
   6:     using System.Globalization;
   7:     using System.Runtime.InteropServices;
   8:  
   9:     [Serializable, StructLayout(LayoutKind.Sequential), ComVisible(true), TypeConverter(typeof(PointConverter))]
  10:     public struct Point
  11:     {
  12:         public static readonly Point Empty;
  13:         private int x;
  14:         private int y;
  15:         public Point(int x, int y)
  16:         {
  17:             this.x = x;
  18:             this.y = y;
  19:         }
  20:  
  21:         public Point(Size sz)
  22:         {
  23:             this.x = sz.Width;
  24:             this.y = sz.Height;
  25:         }
  26:  
  27:         public Point(int dw)
  28:         {
  29:             this.x = (short) LOWORD(dw);
  30:             this.y = (short) HIWORD(dw);
  31:         }
  32:  
  33:         [Browsable(false)]
  34:         public bool IsEmpty
  35:         {
  36:             get
  37:             {
  38:                 return ((this.x == 0) && (this.y == 0));
  39:             }
  40:         }
  41:         public int X
  42:         {
  43:             get
  44:             {
  45:                 return this.x;
  46:             }
  47:             set
  48:             {
  49:                 this.x = value;
  50:             }
  51:         }
  52:         public int Y
  53:         {
  54:             get
  55:             {
  56:                 return this.y;
  57:             }
  58:             set
  59:             {
  60:                 this.y = value;
  61:             }
  62:         }
  63:         public static implicit operator PointF(Point p)
  64:         {
  65:             return new PointF((float) p.X, (float) p.Y);
  66:         }
  67:  
  68:         public static implicit operator System.Drawing.Point(Point p)
  69:         {
  70:             return new System.Drawing.Point(p.X, p.Y);
  71:         }
  72:  
  73:         public static explicit operator Size(Point p)
  74:         {
  75:             return new Size(p.X, p.Y);
  76:         }
  77:  
  78:         public static Point operator +(Point pt, Size sz)
  79:         {
  80:             return Add(pt, sz);
  81:         }
  82:         public static Point operator +(Point pt, Point pt2)
  83:         {
  84:             return Add(pt, pt2);
  85:         }
  86:  
  87:         public static Point operator -(Point pt, Point pt2)
  88:         {
  89:             return Subtract(pt, pt2);
  90:         }
  91:  
  92:         public static Point operator -(Point pt, Size sz)
  93:         {
  94:             return Subtract(pt, sz);
  95:         }
  96:  
  97:         public static bool operator ==(Point left, Point right)
  98:         {
  99:             return ((left.X == right.X) && (left.Y == right.Y));
 100:         }
 101:  
 102:         public static bool operator !=(Point left, Point right)
 103:         {
 104:             return !(left == right);
 105:         }
 106:  
 107:         public static Point Add(Point pt, Size sz)
 108:         {
 109:             return new Point(pt.X + sz.Width, pt.Y + sz.Height);
 110:         }
 111:  
 112:         public static Point Add(Point pt, Point pt2)
 113:         {
 114:             return new Point(pt.X + pt2.X, pt.Y + pt2.Y);
 115:         }
 116:  
 117:         public static Point Subtract(Point pt, Point pt2)
 118:         {
 119:             return new Point(pt.X - pt2.X, pt.Y - pt2.Y);
 120:         }
 121:  
 122:         public static Point Subtract(Point pt, Size sz)
 123:         {
 124:             return new Point(pt.X - sz.Width, pt.Y - sz.Height);
 125:         }
 126:  
 127:         public static Point Ceiling(PointF value)
 128:         {
 129:             return new Point((int) Math.Ceiling((double) value.X), (int) Math.Ceiling((double) value.Y));
 130:         }
 131:  
 132:         public static Point Truncate(PointF value)
 133:         {
 134:             return new Point((int) value.X, (int) value.Y);
 135:         }
 136:  
 137:         public static Point Round(PointF value)
 138:         {
 139:             return new Point((int) Math.Round((double) value.X), (int) Math.Round((double) value.Y));
 140:         }
 141:  
 142:         public override bool Equals(object obj)
 143:         {
 144:             if (!(obj is Point))
 145:             {
 146:                 return false;
 147:             }
 148:             Point point = (Point) obj;
 149:             return ((point.X == this.X) && (point.Y == this.Y));
 150:         }
 151:  
 152:         public override int GetHashCode()
 153:         {
 154:             return (this.x ^ this.y);
 155:         }
 156:  
 157:         public void Offset(int dx, int dy)
 158:         {
 159:             this.X += dx;
 160:             this.Y += dy;
 161:         }
 162:  
 163:         public void Offset(Point p)
 164:         {
 165:             this.Offset(p.X, p.Y);
 166:         }
 167:  
 168:         public override string ToString()
 169:         {
 170:             return ("{X=" + this.X.ToString(CultureInfo.CurrentCulture) + ",Y=" + this.Y.ToString(CultureInfo.CurrentCulture) + "}");
 171:         }
 172:  
 173:         private static int HIWORD(int n)
 174:         {
 175:             return ((n >> 0x10) & 0xffff);
 176:         }
 177:  
 178:         private static int LOWORD(int n)
 179:         {
 180:             return (n & 0xffff);
 181:         }
 182:  
 183:         static Point()
 184:         {
 185:             Empty = new Point();
 186:         }
 187:     }
 188: }
 189:  

Advanced Text Box

Source code of text box which enables
alphanumeric input
only letter
upper case (all characters are converted to upper case)

 

   1: using System;
   2: using System.Collections.Generic;
   3: using System.ComponentModel;
   4: using System.Data;
   5: using System.Drawing;
   6: using System.Linq;
   7: using System.Text;
   8: using System.Windows.Forms;
   9:  
  10: namespace Master
  11: {
  12:     public enum InptTyp
  13:     {
  14:         alphanum,
  15:         onlyUpper,
  16:         onlyUpperLetter,
  17:  
  18:     }
  19:     public partial class AdvTextBox : TextBox
  20:     {
  21:         private InptTyp inputType;
  22:         public InptTyp InputType
  23:         {
  24:             get
  25:             {
  26:                 return inputType;
  27:             }
  28:             set
  29:             {
  30:                 inputType = value;
  31:             }
  32:  
  33:         }
  34:         public AdvTextBox()
  35:         {
  36:             InitializeComponent();
  37:         }
  38:  
  39:         protected override void OnPaint(PaintEventArgs pe)
  40:         {
  41:             base.OnPaint(pe);
  42:         }
  43:         protected override void OnKeyPress(KeyPressEventArgs e)
  44:         {
  45:             int i = (int)e.KeyChar;
  46:             switch (inputType)
  47:             {
  48:                 case InptTyp.alphanum:
  49:                     if ((i < 43 || i > 60) && i != 8)//did not use char.isnumeric because this allows to add - and . like characters
  50:                         e.Handled = true;
  51:                     break;
  52:                 case InptTyp.onlyUpperLetter:
  53:                     if (!Char.IsLetter(e.KeyChar))
  54:                     {
  55:                         e.Handled = true;
  56:                         return;
  57:                     }
  58:                     goto case InptTyp.onlyUpper;
  59:                 case InptTyp.onlyUpper:
  60:                     e.KeyChar = Char.ToUpper(e.KeyChar);
  61:                     break;
  62:             }
  63:  
  64:             base.OnKeyPress(e);
  65:  
  66:         }
  67:  
  68:  
  69:     }
  70: }

Saturday, March 28, 2009

Indexers

class indexerDemo
   {
       int[] arr = new int[] { 34, 344, 8, 34, 62, 625, 342, 546 };
       public int Lenght()
       {
           return arr.Length;
       }
       public int this[int index]
       {
           get
           {
               return arr[index];
           }
           set
           {
               arr[index] = value;
           }
       }
   }
 
   class Program
   {
       static void Main(string[] args)
       {
           indexerDemo indexD = new indexerDemo();
           Console.WriteLine(indexD[0].ToString());
       }
   }

Above code can be used to add indexing to any custom class.

Thursday, February 19, 2009

Accesing a Control From another thread

You can’t access a Form control from a thread other than main thread directly you must

define a delegate

  delegate void SetTextDelegate(string text);
 SetTextDelegate spd = new SetTextDelegate(YourMethodName);
than use invoke
 this.Invoke(spd, new object[] {paramater to pass});
thats all

Thursday, December 11, 2008

Encrypt Decrypt Codes

Here is codes for basi cryptography,I did not write it but found on net and tested.
public static string Encrypt(string toEncrypt, string key, bool useHashing)
{
    byte[] keyArray;
    byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(toEncrypt);
 
    if (useHashing)
    {
        MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
        keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
    }
    else
        keyArray = UTF8Encoding.UTF8.GetBytes(key);
 
    TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
    tdes.Key = keyArray;
    tdes.Mode = CipherMode.ECB;
    tdes.Padding = PaddingMode.PKCS7;
 
    ICryptoTransform cTransform = tdes.CreateEncryptor();
    byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
 
    return Convert.ToBase64String(resultArray, 0, resultArray.Length);
}
public static string Decrypt(string toDecrypt, string key, bool useHashing)
{
    byte[] keyArray;
    byte[] toEncryptArray = Convert.FromBase64String(toDecrypt);
 
    if (useHashing)
    {
        MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
        keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
    }
    else
        keyArray = UTF8Encoding.UTF8.GetBytes(key);
 
    TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
    tdes.Key = keyArray;
    tdes.Mode = CipherMode.ECB;
    tdes.Padding = PaddingMode.PKCS7;
 
    ICryptoTransform cTransform = tdes.CreateDecryptor();
    byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
 
    return UTF8Encoding.UTF8.GetString(resultArray);
}

Thursday, November 20, 2008

Inheritance

There are two kinds of inheritance

1. Implementation inheritance

2.Interface inheritance

Structs can not be derived from classes, they are always derived from System.ValueType.

A class cannot inheritate from multiple classes but can inherit from a class and multiple interfaces, c# is known as single inheritance programming language

When a derived class inherits from a base class, it gains all the methods, fields, properties and events of the base class. To change the data and behavior of a base class, you have two choices: you can replace the base member with a new derived member, or you can override a virtual base member.

If the method in the derived class is not preceded by new or override keywords, the compiler will issue a warning and the method will behave as if the new keyword were present

The base class method can be called from within the derived class using the base keyword.

   1: public class BaseClass
   2: {
   3:     public virtual void Test()
   4:     {
   5:         Console.WriteLine("Base");
   6:     }
   7:  
   8: }
   9: public class AClass : BaseClass
  10: {
  11:         public new void Test()
  12:         {
  13:             Console.WriteLine("AClass");
  14:         }
  15: }
  16: public class BClass : BaseClass
  17: {
  18:     public override void Test()
  19:     {
  20:         Console.WriteLine("BClass");
  21:     }
  22: }
  23: class Program
  24: {
  25:  
  26:     static void Main(string[] args)
  27:     {
  28:  
  29:         BaseClass basev = new BaseClass();
  30:         basev.Test();
  31:         BaseClass aclass = new AClass();
  32:         aclass.Test();
  33:         BaseClass bClass = new BClass();
  34:         bClass.Test();
  35:         Console.Read();
  36:  
  37:     }
  38: }

Sunday, January 27, 2008