- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
LinkLabel
控件有一些烦人的问题:
Color.Blue
而不是 LinkColor
属性的 SystemColors.HotTrack
) 我找到了以下答案 here声称可以解决光标问题:
using System.Runtime.InteropServices;
namespace System.Windows.Forms {
public class LinkLabelEx : LinkLabel {
private const int IDC_HAND = 32649;
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr LoadCursor(IntPtr hInstance, int lpCursorName);
private static readonly Cursor SystemHandCursor = new Cursor(LoadCursor(IntPtr.Zero, IDC_HAND));
protected override void OnMouseMove(MouseEventArgs e) {
base.OnMouseMove(e);
// If the base class decided to show the ugly hand cursor
if(OverrideCursor == Cursors.Hand) {
// Show the system hand cursor instead
OverrideCursor = SystemHandCursor;
}
}
}
}
但是,这个解决方案并不完美。例如,旧的、丑陋的光标在悬停在其上时会在显示正确的光标之前闪烁一帧。
我还阅读了 ComCtl32.dll 中的原生 SysLink
控件,它没有问题,但我找不到一个好的解决方案来使用它在 C#/WinForms 中。但是,无论如何我更喜欢纯 .NET 解决方案。
如何通过解决上述问题使LinkLabel
控件更好?
最佳答案
关于颜色,控件具有允许您更改链接颜色的属性:LinkColor
、ActiveLinkColor
、VisitedLinkColor
和 禁用链接颜色
。
这些属性的默认值来自 Internet Explorer 设置,这些设置存储在 HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\Settings
注册表项中。
要使用不同的颜色,您可以根据自己的喜好设置这些属性。例如,您可以将 LinkColor
设置为 SystemColors.HotTrack
或按照 w3org 的建议进行操作对于颜色,使用 #0000EE
作为默认链接颜色,#551A8B
用于访问链接,#FF0000
用于事件链接。
关于闪烁,这是因为您共享的代码是在基类更改光标后在鼠标移动时设置光标。因此,有可能在设置新游标之前使基类游标闪烁。要解决这个问题,需要处理WM_SETCURSOR
,必要时将光标设置为系统手形光标。
using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
public class MyLinkLabel : LinkLabel
{
public MyLinkLabel()
{
this.LinkColor = Color.FromArgb(0x00, 0x66, 0xCC);
this.VisitedLinkColor = Color.FromArgb(0x80, 0x00, 0x80);
this.ActiveLinkColor = Color.FromArgb(0xFF, 0x00, 0x00);
}
const int IDC_HAND = 32649;
const int WM_SETCURSOR = 0x0020;
const int HTCLIENT = 1;
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
static extern IntPtr LoadCursor(IntPtr hInstance, int lpCursorName);
[DllImport("user32.dll", ExactSpelling = true, CharSet = CharSet.Auto)]
public static extern IntPtr SetCursor(HandleRef hcursor);
static readonly Cursor SystemHandCursor =
new Cursor(LoadCursor(IntPtr.Zero, IDC_HAND));
protected override void WndProc(ref Message msg)
{
if (msg.Msg == WM_SETCURSOR)
WmSetCursor(ref msg);
else
base.WndProc(ref msg);
}
void WmSetCursor(ref Message m)
{
if (m.WParam == (IsHandleCreated ? Handle : IntPtr.Zero) &&
(unchecked((int)(long)m.LParam) & 0xffff) == HTCLIENT)
{
if (OverrideCursor != null)
{
if (OverrideCursor == Cursors.Hand)
SetCursor(new HandleRef(SystemHandCursor, SystemHandCursor.Handle));
else
SetCursor(new HandleRef(OverrideCursor, OverrideCursor.Handle));
}
else
{
SetCursor(new HandleRef(Cursor, Cursor.Handle));
}
}
else
{
DefWndProc(ref m);
}
}
}
关于c# - 改进 LinkLabel - 使用系统手形光标和更改链接颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54160682/
根据 http://msdn.microsoft.com/en-us/library/system.windows.forms.linklabel.aspx , LinkLabel 类有一个 Clic
是否可以在默认计算机网络浏览器中打开 LinkLabel? 最佳答案 是的 - 您可以在“点击链接”事件中使用 System.Diagnostics.Process.Start(url)。 关于c#
我有一个 LinkLabel,我正在为它分配一个图像并对齐图像 MiddleLeft。问题是标签文本(也是 MiddleLeft 对齐)绘制在图像的顶部而不是右侧(或左侧)。有没有办法在保持文本左对齐
我在表单上放置了一个 LinkLabel,默认情况下它具有 TabStop=true。我将 TabStop 更改为 false 并关闭表单。然后再次打开表单 - TabStop 为 true。 会发生
LinkLabel 控件有一些烦人的问题: 默认情况下,它不使用任何系统颜色(即 Color.Blue 而不是 LinkColor 属性的 SystemColors.HotTrack) 它使用旧的、丑
我希望我的一些链接标签具有带有 BackColor 的填充,有点像这样: 点这里 但问题是你不能真正有一个可以点击的填充链接标签,你必须只点击文本(如果你在填充区域内点击,点击不会注册。) 因此,另一
这个问题在这里已经有了答案: Enter "&" symbol into a text Label in Windows Forms? (5 个回答) 8年前关闭。 我想显示文字 a&b在我的Link
我有一个用动态文本填充的标签。它通常是一个段落左右的长度。我需要在本段末尾将 LinkLabel 与“查看更多...”一词无缝集成。 它应该是这样的: Lorem ipsum dolor sit am
我注意到在标准 .NET WinForms 表单中分配给链接标签控件的键盘快捷键不起作用。 我已经创建了一个 LinkLabel 控件实例并将 Text 属性指定为“Select &All”。对于大多
我正在使用 Microsoft Compact Framework 开发 Windows CE 应用程序。我必须使用 LinkLabel,它必须是白色且没有下划线。 因此,在设计器中,我将字体颜色修改
当我在 WindowsForms 中双击一个 LinkLabel 时,它会复制它的文本;我怎样才能防止这种情况发生? 顺便说一句,它是一个 .Net 2.0 应用程序,如果这有什么不同的话。 谢谢 最
如果我在 .net winform 上有一个链接标签,我是否可以,当链接标签被聚焦时,按下 enter 键来导致说LinkLabel点击? 不幸的是,它似乎没有公开 KeyDown 事件。 编辑 最简
我的应用程序需要模拟一个服务帐户,这是我通过对 LogonUser 的 native 调用来完成的。但是,似乎 .Net 库中的随机组件会尝试访问该帐户无权访问的注册表项,从而导致抛出 Securit
我有一个 LinkLabel , 通过设置 BorderStyle=none当我运行表单时,它周围有一个细边框。 我想从链接标签中删除这个细边框。我怎样才能做到这一点? 我的链接标签的代码是
任何人都可以为我提供一个创建带有链接标签的自定义 datagridview 列控件的工作示例吗?首先,我不能使用 LinkColumn,因为我需要在同一个单元格中有多个可点击的链接。在 Interne
当我单击应该打开表单的链接标签时,我遇到了一个奇怪的未处理异常。我试图将代码放在 try-catch block 中的 linklabel_click 事件处理程序中,但我仍然收到以下错误。 See
如果我给 TextRenderer.MeasureText 一些要测量的文本和要使用的宽度,它将返回显示该文本所需的高度。 private static int CalculateHeight(str
有没有办法在 Windows 窗体的 LinkLabel 控件中放置多个链接? 如果我就这样设置 this.linkLabel.Text = ""; foreach (string url in ur
我希望只有一个链接和整个文本都可以点击,并且都可以动态设置。我不知道如何更换它们。我尝试了下面的代码,当它被多次调用时,我得到 null pointer exception错误。 我试过用这个: vo
我是 asp.net 的新手,我想为链接标签添加样式。我的代码如下: Guest .userlabel { display:inline-block; text-decoration:
我是一名优秀的程序员,十分优秀!