就是说我自己的一个form,贴上了背景,想要在form中的某个部位实现菜单的效果,通过上下键可以选择,而且要的是那种菜单选项和背景图案是融为一体的感觉,通过颜色的变化来标识是否被选定,有点像tc中的那种自己写的菜单。可是我用c#试了好几个控件都不行。用button,可是button的四边是黑线,没有了和背景融合的效果;用label,效果很好,可是不能用键盘上下选择。小弟新手,希望各位大侠赐教!万分感谢!
调试欢乐多
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;namespace WindowsApplication1
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null; public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent(); //
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
} /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// label1
//
this.label1.Location = new System.Drawing.Point(56, 36);
this.label1.Name = "label1";
this.label1.TabIndex = 0;
this.label1.Text = "label1";
//
// label2
//
this.label2.Location = new System.Drawing.Point(56, 84);
this.label2.Name = "label2";
this.label2.TabIndex = 1;
this.label2.Text = "label2";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(392, 266);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Name = "Form1";
this.Text = "Form1";
this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.Form1_KeyDown);
this.ResumeLayout(false); }
#endregion /// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
} private void Form1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
if(e.KeyCode==Keys.Down)
{
this.label2.BackColor=Color.Red;
this.label1.BackColor=this.BackColor;
}
if(e.KeyCode==Keys.Up)
{
this.label1.BackColor=Color.Red;
this.label2.BackColor=this.BackColor;
}
} }
}
程序我用了,能行
不过在form中如果加入了button就不行了,上下键的选择只是在button之间,而且我跟踪了一下,form的keydown事件还有button的keydown事件的那段程序都根本就走不进去:(不知如何解决
{
if(e.KeyCode==Keys.N)
{
button1.Select();
this.label2.BackColor=Color.Red;
this.label1.BackColor=this.BackColor;
}
if(e.KeyCode==Keys.M)
{
this.label1.BackColor=Color.Red;
this.label2.BackColor=this.BackColor;
button2.Select();
}
} private void button1_KeyDown(object sender, KeyEventArgs e)
{
if(e.KeyCode==Keys.B)
{
button3.Select();
}
if(e.KeyCode==Keys.A)
{
this.label1.BackColor=Color.Red;
this.label2.BackColor=this.BackColor;
button4.Select();
}
}试了试,总结出以下几条,不知对不对。
看上面的代码,当keypress的是A,B,M,N等等这些键的时候,都能够运行
当有了button之后,按下“上下左右”还有“回车”键,都不会按照程序的步骤运行,好像是系统已经内置了似的,上下左右按下去后,会在几个button之间来回选择,而且程序根本不会进入到自己写的上面的代码中(把ABMN换成了上下左右)。后来又试了keyup事件,这个上下左右都支持,我初步猜想,我想要实现的功能可以通过keyup事件来实现!
private void Form1_KeyUp(object sender, KeyEventArgs e)
{
if(e.KeyCode==Keys.Down)
{
button3.Select();
this.label2.BackColor=Color.Red;
this.label1.BackColor=this.BackColor;
}
if(e.KeyCode==Keys.Up)
{
this.label1.BackColor=Color.Red;
this.label2.BackColor=this.BackColor;
button3.Select();
} }