1 /** DGui project file.
2 
3 Copyright: Trogu Antonio Davide 2011-2013
4 
5 License: $(HTTP boost.org/LICENSE_1_0.txt, Boost License 1.0).
6 
7 Authors: Trogu Antonio Davide
8 */
9 module events;
10 
11 import dgui.all;
12 
13 class MainForm: Form
14 {
15 	private PictureBox _pict;
16 	private Bitmap _orgBmp;
17 	private Bitmap _bmp;
18 
19 	public this()
20 	{
21 		this._orgBmp = Bitmap.fromFile("image.bmp"); //Load the bitmap from file (this is the original)
22 		this._bmp = Bitmap.fromFile("image.bmp"); //Load the bitmap from file (this one will be modified)
23 
24 		this.text = "DGui Events";
25 		this.size = Size(300, 250);
26 		this.startPosition = FormStartPosition.centerScreen; // Set Form Position
27 
28 		this._pict = new PictureBox();
29 		this._pict.sizeMode = SizeMode.autoSize; // Stretch the image
30 		this._pict.dock = DockStyle.fill; // Fill the whole form area
31 		this._pict.image = this._bmp;
32 		this._pict.parent = this;
33 
34 		this.menu = new MenuBar();
35 		MenuItem mi = this.menu.addItem("Bitmap");
36 
37 		MenuItem mOrgColors = mi.addItem("Original Colors");
38 		MenuItem mInvColors = mi.addItem("Invert Colors");
39 		MenuItem mGsColors = mi.addItem("Gray Scale");
40 
41 		mOrgColors.click.attach(&this.onMenuOrgColorsClick);
42 		mInvColors.click.attach(&this.onMenuInvColorsClick);
43 		mGsColors.click.attach(&this.onMenuGsColorsClick);
44 	}
45 
46 	private void onMenuOrgColorsClick(MenuItem sender, EventArgs e)
47 	{
48 		BitmapData bd;
49 		this._orgBmp.getData(bd); //Get the original data
50 		this._bmp.setData(bd); //Set the original bitmap data
51 		this._pict.invalidate(); // Tell at the PictureBox to redraw itself
52 	}
53 
54 	private void onMenuInvColorsClick(MenuItem sender, EventArgs e)
55 	{
56 		BitmapData bd;
57 		this._bmp.getData(bd); //Get the original data
58 
59 		for(int i = 0; i < bd.BitsCount; i++) // Invert Colors!
60 		{
61 			bd.bits[i].rgbRed = ~bd.bits[i].rgbRed;
62 			bd.bits[i].rgbGreen = ~bd.bits[i].rgbGreen;
63 			bd.bits[i].rgbBlue = ~bd.bits[i].rgbBlue;
64 		}
65 
66 		this._bmp.setData(bd); //Set the original bitmap data
67 		this._pict.invalidate(); // Tell at the PictureBox to redraw itself
68 	}
69 
70 	private void onMenuGsColorsClick(MenuItem sender, EventArgs e)
71 	{
72 		BitmapData bd;
73 		this._bmp.getData(bd); //Get the original data
74 
75 		for(int i = 0; i < bd.BitsCount; i++) // Gray Scale!
76 		{
77 			ubyte mid = cast(ubyte)((bd.bits[i].rgbRed + bd.bits[i].rgbGreen + bd.bits[i].rgbBlue) / 3);
78 
79 			bd.bits[i].rgbRed = mid;
80 			bd.bits[i].rgbGreen = mid;
81 			bd.bits[i].rgbBlue = mid;
82 		}
83 
84 		this._bmp.setData(bd); //Set the original bitmap data
85 		this._pict.invalidate(); // Tell at the PictureBox to redraw itself
86 	}
87 }
88 
89 int main(string[] args)
90 {
91 	return Application.run(new MainForm()); // Start the application
92 }