using System;
using System.Drawing;
using System.Runtime.InteropServices;
namespace SecondPointAddIn1
{
//This class represents the timerclock. Contains the showed time, digits, etc.
public class Ido
{
// This methods are neccessary to capture screen in CaptureScreen method.
//There are very old technics from c++ times, but works very well and simple
private const int SRCCOPY = 0xCC0020;
//Imports ReleaseDC
[DllImport("user32", ExactSpelling = true, CharSet = CharSet.Ansi, SetLastError = true)]
private static extern int ReleaseDC(int hwnd, int hdc);
//Imports GetDC
[DllImport("user32", ExactSpelling = true, CharSet = CharSet.Ansi, SetLastError = true)]
private static extern int GetDC(int hwnd);
//Imports BitBLT
[DllImport("GDI32.DLL", ExactSpelling = true, CharSet = CharSet.Auto, SetLastError = true)]
private static extern bool BitBlt(IntPtr hdcDest, int nXDest, int nYDest, int nWidth, int nHeight, IntPtr hdcSrc, int nXSrc, int nYSrc, int dwRop);
// Hour, minute second
public int ora { get; set; }
public int perc { get; set; }
public int masodperc { get; set; }
//Represents the digits of the timerclock
private Image digit1 { get; set; }
private Image digit2 { get; set; }
private Image digit3 { get; set; }
private Image digit4 { get; set; }
private Image digit5 { get; set; }
private Image digit6 { get; set; }
private Image digit7 { get; set; }
//Represents the colon
private Image kettosp { get; set; }
//Storage places for digit-pngs and colon
private Bitmap png0 { get; set; }
private Bitmap png1 { get; set; }
private Bitmap png2 { get; set; }
private Bitmap png3 { get; set; }
private Bitmap png4 { get; set; }
private Bitmap png5 { get; set; }
private Bitmap png6 { get; set; }
private Bitmap png7 { get; set; }
private Bitmap png8 { get; set; }
private Bitmap png9 { get; set; }
//colon
private Bitmap png10 { get; set; }
//Original color, new color, background color
public Color bgcolor { get; set; }
public Color newcolor { get; set; }
public Boolean transparent { get; set; }
public Boolean newslide { get; set; }
public Image oldimage { get; set; }
//This method captures the part of screen. Screen defined by HWND, rectangle starting at x,y, size defined in width, and height.
public Image CaptureImage(int x, int y, int width, int height, int HWND)
{
//in fact not desktop, but screen with given by HWND. In our explample will be the SlightShowWindow.
int desktop_win = HWND;
//Gets the DC of SlightShowWindow
int desktop_dc = GetDC(desktop_win);
//Defines new bitmap
Bitmap bm = new Bitmap(width, height);
//Defines new Graphics
Graphics bm_gr = Graphics.FromImage(bm);
//Gets the new Graphics handle, because BitBlt using handles.
IntPtr bm_hdc = bm_gr.GetHdc();
//BitBlt copies graphics(given by handle) with normal copy (SRCCOPY)
BitBlt(bm_hdc, 0, 0, width, height, (IntPtr)desktop_dc, x, y, SRCCOPY);
// Release the bitmap's and desktop's DCs.
bm_gr.ReleaseHdc(bm_hdc);
ReleaseDC(desktop_win, desktop_dc);
// Return the result image.
Image i = (Image)bm;
return i;
}
//Method for change color of the digits
private void changecolor(Bitmap bmp)
{
//Goes across the bitmap by columns
for (int x = 0; x < bmp.Width; x++)
{
//Goes across the bitmap by points
for (int y = 0; y < bmp.Height; y++)
{
//If the bitmap pixel color matches the oldcolr then change
if (bmp.GetPixel(x, y) == ColorTranslator.FromHtml("#FFB219"))
{
bmp.SetPixel(x, y, newcolor);
}
}
}
}
//Loads original PNG-s from project-resources
private void loadpng()
{
png0 = new Bitmap(SecondPointAddIn1.Properties.Resources.nullt);
png1 = new Bitmap(SecondPointAddIn1.Properties.Resources.egyt);
png2 = new Bitmap(SecondPointAddIn1.Properties.Resources.kettot);
png3 = new Bitmap(SecondPointAddIn1.Properties.Resources.haromt);
png4 = new Bitmap(SecondPointAddIn1.Properties.Resources.negyt);
png5 = new Bitmap(SecondPointAddIn1.Properties.Resources.ott);
png6 = new Bitmap(SecondPointAddIn1.Properties.Resources.hatt);
png7 = new Bitmap(SecondPointAddIn1.Properties.Resources.hett);
png8 = new Bitmap(SecondPointAddIn1.Properties.Resources.nyolct);
png9 = new Bitmap(SecondPointAddIn1.Properties.Resources.kilenct);
png10 = new Bitmap(SecondPointAddIn1.Properties.Resources.pottyt);
}
//Consturctor. Load png-s from resource. Set the original background and foreground colors
public Ido()
{
//Set defult colors, then change them.
bgcolor = ColorTranslator.FromHtml("#FFFFFF");
newcolor = ColorTranslator.FromHtml("#FFB219");
changeallcolor();
}
//changes all foreground colors of the timerclock (digits and colon)
public void changeallcolor()
{
//if newcolor is original color, then do nothing.
if (newcolor == ColorTranslator.FromHtml("#FFB219"))
{
return;
}
//load default png-s from repository
loadpng();
//change points by points.
changecolor(png0);
changecolor(png1);
changecolor(png2);
changecolor(png3);
changecolor(png4);
changecolor(png5);
changecolor(png6);
changecolor(png7);
changecolor(png8);
changecolor(png9);
changecolor(png10);
}
//Converts a number to a digit Image
private Image convert(int be)
{
Image ret = null;
//Simple switch-case to find the appropriate png
switch (be)
{
case 0:
ret = (Image)png0;
break;
case 1:
ret = (Image)png1;
break;
case 2:
ret = (Image)png2;
break;
case 3:
ret = (Image)png3;
break;
case 4:
ret = (Image)png4;
break;
case 5:
ret = (Image)png5;
break;
case 6:
ret = (Image)png6;
break;
case 7:
ret = (Image)png7;
break;
case 8:
ret = (Image)png8;
break;
case 9:
ret = (Image)png9;
break;
default:
break;
}
return ret;
}
//Create the timerclock-face image from time value. Time value stored in ora, perc masodperc values.
//The result will be a bitmap of the timerclock.
public Image szam2digit()
{
Image ret = null;
//integer of division
int div;
//modulo of division
int mod;
//When hour biger then 10 make two digits
if (ora >= 10)
{
div = ora / 10;
mod = ora % 10;
digit1 = convert(div);
digit2 = convert(mod);
}
//Else first digit will zero (Zero leading numbers)
else
{
digit1 = convert(0);
digit2 = convert(ora);
}
//Same with minutes
if (perc >= 10)
{
div = perc / 10;
mod = perc % 10;
digit3 = convert(div);
digit4 = convert(mod);
}
else
{
digit3 = convert(0);
digit4 = convert(perc);
}
//Same with seconds
if (masodperc >= 10)
{
div = masodperc / 10;
mod = masodperc % 10;
digit5 = convert(div);
digit6 = convert(mod);
}
else
{
digit5 = convert(0);
digit6 = convert(masodperc);
}
//Sets the clock-face width. 2 means the hole between numbers 10 means the width of colon, 52 means the width of digits.
int width = 9 * 2 + 6 * 52 + 2 * 10;
//Sets the clock-face height
int height = 79;
//Create colon
kettosp = (Image)png10;
//Create a new bitmap
Bitmap finalImage = new System.Drawing.Bitmap(width, height);
//Draw direct to the new bitmap of finalImage
using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(finalImage))
{
//If picture not transparent then sets the background color of finalimage
if (transparent == true)
{
if (newslide == false)
{
g.DrawImageUnscaled(oldimage, 0, 0);
}
}
else
{
g.Clear(bgcolor);
}
//Draws a hole
int offset = 2;
//Draws digits 1 and 2 with holes
g.DrawImage(digit1, new System.Drawing.Rectangle(offset, 3, digit1.Width, digit1.Height));
offset += digit1.Width + 2;
g.DrawImage(digit2, new System.Drawing.Rectangle(offset, 3, digit2.Width, digit2.Height));
offset += digit2.Width + 2;
//Draws colon with holes
g.DrawImage(kettosp, new System.Drawing.Rectangle(offset, 3, kettosp.Width, kettosp.Height));
offset += kettosp.Width + 2;
//Draws digits 3 and 4 with holes
g.DrawImage(digit3, new System.Drawing.Rectangle(offset, 3, digit3.Width, digit3.Height));
offset += digit3.Width + 2;
g.DrawImage(digit4, new System.Drawing.Rectangle(offset, 3, digit4.Width, digit4.Height));
offset += digit4.Width + 2;
//Draws colon with holes
g.DrawImage(kettosp, new System.Drawing.Rectangle(offset, 3, kettosp.Width, kettosp.Height));
offset += kettosp.Width + 2;
//Draws digits 5 and 6 with holes
g.DrawImage(digit5, new System.Drawing.Rectangle(offset, 3, digit5.Width, digit5.Height));
offset += digit5.Width + 2;
g.DrawImage(digit6, new System.Drawing.Rectangle(offset, 3, digit6.Width, digit6.Height));
offset += digit6.Width + 2;
}
//Returns finalimage- the complete face of timerclock
ret = finalImage;
return ret;
}
//Decrements one second the time. Min, sec, Hour cannot be neagtive. In that case decrements the next digits value.
public void minusz_egy_masodperc()
{
masodperc--;
if (masodperc == -1)
{
masodperc = 59;
perc--;
}
if (perc == -1)
{
perc = 59;
ora--;
}
if (ora == -1)
{
ora = 23;
}
}
//Resets the timevalue. Not needed, I need it for just testing purposes
public void reset()
{
ora = 0;
perc = 0;
masodperc = 0;
}
}
}