C#做上位机时自定义了一个控件,控件对键盘方向键做出反应,但我这个按方向键不好使,按键事件进不去,就好像焦点没找对一样,求助大神啊

飞刀杂耍者 2015-10-06 07:36:02

控件主要是想做一个显示速度的条,当按↑时格子文本框变色
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SpeedBar
{
public partial class SpeedBar : UserControl
{
private Int16 SpeedBarflag = 0;
public SpeedBar()
{

InitializeComponent();
}

private void SpeedBar_KeyDown(object sender, KeyEventArgs e)
{
Keys key = e.KeyCode;

switch (key)
{
//↑按键
case Keys.Up:
SpeedBarflag ++;
if(SpeedBarflag >5)
SpeedBarflag=5;
if (SpeedBarflag >= 1 && SpeedBarflag<6)
{
switch(SpeedBarflag)
{
case 1:
SpeedBar1.BackColor=System.Drawing.Color.Red;
break;
case 2:
SpeedBar2.BackColor=System.Drawing.Color.Yellow;
break;
case 3:
SpeedBar3.BackColor=System.Drawing.Color.Green;
break;
case 4:
SpeedBar4.BackColor=System.Drawing.Color.Blue;
break;
case 5:
SpeedBar5.BackColor=System.Drawing.Color.Black;
break;
}
}

if (SpeedBarflag > -5 && SpeedBarflag< 1)
{

switch(SpeedBarflag)
{
case -4:
SpeedBarminus5.BackColor=System.Drawing.Color.White;
break;
case -3:
SpeedBarminus4.BackColor=System.Drawing.Color.White;
break;
case -2:
SpeedBarminus3.BackColor=System.Drawing.Color.White;
break;
case -1:
SpeedBarminus2.BackColor=System.Drawing.Color.White;
break;
case 0:
SpeedBarminus1.BackColor=System.Drawing.Color.White;
break;
}
}
break;

//↓按键

case Keys.Down:
SpeedBarflag --;
if(SpeedBarflag <-5)
SpeedBarflag=-5;
if (SpeedBarflag <= -1 && SpeedBarflag>-6)
{
switch(SpeedBarflag)
{
case -1:
SpeedBarminus1.BackColor=System.Drawing.Color.Red;
break;
case -2:
SpeedBarminus2.BackColor=System.Drawing.Color.Yellow;
break;
case -3:
SpeedBarminus3.BackColor=System.Drawing.Color.Green;
break;
case -4:
SpeedBarminus4.BackColor=System.Drawing.Color.Blue;
break;
case -5:
SpeedBarminus5.BackColor=System.Drawing.Color.Black;
break;
}
}

if (SpeedBarflag < 5 && SpeedBarflag> -1)
{
switch(SpeedBarflag)
{
case 4:
SpeedBar5.BackColor=System.Drawing.Color.White;
break;
case 3:
SpeedBar4.BackColor=System.Drawing.Color.White;
break;
case 2:
SpeedBar3.BackColor=System.Drawing.Color.White;
break;
case 1:
SpeedBar2.BackColor=System.Drawing.Color.White;
break;
case 0:
SpeedBar1.BackColor=System.Drawing.Color.White;
break;
}
}
break;
}
}




}
}
一直想不通为啥按键无效,一直找不到焦点一样,Form有一个keypreview好像是管这个的,可自定义控件里没这个选项。。。求助求助,谢了
...全文
277 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
crystal_lz 2015-10-06
  • 打赏
  • 举报
回复
还有看着你的代码 略恶心 所以 我自己没事打了个酱油

public class FuckBox : Control
{
	private FuckArg[] _Items = new FuckArg[0];

	public FuckArg[] Items {
		get { return _Items; }
		set {
			if (value == _Items || value == null) return;
			_Items = value;
			this.Invalidate();
			this.Height = 0;//会触发SetBoundsCore
		}
	}

	private Size _ColorBoxSize = new Size(20, 15);

	public Size ColorBoxSize {
		get { return _ColorBoxSize; }
		set {
			if (value == _ColorBoxSize) return;
			_ColorBoxSize = value;
			this.Invalidate();
		}
	}

	private int _Value;

	public int Value {
		get { return _Value; }
		set {
			if (value == _Value || value < 0 || value > this.Items.Length) return;
			_Value = value;
			this.Invalidate();
		}
	}

	public FuckBox() {
		this.SetStyle(ControlStyles.UserPaint, true);
		this.SetStyle(ControlStyles.ResizeRedraw, true);
		this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
		this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
		this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
	}

	protected override void OnPaint(PaintEventArgs e) {
		Graphics g = e.Graphics;
		for (int i = 0; i < this.Items.Length; i++) {
			using (SolidBrush sb = new SolidBrush(this.Items[i].Color)) {
				int h = this.Height - i * (this._ColorBoxSize.Height + 1) - this._ColorBoxSize.Height - 1;
				if (i < this.Value)//只绘制 小于 value 的框
					g.FillRectangle(sb, 1, h, this._ColorBoxSize.Width, this._ColorBoxSize.Height);
				g.DrawRectangle(Pens.Black, 1, h, this._ColorBoxSize.Width - 1, this._ColorBoxSize.Height - 1);
				StringFormat sf = new StringFormat();
				sf.Alignment = StringAlignment.Far;         //水平方向靠右对齐
				sf.LineAlignment = StringAlignment.Center;  //锤子方向 中间对齐
				//绘制文字
				g.DrawString(
					this.Items[i].Text, 
					this.Font,
					Brushes.Black,
					new Rectangle(this.ColorBoxSize.Width + 2, h, this.ColorBoxSize.Width, this.ColorBoxSize.Height),
					sf);
			}
		}
		base.OnPaint(e);
	}

	protected override void OnMouseDown(MouseEventArgs e) {
		this.Focus();
		base.OnMouseDown(e);
	}

	protected override bool ProcessCmdKey(ref Message msg, Keys keyData) {
		switch (keyData) {//处理方向键
			case Keys.Up: this.Value++; return true;
			case Keys.Down: this.Value--; return true;
		}
		return base.ProcessCmdKey(ref msg, keyData);
	}
    //自动设置大小
	protected override void SetBoundsCore(int x, int y, int width, int height, BoundsSpecified specified) {
		width = this._ColorBoxSize.Width + 20;
		height = (this.ColorBoxSize.Height + 1) * this.Items.Length + 1;
		if (height < 5) height = 6;
		base.SetBoundsCore(x, y, width, height, specified);
	}
}

public class FuckArg//保存信息的类
{
	private Color _Color = Color.Gray;
    [Description("选中时候的颜色")]
	public Color Color {
		get { return _Color; }
		set { _Color = value; }
	}

	private string _Text = "0";
    [Description("绘制的文本")]
	public string Text {
		get { return _Text; }
		set { _Text = value; }
	}

	public override string ToString() {//方便好看
		return this._Text + "|" + this._Color;
	}
}
控件 三个属性 Items 需要绘制的方块和文本 全部在这里 [选中时候的颜色,文本] 没选中的默认透明 ColorBoxSize 绘制时候 颜色框的大小 Value 默认选择的位置 如图中的是6 就是从下往上 选择6个
crystal_lz 2015-10-06
  • 打赏
  • 举报
回复
自定义控件的 KeyDown 没法处理方向键的 会被系统自己处理 比如 假设某个控件得掉焦点的时候 方向键 可以控制焦点移动 所以 这个时候 方向键 是被系统接收了 所以 要处理方向键 需要写 OnPreviewKeyDown 函数 这个里面可以抓去到方向键 不过很遗憾 就像我上面说的一样 方向键 会切换焦点 虽然你抓去到了 方向键的事件 不过焦掉却已经去到了下一个控件上面了 也就是说 你可以获取到方向键 但是系统依然会继续他的动作 把焦点切换走 所以 这个时候 你需要重写 ProcessCmdKey 函数 当你处理完了 你自己的代码后 如果不想让系统继续执行他的动作 可以返回 return true 告知 这个按键已经处理了 否则 return base.Pro...
飞刀杂耍者 2015-10-06
  • 打赏
  • 举报
回复
引用 1楼nature023 的回复:
有对事件SpeedBar_KeyDown的委托吗?你设置断点跟踪就能发现问题。
谢谢您,我设置断点发现是进不去这个按键事件,您说的委托具体怎么操作呀。。
nature023 2015-10-06
  • 打赏
  • 举报
回复
有对事件SpeedBar_KeyDown的委托吗?你设置断点跟踪就能发现问题。

111,093

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧