Created
October 28, 2016 23:59
-
-
Save MadalinNitu/b5e58b4b72da2f94894d8ce211ff9946 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Microsoft.Win32; | |
using System; | |
using System.Collections.Generic; | |
using System.ComponentModel; | |
using System.Diagnostics; | |
using System.Drawing; | |
using System.Drawing.Imaging; | |
using System.IO; | |
using System.Linq; | |
using System.Net; | |
using System.Net.Mail; | |
using System.Net.Sockets; | |
using System.Runtime.InteropServices; | |
using System.Security.Cryptography; | |
using System.Text; | |
using System.Text.RegularExpressions; | |
using System.Threading; | |
using System.Threading.Tasks; | |
using System.Windows.Forms; | |
namespace ResourcesClass | |
{ | |
#region Interface IReader | |
/// <summary> | |
/// Interface to make Ryndael class COM exposable | |
/// </summary> | |
public interface IRijndael | |
{ | |
[DispId(1)] | |
string Encrypt(string text, string salt); | |
[DispId(2)] | |
string Decrypt(string cipherText, string salt); | |
} | |
#endregion | |
[ClassInterface(ClassInterfaceType.None)] | |
[ProgId("Encryption.Rijndael")] | |
[Guid("48C93B07-0AB6-4412-B4F6-0F9E088E5533")] | |
[ComVisible(true)] | |
class Rijndael : IRijndael | |
{ | |
public Rijndael() { } | |
#region Rijdael class | |
/// <summary> | |
/// ========================== Rijdael class ====================================== | |
/// === Consts - represent a InputKey which is used for create Key and IV ========= | |
/// === for a more safe encrypt change when you use it =================== | |
/// === Encryption - is a function which return a BASE64 string converting ======== | |
/// === froma byte array, whitch is create with 2 parameters ========= | |
/// === first is the text what have to been encrypted and second ===== | |
/// === is the salt (personal key for encrypted the input text) ====== | |
/// === Decrypt - Contain 2 functions, first 'IsBase64String' verify if the ======= | |
/// === parameter 'base64String' is a string in base64string ============ | |
/// === and the second function 'Decrypt' use the first function for ==== | |
/// === verify if the parameter 'cipherText' is true, and if it is ====== | |
/// === then decrypt the them with the second parameter 'salt', that ==== | |
/// === has the key for encrypt and return the text decrypted =========== | |
/// === NewRijndaelManaged - is a function which return a RijndaelManaged that ==== | |
/// === was initialization with a key and verify it if has==== | |
/// === minimum 8 byte and is used for encrypt and decrypt==== | |
/// =============================================================================== | |
/// </summary> | |
#region Consts | |
/// <summary> | |
/// Change this Inputkey GUID with a new GUID when you use this code in your own program !!! | |
/// Keep this inputkey very safe and prevent someone from decoding it some way !!! | |
/// </summary> | |
internal const string Inputkey = "560A18CD-6346-4CF0-A2E8-671F9B6B9EA7"; | |
#endregion | |
#region Encryption | |
/// <summary> | |
/// Encrypt the given text and give the byte array back as a BASE64 string | |
/// </summary> | |
/// <param name="text">The text to encrypt</param> | |
/// <param name="salt">The pasword salt</param> | |
/// <returns>The encrypted text</returns> | |
public string Encrypt(string text, string salt) | |
{ | |
if (string.IsNullOrEmpty(text)) | |
throw new ArgumentNullException("text"); | |
var aesAlg = NewRijndaelManaged(salt); | |
var encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV); | |
var msEncrypt = new MemoryStream(); | |
using (var csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write)) | |
using (var swEncrypt = new StreamWriter(csEncrypt)) | |
swEncrypt.Write(text); | |
return Convert.ToBase64String(msEncrypt.ToArray()); | |
} | |
#endregion | |
#region Decrypt | |
/// <summary> | |
/// Checks if a string is base64 encoded | |
/// </summary> | |
/// <param name="base64String">The base64 encoded string</param> | |
/// <returns></returns> | |
private static bool IsBase64String(string base64String) | |
{ | |
base64String = base64String.Trim(); | |
return (base64String.Length % 4 == 0) && | |
Regex.IsMatch(base64String, @"^[a-zA-Z0-9\+/]*={0,3}$", RegexOptions.None); | |
} | |
/// <summary> | |
/// Decrypts the given text | |
/// </summary> | |
/// <param name="cipherText">The encrypted BASE64 text</param> | |
/// <param name="salt">The pasword salt</param> | |
/// <returns>De gedecrypte text</returns> | |
public string Decrypt(string cipherText, string salt) | |
{ | |
if (string.IsNullOrEmpty(cipherText)) | |
throw new ArgumentNullException("cipherText"); | |
if (!IsBase64String(cipherText)) | |
throw new Exception("The cipherText input parameter is not base64 encoded"); | |
var aesAlg = NewRijndaelManaged(salt); | |
var decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV); | |
var cipher = Convert.FromBase64String(cipherText); | |
using (var msDecrypt = new MemoryStream(cipher)) | |
using (var csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read)) | |
using (var srDecrypt = new StreamReader(csDecrypt)) | |
return srDecrypt.ReadToEnd(); | |
} | |
#endregion | |
#region NewRijndaelManaged | |
/// <summary> | |
/// Create a new RijndaelManaged class and initialize it | |
/// </summary> | |
/// <param name="salt">The pasword salt</param> | |
/// <returns></returns> | |
private static RijndaelManaged NewRijndaelManaged(string salt) | |
{ | |
if (salt == null) throw new ArgumentNullException("salt"); | |
var saltBytes = Encoding.ASCII.GetBytes(salt); | |
var key = new Rfc2898DeriveBytes(Inputkey, saltBytes); | |
var aesAlg = new RijndaelManaged(); | |
aesAlg.Key = key.GetBytes(aesAlg.KeySize / 8); | |
aesAlg.IV = key.GetBytes(aesAlg.BlockSize / 8); | |
return aesAlg; | |
} | |
#endregion | |
#endregion | |
} | |
class RC4 | |
{ | |
#region RC4 class | |
/// <summary> | |
/// ====================================================================================== | |
/// ======= A single class what encrypted a text 'input' with a key and return =========== | |
/// ======= the text encrypted or decrypted a text encrypted with his key and ============ | |
/// ======= return a text clear decrypted ================================================ | |
/// ====================================================================================== | |
/// </summary> | |
/// <param name="input">This is the text to be encrypted or decrypted! </param> | |
/// <param name="key">This is the key for encrypted or decrypted</param> | |
/// <returns></returns> | |
public static string RC4Class(string input, string key) | |
{ | |
StringBuilder result = new StringBuilder(); | |
int x, y, j = 0; | |
int[] box = new int[256]; | |
for (int i = 0; i < 256; i++) | |
box[i] = i; | |
for (int i = 0; i < 256; i++) | |
{ | |
j = (key[i % key.Length] + box[i] + j) % 256; | |
x = box[i]; | |
box[i] = box[j]; | |
box[j] = x; | |
} | |
for (int i = 0; i < input.Length; i++) | |
{ | |
y = i % 256; | |
j = (box[y] + j) % 256; | |
x = box[y]; | |
box[y] = box[j]; | |
box[j] = x; | |
result.Append((char)(input[i] ^ box[(box[y] + box[j]) % 256])); | |
} | |
return result.ToString(); | |
} | |
#endregion | |
} | |
class RSA | |
{ | |
#region RSA class | |
/// <summary> | |
/// =============================================================================================================== | |
/// ===== RSA class encrypt and decrypt a text with alghoritm RSA ================================================= | |
/// ===== The static variabiles ,prime1,prime2,E,N,D are used for create a private key for encrypt and decrypt ==== | |
/// ===== prime1,and prime 2 need to be different one each other and E is used for function phi, he need to be ==== | |
/// ===== a number prime between the first two , rest of variabiles, D and E is used in algoritm and don't have === | |
/// ===== to been introduced for encrypt or decrypt =============================================================== | |
/// =============================================================================================================== | |
/// </summary> | |
static int E = 0; | |
static int D = 0; | |
static int N = 0; | |
public static bool IsPrime(int number) | |
{ | |
if (number < 2) return false; | |
if (number % 2 == 0) return (number == 2); | |
int root = (int)Math.Sqrt((double)number); | |
for (int i = 3; i <= root; i += 2) | |
{ | |
if (number % i == 0) | |
return false; | |
} | |
return true; | |
} | |
public static long Square(long a) | |
{ | |
return (a * a); | |
} | |
public static long BigMod(int b, int p, int m) | |
{ | |
if (p == 0) | |
return 1; | |
else if (p % 2 == 0) | |
return Square(BigMod(b, p / 2, m)) % m; | |
else | |
return ((b % m) * BigMod(b, p - 1, m)) % m; | |
} | |
public static int n_value(int prime1, int prime2) | |
{ | |
return (prime1 * prime2); | |
} | |
public static int phi(int prime1, int prime2) | |
{ | |
return ((prime1 - 1) * (prime2 - 1)); | |
} | |
public static Int32 PrivateKey(int phi, int E, int N) | |
{ | |
int D = 0; | |
int res = 0; | |
for (D = 1; ; D++) | |
{ | |
res = (D * E) % phi; | |
if (res == 1) break; | |
} | |
return D; | |
} | |
public string encrypt(string text) | |
{ | |
string hex = text; | |
char[] ar = hex.ToCharArray(); | |
String c = ""; | |
for (int i = 0; i < ar.Length; i++) | |
{ | |
if (c != string.Empty) | |
{ | |
c += "-"; | |
} | |
c = c + BigMod(ar[i] & 15, E, N); | |
c += "-"; | |
c = c + BigMod(ar[i] >> 4, E, N); | |
} | |
return c; | |
} | |
public string decrypt(string text) | |
{ | |
string hex = text; | |
char[] ar = hex.ToCharArray(); | |
string c = ""; | |
List<long> dc = new List<long>(); | |
int j = 0; | |
string dc2 = ""; | |
try | |
{ | |
for (int i = 0; i < ar.Length; i++) | |
{ | |
c = ""; | |
for (j = i; ar[j] != '-'; j++) | |
{ | |
c = c + ar[j]; | |
} | |
i = j; | |
int xx = Convert.ToInt16(c); | |
dc.Add(BigMod(xx, D, N)); | |
} | |
for (int i = 0; i < dc.Count / 2; i++) | |
{ | |
dc2 += (char)((dc[i * 2 + 1] << 4) + dc[i * 2]); | |
} | |
} | |
catch (Exception ex) | |
{ | |
Console.WriteLine(ex.Message); | |
} | |
return dc2; | |
} | |
#endregion | |
} | |
class ImageCryptography | |
{ | |
#region Image Cryptography | |
/// <summary> | |
/// ========================================================================================================= | |
/// ==== This class will crypted a string into two png file which will have the string crypted by bytes ===== | |
/// ==== those two picture are used for encrypted the string if them are put one on after, then the text ==== | |
/// ==== will by displayed on the screen ==================================================================== | |
/// ==== GenerateImage - have one parametere 'text' what is the string which be encrypted, this function ==== | |
/// ==== return a bitmap array with 2 images which are the encrypted image ================== | |
/// ==== panel1_Paint - it is a event what print in a Panel the images encrypted if the string is not null == | |
/// ==== saveImageToolStripMenuItem_Click - is an event what save the images encrypted on the desktop ======= | |
/// ========================================================================================================= | |
/// </summary> | |
private Size ImageSize = new Size(437, 106); | |
private const int GenerateImageCount = 2; | |
private Bitmap[] img = null; | |
private Bitmap[] GenerateImage(string text) | |
{ | |
Bitmap finalImage = new Bitmap(ImageSize.Width, ImageSize.Height); | |
Bitmap tempImage = new Bitmap(ImageSize.Width / 2, ImageSize.Height); | |
Bitmap[] image = new Bitmap[GenerateImageCount]; | |
Random rand = new Random(); | |
SolidBrush brush = new SolidBrush(Color.Black); | |
Point mid = new Point(ImageSize.Width / 2, ImageSize.Height / 2); | |
Graphics g = Graphics.FromImage(finalImage); | |
Graphics gtemp = Graphics.FromImage(tempImage); | |
StringFormat sf = new StringFormat(); | |
sf.Alignment = StringAlignment.Center; | |
sf.LineAlignment = StringAlignment.Center; | |
Font font = new Font("Time New Roman", 48); | |
Color fontColor; | |
g.DrawString(text, font, brush, mid, sf); | |
gtemp.DrawImage(finalImage, 0, 0, tempImage.Width, tempImage.Height); | |
for (int i = 0; i < image.Length; i++) | |
{ | |
image[i] = new Bitmap(ImageSize.Width, ImageSize.Height); | |
} | |
int index = -1; | |
int width = tempImage.Width; | |
int height = tempImage.Height; | |
for (int x = 0; x < width; x++) | |
{ | |
for (int y = 0; y < height; y++) | |
{ | |
fontColor = tempImage.GetPixel(x, y); | |
index = rand.Next(image.Length); | |
if (fontColor.Name == Color.Empty.Name) | |
{ | |
for (int i = 0; i < image.Length; i++) | |
{ | |
if (index == 0) | |
{ | |
image[i].SetPixel(x * 2, y, Color.Black); | |
image[i].SetPixel(x * 2 + 1, y, Color.Empty); | |
} | |
else | |
{ | |
image[i].SetPixel(x * 2, y, Color.Empty); | |
image[i].SetPixel(x * 2 + 1, y, Color.Black); | |
} | |
} | |
} | |
else | |
{ | |
for (int i = 0; i < image.Length; i++) | |
{ | |
if ((index + i) % image.Length == 0) | |
{ | |
image[i].SetPixel(x * 2, y, Color.Black); | |
image[i].SetPixel(x * 2 + 1, y, Color.Empty); | |
} | |
else | |
{ | |
image[i].SetPixel(x * 2, y, Color.Empty); | |
image[i].SetPixel(x * 2 + 1, y, Color.Black); | |
} | |
} | |
} | |
} | |
} | |
brush.Dispose(); | |
tempImage.Dispose(); | |
finalImage.Dispose(); | |
return image; | |
} | |
private void panel1_Paint(object sender, PaintEventArgs e) | |
{ | |
if (img != null) | |
{ | |
Graphics g = e.Graphics; | |
Rectangle rect = new Rectangle(0, 0, 0, 0); | |
for (int i = 0; i < img.Length; i++) | |
{ | |
rect.Size = img[i].Size; | |
g.DrawImage(img[i], rect); | |
rect.Y += img[i].Height + 5; | |
} | |
g.DrawLine(new Pen(new SolidBrush(Color.Black), 1), rect.Location, new Point(rect.Width, rect.Y)); | |
rect.Y += 5; | |
for (int i = 0; i < img.Length; i++) | |
{ | |
rect.Size = img[i].Size; | |
g.DrawImage(img[i], rect); | |
} | |
} | |
} | |
private void saveImageToolStripMenuItem_Click(object sender, EventArgs e) | |
{ | |
SaveFileDialog sfd = new SaveFileDialog(); | |
if (img == null) | |
{ | |
Console.WriteLine("Scrie un nume mai intai!"); | |
return; | |
} | |
if (sfd.ShowDialog() == DialogResult.OK) | |
{ | |
string fName = Path.GetFileNameWithoutExtension(sfd.FileName); | |
string fPath = sfd.FileName; | |
string sPath = fName; | |
for (int i = 0; i < img.Length; i++) | |
{ | |
sPath = fPath.Replace(fName, fName + i); | |
img[i].Save(sPath, ImageFormat.Png); | |
} | |
} | |
} | |
#endregion | |
} | |
class MailSender | |
{ | |
#region Mail Sender | |
/// =================================================================================================================== | |
/// =================================== This class will sent a mail =================================================== | |
/// ==== MailSender class has more constructors with diferent parameter for many informations ========================= | |
/// =================================================================================================================== | |
/// <summary> | |
/// <param name="your_id">Here you have to put your email!</param> | |
/// <param name="your_password">Here you have to put your password for 'your_id' parameter!</param> | |
/// <param name="receiverAdress">Here is the email witch you want to send a email!</param> | |
/// <param name="attachments">If you want to put an Attachment you have to put the string what is the local address in your computer </param> | |
/// <param name="subject">Here you have to put the subjent of email what will be sent!</param> | |
/// <param name="body">Here is the message of email will be sent!</param> | |
/// <param name="host">Here is the host for what email do you want to sent. Ex: gmail has host('smtp.gmail.com')</param> | |
/// </summary> | |
public MailSender(string your_id, string your_password, string receiverAdress, string attachments, string subject, string body, string host) | |
{ | |
//Specify senders gmail address | |
string SendersAddress = your_id; | |
//Specify The Address You want to sent Email To(can be any valid email address) | |
string ReceiversAddress = receiverAdress; | |
//Specify The password of gmial account u are using to sent mail(pw of [email protected]) | |
string SendersPassword = your_password; | |
try | |
{ | |
//we will use Smtp client which allows us to send email using SMTP Protocol | |
//i have specified the properties of SmtpClient smtp within{} | |
//gmails smtp server name is smtp.gmail.com and port number is 587 | |
SmtpClient smtp = new SmtpClient | |
{ | |
Host = host, | |
Port = 587, | |
EnableSsl = true, | |
DeliveryMethod = SmtpDeliveryMethod.Network, | |
Credentials = new NetworkCredential(SendersAddress, SendersPassword), | |
Timeout = 3000 | |
}; | |
//MailMessage represents a mail message | |
//it is 4 parameters(From,TO,subject,body) | |
MailMessage message = new MailMessage(SendersAddress, ReceiversAddress, subject, body); | |
message.Attachments.Add(new Attachment(attachments)); | |
/*WE use smtp sever we specified above to send the message(MailMessage message)*/ | |
smtp.Send(message); | |
MessageBox.Show("Message Sent Successfully"); | |
} | |
catch (Exception ex) | |
{ | |
MessageBox.Show(ex.Message); | |
} | |
} | |
public MailSender(string your_id, string your_password, string receiverAdress, string attachments, string subject, string body) | |
{ | |
//Specify senders gmail address | |
string SendersAddress = your_id; | |
//Specify The Address You want to sent Email To(can be any valid email address) | |
string ReceiversAddress = receiverAdress; | |
//Specify The password of gmial account u are using to sent mail(pw of [email protected]) | |
string SendersPassword = your_password; | |
//Write the subject of ur mail | |
try | |
{ | |
//we will use Smtp client which allows us to send email using SMTP Protocol | |
//i have specified the properties of SmtpClient smtp within{} | |
//gmails smtp server name is smtp.gmail.com and port number is 587 | |
SmtpClient smtp = new SmtpClient | |
{ | |
Host = "smtp.gmail.com", | |
Port = 587, | |
EnableSsl = true, | |
DeliveryMethod = SmtpDeliveryMethod.Network, | |
Credentials = new NetworkCredential(SendersAddress, SendersPassword), | |
Timeout = 3000 | |
}; | |
//MailMessage represents a mail message | |
//it is 4 parameters(From,TO,subject,body) | |
MailMessage message = new MailMessage(SendersAddress, ReceiversAddress, subject, body); | |
message.Attachments.Add(new Attachment(attachments)); | |
/*WE use smtp sever we specified above to send the message(MailMessage message)*/ | |
smtp.Send(message); | |
MessageBox.Show("Message Sent Successfully"); | |
} | |
catch (Exception ex) | |
{ | |
MessageBox.Show(ex.Message); | |
} | |
} | |
public MailSender(string your_id, string your_password, string receiverAdress, string subject, string body) | |
{ | |
//Specify senders gmail address | |
string SendersAddress = your_id; | |
//Specify The Address You want to sent Email To(can be any valid email address) | |
string ReceiversAddress = receiverAdress; | |
//Specify The password of gmial account u are using to sent mail(pw of [email protected]) | |
string SendersPassword = your_password; | |
try | |
{ | |
//we will use Smtp client which allows us to send email using SMTP Protocol | |
//i have specified the properties of SmtpClient smtp within{} | |
//gmails smtp server name is smtp.gmail.com and port number is 587 | |
SmtpClient smtp = new SmtpClient | |
{ | |
Host = "smtp.gmail.com", | |
Port = 587, | |
EnableSsl = true, | |
DeliveryMethod = SmtpDeliveryMethod.Network, | |
Credentials = new NetworkCredential(SendersAddress, SendersPassword), | |
Timeout = 3000 | |
}; | |
//MailMessage represents a mail message | |
//it is 4 parameters(From,TO,subject,body) | |
MailMessage message = new MailMessage(SendersAddress, ReceiversAddress, subject, body); | |
/*WE use smtp sever we specified above to send the message(MailMessage message)*/ | |
smtp.Send(message); | |
MessageBox.Show("Message Sent Successfully"); | |
} | |
catch (Exception ex) | |
{ | |
MessageBox.Show(ex.Message); | |
} | |
} | |
public MailSender(string your_id, string your_password, string subject, string body) | |
{ | |
//Specify senders gmail address | |
string SendersAddress = your_id; | |
//Specify The Address You want to sent Email To(can be any valid email address) | |
string ReceiversAddress = your_id; | |
//Specify The password of gmial account u are using to sent mail(pw of [email protected]) | |
string SendersPassword = your_password; | |
//Write the subject of ur mail | |
try | |
{ | |
//we will use Smtp client which allows us to send email using SMTP Protocol | |
//i have specified the properties of SmtpClient smtp within{} | |
//gmails smtp server name is smtp.gmail.com and port number is 587 | |
SmtpClient smtp = new SmtpClient | |
{ | |
Host = "smtp.gmail.com", | |
Port = 587, | |
EnableSsl = true, | |
DeliveryMethod = SmtpDeliveryMethod.Network, | |
Credentials = new NetworkCredential(SendersAddress, SendersPassword), | |
Timeout = 3000 | |
}; | |
//MailMessage represents a mail message | |
//it is 4 parameters(From,TO,subject,body) | |
MailMessage message = new MailMessage(SendersAddress, ReceiversAddress, subject, body); | |
/*WE use smtp sever we specified above to send the message(MailMessage message)*/ | |
smtp.Send(message); | |
MessageBox.Show("Message Sent Successfully"); | |
} | |
catch (Exception ex) | |
{ | |
MessageBox.Show(ex.Message); | |
} | |
} | |
#endregion | |
} | |
class OwnDrawing | |
{ | |
public static void ScreenFormActive(Form obj,string fileSave) | |
{ | |
Rectangle bounds = obj.Bounds; | |
using (Bitmap bitmap = new Bitmap(bounds.Width, bounds.Height)) | |
{ | |
using (Graphics g = Graphics.FromImage(bitmap)) | |
{ | |
g.CopyFromScreen(new Point(bounds.Left, bounds.Top), Point.Empty, bounds.Size); | |
} | |
bitmap.Save(fileSave+"\\screen.jpg", ImageFormat.Jpeg); | |
} | |
} | |
public static void ScreenFull(string fileSave) | |
{ | |
Rectangle bounds = Screen.GetBounds(Point.Empty); | |
using (Bitmap bitmap = new Bitmap(bounds.Width, bounds.Height)) | |
{ | |
using (Graphics g = Graphics.FromImage(bitmap)) | |
{ | |
g.CopyFromScreen(Point.Empty, Point.Empty, bounds.Size); | |
} | |
bitmap.Save(fileSave+"\\screen.jpg", ImageFormat.Jpeg); | |
} | |
} | |
private static bool Equals(Bitmap bmp1, Bitmap bmp2) | |
{ | |
if (!bmp1.Size.Equals(bmp2.Size)) | |
{ | |
return false; | |
} | |
for (int x = 0; x < bmp1.Width; ++x) | |
{ | |
for (int y = 0; y < bmp1.Height; ++y) | |
{ | |
if (bmp1.GetPixel(x, y) != bmp2.GetPixel(x, y)) | |
{ | |
return false; | |
} | |
} | |
} | |
return true; | |
} | |
} | |
public static class KeySender | |
{ | |
#region Key Sender | |
/// <summary> | |
/// =========================================================================== | |
/// ============= This class will send a key for a action in a form. ========== | |
/// == Start need to have the variabiles (interval and WindowsName ============ | |
/// == Initialization ), these mean: for use this class need to call the ====== | |
/// == Start() and give the value for WindowsName , interval and key (Interval= | |
/// == for what while do you want to repet the action key, and windowsName ==== | |
/// == means the windows form where is happening the action =================== | |
/// == For stop the action need to give at "_start" false ===================== | |
/// =========================================================================== | |
/// </summary> | |
/// <param name="ZeroOnly"></param> | |
/// <param name="lpWindowName"></param> | |
/// <returns></returns> | |
[DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)] | |
public static extern IntPtr FindWindow(IntPtr ZeroOnly, string lpWindowName); | |
[DllImport("user32.dll")] | |
public static extern void keybd_event(byte bvk, byte bScan, uint dwFlags, IntPtr dwExtraInfo); | |
[DllImport("user32.dll")] | |
[return: MarshalAs(UnmanagedType.Bool)] | |
static extern bool SetForegroundWindow(IntPtr hWnd); | |
public static bool _start; | |
public static string WindowsName; | |
public static int interval, key; | |
public static void Start() | |
{ | |
_start = true; | |
IntPtr _pointer = FindWindow(new IntPtr(0), WindowsName); | |
SetForegroundWindow(_pointer); | |
while (_start) | |
{ | |
SendKeys(); | |
Thread.Sleep((int)interval * 1000); | |
Application.DoEvents(); | |
} | |
} | |
private static void SendKeys() | |
{ | |
const int keyeventf_keyup = 0x0002; | |
keybd_event((byte)key, 0, 0, IntPtr.Zero); | |
keybd_event((byte)key, 0, keyeventf_keyup, IntPtr.Zero); | |
} | |
#endregion | |
#region Keys Action | |
/// <summary> | |
/// =============================================================================== | |
/// == Those function are used for get the name at Windows Form Active and get ==== | |
/// == the key state, or "IsKeyPressed" for verify if a key is press ============== | |
/// =============================================================================== | |
/// </summary> | |
/// <returns></returns> | |
[DllImport("user32", CharSet = CharSet.Ansi, ExactSpelling = true, SetLastError = true)] | |
public static extern IntPtr GetForegroundWindow(); | |
[DllImport("user32", CharSet = CharSet.Auto, SetLastError = true)] | |
public static extern int GetWindowText(IntPtr hWnd, StringBuilder lpsString, int cch); | |
[DllImport("User32.dll")] | |
internal static extern short GetKeyState(Keys nVirtualKey); | |
public static bool IsToggled(this Keys key) | |
{ | |
return GetKeyState(key) == 0x1; | |
} | |
public static bool IsKeyPressed(this Keys key) | |
{ | |
var result = GetKeyState(key); | |
switch (result) | |
{ | |
case 0: return false; | |
case 1: return false; | |
default: return true; | |
} | |
} | |
public static string GetActiveWindowText() | |
{ | |
var handle = GetForegroundWindow(); | |
var sb = new StringBuilder(); | |
GetWindowText(handle, sb, 1000); | |
return sb.Length == 0 ? "UnNamed Window" : sb.ToString(); | |
} | |
#endregion | |
#region Key Map | |
public struct KeyMap | |
{ | |
public Keys Key { get; private set; } | |
public string Modifiend { get; private set; } | |
public string Original { get; private set; } | |
public KeyMap(Keys key, string original, string modified = null) | |
: this() | |
{ | |
Key = key; | |
Original = original; | |
Modifiend = modified; | |
} | |
} | |
#endregion | |
#region Key Mapper | |
internal class KeyMapper | |
{ | |
public readonly List<KeyMap> keyMapList = new List<KeyMap>(); | |
public void AddKeyToList(Keys key, string original, string modified = null) | |
{ | |
keyMapList.Add(new KeyMap(key, original, modified)); | |
} | |
public KeyMapper() | |
{ | |
AddKeyToList(Keys.Back, " {Backspace} "); | |
AddKeyToList(Keys.Return, " {ENTER} " + Environment.NewLine); | |
AddKeyToList(Keys.Capital, string.Empty); | |
AddKeyToList(Keys.Space, " "); | |
AddKeyToList(Keys.Tab, "\t"); | |
AddKeyToList(Keys.LMenu, " {ALT} "); | |
AddKeyToList(Keys.Alt, " {ALT} "); | |
AddKeyToList(Keys.LWin, string.Empty); | |
AddKeyToList(Keys.RWin, string.Empty); | |
AddKeyToList(Keys.NumLock, string.Empty); | |
AddKeyToList(Keys.Scroll, string.Empty); | |
AddKeyToList(Keys.PrintScreen, " {PRINTSCREEN} "); | |
AddKeyToList(Keys.Control, string.Empty); | |
AddKeyToList(Keys.Pause, string.Empty); | |
AddKeyToList(Keys.PageDown, string.Empty); | |
AddKeyToList(Keys.PageUp, string.Empty); | |
AddKeyToList(Keys.Insert, string.Empty); | |
AddKeyToList(Keys.Home, " {HOME} "); | |
AddKeyToList(Keys.End, " {END} "); | |
AddKeyToList(Keys.LShiftKey, string.Empty); | |
AddKeyToList(Keys.RShiftKey, string.Empty); | |
AddKeyToList(Keys.D0, "0", ")"); | |
AddKeyToList(Keys.D1, "1", "!"); | |
AddKeyToList(Keys.D2, "2", "@"); | |
AddKeyToList(Keys.D3, "3", "#"); | |
AddKeyToList(Keys.D4, "4", "$"); | |
AddKeyToList(Keys.D5, "5", "%"); | |
AddKeyToList(Keys.D6, "6", "^"); | |
AddKeyToList(Keys.D7, "7", "&"); | |
AddKeyToList(Keys.D8, "8", "*"); | |
AddKeyToList(Keys.D9, "9", "("); | |
AddKeyToList(Keys.OemSemicolon, ";", ":"); | |
AddKeyToList(Keys.OemOpenBrackets, "[", "{"); | |
AddKeyToList(Keys.OemCloseBrackets, "]", "}"); | |
AddKeyToList(Keys.OemPeriod, ".", ">"); | |
AddKeyToList(Keys.Oem5, "\\", "|"); | |
AddKeyToList(Keys.OemBackslash, "\\", "|"); | |
AddKeyToList(Keys.Oem7, "'", "\""); | |
AddKeyToList(Keys.OemQuestion, "/", "?"); | |
AddKeyToList(Keys.Oemcomma, ",", "<"); | |
AddKeyToList(Keys.Oemplus, "=", "+"); | |
AddKeyToList(Keys.OemMinus, "-", "_"); | |
AddKeyToList(Keys.Oemtilde, "`", "~"); | |
} | |
public string GetKeyText(Keys key) | |
{ | |
foreach (var map in keyMapList.Where(map => map.Key == key)) | |
{ | |
if (Keys.LShiftKey.IsKeyPressed() || Keys.RShiftKey.IsKeyPressed()) | |
return map.Modifiend; | |
return map.Original; | |
} | |
if (Keys.LShiftKey.IsKeyPressed() || Keys.RShiftKey.IsKeyPressed() || Keys.CapsLock.IsToggled()) | |
return key.ToString().ToUpper(); | |
return key.ToString().ToLower(); | |
} | |
} | |
#endregion | |
} | |
class OwnRandom | |
{ | |
#region Random String | |
private static string chars1 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; | |
private static string chars2 = "0123456789"; | |
private static string chars3 = "!@#$%^&*()_+=-.,;'][:><?"; | |
private static string chars4 = "abcdefghlmnopkstronmqrzx"; | |
private static string chars = string.Empty; | |
private static Random random = new Random(); | |
public static string RandomString(int length) | |
{ | |
chars = chars1 + chars2; | |
return (new string(Enumerable.Repeat(chars, length) | |
.Select(s => s[random.Next(s.Length)]).ToArray())); | |
} | |
public static string RandomString2(int length) | |
{ | |
chars = chars1 + chars2 + chars3; | |
return (new string(Enumerable.Repeat(chars, length) | |
.Select(s => s[random.Next(s.Length)]).ToArray())); | |
} | |
public static string RandomString3(int length) | |
{ | |
chars = chars1 + chars2 + chars3 + chars4; | |
return (new string(Enumerable.Repeat(chars, length) | |
.Select(s => s[random.Next(s.Length)]).ToArray())); | |
} | |
#endregion | |
} | |
class OwnProprietesList | |
{ | |
#region Sort List By a parameter | |
/// <summary> | |
/// This function will sorted a list by a argument of themselves | |
/// OwnList - Is your list; | |
/// SubListForSort - Is your argument in T class what you want to sort; | |
/// </summary> | |
/// <typeparam name="T">Here need the class what is used in list!</typeparam> | |
/// <param name="OwnList">Here need the List what you want to be sorted!</param> | |
/// <returns></returns> | |
public static List<T> Sort<T>(List<T>OwnList) | |
{ | |
List<T> sortedList = new List<T>(); | |
//sortedList = OwnList.OrderBy(order => order.SubListForSort).ThenBy(order => order.SubListForSort).ToList(); | |
return sortedList; | |
} | |
#endregion | |
public static void RunStartUp(string NameApp) | |
{ | |
RegistryKey add = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true); | |
add.SetValue(NameApp, "\"" + Application.ExecutablePath.ToString() + "\""); | |
} | |
public static string CurentData() | |
{ | |
return (DateTime.Now.ToString("HH:mm:ss")); //result 22:11:45 | |
} | |
public static string CurentData2() | |
{ | |
return (DateTime.Now.ToString("HH:mm:ss tt")); //result 11:11:45 PM | |
} | |
public static string CurentData3() | |
{ | |
return (DateTime.Now.ToShortDateString()); //result 30.5.2012 | |
} | |
public static string FullCuretData() | |
{ | |
return (DateTime.Now.ToString()); // result 11:11:45 PM 30.5.2012 | |
} | |
public static void OpenAnotherProcess(string process) | |
{ | |
//process need to be only the name of exe for accesing ex notepad.exe | |
Process proces = new Process(); | |
proces.StartInfo.FileName = "process"; | |
proces.Start(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment