C#Winforms Region.可见

编程入门 行业动态 更新时间:2024-10-21 12:46:47
本文介绍了C#Winforms Region.可见的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想检测在我自定义创建的区域上的鼠标单击.

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.可见

本文发布于:2023-11-08 07:12:34,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1568696.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Winforms   Region

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!