1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213: 214: 215: 216: 217: 218: 219: 220: 221: 222: 223: 224: 225: 226: 227: 228: 229: 230: 231: 232: 233: 234: 235: 236: 237: 238: 239: 240: 241: 242: 243: 244: 245: 246: 247: 248: 249: 250: 251: 252: 253: 254: 255: 256: 257: 258: 259: 260: 261: 262: 263: 264: 265: 266: 267: 268: 269: 270: 271: 272: 273: 274: 275: 276: 277: 278: 279: 280: 281: 282: 283: 284: 285: 286: 287: 288: 289: 290: 291: 292: 293: 294: 295: 296: 297: 298: 299: 300: 301: 302: 303: 304: 305: 306: 307: 308: 309: 310: 311: 312: 313: 314: 315: 316: 317: 318: 319: 320: 321: 322: 323: 324: 325: 326: 327: 328: 329: 330: 331: 332: 333: 334: 335: 336: 337: 338: 339: 340: 341: 342: 343: 344: 345: 346: 347: 348: 349: 350: 351: 352: 353: 354: 355: 356: 357: 358: 359: 360: 361: 362: 363: 364: 365: 366: 367: 368: 369: 370: 371: 372: 373: 374: 375: 376: 377: 378: 379: 380: 381: 382: 383: 384: 385: 386: 387: 388: 389: 390: 391: 392: 393: 394: 395: 396: 397: 398: 399: 400: 401: 402: 403: 404: 405: 406: 407: 408: 409: 410: 411: 412: 413: 414: 415: 416: 417: 418: 419: 420: 421: 422: 423: 424: 425: 426: 427: 428: 429: 430: 431: 432: 433: 434: 435: 436: 437: 438: 439: 440: 441: 442: 443: 444: 445: 446: 447: 448: 449: 450: 451: 452: 453: 454: 455: 456: 457: 458: 459: 460: 461: 462: 463: 464: 465: 466: 467: 468: 469: 470: 471: 472: 473: 474: 475: 476: 477: 478: 479: 480: 481: 482: 483: 484: 485: 486: 487: 488: 489: 490: 491: 492: 493: 494: 495: 496: 497: 498: 499: 500: 501: 502: 503: 504: 505: 506: 507: 508: 509: 510: 511: 512: 513: 514: 515: 516: 517: 518: 519: 520: 521: 522: 523: 524: 525: 526: 527: 528: 529: 530: 531: 532: 533: 534: 535: 536: 537: 538: 539: 540: 541: 542: 543: 544:
| using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows.Forms; using System.Drawing; using System.ComponentModel; using System.Runtime.InteropServices;
namespace Orbit_FTP_Client { class FormStyle:Form { #region ContextMenuTaskbar const Int32 SC_SIZE = 0xf000; const Int32 SC_MOVE = 0xf010; const Int32 SC_MINIMIZE = 0xf020; const Int32 SC_MAXIMIZE = 0xf030; const Int32 SC_CLOSE = 0xf060; const Int32 SC_RESTORE = 0xf120; const Int32 MF_BYCOMMAND = 0x0; const Int32 MF_SEPARATOR = 0x800; private const int HTCAPTION = 2; private const int TPM_RETURNCMD = 0x100; private const int WM_SYSCOMMAND = 0x112;
[DllImport("user32.dll", EntryPoint = "EnableMenuItem")] private static extern int EnableMenuItem(IntPtr hMenu, int wIDEnableItem, int wEnable);
[DllImport("user32.dll", EntryPoint = "ReleaseCapture")] private static extern int ReleaseCapture();
[DllImport("user32.dll", EntryPoint = "GetSystemMenu")] private static extern IntPtr GetSystemMenu(IntPtr hwnd, int bRevert);
[DllImport("user32.dll", EntryPoint = "TrackPopupMenuEx")] private static extern int TrackPopupMenuEx(IntPtr hMenu, int wFlags, int x, int y, IntPtr hWnd, int lpTpm);
protected override CreateParams CreateParams { get { const int WS_SYSMENU = 0x80000; const int WS_MAXIMIZEBOX = 0x10000; const int WS_MINIMIZEBOX = 0x20000; CreateParams oReturn = base.CreateParams; oReturn.Style = oReturn.Style | WS_SYSMENU | WS_MAXIMIZEBOX | WS_MINIMIZEBOX; return oReturn; } } #endregion
#region Global Vars private Size titlebarLeft; private Size titlebarRight; private Size titlebarMid; private Size borderLeft; private Size borderRight; private Size borderBottom; private Size borderCornerLeft; private Size borderCornerRight; private Size closeButton; private Size maximizeButton; private Size minimizeButton; private Size closeButton_tool;
private Font TextFont_; private Color TextColor_;
private Image BG_Image = null; private Color BG_Color;
private WindowStyle_ curWinStyle = WindowStyle_.Sizeable;
private CursorState curstate_ = CursorState.Normal; private CursorState chSstate_ = CursorState.Normal;
private ButtonState closeButtonState = ButtonState.Normal; private ButtonState minimizeButtonState = ButtonState.Normal; private ButtonState maximizeButtonState = ButtonState.Normal;
private bool move_ = false; private bool mousedown_ = false;
private Point oldpos_; private bool mov_chS = false; private bool chS = false; private Point oldformpos_;
private Point oldscreenpos_; private Size oldsize_; #endregion
#region Enums public enum CursorState { TopLeft, TopMid, TopRight, BorderRight, BorderCornerRight, BorderBottom, BorderCornerLeft, BorderLeft, Normal } public enum ButtonState { Normal, Over, Down } public enum WindowStyle_ { ToolBox, MSG_Box, Sizeable, Fixed } #endregion
#region properties public Font Font_Title { get { return TextFont_; }
set { TextFont_ = value; } }
public Color ForeColor_Title { get { return TextColor_; }
set { TextColor_ = value; } }
public Image BackGroundImage_Form { get { return BG_Image; } set { BG_Image = value; } }
public Color BackGroundColor_Form { get { return BG_Color; }
set { BG_Color = value; } }
public WindowStyle_ Fensterstyle { get { return curWinStyle; }
set { curWinStyle = value; } } protected override System.Windows.Forms.Padding DefaultPadding { get { return new Padding(borderLeft.Width, titlebarMid.Height, borderRight.Width, borderBottom.Height); } }
[EditorBrowsable(EditorBrowsableState.Never)] [Browsable(false)] public override System.Drawing.Color BackColor { get { return base.BackColor; } set { base.BackColor = this.TransparencyKey; } }
[EditorBrowsable(EditorBrowsableState.Never)] [Browsable(false)] public override System.Drawing.Image BackgroundImage { get { return null; }
set { } } #endregion
#region Initalisiren public void New() { this.SetStyle(ControlStyles.SupportsTransparentBackColor, true); this.DoubleBuffered = true; this.FormBorderStyle = FormBorderStyle.None; BG_Color = SystemColors.Control; this.BackColor = Color.LavenderBlush; this.TransparencyKey = Color.LavenderBlush; this.MaximizedBounds = SystemInformation.WorkingArea; titlebarLeft = Properties.Resources.titlebarLeft.Size; titlebarMid = new Size(this.Width - titlebarLeft.Width - titlebarRight.Width +2,titlebarRight.Height); titlebarRight = Properties.Resources.titlebarRight.Size; borderRight = new Size(7,this.Height-borderBottom.Height-titlebarLeft.Height +2); borderCornerRight = Properties.Resources.borderCornerRight.Size; borderBottom = new Size(this.Width, 7); borderCornerLeft = Properties.Resources.borderCornerLeft.Size; borderLeft = new Size(7, this.Height - titlebarMid.Height - borderCornerLeft.Height); closeButton = Properties.Resources.closeNormal.Size; closeButton_tool = Properties.Resources.closeNormal_tool.Size; maximizeButton = Properties.Resources.maximizeNormal.Size; minimizeButton = Properties.Resources.minimizeNorm.Size; TextColor_ = Color.Black; try { TextFont_ = new Font("Arial", 9, FontStyle.Regular); } catch { TextFont_ = this.Font; } } private void InitializeComponent() { this.SuspendLayout(); this.ClientSize = new System.Drawing.Size(284, 262); this.Name = "FormStyle"; this.Paint += new System.Windows.Forms.PaintEventHandler(this.FormStyle_Paint); this.ResumeLayout(false);
} #endregion
#region Zeichnen
private void DrawSizableWindow(object sender, PaintEventArgs e) { Graphics g = e.Graphics; SolidBrush borderColor_ = new SolidBrush(Color.FromArgb(102, 102, 102)); Rectangle B_Top = new Rectangle(titlebarLeft.Width -1,0,this.Width-titlebarLeft.Width-titlebarRight.Width +2,1); Rectangle T_Top = new Rectangle(titlebarLeft.Width - 1, 1, this.Width - titlebarLeft.Width - titlebarRight.Width + 2,6); Rectangle B_Bottom = new Rectangle(borderCornerLeft.Width - 1, this.Height - 1, this.Width - borderCornerLeft.Width - borderCornerRight.Width + 2, 1); Rectangle T_Bottom = new Rectangle(borderCornerLeft.Width - 1, this.Height - 7, this.Width - borderCornerLeft.Width - borderCornerRight.Width + 2, 6); Rectangle B_Right = new Rectangle(0,titlebarLeft.Height -1,1,this.Height-titlebarLeft.Height-borderCornerLeft.Height +2); Rectangle T_Right = new Rectangle(7, titlebarLeft.Height - 1, 6, this.Height - titlebarLeft.Height - borderCornerLeft.Height + 2); Rectangle B_Left = new Rectangle(this.Width - 1, titlebarRight.Height - 1, 1, this.Height - titlebarRight.Height - borderCornerRight.Height + 2); Rectangle T_Left = new Rectangle (this.Width - 7, titlebarRight.Height - 1, 6, this.Height - titlebarRight.Height - borderCornerRight.Height + 2); g.FillRectangle(borderColor_, B_Top); g.FillRectangle(borderColor_, B_Right); g.FillRectangle(borderColor_, B_Left); g.FillRectangle(borderColor_, B_Bottom);
g.FillRectangle(Brushes.White, T_Bottom); g.FillRectangle(Brushes.White, T_Left); g.FillRectangle(Brushes.White, T_Right); g.FillRectangle(Brushes.White, T_Top); }
private void FormStyle_Paint(object sender, PaintEventArgs e) { if(this.Fensterstyle == WindowStyle_.Sizeable) DrawSizableWindow(sender,e); } #endregion
#region Mouse Events
private void FormStyle_MouseDown(System.Object sender, System.Windows.Forms.MouseEventArgs e) { if (e.Button == MouseButtons.Left) { mousedown_ = true;
if (new Rectangle(0, borderCornerLeft.Height, titlebarLeft.Width, this.Height - borderCornerLeft.Height - borderCornerLeft.Height).Contains(e.X, e.Y)) { chSstate_ = CursorState.TopLeft; oldpos_ = e.Location; oldformpos_ = this.Location; oldscreenpos_ = PointToScreen(e.Location); oldsize_ = this.Size; } else if (new Rectangle(0, 0, borderCornerLeft.Width, borderCornerLeft.Height).Contains(e.X, e.Y)) { chSstate_ = CursorState.TopLeft; oldpos_ = e.Location; oldformpos_ = this.Location; oldscreenpos_ = PointToScreen(e.Location); oldsize_ = this.Size; } else if (new Rectangle(0, 0, this.Width - borderCornerLeft.Width, borderLeft.Width).Contains(e.X, e.Y)) { chSstate_ = CursorState.TopMid; oldpos_ = e.Location; oldformpos_ = this.Location; oldscreenpos_ = PointToScreen(e.Location); oldsize_ = this.Size; } else if (new Rectangle(this.Width - borderCornerLeft.Width, 0, borderCornerLeft.Width, borderCornerLeft.Height).Contains(e.X, e.Y)) { chSstate_ = CursorState.TopRight; oldpos_ = e.Location; oldformpos_ = this.Location; oldscreenpos_ = PointToScreen(e.Location); oldsize_ = this.Size; } else if (new Rectangle(this.Width - borderCornerRight.Width, borderCornerLeft.Height, borderCornerRight.Width, this.Height - 2 * borderCornerLeft.Height).Contains(e.X, e.Y)) { chSstate_ = CursorState.BorderCornerRight; oldpos_ = e.Location; oldformpos_ = this.Location; oldscreenpos_ = PointToScreen(e.Location); oldsize_ = this.Size; } else if (new Rectangle(this.Width - borderCornerLeft.Width, this.Height - borderCornerLeft.Height, borderCornerRight.Width, borderCornerRight.Height).Contains(e.X, e.Y)) { chSstate_ = CursorState.BorderCornerRight; oldpos_ = e.Location; oldformpos_ = this.Location; oldscreenpos_ = PointToScreen(e.Location); oldsize_ = this.Size; } else if (new Rectangle(borderCornerLeft.Width, this.Height - borderBottom.Height, this.Width - 2 * borderCornerLeft.Width, borderBottom.Height).Contains(e.X, e.Y)) { chSstate_ = CursorState.BorderBottom; oldpos_ = e.Location; oldformpos_ = this.Location; oldscreenpos_ = PointToScreen(e.Location); oldsize_ = this.Size; } else if (new Rectangle(0, this.Height - borderCornerLeft.Height, borderCornerLeft.Width, borderCornerLeft.Height).Contains(e.X, e.Y)) { chSstate_ = CursorState.BorderCornerLeft; oldpos_ = e.Location; oldformpos_ = this.Location; oldscreenpos_ = PointToScreen(e.Location); oldsize_ = this.Size; }
if (minimizeButtonState == ButtonState.Over) { minimizeButtonState = ButtonState.Down; Invalidate(); } else if (maximizeButtonState == ButtonState.Over) { maximizeButtonState = ButtonState.Down; Invalidate(); } else if (closeButtonState == ButtonState.Over) { closeButtonState = ButtonState.Down; Invalidate(); } } }
private void FormStyle_MouseLeave(System.Object sender, System.EventArgs e) { minimizeButtonState = ButtonState.Normal; maximizeButtonState = ButtonState.Normal; closeButtonState = ButtonState.Normal; Invalidate(); }
private void IT_Tech_NET_Formstyle_MouseMove(System.Object sender, System.Windows.Forms.MouseEventArgs e) { Point pts = PointToScreen(e.Location);
if (move_) { this.Location = new Point(this.Location.X + e.X - oldpos_.X, this.Location.Y + e.Y - oldpos_.Y); } else { if (!mousedown_) { if (new Rectangle(this.Width - titlebarRight.Width + 2 - closeButton.Width - maximizeButton.Width - minimizeButton.Width, 0, minimizeButton.Width, minimizeButton.Height).Contains(e.X, e.Y)) { this.Cursor = Cursors.Default; minimizeButtonState = ButtonState.Over; maximizeButtonState = ButtonState.Normal; closeButtonState = ButtonState.Normal; } else if (new Rectangle(this.Width - titlebarRight.Width + 2 - closeButton.Width - maximizeButton.Width, 0, maximizeButton.Width, maximizeButton.Height).Contains(e.X, e.Y)) { this.Cursor = Cursors.Default; maximizeButtonState = ButtonState.Over; minimizeButtonState = ButtonState.Normal; closeButtonState = ButtonState.Normal; } else if (new Rectangle(this.Width - titlebarRight.Width + 2 - closeButton.Width, 0, closeButton.Width, closeButton.Height).Contains(e.X, e.Y)) { this.Cursor = Cursors.Default; closeButtonState = ButtonState.Over; minimizeButtonState = ButtonState.Normal; maximizeButtonState = ButtonState.Normal; } else { closeButtonState = ButtonState.Normal; minimizeButtonState = ButtonState.Normal; maximizeButtonState = ButtonState.Normal; } Invalidate(); }
if (new Rectangle(0, borderCornerLeft.Height, borderLeft.Width, this.Height - borderCornerLeft.Height - borderCornerLeft.Height).Contains(e.X, e.Y)) { this.Cursor = Cursors.SizeWE; curstate_ = CursorState.BorderLeft; } else if (new Rectangle(0, 0, borderCornerLeft.Width, borderCornerLeft.Height).Contains(e.X, e.Y)) { this.Cursor = Cursors.SizeNWSE; curstate_ = CursorState.TopLeft; } else if (new Rectangle(borderCornerLeft.Width, 0, this.Width - borderCornerLeft.Width - titlebarRight.Width + 2 - closeButton.Width - maximizeButton.Width - minimizeButton.Width, borderLeft.Width).Contains(e.X, e.Y)) { this.Cursor = Cursors.SizeNS; curstate_ = CursorState.TopMid; } else if (new Rectangle(this.Width - borderCornerLeft.Width, 0, borderCornerLeft.Width, borderCornerLeft.Height).Contains(e.X, e.Y)) { this.Cursor = Cursors.SizeNESW; curstate_ = CursorState.TopRight; } else if (new Rectangle(this.Width - borderRight.Width, borderCornerLeft.Height, borderRight.Width, this.Height - 2 * borderCornerLeft.Height).Contains(e.X, e.Y)) { this.Cursor = Cursors.SizeWE; curstate_ = CursorState.BorderRight; } else if (new Rectangle(this.Width - borderCornerLeft.Width, this.Height - borderCornerLeft.Height, borderCornerRight.Width, borderCornerRight.Height).Contains(e.X, e.Y)) { this.Cursor = Cursors.SizeNWSE; curstate_ = CursorState.BorderCornerRight; } else if (new Rectangle(borderCornerLeft.Width, this.Height - borderBottom.Height, this.Width - 2 * borderCornerLeft.Width, borderBottom.Height).Contains(e.X, e.Y)) { this.Cursor = Cursors.SizeNS; curstate_ = CursorState.BorderBottom; } else if (new Rectangle(0, this.Height - borderCornerLeft.Height, borderCornerLeft.Width, borderCornerLeft.Height).Contains(e.X, e.Y)) { this.Cursor = Cursors.SizeNESW; curstate_ = CursorState.BorderCornerLeft; } else { this.Cursor = Cursors.Default; curstate_ = CursorState.Normal; } }
if (chSstate_ == CursorState.TopLeft) { this.Location = pts; this.Size = new Size(oldsize_.Width + oldscreenpos_.X - pts.X, oldsize_.Height + oldscreenpos_.Y - pts.Y); } else if (chSstate_ == CursorState.TopRight) { this.Location = new Point(oldformpos_.X, pts.Y); this.Size = new Size(oldsize_.Width + pts.X - oldscreenpos_.X, oldsize_.Height + oldformpos_.Y - this.Location.Y); } else if (chSstate_ == CursorState.BorderCornerLeft) { this.Location = new Point(oldformpos_.X - oldscreenpos_.X + pts.X, oldformpos_.Y); this.Size = new Size(oldsize_.Width - pts.X + oldscreenpos_.X, oldsize_.Height + pts.Y - oldscreenpos_.Y); } else if (chSstate_ == CursorState.BorderCornerRight) { this.Size = new Size(oldsize_.Width + pts.X - oldscreenpos_.X, oldsize_.Height + pts.Y - oldscreenpos_.Y); } else if (chSstate_ == CursorState.TopMid) { this.Location = new Point(oldformpos_.X, oldformpos_.Y + pts.Y - oldscreenpos_.Y); this.Size = new Size(oldsize_.Width, oldsize_.Height - pts.Y + oldscreenpos_.Y); } else if (chSstate_ == CursorState.BorderLeft) { this.Location = new Point(oldformpos_.X - oldscreenpos_.X + pts.X, oldformpos_.Y); this.Size = new Size(oldsize_.Width - pts.X + oldscreenpos_.X, oldsize_.Height); } else if (chSstate_ == CursorState.BorderBottom) { this.Size = new Size(oldsize_.Width, oldsize_.Height + pts.Y - oldscreenpos_.Y); } else if (chSstate_ == CursorState.BorderRight) { this.Size = new Size(oldsize_.Width + pts.X - oldscreenpos_.X, oldsize_.Height); } }
private void FormStyle_MouseUp(System.Object sender, System.Windows.Forms.MouseEventArgs e) { if (e.Button == MouseButtons.Left) { mousedown_ = false; move_ = false; mov_chS = false; chS = false; chSstate_ = CursorState.Normal;
if (closeButtonState == ButtonState.Down) { this.Close(); } else if ((minimizeButtonState == ButtonState.Down) & this.MinimizeBox) { this.WindowState = FormWindowState.Minimized; } else if ((maximizeButtonState == ButtonState.Down) & this.MaximizeBox) { if (this.WindowState == FormWindowState.Maximized) { this.WindowState = FormWindowState.Normal; } else if (this.WindowState == FormWindowState.Normal) { this.WindowState = FormWindowState.Maximized; } Invalidate(); } } }
private void FormStyle_MouseDoubleClick(System.Object sender, System.Windows.Forms.MouseEventArgs e) { if (e.Button == MouseButtons.Left) { if (new Rectangle(0, 0, this.Width - titlebarRight.Width, titlebarMid.Height).Contains(e.X, e.Y) & (curstate_ == CursorState.Normal)) { if (this.WindowState == FormWindowState.Normal) { this.WindowState = FormWindowState.Maximized; } else if (this.WindowState == FormWindowState.Maximized) { this.WindowState = FormWindowState.Normal; } Invalidate(); } else if (new Rectangle(this.Width - titlebarRight.Width, minimizeButton.Height, this.Width, 1).Contains(e.X, e.Y) & (curstate_ == CursorState.Normal)) { if (this.WindowState == FormWindowState.Normal) { this.WindowState = FormWindowState.Maximized; } else if (this.WindowState == FormWindowState.Maximized) { this.WindowState = FormWindowState.Normal; } Invalidate(); } } } #endregion
private Size ZoomSize(Size def, Size z) { double q = z.Width / z.Height; Size res = default(Size); if ((def.Width / def.Height) < q) { res = new Size(def.Width, Convert.ToInt32(def.Width / q)); } else if ((def.Width / def.Height) == q) { res = def; } else if ((def.Width / def.Height) > q) { res = new Size(Convert.ToInt32(def.Height * q), def.Height); } return res; }
} } |