Csharp/C Sharp/GUI Windows Form/ToolBar — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
(нет различий)
|
Текущая версия на 14:33, 26 мая 2010
Содержание
Floating Toolbar
<source lang="csharp"> /* User Interfaces in C#: Windows Forms and Custom Controls by Matthew MacDonald Publisher: Apress ISBN: 1590590457
- /
using System; using System.Drawing; using System.Collections; using System.ruponentModel; using System.Windows.Forms; using System.Data; namespace FloatingToolbar {
/// <summary> /// Summary description for FloatingToolbar. /// </summary> public class FloatingToolbar : System.Windows.Forms.Form { internal System.Windows.Forms.ImageList imgButtons; internal System.Windows.Forms.ToolBar toolBar1; internal System.Windows.Forms.ToolBarButton cmdNew; internal System.Windows.Forms.ToolBarButton cmdOpen; internal System.Windows.Forms.ToolBarButton cmdClose; internal System.Windows.Forms.ToolBarButton cmdSave; internal System.Windows.Forms.ToolBarButton ToolBarButton1; internal System.Windows.Forms.ToolBarButton cmdPreview; private System.ruponentModel.IContainer components; public FloatingToolbar() { // // Required for Windows Form Designer support // InitializeComponent(); // // TODO: Add any constructor code after InitializeComponent call // } /// <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.ruponents = new System.ruponentModel.Container(); System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(FloatingToolbar)); this.imgButtons = new System.Windows.Forms.ImageList(this.ruponents); this.toolBar1 = new System.Windows.Forms.ToolBar(); this.cmdNew = new System.Windows.Forms.ToolBarButton(); this.cmdOpen = new System.Windows.Forms.ToolBarButton(); this.cmdClose = new System.Windows.Forms.ToolBarButton(); this.cmdSave = new System.Windows.Forms.ToolBarButton(); this.ToolBarButton1 = new System.Windows.Forms.ToolBarButton(); this.cmdPreview = new System.Windows.Forms.ToolBarButton(); this.SuspendLayout(); // // imgButtons // this.imgButtons.ColorDepth = System.Windows.Forms.ColorDepth.Depth8Bit; this.imgButtons.ImageSize = new System.Drawing.Size(16, 16); this.imgButtons.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imgButtons.ImageStream"))); this.imgButtons.TransparentColor = System.Drawing.Color.Transparent; // // toolBar1 // this.toolBar1.Appearance = System.Windows.Forms.ToolBarAppearance.Flat; this.toolBar1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D; this.toolBar1.Buttons.AddRange(new System.Windows.Forms.ToolBarButton[] { this.cmdNew, this.cmdOpen, this.cmdClose, this.cmdSave, this.ToolBarButton1, this.cmdPreview}); this.toolBar1.DropDownArrows = true; this.toolBar1.ImageList = this.imgButtons; this.toolBar1.Name = "toolBar1"; this.toolBar1.ShowToolTips = true; this.toolBar1.Size = new System.Drawing.Size(292, 41); this.toolBar1.TabIndex = 5; this.toolBar1.MouseUp += new System.Windows.Forms.MouseEventHandler(this.toolBar1_MouseUp); this.toolBar1.MouseMove += new System.Windows.Forms.MouseEventHandler(this.toolBar1_MouseMove); this.toolBar1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.toolBar1_MouseDown); // // cmdNew // this.cmdNew.ImageIndex = 3; this.cmdNew.Text = "New"; // // cmdOpen // this.cmdOpen.ImageIndex = 0; this.cmdOpen.Text = "Open"; // // cmdClose // this.cmdClose.ImageIndex = 1; this.cmdClose.Text = "Close"; // // cmdSave // this.cmdSave.ImageIndex = 2; this.cmdSave.Text = "Save"; // // ToolBarButton1 // this.ToolBarButton1.Style = System.Windows.Forms.ToolBarButtonStyle.Separator; // // cmdPreview // this.cmdPreview.ImageIndex = 4; this.cmdPreview.Text = "Preview"; // // FloatingToolbar // this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(292, 266); this.Controls.AddRange(new System.Windows.Forms.Control[] { this.toolBar1}); this.IsMdiContainer = true; this.Name = "FloatingToolbar"; this.Text = "Floating Toolbar"; this.ResumeLayout(false); } #endregion /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main() { Application.Run(new FloatingToolbar()); } private bool draggingToolbar; private Point draggedFrom;
private void toolBar1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e) { if (draggingToolbar) { //if (toolBar1.Dock == DockStyle.Top || toolBar1.Dock == DockStyle.Left) if (toolBar1.Dock == DockStyle.Top) { // Check it the dragging has reached the threshold. if (draggedFrom.X < (e.X - 20) || draggedFrom.Y < (e.Y - 20)) { draggingToolbar = false; // Disconnect the toolbar. toolBar1.Dock = DockStyle.None; toolBar1.Location = new Point(10, 10); toolBar1.Size = new Size(200, 100); toolBar1.BorderStyle = BorderStyle.FixedSingle; } } else if (toolBar1.Dock == DockStyle.None) { toolBar1.Left = e.X + toolBar1.Left - draggedFrom.X; toolBar1.Top = e.Y + toolBar1.Top - draggedFrom.Y; if (toolBar1.Top < 5) { draggingToolbar = false; // Re-dock the control. toolBar1.Dock = DockStyle.Top; toolBar1.BorderStyle = BorderStyle.None; } else if (toolBar1.Left < 5) { draggingToolbar = false; // Re-dock the control. toolBar1.Dock = DockStyle.Left; toolBar1.BorderStyle = BorderStyle.None; } } } } private void toolBar1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e) { draggingToolbar = true; draggedFrom = new Point(e.X, e.Y); toolBar1.Capture = true; } private void toolBar1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e) { draggingToolbar = false; toolBar1.Capture = false; } }
}
</source>
<A href="http://www.nfex.ru/Code/CSharpDownload/FloatingToolbar.zip">FloatingToolbar.zip( 30 k)</a>
ImageList for ToolBar
<source lang="csharp"> using System; using System.Drawing; using System.Windows.Forms;
class SimpleToolBar: Form {
public static void Main() { Application.Run(new SimpleToolBar()); } public SimpleToolBar() { Menu = new MainMenu(); Menu.MenuItems.Add("File"); Menu.MenuItems.Add("Edit"); Menu.MenuItems.Add("View"); Menu.MenuItems.Add("Help"); Bitmap bm = new Bitmap(GetType(),"SimpleToolBar.bmp"); ImageList imglst = new ImageList(); imglst.Images.AddStrip(bm); imglst.TransparentColor = Color.Cyan; ToolBar tbar = new ToolBar(); tbar.Parent = this; tbar.ImageList = imglst; tbar.ShowToolTips = true; string[] astr = {"New", "Open", "Save", "Print", "Cut", "Copy", "Paste" }; for (int i = 0; i < 7; i++) { ToolBarButton tbarbtn = new ToolBarButton(); tbarbtn.ImageIndex = i; tbarbtn.ToolTipText = astr[i]; tbar.Buttons.Add(tbarbtn); } }
}
</source>
ToolBar Example
<source lang="csharp"> /* User Interfaces in C#: Windows Forms and Custom Controls by Matthew MacDonald Publisher: Apress ISBN: 1590590457
- /
using System; using System.Drawing; using System.Collections; using System.ruponentModel; using System.Windows.Forms; using System.Data;
/// <summary> /// Summary description for ToolBarExample. /// </summary> public class ToolBarExample : System.Windows.Forms.Form { internal System.Windows.Forms.ContextMenu ContextMenu1; internal System.Windows.Forms.MenuItem MenuItem1; internal System.Windows.Forms.MenuItem MenuItem2; internal System.Windows.Forms.MenuItem MenuItem3; internal System.Windows.Forms.ImageList imagesToolBar; internal System.Windows.Forms.ImageList imagesLarge; internal System.Windows.Forms.ToolBar ToolBar4; internal System.Windows.Forms.ToolBarButton ToolBarButton13; internal System.Windows.Forms.ToolBarButton ToolBarButton16; internal System.Windows.Forms.ToolBar ToolBar3; internal System.Windows.Forms.ToolBarButton ToolBarButton8; internal System.Windows.Forms.ToolBarButton ToolBarButton9; internal System.Windows.Forms.ToolBarButton ToolBarButton12; internal System.Windows.Forms.ToolBar ToolBar2; internal System.Windows.Forms.ToolBarButton ToolBarButton4; internal System.Windows.Forms.ToolBarButton ToolBarButton5; internal System.Windows.Forms.ToolBarButton ToolBarButton6; internal System.Windows.Forms.ToolBarButton ToolBarButton7; internal System.Windows.Forms.ToolBar ToolBar1; internal System.Windows.Forms.ToolBarButton ToolBarButton1; internal System.Windows.Forms.ToolBarButton ToolBarButton2; internal System.Windows.Forms.ToolBarButton ToolBarButton3; internal System.Windows.Forms.ToolBarButton ToolBarButton10; internal System.Windows.Forms.Label Label4; internal System.Windows.Forms.Label Label3; internal System.Windows.Forms.Label Label2; internal System.Windows.Forms.Label Label1; private System.ruponentModel.IContainer components; public ToolBarExample() { // // Required for Windows Form Designer support // InitializeComponent(); // // TODO: Add any constructor code after InitializeComponent call // } /// <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.ruponents = new System.ruponentModel.Container(); System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(ToolBarExample)); this.ContextMenu1 = new System.Windows.Forms.ContextMenu(); this.MenuItem1 = new System.Windows.Forms.MenuItem(); this.MenuItem2 = new System.Windows.Forms.MenuItem(); this.MenuItem3 = new System.Windows.Forms.MenuItem(); this.imagesToolBar = new System.Windows.Forms.ImageList(this.ruponents); this.imagesLarge = new System.Windows.Forms.ImageList(this.ruponents); this.ToolBar4 = new System.Windows.Forms.ToolBar(); this.ToolBarButton13 = new System.Windows.Forms.ToolBarButton(); this.ToolBarButton16 = new System.Windows.Forms.ToolBarButton(); this.ToolBar3 = new System.Windows.Forms.ToolBar(); this.ToolBarButton8 = new System.Windows.Forms.ToolBarButton(); this.ToolBarButton9 = new System.Windows.Forms.ToolBarButton(); this.ToolBarButton12 = new System.Windows.Forms.ToolBarButton(); this.ToolBar2 = new System.Windows.Forms.ToolBar(); this.ToolBarButton4 = new System.Windows.Forms.ToolBarButton(); this.ToolBarButton5 = new System.Windows.Forms.ToolBarButton(); this.ToolBarButton6 = new System.Windows.Forms.ToolBarButton(); this.ToolBarButton7 = new System.Windows.Forms.ToolBarButton(); this.ToolBar1 = new System.Windows.Forms.ToolBar(); this.ToolBarButton1 = new System.Windows.Forms.ToolBarButton(); this.ToolBarButton2 = new System.Windows.Forms.ToolBarButton(); this.ToolBarButton3 = new System.Windows.Forms.ToolBarButton(); this.ToolBarButton10 = new System.Windows.Forms.ToolBarButton(); this.Label4 = new System.Windows.Forms.Label(); this.Label3 = new System.Windows.Forms.Label(); this.Label2 = new System.Windows.Forms.Label(); this.Label1 = new System.Windows.Forms.Label(); this.SuspendLayout(); // // ContextMenu1 // this.ContextMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] { this.MenuItem1, this.MenuItem2, this.MenuItem3}); // // MenuItem1 // this.MenuItem1.Index = 0; this.MenuItem1.Text = "Menu 1"; // // MenuItem2 // this.MenuItem2.Index = 1; this.MenuItem2.Text = "Menu 2"; // // MenuItem3 // this.MenuItem3.Index = 2; this.MenuItem3.Text = "Menu 3"; // // imagesToolBar // this.imagesToolBar.ColorDepth = System.Windows.Forms.ColorDepth.Depth8Bit; this.imagesToolBar.ImageSize = new System.Drawing.Size(16, 16); this.imagesToolBar.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imagesToolBar.ImageStream"))); this.imagesToolBar.TransparentColor = System.Drawing.Color.Transparent; // // imagesLarge // this.imagesLarge.ColorDepth = System.Windows.Forms.ColorDepth.Depth8Bit; this.imagesLarge.ImageSize = new System.Drawing.Size(32, 32); this.imagesLarge.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imagesLarge.ImageStream"))); this.imagesLarge.TransparentColor = System.Drawing.Color.Transparent; // // ToolBar4 // this.ToolBar4.Appearance = System.Windows.Forms.ToolBarAppearance.Flat; this.ToolBar4.Buttons.AddRange(new System.Windows.Forms.ToolBarButton[] { this.ToolBarButton13, this.ToolBarButton16}); this.ToolBar4.DropDownArrows = true; this.ToolBar4.ImageList = this.imagesToolBar; this.ToolBar4.Location = new System.Drawing.Point(0, 89); this.ToolBar4.Name = "ToolBar4"; this.ToolBar4.ShowToolTips = true; this.ToolBar4.Size = new System.Drawing.Size(344, 25); this.ToolBar4.TabIndex = 9; this.ToolBar4.TextAlign = System.Windows.Forms.ToolBarTextAlign.Right; // // ToolBarButton13 // this.ToolBarButton13.DropDownMenu = this.ContextMenu1; this.ToolBarButton13.ImageIndex = 0; this.ToolBarButton13.Style = System.Windows.Forms.ToolBarButtonStyle.DropDownButton; this.ToolBarButton13.Text = "One"; // // ToolBarButton16 // this.ToolBarButton16.Style = System.Windows.Forms.ToolBarButtonStyle.Separator; // // ToolBar3 // this.ToolBar3.Appearance = System.Windows.Forms.ToolBarAppearance.Flat; this.ToolBar3.Buttons.AddRange(new System.Windows.Forms.ToolBarButton[] { this.ToolBarButton8, this.ToolBarButton9, this.ToolBarButton12}); this.ToolBar3.DropDownArrows = true; this.ToolBar3.ImageList = this.imagesToolBar; this.ToolBar3.Location = new System.Drawing.Point(0, 64); this.ToolBar3.Name = "ToolBar3"; this.ToolBar3.ShowToolTips = true; this.ToolBar3.Size = new System.Drawing.Size(344, 25); this.ToolBar3.TabIndex = 8; this.ToolBar3.TextAlign = System.Windows.Forms.ToolBarTextAlign.Right; // // ToolBarButton8 // this.ToolBarButton8.ImageIndex = 0; this.ToolBarButton8.Pushed = true; this.ToolBarButton8.Style = System.Windows.Forms.ToolBarButtonStyle.ToggleButton; this.ToolBarButton8.Text = "One"; // // ToolBarButton9 // this.ToolBarButton9.ImageIndex = 1; this.ToolBarButton9.Style = System.Windows.Forms.ToolBarButtonStyle.ToggleButton; this.ToolBarButton9.Text = "Two"; // // ToolBarButton12 // this.ToolBarButton12.Style = System.Windows.Forms.ToolBarButtonStyle.Separator; // // ToolBar2 // this.ToolBar2.Appearance = System.Windows.Forms.ToolBarAppearance.Flat; this.ToolBar2.Buttons.AddRange(new System.Windows.Forms.ToolBarButton[] { this.ToolBarButton4, this.ToolBarButton5, this.ToolBarButton6, this.ToolBarButton7}); this.ToolBar2.DropDownArrows = true; this.ToolBar2.ImageList = this.imagesToolBar; this.ToolBar2.Location = new System.Drawing.Point(0, 39); this.ToolBar2.Name = "ToolBar2"; this.ToolBar2.ShowToolTips = true; this.ToolBar2.Size = new System.Drawing.Size(344, 25); this.ToolBar2.TabIndex = 7; this.ToolBar2.TextAlign = System.Windows.Forms.ToolBarTextAlign.Right; // // ToolBarButton4 // this.ToolBarButton4.ImageIndex = 0; this.ToolBarButton4.Text = "One"; // // ToolBarButton5 // this.ToolBarButton5.ImageIndex = 1; this.ToolBarButton5.Text = "Two"; // // ToolBarButton6 // this.ToolBarButton6.ImageIndex = 2; this.ToolBarButton6.Text = "Three"; // // ToolBarButton7 // this.ToolBarButton7.Style = System.Windows.Forms.ToolBarButtonStyle.Separator; // // ToolBar1 // this.ToolBar1.Appearance = System.Windows.Forms.ToolBarAppearance.Flat; this.ToolBar1.Buttons.AddRange(new System.Windows.Forms.ToolBarButton[] { this.ToolBarButton1, this.ToolBarButton2, this.ToolBarButton3, this.ToolBarButton10}); this.ToolBar1.DropDownArrows = true; this.ToolBar1.ImageList = this.imagesToolBar; this.ToolBar1.Name = "ToolBar1"; this.ToolBar1.ShowToolTips = true; this.ToolBar1.Size = new System.Drawing.Size(344, 39); this.ToolBar1.TabIndex = 6; // // ToolBarButton1 // this.ToolBarButton1.ImageIndex = 0; this.ToolBarButton1.Text = "One"; // // ToolBarButton2 // this.ToolBarButton2.ImageIndex = 1; this.ToolBarButton2.Text = "Two"; // // ToolBarButton3 // this.ToolBarButton3.ImageIndex = 2; this.ToolBarButton3.Text = "Three"; // // ToolBarButton10 // this.ToolBarButton10.Style = System.Windows.Forms.ToolBarButtonStyle.Separator; // // Label4 // this.Label4.ForeColor = System.Drawing.Color.Navy; this.Label4.Location = new System.Drawing.Point(104, 92); this.Label4.Name = "Label4"; this.Label4.Size = new System.Drawing.Size(112, 16); this.Label4.TabIndex = 13; this.Label4.Text = "Drop-Down Button"; // // Label3 // this.Label3.ForeColor = System.Drawing.Color.Navy; this.Label3.Location = new System.Drawing.Point(136, 68); this.Label3.Name = "Label3"; this.Label3.Size = new System.Drawing.Size(88, 16); this.Label3.TabIndex = 12; this.Label3.Text = "Toggle Buttons"; // // Label2 // this.Label2.ForeColor = System.Drawing.Color.Navy; this.Label2.Location = new System.Drawing.Point(192, 40); this.Label2.Name = "Label2"; this.Label2.Size = new System.Drawing.Size(72, 16); this.Label2.TabIndex = 11; this.Label2.Text = "Text on Right"; // // Label1 // this.Label1.ForeColor = System.Drawing.Color.Navy; this.Label1.Location = new System.Drawing.Point(128, 12); this.Label1.Name = "Label1"; this.Label1.Size = new System.Drawing.Size(64, 16); this.Label1.TabIndex = 10; this.Label1.Text = "Flat Buttons"; // // ToolBarExample // this.AutoScaleBaseSize = new System.Drawing.Size(5, 14); this.ClientSize = new System.Drawing.Size(344, 266); this.Controls.AddRange(new System.Windows.Forms.Control[] { this.Label4, this.Label3, this.Label2, this.Label1, this.ToolBar4, this.ToolBar3, this.ToolBar2, this.ToolBar1}); this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0))); this.Name = "ToolBarExample"; this.Text = "ToolBarExample"; this.ResumeLayout(false); } #endregion /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main() { Application.Run(new ToolBarExample()); } }
</source>
<A href="http://www.nfex.ru/Code/CSharpDownload/ToolBarExample.zip">ToolBarExample.zip( 17 k)</a>
ToolBar float window
<source lang="csharp"> using System; using System.Collections.Generic; using System.ruponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; public class Palette : Form {
private System.Windows.Forms.Label lblPictureThree; private System.Windows.Forms.Label lblPictureTwo; private System.Windows.Forms.Label lblPictureOne; public Palette() { InitializeComponent(); } private void lbl_MouseDown(object sender, MouseEventArgs e) { Label lbl = (Label)sender; lbl.DoDragDrop(lbl.Image, DragDropEffects.Copy); } private void InitializeComponent() { this.lblPictureThree = new System.Windows.Forms.Label(); this.lblPictureTwo = new System.Windows.Forms.Label(); this.lblPictureOne = new System.Windows.Forms.Label(); this.SuspendLayout(); // // lblPictureThree // this.lblPictureThree.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D; this.lblPictureThree.Image = new Bitmap("winter.jpg"); this.lblPictureThree.Location = new System.Drawing.Point(12, 113); this.lblPictureThree.Name = "lblPictureThree"; this.lblPictureThree.Size = new System.Drawing.Size(56, 48); this.lblPictureThree.TabIndex = 6; this.lblPictureThree.MouseDown += new System.Windows.Forms.MouseEventHandler(this.lbl_MouseDown); // // lblPictureTwo // this.lblPictureTwo.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D; this.lblPictureTwo.Image = new Bitmap("winter.jpg"); this.lblPictureTwo.Location = new System.Drawing.Point(12, 61); this.lblPictureTwo.Name = "lblPictureTwo"; this.lblPictureTwo.Size = new System.Drawing.Size(56, 48); this.lblPictureTwo.TabIndex = 5; this.lblPictureTwo.MouseDown += new System.Windows.Forms.MouseEventHandler(this.lbl_MouseDown); // // lblPictureOne // this.lblPictureOne.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D; this.lblPictureOne.Image = new Bitmap("winter.jpg"); this.lblPictureOne.Location = new System.Drawing.Point(12, 9); this.lblPictureOne.Name = "lblPictureOne"; this.lblPictureOne.Size = new System.Drawing.Size(56, 48); this.lblPictureOne.TabIndex = 4; this.lblPictureOne.MouseDown += new System.Windows.Forms.MouseEventHandler(this.lbl_MouseDown); // // Palette // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(83, 173); this.Controls.Add(this.lblPictureTwo); this.Controls.Add(this.lblPictureOne); this.Controls.Add(this.lblPictureThree); this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow; this.Name = "Palette"; this.ShowInTaskbar = false; this.Text = "Palette"; this.ResumeLayout(false); } [STAThread] static void Main() { Application.Run(new DrawingArea()); }
}
public class DrawingArea : Form { private System.Windows.Forms.PictureBox picDrawingArea; public DrawingArea() { InitializeComponent(); } private void DrawingArea_Load(object sender, EventArgs e) { Palette frmTool = new Palette(); this.AddOwnedForm(frmTool); frmTool.Show(); picDrawingArea.AllowDrop = true; } private void picDrawingArea_DragEnter(object sender, DragEventArgs e) { if (e.Data.GetDataPresent(DataFormats.Bitmap)) { e.Effect = DragDropEffects.Copy; } else { e.Effect = DragDropEffects.None; } } private void picDrawingArea_DragDrop(object sender, DragEventArgs e) { Graphics g = picDrawingArea.CreateGraphics(); g.DrawImage((Image)e.Data.GetData(DataFormats.Bitmap), new Point(e.X - this.Left, e.Y - this.Top)); } private void InitializeComponent() { this.picDrawingArea = new System.Windows.Forms.PictureBox(); ((System.ruponentModel.ISupportInitialize)(this.picDrawingArea)).BeginInit(); this.SuspendLayout(); // // picDrawingArea // this.picDrawingArea.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right))); this.picDrawingArea.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D; this.picDrawingArea.Location = new System.Drawing.Point(1, 2); this.picDrawingArea.Name = "picDrawingArea"; this.picDrawingArea.Size = new System.Drawing.Size(377, 270); this.picDrawingArea.TabIndex = 2; this.picDrawingArea.TabStop = false; this.picDrawingArea.DragDrop += new System.Windows.Forms.DragEventHandler(this.picDrawingArea_DragDrop); this.picDrawingArea.DragEnter += new System.Windows.Forms.DragEventHandler(this.picDrawingArea_DragEnter); // // DrawingArea // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(379, 274); this.Controls.Add(this.picDrawingArea); this.Name = "DrawingArea"; this.Text = "Drawing Area"; this.Load += new System.EventHandler(this.DrawingArea_Load); ((System.ruponentModel.ISupportInitialize)(this.picDrawingArea)).EndInit(); this.ResumeLayout(false); } } </source>
Using ToolBarButton
<source lang="csharp">
using System; using System.Drawing; using System.Windows.Forms;
class ToggleButtons: Form {
protected Panel panel = new Panel(); protected ToolBar tbar; protected string strText = "Toggle"; protected Color clrText = SystemColors.WindowText; FontStyle fontstyle = FontStyle.Regular; public static void Main() { Application.Run(new ToggleButtons()); } public ToggleButtons() { panel.Parent = this; panel.Dock = DockStyle.Fill; panel.BackColor = SystemColors.Window; panel.ForeColor = SystemColors.WindowText; panel.Resize += new EventHandler(PanelOnResize); panel.Paint += new PaintEventHandler(PanelOnPaint); Bitmap bm = new Bitmap(GetType(), "ToggleButtons.bmp"); ImageList imglst = new ImageList(); imglst.ImageSize = new Size(bm.Width / 4, bm.Height); imglst.Images.AddStrip(bm); imglst.TransparentColor = Color.White; tbar = new ToolBar(); tbar.ImageList = imglst; tbar.Parent = this; tbar.ShowToolTips = true; tbar.ButtonClick += new ToolBarButtonClickEventHandler(ToolBarOnClick); FontStyle[] afs = { FontStyle.Bold, FontStyle.Italic, FontStyle.Underline, FontStyle.Strikeout }; for (int i = 0; i < 4; i++) { ToolBarButton tbarbtn = new ToolBarButton(); tbarbtn.ImageIndex = i; tbarbtn.Style = ToolBarButtonStyle.ToggleButton; tbarbtn.ToolTipText = afs[i].ToString(); tbarbtn.Tag = afs[i]; tbar.Buttons.Add(tbarbtn); } } void ToolBarOnClick(object obj, ToolBarButtonClickEventArgs tbbcea) { ToolBarButton tbarbtn = tbbcea.Button; if (tbarbtn.Tag == null || tbarbtn.Tag.GetType() != typeof(FontStyle)) return; if (tbarbtn.Pushed) fontstyle |= (FontStyle) tbarbtn.Tag; else fontstyle &= ~(FontStyle) tbarbtn.Tag; panel.Invalidate(); } void PanelOnResize(object obj, EventArgs ea) { Panel panel = (Panel) obj; panel.Invalidate(); } void PanelOnPaint(object obj, PaintEventArgs pea) { Panel panel = (Panel) obj; Graphics grfx = pea.Graphics; Font font = new Font("Times New Roman", 72, fontstyle); SizeF sizef = grfx.MeasureString(strText, font); grfx.DrawString(strText, font, new SolidBrush(clrText), (panel.Width - sizef.Width) / 2, (panel.Height - sizef.Height) / 2); }
}
</source>