how to disable part ToolStripSplitButton - c#

I would like to disable the button part of a ToolStripSplitButton in a c# winforms app. As far as I see it is not possible and I would like to avoid a complex solution (rewriting entire toolstripsplitbutton) so I'm trying to disable visually at least, ie. draw a grayed icon when button part disabled.
First I browsed referencesource and found that ToolStripRenderer and ToolStripProfessionalRenderer uses some 'internal' properties and methods in OnRenderItemImage(ToolStripItemImageRenderEventArgs e) so I cannot mimic (copy-and-modify-a-bit) the behaviour of OnRenderItemImage.
Next I tried the following code.
Basically it works, the toolStripSplitButton1 is grayed out when the Tag is boolean false.
But this solution kills all my System.Windows.Forms.Timer somehow! Try this code, when toolStripSplitButton1.Tag == false then the toolstrip-independent timer1 does not tick anymore. And the toolStripSplitButton1 tooltip does not show up (guess because it uses Timer as well).
(button1 and button1_Click is just for toggle toolStripSplitButton1.Tag)
My first question is why OnRenderItemImage kills all System.Windows.Forms.Timer?
Second question is how to achieve the original aim to gray out button icon at least visually independently of the button itself?
using System;
using System.Windows.Forms;
namespace WindowsFormsApplication
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
toolStrip1.Renderer = new MyToolStripProfessionalRenderer();
toolStripSplitButton1.Tag = false; // this is for disabling button part
toolStripSplitButton1.ToolTipText = "toolStripSplitButton1 ToolTip";
System.Windows.Forms.Timer timer1 = new Timer();
timer1.Interval = 1000;
timer1.Tick += T_Tick;
timer1.Start();
}
int ticks = 0;
private void T_Tick(object sender, EventArgs e)
{
System.Diagnostics.Debug.WriteLine(ticks++);
}
private void button1_Click(object sender, EventArgs e)
{
toolStripSplitButton1.Tag = !((bool)toolStripSplitButton1.Tag);
}
}
class MyToolStripProfessionalRenderer : ToolStripProfessionalRenderer
{
protected override void OnRenderItemImage(ToolStripItemImageRenderEventArgs e)
{
try
{
if (e.Item.Enabled &&
e.Item.Tag?.GetType() == typeof(bool) &&
!(bool)e.Item.Tag)
{
e.Item.Enabled = false;
base.OnRenderItemImage(e);
e.Item.Enabled = true;
}
else
base.OnRenderItemImage(e);
}
catch (Exception ex)
{
// this never reached, there's no exceptions
System.Diagnostics.Debug.WriteLine(ex);
}
}
}
}
Designer.cs:
namespace WindowsFormsApplication
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (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()
{
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1));
this.toolStrip1 = new System.Windows.Forms.ToolStrip();
this.toolStripSplitButton1 = new System.Windows.Forms.ToolStripSplitButton();
this.button1 = new System.Windows.Forms.Button();
this.toolStrip1.SuspendLayout();
this.SuspendLayout();
//
// toolStrip1
//
this.toolStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.toolStripSplitButton1});
this.toolStrip1.Location = new System.Drawing.Point(0, 0);
this.toolStrip1.Name = "toolStrip1";
this.toolStrip1.Size = new System.Drawing.Size(352, 25);
this.toolStrip1.TabIndex = 0;
this.toolStrip1.Text = "toolStrip1";
//
// toolStripSplitButton1
//
this.toolStripSplitButton1.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
this.toolStripSplitButton1.Image = ((System.Drawing.Image)(resources.GetObject("toolStripSplitButton1.Image")));
this.toolStripSplitButton1.ImageTransparentColor = System.Drawing.Color.Magenta;
this.toolStripSplitButton1.Name = "toolStripSplitButton1";
this.toolStripSplitButton1.Size = new System.Drawing.Size(32, 22);
this.toolStripSplitButton1.Text = "toolStripSplitButton1";
//
// button1
//
this.button1.Location = new System.Drawing.Point(13, 64);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 1;
this.button1.Text = "button1";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(352, 265);
this.Controls.Add(this.button1);
this.Controls.Add(this.toolStrip1);
this.Name = "Form1";
this.Text = "Form1";
this.toolStrip1.ResumeLayout(false);
this.toolStrip1.PerformLayout();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.ToolStrip toolStrip1;
private System.Windows.Forms.ToolStripSplitButton toolStripSplitButton1;
private System.Windows.Forms.Button button1;
}
}

I don't think that using a timer for tracking the change is a good idea. What I suggest is create a changeOccurred event and do the operations inside the event.

Related

Why aren't the generated buttons showing in the FlowLayoutPanel?

So, I'm trying to create an accordion with dynamically loaded buttons. In the future, the title of the buttons will change depending on the details I've retrieved from somewhere. For now, what I'm trying to do is to load buttons to look like these:
I've tried doing the following below:
// Forms1.cs
using System;
using System.Drawing;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
int buttonCount = 3;
var buttons = new FontAwesome.Sharp.IconButton[buttonCount];
for (int i = 0; i < buttonCount; i++)
{
var btn = new FontAwesome.Sharp.IconButton
{
Text = "Button " + i,
TextAlign = ContentAlignment.MiddleLeft,
IconChar = FontAwesome.Sharp.IconChar.Book,
IconColor = ColorTranslator.FromHtml("#6A6A73"),
IconSize = 20,
IconFont = FontAwesome.Sharp.IconFont.Auto,
TextImageRelation = TextImageRelation.ImageBeforeText,
FlatStyle = FlatStyle.Flat
};
btn.FlatAppearance.BorderSize = 0;
btn.ForeColor = ColorTranslator.FromHtml("#6A6A73");
btn.BackColor = ColorTranslator.FromHtml("#FDFEFF");
btn.Dock = DockStyle.Top;
buttons[i] = btn;
}
flowLayoutPanel1.Controls.AddRange(buttons);
}
}
}
// Forms1.Designer.cs
namespace WindowsFormsApp1
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (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.flowLayoutPanel1 = new System.Windows.Forms.FlowLayoutPanel();
this.SuspendLayout();
//
// flowLayoutPanel1
//
this.flowLayoutPanel1.BackColor = System.Drawing.SystemColors.ControlLight;
this.flowLayoutPanel1.Dock = System.Windows.Forms.DockStyle.Left;
this.flowLayoutPanel1.Location = new System.Drawing.Point(0, 0);
this.flowLayoutPanel1.Name = "flowLayoutPanel1";
this.flowLayoutPanel1.Size = new System.Drawing.Size(200, 450);
this.flowLayoutPanel1.TabIndex = 0;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(800, 450);
this.Controls.Add(this.flowLayoutPanel1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.FlowLayoutPanel flowLayoutPanel1;
}
}
Here's what it looks like after building and running the application:
What am I doing wrong?
Form1_Load method is not subscribed to Load event of your form. Body of InitializeComponent is missing following line of code.
this.Load += new System.EventHandler(this.Form1_Load);
Insert this line before this.ResumeLayout(false);. You can fix it in designer as well.

Access button from the form in other class in C#

I want to know if a button is pressed in C# from another class. I have my standard class Form and another class Device. Now I want to access the button from InitializeComponent in Form in my class Device. Does anyone know a good way to do this?
If I press on the btnInitialise I want to show a messagebox (with text "test") to start with. I want to use this button in the class Device. I don't rely know how I can reference the button btnInitialise that is automatically made in my form to the class Device.
public class Form1 : System.Windows.Forms.Form
{
#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.tabControl1 = new System.Windows.Forms.TabControl();
this.tabPage1 = new System.Windows.Forms.TabPage();
this.btnInitialise = new System.Windows.Forms.Button();
this.cmbdevice = new System.Windows.Forms.ComboBox();
this.tabControl1.SuspendLayout();
this.tabPage1.SuspendLayout();
this.SuspendLayout();
//
// tabControl1
//
this.tabControl1.Controls.Add(this.tabPage1);
this.tabControl1.Controls.Add(this.tabPage2);
this.tabControl1.Location = new System.Drawing.Point(42, 41);
this.tabControl1.Name = "tabControl1";
this.tabControl1.SelectedIndex = 0;
this.tabControl1.Size = new System.Drawing.Size(645, 414);
this.tabControl1.TabIndex = 0;
//
// tabPage1
//
this.tabPage1.Controls.Add(this.grpDevice);
this.tabPage1.Location = new System.Drawing.Point(4, 22);
this.tabPage1.Name = "tabPage1";
this.tabPage1.Padding = new System.Windows.Forms.Padding(3);
this.tabPage1.Size = new System.Drawing.Size(637, 388);
this.tabPage1.TabIndex = 0;
this.tabPage1.Text = "tabPage1";
this.tabPage1.UseVisualStyleBackColor = true;
//
// btnInitialise
//
this.btnInitialise.Location = new System.Drawing.Point(351, 16);
this.btnInitialise.Name = "btnInitialise";
this.btnInitialise.Size = new System.Drawing.Size(96, 25);
this.btnInitialise.TabIndex = 21;
this.btnInitialise.Text = "Initialize";
this.btnInitialise.Click += new System.EventHandler(this.btnInitialise_Click);
//
// Form1
//
this.ClientSize = new System.Drawing.Size(1005, 532);
this.Controls.Add(this.tabControl1);
this.Name = "Form1";
this.tabControl1.ResumeLayout(false);
this.tabPage1.ResumeLayout(false);
this.grpDevice.ResumeLayout(false);
this.grpDevice.PerformLayout();
this.ResumeLayout(false);
}
private TabControl tabControl1;
private TabPage tabPage1;
private Button btnInitialise;
#endregion "Windows Form Designer generated code"
#region "Global variables"
// OpenLayers fields
////Encapsulates a DT-open layers deviceand manages and distributes subsystems for the device
private Device device = null;
#endregion "Global variables"
//Automatically to initialize components of form
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//Set the culture to en-US for decimal point instead of decimal comma in results
CultureInfo english = new CultureInfo("en-US");
CultureInfo.DefaultThreadCurrentCulture = english;
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose(bool disposing)
{
if (disposing)
{
if (device != null)
{
device.Dispose();
}
}
base.Dispose(disposing);
}
/// <summary>
/// The main entry point for the application.
/// </summary>
///
//Run application, show error message if appl doesnt run
[STAThread]
private static void Main()
{
try
{
Application.Run(new Form1());
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString(), "Error");
}
}
}
public class Device
{
//When clicking on the initialize button show messagebox
private void btnInitialise_Click(object sender, EventArgs e)
{
MessageBox.Show("test");
}
}

Remove the title bar in Windows Forms

How can I remove the blue border that's on top of the Window Form? (I don't know the name of it exactly.)
You can set the Property FormBorderStyle to none in the designer,
or in code:
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
if by Blue Border thats on top of the Window Form you mean titlebar, set Forms ControlBox property to false and Text property to empty string ("").
here's a snippet:
this.ControlBox = false;
this.Text = String.Empty;

			
				
Also add this bit of code to your form to allow it to be draggable still.
Just add it right before the constructor (the method that calls InitializeComponent()
private const int WM_NCHITTEST = 0x84;
private const int HTCLIENT = 0x1;
private const int HTCAPTION = 0x2;
///
/// Handling the window messages
///
protected override void WndProc(ref Message message)
{
base.WndProc(ref message);
if (message.Msg == WM_NCHITTEST && (int)message.Result == HTCLIENT)
message.Result = (IntPtr)HTCAPTION;
}
That code is from: https://jachman.wordpress.com/2006/06/08/enhanced-drag-and-move-winforms-without-having-a-titlebar/
Now to get rid of the title bar but still have a border combine the code from the other response:
this.ControlBox = false;
this.Text = String.Empty;
with this line:
this.FormBorderStyle = FormBorderStyle.FixedSingle;
Put those 3 lines of code into the form's OnLoad event and you should have a nice 'floating' form that is draggable with a thin border (use FormBorderStyle.None if you want no border).
Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None
Set FormsBorderStyle of the Form to None.
If you do, it's up to you how to implement the dragging and closing functionality of the window.
I am sharing my code.
form1.cs:-
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace BorderExp
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
}
private void ExitClick(object sender, EventArgs e)
{
Application.Exit();
}
private void MaxClick(object sender, EventArgs e)
{
if (WindowState ==FormWindowState.Normal)
{
this.WindowState = FormWindowState.Maximized;
}
else
{
this.WindowState = FormWindowState.Normal;
}
}
private void MinClick(object sender, EventArgs e)
{
this.WindowState = FormWindowState.Minimized;
}
}
}
Now, the designer:-
namespace BorderExp
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (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.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.button1.BackColor = System.Drawing.SystemColors.ButtonFace;
this.button1.BackgroundImage = global::BorderExp.Properties.Resources.blank_1_;
this.button1.FlatAppearance.BorderSize = 0;
this.button1.FlatAppearance.MouseOverBackColor = System.Drawing.Color.FromArgb(((int)(((byte)(224)))), ((int)(((byte)(224)))), ((int)(((byte)(224)))));
this.button1.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.button1.Location = new System.Drawing.Point(376, 1);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(27, 26);
this.button1.TabIndex = 0;
this.button1.Text = "X";
this.button1.UseVisualStyleBackColor = false;
this.button1.Click += new System.EventHandler(this.ExitClick);
//
// button2
//
this.button2.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.button2.BackColor = System.Drawing.SystemColors.ButtonFace;
this.button2.BackgroundImage = global::BorderExp.Properties.Resources.blank_1_;
this.button2.FlatAppearance.BorderSize = 0;
this.button2.FlatAppearance.MouseOverBackColor = System.Drawing.Color.FromArgb(((int)(((byte)(224)))), ((int)(((byte)(224)))), ((int)(((byte)(224)))));
this.button2.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.button2.Location = new System.Drawing.Point(343, 1);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(27, 26);
this.button2.TabIndex = 1;
this.button2.Text = "[]";
this.button2.UseVisualStyleBackColor = false;
this.button2.Click += new System.EventHandler(this.MaxClick);
//
// button3
//
this.button3.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.button3.BackColor = System.Drawing.SystemColors.ButtonFace;
this.button3.BackgroundImage = global::BorderExp.Properties.Resources.blank_1_;
this.button3.FlatAppearance.BorderSize = 0;
this.button3.FlatAppearance.MouseOverBackColor = System.Drawing.Color.FromArgb(((int)(((byte)(224)))), ((int)(((byte)(224)))), ((int)(((byte)(224)))));
this.button3.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.button3.Location = new System.Drawing.Point(310, 1);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(27, 26);
this.button3.TabIndex = 2;
this.button3.Text = "___";
this.button3.UseVisualStyleBackColor = false;
this.button3.Click += new System.EventHandler(this.MinClick);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.BackgroundImage = global::BorderExp.Properties.Resources.blank_1_;
this.ClientSize = new System.Drawing.Size(403, 320);
this.ControlBox = false;
this.Controls.Add(this.button3);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button3;
}
}
the screenshot:-
NoBorderForm

TableLayoutPanel and AutoScroll

I have a TableLayoutPanel to which I add rows dynamically.
Each row has an absolute size.
I set the AutoScroll to "true", but when I add rows that go out of the display of the TableLayoutPanel, I don't see the scroll.
This is the designer code:
namespace WindowsFormsApplication1
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (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.tableLayoutPanel1 = new System.Windows.Forms.TableLayoutPanel();
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// tableLayoutPanel1
//
this.tableLayoutPanel1.AutoScroll = true;
this.tableLayoutPanel1.CellBorderStyle = System.Windows.Forms.TableLayoutPanelCellBorderStyle.Single;
this.tableLayoutPanel1.ColumnCount = 1;
this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
this.tableLayoutPanel1.Location = new System.Drawing.Point(24, 48);
this.tableLayoutPanel1.Name = "tableLayoutPanel1";
this.tableLayoutPanel1.RowCount = 1;
this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F));
this.tableLayoutPanel1.Size = new System.Drawing.Size(239, 163);
this.tableLayoutPanel1.TabIndex = 0;
//
// button1
//
this.button1.Location = new System.Drawing.Point(24, 10);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(239, 28);
this.button1.TabIndex = 1;
this.button1.Text = "button1";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.Add(this.button1);
this.Controls.Add(this.tableLayoutPanel1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.TableLayoutPanel tableLayoutPanel1;
private System.Windows.Forms.Button button1;
}
}
and this is how I add rows:
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
tableLayoutPanel1.RowCount++;
tableLayoutPanel1.RowStyles.Add(new RowStyle(SizeType.Absolute, 30));
Button b = new Button();
tableLayoutPanel1.Controls.Add(b, 0, tableLayoutPanel1.RowCount - 1);
}
}
}
Does anybody know what's going on?
thanks :)
Set the AutoScroll property to True.
Make sure that the default rows that get added by the designer do not cause any trouble. Make your constructor look like this:
public Form1() {
InitializeComponent();
tableLayoutPanel1.RowCount = 0;
tableLayoutPanel1.RowStyles.Clear();
tableLayoutPanel1.AutoScroll = true;
}
Looks like you're missing tableLayoutPanel.AutoSize = true.
Try the below line which may work for you:
this.tableLayoutPanel1.AutoScroll = true;
this.tableLayoutPanel1.Dock = DockStyle.Top;

Control resizing mathematics

Ok... So I've got this pretty killer Screenshot system I've been using for a while... It pretty much involves creating a somewhat transparent Form that auto-sizes to fit all monitors... Which gives the screen(s) a "dark-out" effect... I then have a hidden "button" control whos position is set upon mouse-down... Then, until mouse-up, the button will resize in real-time to create a sort of "selection" area... The Button is granted full transparency to implement this effect.
Source Example: http://db.tt/LWxfDB6 [Also posted below]
I'm having two issues that are directly related, I believe.
1.) The coordinates are off for multimonitors, it properly returns the coordinates that are clicked upon (highlighted), but the highlight box (effect) is often on the wrong monitor.
2.) You can only select from top-left ---> bottom-right and not universal.
Sorry if I didn't explain it well, the source should explain better.
Thanks, in advance, for any and all help received. :)
:MarkRect.cs:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Imaging;
using System.IO;
namespace TW_Media_Chat_
{
public partial class MarkRect : Form
{
Point Point1;
Point Point2;
public MarkRect()
{
InitializeComponent();
// Programatically maximize to all monitors
Screen[] Screens = Screen.AllScreens;
int AllWidth = 0;
int AllHeight = 0;
for (int index = 0; index < Screens.Length; index++)
{
AllWidth += Screens[index].Bounds.Width;
AllHeight += Screens[index].Bounds.Height;
}
this.Width = AllWidth;
this.Height = AllHeight;
}
private void Transparency_MouseDown(object sender, MouseEventArgs e)
{
button1.Visible = true;
button1.Location = Cursor.Position;
Point1 = Cursor.Position;
}
private void Transparency_MouseUp(object sender, MouseEventArgs e)
{
this.Visible = false;
Point2 = Cursor.Position;
AjaxChatBridge.AjaxVars.Point1 = Point1;
AjaxChatBridge.AjaxVars.Point2 = Point2;
this.Close();
}
private void MarkRect_MouseMove(object sender, MouseEventArgs e)
{
this.button1.Width = Cursor.Position.X - this.button1.Left;
this.button1.Height = Cursor.Position.Y - this.button1.Top;
}
}
}
:MarkRect.Designer.cs:
namespace TW_Media_Chat_
{
partial class MarkRect
{
///
/// Required designer variable.
///
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (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.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.BackColor = System.Drawing.Color.Lime;
this.button1.ForeColor = System.Drawing.Color.Lime;
this.button1.Location = new System.Drawing.Point(220, 172);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(19, 18);
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.UseVisualStyleBackColor = false;
this.button1.Visible = false;
//
// MarkRect
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.BackColor = System.Drawing.Color.Black;
this.ClientSize = new System.Drawing.Size(490, 406);
this.Controls.Add(this.button1);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.Name = "MarkRect";
this.Opacity = 0.5D;
this.Text = "Transparency";
this.TopMost = true;
this.TransparencyKey = System.Drawing.Color.Lime;
this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.Transparency_MouseDown);
this.MouseMove += new System.Windows.Forms.MouseEventHandler(this.MarkRect_MouseMove);
this.MouseUp += new System.Windows.Forms.MouseEventHandler(this.Transparency_MouseUp);
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.Button button1;
}
}
You should implement control resizing logic easier:
void MarkRect_MouseMove(...)
{
var mouseX = Cursor.Position.X;
var originalMouseX = Point1.X;
button1.Left = Math.Min(mouseX, originalMouseX);
button1.Width = Math.Abs(mouseX - originalMouseX);
// the same for Y
}
You should post that code here. We are to lazy to go to your dropbox to see the code. There are easier questions to answer here.

Categories