我用 c# 编写了一个小程序,在其中显示了一些指向带有 LinkLabels 的文件的链接。url 和显示的文本是从 xml 文件加载的,xml 文件如下所示:
<member>
<qenum>2</qenum>
<vindex>[3]</vindex>
<name>Aldehyde-Wikipedia (Graphik)</name>
<filename>Wikipedia\Aldehyde - Wikipedia.htm</filename>
</member>
要处理链接,我使用命令 System.Diagnostics.Process.Start(this.path + this.filename); (路径和文件名都是从 xml 文件中读取的字符串值)
所以一切正常,除非文件名中的字符是“-”。好的,这可能是由字符集引起的。但奇怪的是,当我进入 xml 文件时,从特定文件(此处:wikipedia 文件)中选择文件名,将其粘贴到我的 xml 文件中并保护它,它工作得很好,链接得到处理。为什么?
哦,顺便说一句,我用 selfbuild xml 编辑器创建了我的 xml 文件。在那里,这些值被写成一个字符串。
如何设置 LinkLabel 来处理特殊字符?
编辑[18.4.14]:
这是处理喜欢的类的完整代码:
using System;
using System.IO;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using Forms = System.Windows.Forms;
using System.Windows.Forms;
using System.Diagnostics;
namespace SourceView
{
class linklabel
{
private Forms.LinkLabel link;
private Forms.Label qlabel = new Forms.Label();
private int x, x2, y;
private String filename;
private String type;
private String path;
public linklabel(int X, int Y, String type)
{
link = new LinkLabel();
x = 95;
x2 = 15;
y = 30;
this.type = type;
switch (type)
{
case "web":
this.path = @"web\";
break;
case "pdf":
this.path = @"pdf\";
break;
case "pic":
this.path = @"graphics\";
break;
}
this.link.AutoSize = true;
this.link.Location = new System.Drawing.Point(x + X, y + Y);
this.link.Name = "link";
this.link.Size = new System.Drawing.Size(72, 17);
this.link.TabIndex = 4;
this.link.TabStop = true;
this.link.Text = "link";
this.link.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.link_LinkClicked);
this.qlabel.AutoSize = true;
this.qlabel.Location = new System.Drawing.Point(x2 + X, y + Y);
this.qlabel.Name = "qlabel";
this.qlabel.Size = new System.Drawing.Size(50, 17);
this.qlabel.Text = "vindex";
this.qlabel.TabIndex = 5;
this.qlabel.TabStop = true;
x2 = x2 + X;
x = x + X;
y = y + Y;
}
private void link_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
Console.WriteLine(this.filename);
try
{
if (this.filename.Contains("-"))
{
MessageBox.Show("Es ist ein Fehler in der von Ihnen angeforderten Datei aufgetreten. Bitte überprüfen Sie den Dateinamen auf Sonderzeichen und entfernen Sie diese auch in der entsprechenden Referenzdatei.");
}
System.Diagnostics.Process.Start(this.path + this.filename); //this.path + this.filename
}
catch (Exception ex)
{
MessageBox.Show(ex.Message + "\n" + "Bitte korrigieren Sie den Dateipfad in der Datei " + type + "Data.xml" + "\n" + "Fehler im String " + this.path + this.filename);
}
}
public void setTitle(String vindex, String title)
{
this.qlabel.Text = vindex;
this.link.Text = title;
}
public void setLink(String filename)
{
this.filename = filename;
}
public Forms.LinkLabel label
{
get { return link; }
}
public Forms.Label Qlabel
{
get { return qlabel; }
}
}
}
这是我用来显示项目的 4 个类之一:
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using Forms = System.Windows.Forms;
using System.Windows.Forms;
using System.Xml;
using System.Runtime.InteropServices;
using System.Drawing;
namespace SourceView
{
class WebPnl : IMessageFilter
{
private MainForm mf;
private Forms.Panel pnlWeb = new Forms.Panel();
private Forms.Label lblInfo = new Forms.Label();
private linklabel[] links;
String spacer = " ";
public WebPnl(MainForm aufrufer)
{
mf = aufrufer;
//
//lblInfo
///
this.lblInfo.Location = new System.Drawing.Point(5, 5);
this.lblInfo.Text = "Querverweis" + spacer + "Titel";
this.lblInfo.Size = new System.Drawing.Size(150, 25);
//
//pnlWeb
//
this.pnlWeb.Controls.Add(this.lblInfo);
this.pnlWeb.BackColor = System.Drawing.Color.Transparent;
this.pnlWeb.Location = new System.Drawing.Point(890, 75);
this.pnlWeb.Name = "pnlWeb";
this.pnlWeb.Size = new System.Drawing.Size(350, 695);
if(mf.Width >= 1400)
this.pnlWeb.Size = new System.Drawing.Size(490, 695);
this.pnlWeb.TabIndex = 4;
this.pnlWeb.AutoScroll = true;
this.pnlWeb.Visible = false;
}
//mouse wheel support for scrollable controls
#region mouse wheel support
[DllImport("user32.dll")]
private static extern IntPtr WindowFromPoint(Point pt);
[DllImport("user32.dll")]
private static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wp, IntPtr lp);
public bool PreFilterMessage(ref Message m)
{
if (m.Msg == 0x20a)
{
Point pos = new Point(m.LParam.ToInt32() & 0xffff, m.LParam.ToInt32() >> 16);
IntPtr hWnd = WindowFromPoint(pos);
if (hWnd != IntPtr.Zero && hWnd != m.HWnd && Control.FromHandle(hWnd) != null)
{
SendMessage(hWnd, m.Msg, m.WParam, m.LParam);
return true;
}
}
return false;
}
#endregion
public void Init()
{
loadWebPages();
mf.Controls.Add(pnlWeb);
}
public void show()
{
this.pnlWeb.Visible = true;
}
public void remove()
{
this.pnlWeb.Visible = false;
}
//helper function for loading
private string getElement(string field, ref XmlElement element)
{
string value = "";
try
{
value = element.GetElementsByTagName(field)[0].InnerText;
}
catch (Exception e)
{
//ignore error, return empty
Console.WriteLine(e.Message);
}
return value;
}
private void loadWebPages()
{
int l = 0;
try
{
XmlDocument doc = new XmlDocument();
doc.Load("webData.xml");
XmlNodeList list = doc.GetElementsByTagName("member");
l = list.Count;
links = new linklabel[l];
int i = 0;
int y = 5;
foreach (XmlNode node in list)
{
XmlElement element = (XmlElement)node;
links[i] = new linklabel(0, y, "web");
links[i].setTitle(getElement("vindex", ref element), getElement("name", ref element));
links[i].setLink(getElement("filename", ref element));
Console.WriteLine(getElement("filename", ref element));
this.pnlWeb.Controls.Add(links[i].label);
this.pnlWeb.Controls.Add(links[i].Qlabel);
Console.WriteLine(i);
i++;
y = y + 25;
}
}
catch (Exception es)
{
MessageBox.Show(es.Message);
}
}
}
}
我在win8下在这里做这个。当我单击其中一个维基百科条目时,它给了我一条错误消息,上面写着找不到该文件(维基百科 htm 文件)。在win7下,当我想显示链接(当我单击按钮时显示在面板上)时,错误消息会直接提示。它说,xml 文件中第一个“-”字符的位置存在字符编码错误。
然后我找到了一种方法来欺骗这个错误。我只是从 windows-explorer 复制了文件名 (htm) 并替换了 xml 文件中的字符串。瞧,它工作得很好。但为什么?