求解 richTextBox的问题
此时程序接收到了一句话,他要把该句话添加到richTextBox控件中,并给其中两个词“接受”、“拒绝”做成链接文本的样子,就像LinkLabel那样,单击它们,分别引发Accept()和Decline()两个事件。如何实现?
注意:链接文本是在程序运行时添加的.
------解决方案--------------------C#
private void RichTextBox_LinkClicked(object sender, System.Windows.Forms.LinkClickedEventArgs e)
{
//点击Http链接!
System.Diagnostics.Process.Start(e.LinkText);
}
这时候,你不要直接引发链接的跳转——而是打开那个对话框,你就可以进行下一步的工作了。
==================================================================
博客空间:http://blog.csdn.net/lovingkiss
资源下载:http://download.csdn.net/user/lovingkiss
优惠接单开发,组件控件定制开发,成品源代码批发
联系方式:Q64180940 全天在线
==================================================================
------解决方案--------------------//richTextBox1.DetectUrls = false; // 自动识别连接关闭
using System.Runtime.InteropServices;
[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;
}
[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;
public 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 button1_Click(object sender, EventArgs e)
{
string insertText = @ "床上等你 ";
string insertLink = @ "#http://www.csdn.net ";
string insertRtf = @ "{\rtf1 " +
TextToRtf(insertText) + @ "\v " + TextToRtf(insertLink) + @ "\v0} ";
int start = richTextBox1.SelectionStart;
richTextBox1.SelectedRtf = insertRtf;
richTextBox1.SelectionStart = start;
richTextBox1.SelectionLength = insertText.Length + insertLink.Length;
CHARFORMAT2A vCharFormat2a = new CHARFORMAT2A();
vCharFormat2a.cbSize = Marshal.SizeOf(typeof(CHARFORMAT2A));
vCharFormat2a.dwMask = CFM_LINK;
vCharFormat2a.dwEffects = CFE_LINK;
S