我想检测在我自定义创建的区域上的鼠标单击.
I want to detect mouse click on my custom created region.
1)我用矩形尝试了这段代码,并且可以用,但是用字符串却没有用
1) I ve tried this code with rectangle and it worked, but with string it doesnt
GraphicsPath gp = new GraphicsPath(); Region reg = new Region(); private void Form1_Load(object sender, EventArgs e) { gp.AddString("TEXT", new FontFamily("Arial"),0, 20.0f, new Point(300, 10), StringFormat.GenericDefault); gp.Widen(Pens.AliceBlue); reg = new Region(gp); }这是第2部分
private void panel1_MouseDown(object sender, MouseEventArgs e) { if (reg.IsVisible(e.Location)) { MessageBox.Show("aaaa"); } }它不显示消息框. :)
It doesnt show the message box. :)
这是我的绘画事件,查看我的字符串在哪里
EDIT :here is my Paint event to see where my string is
private void panel1_Paint(object sender, PaintEventArgs e) { e.Graphics.DrawString("TEXT", new Font("Arial", 20), Brushes.Yellow, 300,100 ); }推荐答案
最基本的错误是错字:一次在y = 10上绘画,另一次在y = 100上绘画.
The most basic error is a typo: One time you draw at y = 10, the other time at y = 100.
但是还有另一个根本不那么明显的问题:
But there is another issue which is not so obvious at all:
添加
e.Graphics.FillPath(Brushes.Firebrick, gp);到Paint事件,您会看到它:字体大小完全不同.
to the Paint event and you'll see it: The fonts have quite a different size.
这是因为在将文本添加到GraphicsPath时,它使用的缩放比例(称为'emSize')与使用'Point'.
That is because when adding text to a GraphicsPath it is using a different scale (called 'emSize') than Graphics.DrawString does, which uses 'Point'.
要进行调整,您可以使用以下方法:
To adapt you can use this:
float fontsize = 20.0f; using (Graphics g = panel1.CreateGraphics()) fontsize *= g.DpiY / 72f;现在,您可以使用正确的坐标来最好地构建GraphicsPath.
Now you can build the GraphicsPath, best with the correct coordinates..:
gp.AddString("TEXT", new FontFamily("Arial"), 0, fontsize, new Point(300, 100), StringFormat.GenericDefault);更多推荐
C#Winforms Region.可见
发布评论