1

我在 GUI 中呈现一些文本并尝试重新识别 URL。我还需要在执行点击事件时在浏览器中打开 URL。我在下面附上了模型代码片段。我的问题是如何使这些文本成为有效的 URL,以及如何使它们响应点击事件。还有一件事,由于某些要求,我无法更改以下代码片段的结构。

namespace Model
{
    public partial class ParentView : Form
    {        
        public ParentView()
        {
            InitializeComponent();            
        }
        private void ParentView_Paint(object sender, PaintEventArgs e)
        {
            GraphicsState state = e.Graphics.Save();
            GraphicsRenderer renderer = new GraphicsRenderer(e.Graphics);
            renderer.RenderAsImage();
            e.Graphics.Restore(state);
            e.Graphics.DrawRectangle(new Pen(Brushes.Black), 0, 0, 300, 300);
            //I believe need to do something here
        }        
    }  

public class GraphicsRenderer
{ 
    Graphics PageGraphics;
    public GraphicsRenderer(Graphics g)
    {
        PageGraphics = g;
    }

    public void RenderAsImage()
    {
        Point currentLocation = new Point(0, 0);

        PageGraphics.TranslateTransform(20, 40);
        PageGraphics.DrawString("www.google.com", new Font("Arial", 12, FontStyle.Regular, GraphicsUnit.Point), Brushes.Black, currentLocation);

        PageGraphics.TranslateTransform(20, -40);
        PageGraphics.DrawString("www.stackoverflow.com", new Font("Arial", 12, FontStyle.Regular, GraphicsUnit.Point), Brushes.Black, currentLocation);

        currentLocation = new Point(50, 60);
        PageGraphics.DrawString("www.stackoverflow.com", new Font("Arial", 12, FontStyle.Regular, GraphicsUnit.Point), Brushes.Black, currentLocation);
    }
}    
}

谢谢, Mkn

4

2 回答 2

0

如果您只是将文本作为图形写入屏幕,则无法使其可点击。您需要LinkLabel在正确的位置放置一个或其他可点击控件,以触发在浏览器中打开 URL。

于 2012-09-18T09:23:38.353 回答
0

感谢您的答复。我进一步分析并找到了方法。在文本上绘制矩形并使用 Rectangle 在鼠标单击事件中查找矩形。包含(MouseLeftClikLocation)。

现在我遇到了另一个问题。如果执行比例转换,我无法在恢复图形后获得位置。

请在下面找到代码片段。

private void ParentView_Paint(object sender, PaintEventArgs e)
{
    GraphicsState state = e.Graphics.Save();
    GraphicsRenderer renderer = new GraphicsRenderer(e.Graphics);
    renderer.RenderAsImage();
    e.Graphics.Restore(state);
    e.Graphics.DrawRectangle(new Pen(Brushes.Black), 0, 0, 300, 300);

    //I am changing here
    float[] transformPoints = renderer.transformPoints.Elements;
    e.Graphics.TranslateTransform(transformPoints[4], transformPoints[5]);
    Point currentLocation = renderer.currentLocation;
    e.Graphics.DrawRectangle(new Pen(Brushes.Black),new Rectangle(currentLocation.X,currentLocation.Y,190,30));
}
public class GraphicsRenderer
{
    Graphics PageGraphics;
    internal Matrix transformPoints = new Matrix();
    internal Point currentLocation = new Point(0, 0);

    public GraphicsRenderer(Graphics g)
    {
        PageGraphics = g;
    }

    public void RenderAsImage()
    {
        currentLocation = new Point(50, 60);
        //PageGraphics.ScaleTransform(3, 3);
        PageGraphics.DrawString("www.bing.com", new Font("Arial", 12, FontStyle.Regular, GraphicsUnit.Point), Brushes.Black, currentLocation);
        transformPoints = PageGraphics.Transform;
    }
}
于 2012-09-20T05:29:09.127 回答