我在GUI中渲染一些文本并尝试重新识别URL。此外,我还需要在执行单击事件时在浏览器中打开URL。我在下面附上了模型代码片段。我的问题是如何使这些文本成为有效的URL以及如何使它们响应点击事件。还有一件事由于某些要求我无法改变下面代码snnipet的结构。
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
答案 0 :(得分:0)
如果您只是将文字作为图形写入屏幕,则无法使其可点击。您需要在正确的位置放置LinkLabel
或其他可点击控件,以触发浏览器中的网址开启。
答案 1 :(得分:0)
感谢您的回复。我进一步分析并找到了一种方法。在文本上绘制矩形并使用Rectangle在鼠标单击事件中查找矩形。含有(MouseLeftClikLocation)。
现在我有另一个问题。如果执行比例转换,我无法在恢复图形后获取该位置。
请在下面找到代码snnipet。
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;
}
}