В настоящее время я занимаюсь разработкой программного обеспечения для интеллектуального устройства с использованием Visual Studio 2008, C #. Устройство имеет сенсорный экран в качестве метода ввода. Я не могу использовать MouseDown
событие. Я создал пользовательскую кнопку, унаследованную от System.Windows.Forms.Button
класса.
Попытка переопределить OnMouseDown
и OnMouseUp
события. Но эти события не стреляют. Я искал в интернете и нашел решение с WndProc
, но его не удалось использовать с .NET Compact Framework.
Пожалуйста, помогите мне с этим. Спасибо всем. Пример кода очень прост, и вы можете найти его ниже.
protected override void OnMouseUp(MouseEventArgs e)
{
Text = "Mouse Up";
base.OnMouseUp(e);
}
Edit:
Как я уже упоминал в своем комментарии к @josef ниже, я создал свой собственный элемент управления. Элемент управления представляет собой панель из бутонов. Во-первых, я попытался добавить метку для текста кнопки, на самом деле нет необходимости делать это. Это можно легко сделать с помощью метода переопределения события OnPaint. Этот элемент управления может иметь некоторые ненужные коды, но вы можете оптимизировать его. Если вам это нужно, вы можете найти мое решение ниже:
class ButonatedPanel : Panel
{
public int ShadeWidth { get; set; }
public bool isMouseDown { get; set; }
protected override void OnMouseDown(MouseEventArgs e)
{
Invalidate();
isMouseDown = true;
Text = "Mouse Down";
base.OnMouseDown(e);
}
protected override void OnMouseUp(MouseEventArgs e)
{
Invalidate();
isMouseDown = false;
Text = "Mouse Up";
base.OnMouseUp(e);
}
public override string Text
{
get
{
return Lb.Text;
}
set
{
Lb.Text = value;
}
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
Lb.Size = new Size(Width - ShadeWidth * 2, Height - 2 * ShadeWidth);
Lb.Location = new Point(ShadeWidth, ShadeWidth);
SizeF iSizeF = new SizeF(e.Graphics.MeasureString(Text, Lb.Font).Width,
e.Graphics.MeasureString(Text, Lb.Font).Height);
e.Graphics.DrawString(Text, Lb.Font, new SolidBrush(Color.FromArgb(0, 0, 0)),
(Width - 2 * ShadeWidth - iSizeF.Width) / 2, (Height - 2 * ShadeWidth - iSizeF.Height) / 2);
//gray 128,128,128
Rectangle Rect1 = new Rectangle(0, 0, ShadeWidth, Height - ShadeWidth);
Rectangle Rect2 = new Rectangle(0, 0, Width - ShadeWidth, ShadeWidth);
Rectangle Rect3 = new Rectangle(Width - 2 * ShadeWidth, 0, ShadeWidth, Height - ShadeWidth);
Rectangle Rect4 = new Rectangle(0, Height - 2 * ShadeWidth, Width - ShadeWidth, ShadeWidth);
//white 228,228,228
Rectangle Rect5 = new Rectangle(ShadeWidth, ShadeWidth, ShadeWidth, Height - 4 * ShadeWidth);
Rectangle Rect6 = new Rectangle(ShadeWidth, ShadeWidth, Width - 4 * ShadeWidth, ShadeWidth);
//black 0,0,0
Rectangle Rect7 = new Rectangle(Width - ShadeWidth, 0, ShadeWidth, Height);
Rectangle Rect8 = new Rectangle(0, Height - ShadeWidth, Width, ShadeWidth);
if (!isMouseDown)
{
e.Graphics.DrawRectangle(new Pen(Color.FromArgb(128, 128, 128)), Rect1);
e.Graphics.DrawRectangle(new Pen(Color.FromArgb(128, 128, 128)), Rect2);
e.Graphics.FillRectangle(new SolidBrush(Color.FromArgb(128, 128, 128)), Rect1);
e.Graphics.FillRectangle(new SolidBrush(Color.FromArgb(128, 128, 128)), Rect2);
e.Graphics.DrawRectangle(new Pen(Color.FromArgb(128, 128, 128)), Rect3);
e.Graphics.DrawRectangle(new Pen(Color.FromArgb(128, 128, 128)), Rect4);
e.Graphics.FillRectangle(new SolidBrush(Color.FromArgb(128, 128, 128)), Rect3);
e.Graphics.FillRectangle(new SolidBrush(Color.FromArgb(128, 128, 128)), Rect4);
e.Graphics.DrawRectangle(new Pen(Color.FromArgb(228, 228, 228)), Rect5);
e.Graphics.DrawRectangle(new Pen(Color.FromArgb(228, 228, 228)), Rect6);
e.Graphics.FillRectangle(new SolidBrush(Color.FromArgb(228, 228, 228)), Rect5);
e.Graphics.FillRectangle(new SolidBrush(Color.FromArgb(228, 228, 228)), Rect6);
e.Graphics.DrawRectangle(new Pen(Color.FromArgb(0, 0, 0)), Rect7);
e.Graphics.DrawRectangle(new Pen(Color.FromArgb(0, 0, 0)), Rect8);
e.Graphics.FillRectangle(new SolidBrush(Color.FromArgb(0, 0, 0)), Rect7);
e.Graphics.FillRectangle(new SolidBrush(Color.FromArgb(0, 0, 0)), Rect8);
}
else
{
e.Graphics.DrawRectangle(new Pen(Color.FromArgb(0, 0, 0)), Rect1);
e.Graphics.DrawRectangle(new Pen(Color.FromArgb(0, 0, 0)), Rect2);
e.Graphics.FillRectangle(new SolidBrush(Color.FromArgb(0, 0, 0)), Rect1);
e.Graphics.FillRectangle(new SolidBrush(Color.FromArgb(0, 0, 0)), Rect2);
e.Graphics.DrawRectangle(new Pen(Color.FromArgb(228, 228, 228)), Rect3);
e.Graphics.DrawRectangle(new Pen(Color.FromArgb(228, 228, 228)), Rect4);
e.Graphics.FillRectangle(new SolidBrush(Color.FromArgb(228, 228, 228)), Rect3);
e.Graphics.FillRectangle(new SolidBrush(Color.FromArgb(228, 228, 228)), Rect4);
e.Graphics.DrawRectangle(new Pen(Color.FromArgb(128, 128, 128)), Rect5);
e.Graphics.DrawRectangle(new Pen(Color.FromArgb(128, 128, 128)), Rect6);
e.Graphics.FillRectangle(new SolidBrush(Color.FromArgb(128, 128, 128)), Rect5);
e.Graphics.FillRectangle(new SolidBrush(Color.FromArgb(128, 128, 128)), Rect6);
e.Graphics.DrawRectangle(new Pen(Color.FromArgb(128, 128, 128)), Rect7);
e.Graphics.DrawRectangle(new Pen(Color.FromArgb(128, 128, 128)), Rect8);
e.Graphics.FillRectangle(new SolidBrush(Color.FromArgb(128, 128, 128)), Rect7);
e.Graphics.FillRectangle(new SolidBrush(Color.FromArgb(128, 128, 128)), Rect8);
}
}
Label Lb = new Label();
public ButonatedPanel()
: base()
{
Invalidate();
ShadeWidth = 1;
Lb.Size = new Size(Width - ShadeWidth * 2, Height - 2 * ShadeWidth);
Lb.Location = new Point(ShadeWidth, ShadeWidth);
Lb.Visible = false;
Controls.Add(Lb);
}