逍遥先森丶 2018-06-20 08:01 采纳率: 100%
浏览 973
已采纳

Winform中RichTextBox 一次添加多个超链接问题

上图是我想实现的效果。

现在的问题是:
当我在一个RichTextBox中同时插入多个超链接文本时,只有最后一个的linktext是正确的,之前的所有超链接,在触发LinkClicked事件时,LinkText都不完整。

求大神支招!!
下面是完整代码。

public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

    [DllImport("user32.dll ", CharSet = CharSet.Auto)]
    public static extern IntPtr SendMessage(IntPtr hWnd, int msg, int wParam,
            [In, Out, MarshalAs(UnmanagedType.LPStruct)]   CHARFORMAT2A lParam);

    public const int CFE_LINK = 0x20;
    public const int CFM_LINK = 0x20;
    public const int EM_SETCHARFORMAT = 0x444;
    public const int SCF_SELECTION = 1;

    private void Form1_Load(object sender, EventArgs e)
    {

    }


    private void button1_Click(object sender, EventArgs e)
    {
        FileMsgAdd(@"打开所在文件夹", @"#" + Application.StartupPath + "\\ReceiveFile\\", richTextBox1);
        this.richTextBox1.AppendText("    ");
        FileMsgAdd(@"打开文件", @"#" + Application.StartupPath + "\\ReceiveFile\\22.txt", richTextBox1);
    }

    private void FileMsgAdd(string fileMsg, string path, RichTextBox box)
    {
        string insertRtf = @"{\rtf1" + TextToRtf(fileMsg) + @"\v" + TextToRtf(path) + @"\v0}";
        int iniPos = box.SelectionStart;
        box.SelectedRtf = insertRtf;
        box.SelectionStart = iniPos;
        box.SelectionLength = fileMsg.Length + path.Length;

        CHARFORMAT2A vCharFormat2a = new CHARFORMAT2A();
        vCharFormat2a.cbSize = Marshal.SizeOf(typeof(CHARFORMAT2A));
        vCharFormat2a.dwMask = CFM_LINK;
        vCharFormat2a.dwEffects = CFE_LINK;
        SendMessage(box.Handle, EM_SETCHARFORMAT, SCF_SELECTION,
                vCharFormat2a);
    }

    private string TextToRtf(string AText)
    {
        string vReturn = "";
        foreach (char vChar in AText)
        {
            switch (vChar)
            {
                case '\\':
                    vReturn += @"\\";
                    break;
                case '{':
                    vReturn += @"\{";
                    break;
                case '}':
                    vReturn += @"\}";
                    break;
                default:
                    if (vChar > (char)127)
                        vReturn += @"\u" + ((int)vChar).ToString() + "?";
                    else
                        vReturn += vChar;
                    break;
            }
        }
        return vReturn;
    }

    private void richTextBox1_LinkClicked(object sender, LinkClickedEventArgs e)
    {
        System.Diagnostics.Process.Start(e.LinkText.Substring(e.LinkText.IndexOf('#') + 1));
    }
}

[StructLayout(LayoutKind.Sequential, Pack = 4)]
public class CHARFORMAT2A
{
    public int cbSize;
    public int dwMask;
    public int dwEffects;
    public int yHeight;
    public int yOffset;
    public int crTextColor;
    public byte bCharSet;
    public byte bPitchAndFamily;
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 0x20)]
    public byte[] szFaceName = new byte[0x20];
    public short wWeight;
    public short sSpacing;
    public int crBackColor;
    public int lcid;
    public int dwReserved;
    public short sStyle;
    public short wKerning;
    public byte bUnderlineType;
    public byte bAnimation;
    public byte bRevAuthor;
}
  • 写回答

1条回答 默认 最新

  • devmiao 2018-06-20 15:06
    关注

    调试看下你的AText对不对

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 maccms影视模板 制作影视网站失败 求
  • ¥15 stm32按键设置闹钟数进退位不正常
  • ¥15 三电平逆变器中点电位平衡问题
  • ¥20 这怎么写啊 java课设
  • ¥15 用C语言完成一个复杂的游戏
  • ¥15 如何批量更改很多个文件夹里的文件名中包含文件夹名?
  • ¥50 MTK手机模拟HID鼠标出现卡顿
  • ¥20 求下下面这个数据结构代码
  • ¥20 前端 二进制文件流图片转化异常
  • ¥15 github上的这个C语言项目如何跑起来