Marko

Iz BazaZnanja
Skoči na: navigacija, iskanje

/*

* Created by SharpDevelop.
* User: kako
* Date: 11.12.2005
* Time: 14:47
* 
* To change this template use Tools | Options | Coding | Edit Standard Headers.
*/
 using System;

using System.Runtime.InteropServices; using System.Drawing; using System.Drawing.Imaging; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data;


namespace pixelcolor {

 /// <summary>
 /// Summary description for Form1.
 /// </summary>
 public class Form1 : System.Windows.Forms.Form {
   [DllImport("Gdi32.dll")]
   public static extern int GetPixel(
   System.IntPtr hdc,    // handle to DC
   int nXPos,  // x-coordinate of pixel
   int nYPos   // y-coordinate of pixel
   );
   
   [DllImport("gdi32.dll")]
   private static extern bool BitBlt(
     IntPtr hdcDest, // handle to destination DC
     int nXDest, // x-coord of destination upper-left corner
     int nYDest, // y-coord of destination upper-left corner
     int nWidth, // width of destination rectangle
     int nHeight, // height of destination rectangle
     IntPtr hdcSrc, // handle to source DC
     int nXSrc, // x-coordinate of source upper-left corner
     int nYSrc, // y-coordinate of source upper-left corner
     System.Int32 dwRop // raster operation code
     );

   [DllImport("User32.dll")]
   public static extern IntPtr GetDC(IntPtr wnd);


   [DllImport("User32.dll")]
   public static extern void ReleaseDC(IntPtr dc);

   


   private System.Windows.Forms.Panel panel1;
   private System.Timers.Timer timer1;

  /// <summary>
   /// Required designer variable.
   /// </summary>
   private System.ComponentModel.Container components = null;


//private Image iFrameUp; //private Image iFrameDown; private Image iFrameSaved; private Image iFrameCurrent;

private MouseButtons mbCur = MouseButtons.None; private MouseButtons mbPrev = MouseButtons.None;

private Point posStartDrag = new Point(0, 0); private Point posEndDrag = new Point(11, 11);

int iNewWidth = 0; int iNewHeight = 0;

private string sText = "Nedefinirano"; private StatusBar sb; private Label lbl;


   public Form1() {
   	try {
   		iFrameSaved = Image.FromFile("c:/Spik/Marko/img/tplay.gif");
   		//iFrameDown = Image.FromFile("c:/Spik/Marko/img/tplayh.gif");

} catch (Exception e) { MessageBox.Show(e.ToString()); }

//this.MouseDown += new MouseEventHandler(MisPritisnjena); //this.MouseUp += new MouseEventHandler(MisSpuscena);

     //
     // Required for Windows Form Designer support
     //
     InitializeComponent();
     this.SetStyle(ControlStyles.ResizeRedraw,true);
     
     sb = new StatusBar();
     sb.Text = "ok! začnimo";
     this.Controls.Add(sb);
     
     lbl = new Label();
     lbl.Text = "nič";
     this.Controls.Add(lbl);
     
     
   }


   /// <summary>
   /// Clean up any resources being used.
   /// </summary>
   protected override void Dispose(bool disposing) {
     if( disposing ) {
       if (components != null) {
         components.Dispose();
       }
     }
     base.Dispose( disposing );
   }


   #region Windows Form Designer generated code
   /// <summary>
   /// Required method for Designer support - do not modify
   /// the contents of this method with the code editor.
   /// </summary>
   private void InitializeComponent()
   {
     this.panel1 = new System.Windows.Forms.Panel();
     this.timer1 = new System.Timers.Timer();
     ((System.ComponentModel.ISupportInitialize)(this.timer1)).BeginInit();
     this.SuspendLayout();
     
     //
     // panel1
     //
     this.panel1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
     this.panel1.Location = new System.Drawing.Point(216, 8);
     this.panel1.Name = "panel1";
     this.panel1.Size = new System.Drawing.Size(64, 56);
     this.panel1.TabIndex = 0;
     //
     // timer1
     //
     this.timer1.Enabled = true;
     this.timer1.SynchronizingObject = this;
     this.timer1.Elapsed += new System.Timers.ElapsedEventHandler(this.timer1_Elapsed);
     //
     // Form1
     //
     this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
     this.BackColor = System.Drawing.Color.White;
     this.ClientSize = new System.Drawing.Size(292, 273);
     this.Controls.Add(this.panel1);
     this.Name = "Form1";
     this.Text = "Form1";
     this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);
     ((System.ComponentModel.ISupportInitialize)(this.timer1)).EndInit();
     this.ResumeLayout(false);


   }
   #endregion


   /// <summary>
   /// The main entry point for the application.
   /// </summary>
   [STAThread]
   static void Main() {
     Application.Run(new Form1());
   }


   private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e) {

/*

     Random r=new Random(1);
     for(int x=0;x<100;x++)
     {
       SolidBrush b=new SolidBrush(Color.FromArgb(r.Next(255),r.Next(255),r.Next(255)));
       e.Graphics.FillRectangle(b,r.Next(this.ClientSize.Width),r.Next(this.ClientSize.Height),r.Next(100),r.Next(100));
     }
*/
   }


   private void MisPritisnjena(object o, MouseEventArgs e) {
   	//e.
   	sText = "Pritisnjena";
   }
   private void MisSpuscena(object o, MouseEventArgs e) {
   	//e.
   	sText = "Spuščena";
   }


   private void timer1_Elapsed(object sender, System.Timers.ElapsedEventArgs e) {

//dobi status miške //

   	Point p = Control.MousePosition;

mbCur = Control.MouseButtons; Keys mkCur = Control.ModifierKeys;

//dobi barvo pod kurzorjem // IntPtr dc = GetDC(IntPtr.Zero); this.panel1.BackColor = ColorTranslator.FromWin32(GetPixel(dc,p.X,p.Y)); ReleaseDC(dc);


//če je pritisnjena desna tipka, briši okno // if (mbCur == MouseButtons.Left && mkCur == Keys.Control) iFrameSaved = null;

// if (mbCur == MouseButtons.Left && mbPrev == MouseButtons.Left && mkCur == Keys.Shift) { //če je sedaj pritisnjena tipka, in je bila tudi prej // posEndDrag = p; ControlPaint.DrawReversibleFrame(new Rectangle(posStartDrag.X, posStartDrag.Y, posEndDrag.X - posStartDrag.X, posEndDrag.Y - posStartDrag.Y), Color.Yellow, FrameStyle.Dashed); sText = "Čekiram";

} else if (mbCur == MouseButtons.Left && mbPrev == MouseButtons.None && mkCur == Keys.Shift) { //če je sedaj pritisnjena tipka, prej pa ne // posStartDrag = p;

sText = "Pritisnjena"; } else if (mbCur == MouseButtons.None && mbPrev == MouseButtons.Left && mkCur == Keys.Shift) { //če je sedaj spuščena tipka, prej pa ne // posEndDrag = p; sText = "Spuščena";

//dobi novo selekcijo // iNewWidth = Math.Abs(posEndDrag.X - posStartDrag.X); iNewHeight = Math.Abs(posEndDrag.Y - posStartDrag.Y);

if (iNewWidth != 0 && iNewHeight != 0) {

iFrameSaved = DobiFrame(posStartDrag.X, posStartDrag.Y, iNewWidth, iNewHeight, dc);

//to dela, slika pa ne :( //gc.FillRectangle(new SolidBrush(Color.Green), 0,0, iFrameDown.Width, iFrameDown.Height); sText += " Dimenzije: X:" + posStartDrag.X + "+" + iFrameSaved.Width + ", Y:" + posStartDrag.Y + "+" + iFrameSaved.Height; }


} mbPrev = mbCur;


//če je kaka shranjena slika if (iFrameSaved != null && iNewWidth != 0 && iNewHeight != 0) {

//primerjaj trenutno in shranjeno sliko // iFrameCurrent = DobiFrame(posStartDrag.X, posStartDrag.Y, iNewWidth, iNewHeight, dc);

if (iFrameSaved == iFrameCurrent) lbl.Text = "ISTO!"; else lbl.Text = "RAZLIČNO!";

//lbl.Text += System.Diagnostics.Process.

//dobi trenuten ekran // Graphics g = Graphics.FromHdc(dc);


//Graphics gc = Graphics.FromImage(iFrameDown); //gc.DrawImage(dc, new Rectangle(0, 0, iFrameDown.Width,iFrameDown.Height), 100,50,iFrameDown.Width,iFrameDown.Height,GraphicsUnit.Pixel); //Bi //gc.Dispose();

//g.DrawRectangle(new Pen(Color.Orange), new Rectangle(posStartDrag.X, posStartDrag.Y, posEndDrag.X - posStartDrag.X, posEndDrag.Y - posStartDrag.Y)); //g.DrawString(sText, this.Font, new SolidBrush(Color.Red), p.X, p.Y);


//nariši ob kurzorju shranjeno sliko in trenutno sliko, če je pritisnjen alt // if (mkCur == Keys.Alt) { g.DrawImage(iFrameSaved, p.X, p.Y); g.DrawImage(iFrameCurrent, p.X, p.Y + iFrameCurrent.Height + 3);

//naredi nov bitmap za primerjavo // Bitmap i3 = new Bitmap(iFrameCurrent.Width, iFrameCurrent.Height); for (int x = 0; x < i3.Width; x++) { for (int y = 0; y < i3.Height; y++) { Color cc = ((Bitmap)iFrameCurrent).GetPixel(x, y); Color cs = ((Bitmap)iFrameSaved).GetPixel(x, y); Color nc = Color.FromArgb(cc.R-cs.R, cc.G-cs.G, cc.B-cs.B); i3.SetPixel(x, y, nc); } }


g.DrawImage(i3, p.X, p.Y + iFrameCurrent.Height*2 + 6);

i3.Dispose();

}

//g.ReleaseHdc(dc); g.Dispose();

sb.Text = sText + " " + iFrameSaved.Width + ", " + iFrameSaved.Height; } else sb.Text = sText;



   }
   //izreže iz zaslona kvadrat in naredi iz tega sliko
   //
   private Image DobiFrame(int startX, int startY, int width, int height, IntPtr srcDC) {
   
   	Image iFrame = new Bitmap(width, height);
   	Graphics gc = Graphics.FromImage(iFrame);

IntPtr destDC = gc.GetHdc(); BitBlt(destDC, 0, 0, width, height, srcDC, startX, startY, 0x00CC0020); //srccopy gc.ReleaseHdc(destDC);

return iFrame;

   }
   
   
 }

}