using System; using System.Windows.Forms; using System.Drawing; public class PictureTrimmer : Form { [System.STAThread] // Main の直前にこれがないと日本語入力とかできない! static void Main() { Application.Run(new PictureTrimmer()); } // ========================================================================= public Carrier_O carrier_A, carrier_B, carrier_C; public Carrier_Z carrier_Z; // === carrier_A, carrier_B, carrier_C, carrier_Z は Opacity==1 である。 // === Opacity < 1 の要素だけだと virus と見做されるらしく、これら必須! Timer timer; // === System.Windows.Forms.Timer TextBox text1 = new TextBox(); // dummy Button btsave = new Button(), btclose=new Button(); static int gg = 50; Color basecolor = Color.FromArgb(gg,gg,gg); int frT = 60, frLR = 50, frB = 40, bdr=5, Pw = 200, Ph = 200; Bitmap bmout, bmin, bmgoal; public PictureTrimmer() { this.FormBorderStyle=FormBorderStyle.None; this.Size = new Size(Pw+2*frLR, Ph+frT+frB); this.CenterToScreen(); // 場所は中央に this.Opacity=0.6; this.BackColor = basecolor; text1 = new TextBox(); text1.SetBounds(0, 1, 60, 4); text1.Text = Pw.ToString()+", "+Ph.ToString(); text1.TextChanged+=(o,e)=>{ Rectangle scr = Screen.GetBounds(this); // ディスプレイサイズ取得 int i1=text1.Text.IndexOf(","), i2=text1.Text.IndexOf(" "); if (i1 == -1) { i1 = text1.Text.Length; } if (i2 == -1) { i2 = text1.Text.Length; } if (i1 > i2) { int rem = i1; i1 = i2; i2 = rem; } try { int w = int.Parse(text1.Text.Substring(0, i1).Trim()); int h = int.Parse(text1.Text.Substring(i2).Trim()); if (w > 0 && h > 0 && w < scr.Width-2*frLR && h < scr.Height-frT-frB) { Pw = w; Ph = h; this.Size = new Size(Pw+2*frLR, Ph+frT+frB); setchildren(); setgraphic(); } } catch { } }; carrier_A = new Carrier_O(this); carrier_A.Controls.Add(text1); carrier_A.Show(this); btsave.SetBounds(-1, 1, 60, 20); btsave.Text = "save"; btsave.BackColor = Color.White; btclose.SetBounds(-1, 1, 60, 20); btclose.Text = "close"; btclose.BackColor = Color.White; btclose.Click += (o, e) => { if(timer != null) { timer.Stop(); timer.Dispose(); } Dispose(true); }; carrier_B = new Carrier_O(this); carrier_B.Controls.Add(btsave); carrier_B.Show(this); carrier_C = new Carrier_O(this); carrier_C.Controls.Add(btclose); carrier_C.Show(this); carrier_Z=new Carrier_Z(this); carrier_Z.Show(this); setchildren(); btsave.Click += (o, e) => { escapef = true; // === Timer の機能を止めないと、既存ファイル指定時にトラブル! // === Timer.Stop(), Timer.Dispose(), Timer.Disposed みな無能! FileDialog saveFileDialog1 = new SaveFileDialog(); saveFileDialog1.Title="保存する"; saveFileDialog1.FileName=""; saveFileDialog1.Filter = "png files(*.Png)|*.Png"+ "|Jpeg files(*.Jpeg)|*.Jpeg|Gif files(*.Gif)|*.Gif" + "|Emf files(*.Emf)|*.Emf|Bmp files(*.Bmp)|*.Bmp" + "|all files(*.*)|*.*"; saveFileDialog1.FilterIndex=6; // === 1 .. !!! saveFileDialog1.RestoreDirectory=true; DialogResult result=saveFileDialog1.ShowDialog(); if (result == DialogResult.OK) { string fname = saveFileDialog1.FileName; bmgoal.Save(fname); } escapef = false; }; timer = new Timer(); timer.Interval = 200; timer.Tick += (o, e) => { setgraphic(); }; timer.Start(); } // ========================================================================= bool escapef = false; int xdevi, ydevi; bool dragging=false; protected override void OnActivated(EventArgs e) { this.TopMost = true; } // ========================================================================= protected override void OnSizeChanged(EventArgs e) { bmgoal = new Bitmap(Pw, Ph); bmout = new Bitmap(Pw+bdr*2, Ph+bdr*2); bmin = new Bitmap(Pw+bdr*2, Ph+bdr*2); Graphics.FromImage(bmout).FillRectangle( new SolidBrush(basecolor), 0, 0, Pw+bdr*2, Ph+bdr*2); setgraphic(); } // ========================================================================= protected override void OnMouseDown(MouseEventArgs e) { Point ptmouseinit=MousePosition, ptforminit=this.Location; xdevi=ptmouseinit.X-ptforminit.X; ydevi=ptmouseinit.Y-ptforminit.Y; dragging=true; } // ========================================================================= protected override void OnMouseMove(MouseEventArgs e) { if (!dragging) { setgraphic(); return; } Point ptmouse=MousePosition; int newx = ptmouse.X - xdevi, newy = ptmouse.Y - ydevi; this.Location=new Point(newx, newy); setchildren(); setgraphic(); } // ========================================================================= protected override void OnMouseUp(MouseEventArgs e) { Point ptmouse=MousePosition; int newx = ptmouse.X - xdevi, newy = ptmouse.Y - ydevi; this.Location=new Point(newx, newy); setchildren(); setgraphic(); dragging=false; } // ========================================================================= public void setchildren() { carrier_Z.Location=new Point( this.Location.X+this.Width-20, this.Location.Y+this.Height-20); carrier_A.Location=this.Location; carrier_B.Location=new Point(this.Location.X+80, this.Location.Y); carrier_C.Location=new Point(this.Location.X+160, this.Location.Y); } // ========================================================================= protected override void OnPaint(PaintEventArgs e) { Graphics g = e.Graphics; g.DrawImage(bmout, new Point(frLR-bdr, frT-bdr)); setgraphic(); } // ========================================================================= void setgraphic() { if (escapef) { return; } Graphics gms = Graphics.FromImage(bmin); Point ptgenerous=new Point( this.Location.X+frLR-bdr, this.Location.Y+frT-bdr); gms.CopyFromScreen(ptgenerous, new Point(0, 0), bmin.Size); int br = basecolor.R, bg = basecolor.G, bb = basecolor.B; bool workedf = false; for (int i=bdr; i 255) { r = 255; } if (g > 255) { g = 255; } if (b > 255) { b = 255; } return Color.FromArgb(r, g, b); } // ========================================================================= public void setSize(Size sizetobe) { // === called from Carrier_Z !!! Rectangle scr = Screen.GetBounds(this); // ディスプレイのサイズを取得 if (sizetobe.Width > scr.Width) {sizetobe.Width=scr.Width; } if (sizetobe.Height > scr.Height) {sizetobe.Height=scr.Height; } if (sizetobe.Width <= 2*frLR) {sizetobe.Width=2*frLR+1; } if (sizetobe.Height <= frT+frB) {sizetobe.Height=frT+frB+1; } Pw = sizetobe.Width - 2 * frLR; Ph = sizetobe.Height-frT-frB; this.Width = sizetobe.Width; this.Height = sizetobe.Height; text1.Text = Pw.ToString() + ", " + Ph.ToString(); bmgoal = new Bitmap(Pw, Ph); bmout = new Bitmap(Pw+bdr*2, Ph+bdr*2); bmin = new Bitmap(Pw+bdr*2, Ph+bdr*2); Graphics.FromImage(bmout).FillRectangle( new SolidBrush(basecolor), 0, 0, Pw+bdr*2, Ph+bdr*2); } // ========================================================================= } // ############################################################################# public class Carrier_O : Form { public Carrier_O(PictureTrimmer partf) { this.FormBorderStyle=FormBorderStyle.None; this.BackColor = Color.White; this.Opacity=1.0; } // ========================================================================= protected override void OnActivated(EventArgs e) { this.Size=new Size(56, 18); } // ========================================================================= } // ############################################################################# public class Carrier_Z : Form { PictureTrimmer par; public Carrier_Z(PictureTrimmer partf) { par=partf; this.FormBorderStyle=FormBorderStyle.None; int gg = 130; this.BackColor = Color.FromArgb(gg,gg,gg); } // ========================================================================= protected override void OnActivated(EventArgs e) { this.Size=new Size(20, 20); } // ========================================================================= public bool dragging=false; Point ptmouseinit, ptforminit; Size parsizeinit; protected override void OnMouseDown(MouseEventArgs e) { ptmouseinit=MousePosition; ptforminit=this.Location; parsizeinit = par.Size; dragging=true; } // ========================================================================= protected override void OnMouseMove(MouseEventArgs e) { if (!dragging) {return; } Point ptmouse=MousePosition; Point mousedevi = pointdevi(ptmouse, ptmouseinit); this.Location=pointadd(ptforminit, mousedevi); par.setSize(sizeadd(parsizeinit, mousedevi)); } // ========================================================================= protected override void OnMouseUp(MouseEventArgs e) { Point ptmouse=MousePosition; Point mousedevi = pointdevi(ptmouse, ptmouseinit); this.Location=pointadd(ptforminit, mousedevi); par.setSize(sizeadd(parsizeinit, mousedevi)); dragging=false; par.setchildren(); par.Refresh(); } // ========================================================================= protected override void OnPaint(PaintEventArgs e) { Graphics g = e.Graphics; Pen pen = new Pen(Color.Black); for (int i=3; i<=15; i+=4) { g.DrawLine(pen, 18 - i, 18, 18, 18 - i); } } // ========================================================================= public static Point pointadd(object a, object b) { Tuple ta=gettwo(a), tb=gettwo(b); Point point = getPoint(addtwo(ta, tb)); return point; } // ========================================================================= static Point pointdevi(object a, object b) { Tuple ta=gettwo(a), tb=gettwo(b); Point point = getPoint(subttwo(ta, tb)); return point; } // ========================================================================= public static Size sizeadd(object a, object b) { Tuple ta=gettwo(a), tb=gettwo(b); Size size = getSize(addtwo(ta, tb)); return size; } // ========================================================================= static Point getPoint(Tuple a) { return new Point(a.Item1, a.Item2); } // ========================================================================= static Size getSize(Tuple a) { return new Size(a.Item1, a.Item2); } // ========================================================================= static Tuple addtwo(Tuple a, Tuple b) { return new Tuple(a.Item1 + b.Item1, a.Item2 + b.Item2); } // ========================================================================= static Tuple subttwo(Tuple a, Tuple b) { return new Tuple(a.Item1 - b.Item1, a.Item2 - b.Item2); } // ========================================================================= static Tuple gettwo(object a) { // === object should be either Point or Size int ax=0, ay=0; if (a is Point) {ax=((Point)a).X; ay=((Point)a).Y; } else if (a is Size) { ax = ((Size)a).Width; ay = ((Size)a).Height; } return new Tuple(ax, ay); } // ========================================================================= } // #############################################################################