我编写了一个 Win Forms 应用程序来测试 LinkLabel 类的工作方式。在我单击更改后的 LinkLabel 之前,它似乎很好。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.Windows.Forms;
using System.Diagnostics;
namespace LinkLabelTest
{
// Editor: code not fully indented - laziness!
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnLinkColor_Click(object sender, EventArgs e)
{
colorDialog1.ShowDialog();
llblinkLabel1.LinkColor = colorDialog1.Color;
}
private void btnActiveLinkColor_Click(object sender, EventArgs e)
{
colorDialog1.ShowDialog();
llblinkLabel1.ActiveLinkColor = colorDialog1.Color;
}
private void btnVisitedLinkColor_Click(object sender, EventArgs e)
{
colorDialog1.ShowDialog();
llblinkLabel1.VisitedLinkColor = colorDialog1.Color;
}
private void llblinkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
try
{
if (!(e.Link.Description == null))
{
if (e.Button == MouseButtons.Right)
{
PopulateLinkDetails(e.Link);
}
if (e.Button == MouseButtons.Left)
{
e.Link.Visited = true;
Debugger.Break();
}
}
}
catch (Exception exc)
{
Debugger.Break();
MessageBox.Show(exc.Message.ToString());
}
}
private void PopulateLinkDetails(LinkLabel.Link link)
{
textDescription.Text = link.Description;
textLinkData.Text = (string)link.LinkData;
textName.Text = link.Name;
checkBoxEnabled.Checked = link.Enabled;
checkBoxVisited.Checked = link.Visited;
nudLinkAreaStart.Value = link.Start;
nudLinkAreaEnd.Value = link.Length;
}
private void Form1_Load(object sender, EventArgs e)
{
cmbLinkBehaviour.DataSource = Enum.GetValues(typeof(LinkBehavior));
}
private void cmbLinkBehaviour_SelectedIndexChanged(object sender, EventArgs e)
{
llblinkLabel1.LinkBehavior = (LinkBehavior)cmbLinkBehaviour.SelectedItem;
}
private void AddLink_Click(object sender, EventArgs e)
{
LinkLabel.Link link = new LinkLabel.Link();
link.Description = textDescription.Text.ToString();
link.LinkData = textLinkData.Text.ToString();
link.Name = textName.Text.ToString();
link.Enabled = checkBoxEnabled.Checked;
link.Visited = checkBoxVisited.Checked;
link.Start = (int)nudLinkAreaStart.Value;
link.Length = (int)nudLinkAreaEnd.Value;
llblinkLabel1.Links.Add(link);
}
}
}
堆栈跟踪如下:
在 System.Drawing.Region.GetHrgn(Graphics g) 在 System.Windows.Forms.Control.Invalidate(Region region, Boolean invalidateChildren) 在 System.Windows.Forms.LinkLabel.InvalidateLink(Link link) 在 System.Windows.Forms。 LinkLabel.OnGotFocus(EventArgs e) 在 System.Windows.Forms.Control.WmSetFocus(Message& m) 在 System.Windows.Forms.Control.WndProc(Message& m) 在 System.Windows.Forms.Label.WndProc(Message& m) 在System.Windows.Forms.LinkLabel.WndProc(Message& msg) 在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) 在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) 在 System.Windows。在 System.Windows.Forms.UnsafeNativeMethods.SetFocus(HandleRef hWnd) 在 System.Windows 的 Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)。System.Windows.Forms.Control.WmMouseDown(Message& m, MouseButtons button, Int32 clicks) 在 System.Windows.Forms.Control 的 System.Windows.Forms.LinkLabel.OnMouseDown(MouseEventArgs e) 的 Forms.Control.FocusInternal()。 WndProc(Message& m) 在 System.Windows.Forms.Label.WndProc(Message& m) 在 System.Windows.Forms.LinkLabel.WndProc(Message& msg) 在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) 在System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) 在 System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) 在 System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG&味精)在 System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager。FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData) at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context) at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context) at System.Windows.Forms.Application.Run(Form mainForm) at LinkLabelTest.Program.Main() 在 C:\Users\Tony\Documents\Visual Studio 2008\Projects\LinkLabelTest\LinkLabelTest\Program.cs:line 22C:\Users\Tony\Documents\Visual Studio 2008\Projects\LinkLabelTest\LinkLabelTest\Program.cs:line 22 中的 Main()C:\Users\Tony\Documents\Visual Studio 2008\Projects\LinkLabelTest\LinkLabelTest\Program.cs:line 22 中的 Main()
下面是设计器生成的代码:
using System.Diagnostics;
namespace LinkLabelTest
{
// Editor: code not fully indented - laziness again!
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.llblinkLabel1 = new System.Windows.Forms.LinkLabel();
this.colorDialog1 = new System.Windows.Forms.ColorDialog();
this.btnLinkColor = new System.Windows.Forms.Button();
this.btnActiveLinkColor = new System.Windows.Forms.Button();
this.btnVisitedLinkColor = new System.Windows.Forms.Button();
this.nudLinkAreaStart = new System.Windows.Forms.NumericUpDown();
this.lbllabel1 = new System.Windows.Forms.Label();
this.nudLinkAreaEnd = new System.Windows.Forms.NumericUpDown();
this.lbllabel2 = new System.Windows.Forms.Label();
this.cmbLinkBehaviour = new System.Windows.Forms.ComboBox();
this.lbllabel3 = new System.Windows.Forms.Label();
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.textDescription = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.textName = new System.Windows.Forms.TextBox();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.textLinkData = new System.Windows.Forms.TextBox();
this.label4 = new System.Windows.Forms.Label();
this.checkBoxEnabled = new System.Windows.Forms.CheckBox();
this.AddLink = new System.Windows.Forms.Button();
this.btnAmendLink = new System.Windows.Forms.Button();
this.checkBoxVisited = new System.Windows.Forms.CheckBox();
this.panel1 = new System.Windows.Forms.Panel();
this.textBox1 = new System.Windows.Forms.TextBox();
((System.ComponentModel.ISupportInitialize)(this.nudLinkAreaStart)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.nudLinkAreaEnd)).BeginInit();
this.groupBox1.SuspendLayout();
this.panel1.SuspendLayout();
this.SuspendLayout();
//
// llblinkLabel1
//
this.llblinkLabel1.AutoSize = true;
this.llblinkLabel1.Location = new System.Drawing.Point(13, 27);
this.llblinkLabel1.Name = "llblinkLabel1";
this.llblinkLabel1.Size = new System.Drawing.Size(96, 13);
this.llblinkLabel1.TabIndex = 0;
this.llblinkLabel1.TabStop = true;
this.llblinkLabel1.Text = "This is a link label !";
this.llblinkLabel1.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.llblinkLabel1_LinkClicked);
//
// btnLinkColor
//
this.btnLinkColor.Location = new System.Drawing.Point(0, 19);
this.btnLinkColor.Name = "btnLinkColor";
this.btnLinkColor.Size = new System.Drawing.Size(93, 23);
this.btnLinkColor.TabIndex = 2;
this.btnLinkColor.Text = "LinkColor";
this.btnLinkColor.UseVisualStyleBackColor = true;
this.btnLinkColor.Click += new System.EventHandler(this.btnLinkColor_Click);
//
// btnActiveLinkColor
//
this.btnActiveLinkColor.Location = new System.Drawing.Point(0, 48);
this.btnActiveLinkColor.Name = "btnActiveLinkColor";
this.btnActiveLinkColor.Size = new System.Drawing.Size(93, 23);
this.btnActiveLinkColor.TabIndex = 3;
this.btnActiveLinkColor.Text = "ActiveLinkColor";
this.btnActiveLinkColor.UseVisualStyleBackColor = true;
this.btnActiveLinkColor.Click += new System.EventHandler(this.btnActiveLinkColor_Click);
//
// btnVisitedLinkColor
//
this.btnVisitedLinkColor.Location = new System.Drawing.Point(0, 77);
this.btnVisitedLinkColor.Name = "btnVisitedLinkColor";
this.btnVisitedLinkColor.Size = new System.Drawing.Size(93, 23);
this.btnVisitedLinkColor.TabIndex = 4;
this.btnVisitedLinkColor.Text = "VisitedLinkColor";
this.btnVisitedLinkColor.UseVisualStyleBackColor = true;
this.btnVisitedLinkColor.Click += new System.EventHandler(this.btnVisitedLinkColor_Click);
//
// nudLinkAreaStart
//
this.nudLinkAreaStart.Location = new System.Drawing.Point(152, 64);
this.nudLinkAreaStart.Maximum = new decimal(new int[] {
21,
0,
0,
0});
this.nudLinkAreaStart.Name = "nudLinkAreaStart";
this.nudLinkAreaStart.Size = new System.Drawing.Size(66, 20);
this.nudLinkAreaStart.TabIndex = 6;
//
// lbllabel1
//
this.lbllabel1.AutoSize = true;
this.lbllabel1.Location = new System.Drawing.Point(224, 71);
this.lbllabel1.Name = "lbllabel1";
this.lbllabel1.Size = new System.Drawing.Size(74, 13);
this.lbllabel1.TabIndex = 7;
this.lbllabel1.Text = "Link area start";
//
// nudLinkAreaEnd
//
this.nudLinkAreaEnd.Location = new System.Drawing.Point(152, 99);
this.nudLinkAreaEnd.Maximum = new decimal(new int[] {
21,
0,
0,
0});
this.nudLinkAreaEnd.Name = "nudLinkAreaEnd";
this.nudLinkAreaEnd.Size = new System.Drawing.Size(67, 20);
this.nudLinkAreaEnd.TabIndex = 8;
//
// lbllabel2
//
this.lbllabel2.AutoSize = true;
this.lbllabel2.Location = new System.Drawing.Point(224, 105);
this.lbllabel2.Name = "lbllabel2";
this.lbllabel2.Size = new System.Drawing.Size(83, 13);
this.lbllabel2.TabIndex = 9;
this.lbllabel2.Text = "Link area length";
//
// cmbLinkBehaviour
//
this.cmbLinkBehaviour.FormattingEnabled = true;
this.cmbLinkBehaviour.Location = new System.Drawing.Point(0, 106);
this.cmbLinkBehaviour.Name = "cmbLinkBehaviour";
this.cmbLinkBehaviour.Size = new System.Drawing.Size(93, 21);
this.cmbLinkBehaviour.TabIndex = 10;
this.cmbLinkBehaviour.SelectedIndexChanged += new System.EventHandler(this.cmbLinkBehaviour_SelectedIndexChanged);
//
// lbllabel3
//
this.lbllabel3.AutoSize = true;
this.lbllabel3.Location = new System.Drawing.Point(-3, 130);
this.lbllabel3.Name = "lbllabel3";
this.lbllabel3.Size = new System.Drawing.Size(78, 13);
this.lbllabel3.TabIndex = 11;
this.lbllabel3.Text = "Link Behaviour";
//
// groupBox1
//
this.groupBox1.Controls.Add(this.btnLinkColor);
this.groupBox1.Controls.Add(this.btnActiveLinkColor);
this.groupBox1.Controls.Add(this.lbllabel3);
this.groupBox1.Controls.Add(this.btnVisitedLinkColor);
this.groupBox1.Controls.Add(this.cmbLinkBehaviour);
this.groupBox1.Location = new System.Drawing.Point(12, 57);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(124, 155);
this.groupBox1.TabIndex = 13;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "THE link label";
//
// textDescription
//
this.textDescription.Location = new System.Drawing.Point(152, 136);
this.textDescription.Name = "textDescription";
this.textDescription.Size = new System.Drawing.Size(100, 20);
this.textDescription.TabIndex = 14;
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(259, 143);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(60, 13);
this.label1.TabIndex = 15;
this.label1.Text = "Description";
//
// textName
//
this.textName.Location = new System.Drawing.Point(152, 163);
this.textName.Name = "textName";
this.textName.Size = new System.Drawing.Size(100, 20);
this.textName.TabIndex = 16;
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(262, 170);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(35, 13);
this.label2.TabIndex = 17;
this.label2.Text = "Name";
//
// label3
//
this.label3.AutoSize = true;
this.label3.Location = new System.Drawing.Point(16, 11);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(283, 13);
this.label3.TabIndex = 18;
this.label3.Text = "Right Click on a link to retrieve properties. Left Click to visit";
//
// textLinkData
//
this.textLinkData.Location = new System.Drawing.Point(152, 191);
this.textLinkData.Name = "textLinkData";
this.textLinkData.Size = new System.Drawing.Size(100, 20);
this.textLinkData.TabIndex = 19;
//
// label4
//
this.label4.AutoSize = true;
this.label4.Location = new System.Drawing.Point(262, 198);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(53, 13);
this.label4.TabIndex = 20;
this.label4.Text = "Link Data";
//
// checkBoxEnabled
//
this.checkBoxEnabled.AutoSize = true;
this.checkBoxEnabled.Location = new System.Drawing.Point(151, 218);
this.checkBoxEnabled.Name = "checkBoxEnabled";
this.checkBoxEnabled.Size = new System.Drawing.Size(65, 17);
this.checkBoxEnabled.TabIndex = 21;
this.checkBoxEnabled.Text = "Enabled";
this.checkBoxEnabled.UseVisualStyleBackColor = true;
//
// AddLink
//
this.AddLink.Location = new System.Drawing.Point(385, 64);
this.AddLink.Name = "AddLink";
this.AddLink.Size = new System.Drawing.Size(75, 23);
this.AddLink.TabIndex = 22;
this.AddLink.Text = "Add Link";
this.AddLink.UseVisualStyleBackColor = true;
this.AddLink.Click += new System.EventHandler(this.AddLink_Click);
//
// btnAmendLink
//
this.btnAmendLink.Enabled = false;
this.btnAmendLink.Location = new System.Drawing.Point(385, 99);
this.btnAmendLink.Name = "btnAmendLink";
this.btnAmendLink.Size = new System.Drawing.Size(75, 23);
this.btnAmendLink.TabIndex = 23;
this.btnAmendLink.Text = "AmendLink";
this.btnAmendLink.UseVisualStyleBackColor = true;
//
// checkBoxVisited
//
this.checkBoxVisited.AutoSize = true;
this.checkBoxVisited.Location = new System.Drawing.Point(151, 241);
this.checkBoxVisited.Name = "checkBoxVisited";
this.checkBoxVisited.Size = new System.Drawing.Size(57, 17);
this.checkBoxVisited.TabIndex = 24;
this.checkBoxVisited.Text = "Visited";
this.checkBoxVisited.UseVisualStyleBackColor = true;
//
// panel1
//
this.panel1.Controls.Add(this.textBox1);
this.panel1.Location = new System.Drawing.Point(474, 13);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(200, 239);
this.panel1.TabIndex = 25;
//
// textBox1
//
this.textBox1.Dock = System.Windows.Forms.DockStyle.Fill;
this.textBox1.Location = new System.Drawing.Point(0, 0);
this.textBox1.Multiline = true;
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(200, 239);
this.textBox1.TabIndex = 0;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(704, 264);
this.Controls.Add(this.panel1);
this.Controls.Add(this.checkBoxVisited);
this.Controls.Add(this.btnAmendLink);
this.Controls.Add(this.AddLink);
this.Controls.Add(this.checkBoxEnabled);
this.Controls.Add(this.label4);
this.Controls.Add(this.textLinkData);
this.Controls.Add(this.label3);
this.Controls.Add(this.label2);
this.Controls.Add(this.textName);
this.Controls.Add(this.label1);
this.Controls.Add(this.textDescription);
this.Controls.Add(this.groupBox1);
this.Controls.Add(this.lbllabel2);
this.Controls.Add(this.nudLinkAreaEnd);
this.Controls.Add(this.lbllabel1);
this.Controls.Add(this.nudLinkAreaStart);
this.Controls.Add(this.llblinkLabel1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
((System.ComponentModel.ISupportInitialize)(this.nudLinkAreaStart)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.nudLinkAreaEnd)).EndInit();
this.groupBox1.ResumeLayout(false);
this.groupBox1.PerformLayout();
this.panel1.ResumeLayout(false);
this.panel1.PerformLayout();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.LinkLabel llblinkLabel1;
private System.Windows.Forms.ColorDialog colorDialog1;
private System.Windows.Forms.Button btnLinkColor;
private System.Windows.Forms.Button btnActiveLinkColor;
private System.Windows.Forms.Button btnVisitedLinkColor;
private System.Windows.Forms.NumericUpDown nudLinkAreaStart;
private System.Windows.Forms.Label lbllabel1;
private System.Windows.Forms.NumericUpDown nudLinkAreaEnd;
private System.Windows.Forms.Label lbllabel2;
private System.Windows.Forms.ComboBox cmbLinkBehaviour;
private System.Windows.Forms.Label lbllabel3;
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.TextBox textDescription;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox textName;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.TextBox textLinkData;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.CheckBox checkBoxEnabled;
private System.Windows.Forms.Button AddLink;
private System.Windows.Forms.Button btnAmendLink;
private System.Windows.Forms.CheckBox checkBoxVisited;
private System.Windows.Forms.Panel panel1;
private System.Windows.Forms.TextBox textBox1;
}
}
请有人建议。是 LinkLabel 的 Paint 事件发生时没有窗口句柄的问题。
提前谢谢了
托尼