使用iamge作为c#形式的按钮? [英] use iamge as button in c# form ?

查看:77
本文介绍了使用iamge作为c#形式的按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好所有

i尝试使用地图等图片,当我点击地图时我会做一些动作。,,

问题是我该如何使用这张地图作为按钮没有使用按钮背景图像?

因为我需要显示这个地图只有它的边框没有映射到按钮上?

请帮助

解决方案

如果我理解你想要的东西,你想要一个与地图某些部分相关联的动作吗?



在这种情况下你需要将地图放在一个面板中,例如。并使用onmousedown / up事件捕获鼠标坐标,您可以确定在地图上用户单击的位置。此外,您可以使用鼠标悬停事件突出显示具有圆形,矩形,...或其他内容的区域。 (使用Graphics对象并传递面板的句柄)



听起来并不是那么难,MSDN有一些很好的例子。



希望这有帮助。


在winforms中没有直接等效的图像映射。有一种解决方法可以让你模仿它:



  1. 挂钩到托管图片的控件中MouseUp event
  2. 在事件处理程序中,获取鼠标的位置并对其做出反应/ li>





例如类似于:

  private   void  无论 _MouseUp(对象发​​送者,MouseEventArgs e)
{
Rectangle rect = new 矩形( 0 0 50 50 ); // 这是您想要点击的矩形
if (rect.Contains(e.Location))
{
// 做任何事
}
}







你可能是将有多个矩形 - 我建议将它们存储在Dictionary对象中(为了防止重复 - 你必须做额外的工作以避免重叠区域),该值将是一个被调用的委托方法。如果你打算使用更复杂的形状,那么你就不能使用Rectangle对象了 - 原则就是这样,但是你必须自己想出一些东西,不过我敢说快速的谷歌会有所帮助。


有几种方法可以解决这个问题,但是你需要澄清你的目标。



0.是你想要的区域的结构 active:ie,表现得像一个按钮,常规/平铺,以便您可以根据MouseDown事件发生的点使用数学轻松计算哪个区域被命中?示例:像棋盘一样的结构。



1.您想要激活的地图中有多个区域?



2.您希望活动矩形区域,或圆形区域,或由不规则路径(区域)定义的复杂区域?



3.你是当鼠标移动到活动区域时,是否希望某种可见指示符出现在地图上?



4.您希望最终用户在运行时间到创建/编辑活动区域,或者,它们是否都是在应用程序中的代码中预先计算出来的?



根据您对这些问题的回答,解决方案范围从简单到复杂。



为什么不通过研究CP上的这篇文章来获得一些想法:C#Windows Forms ImageMap Control:[ ^ ]。并且,搜索CP以获取ImageMap以获得其他想法。



如果所有活动区域都是矩形,并且您不需要某种形式的叠加来弹出当鼠标在活动区域​​上方,并且所有活动区域都是由您在设计时创建的,我有一个简单的快速而肮脏的解决方案我愿意发布...如果您通过回答问题来阐明您的目标上方。

hello all
i try to use image such as map , when i click on map i do some actions .,,
the question is how can i use this map as button without using button background image ?
because i need to display this map only with its borders not map on button ?
please help

解决方案

If I understand what you want you'ld like to have an action associated with certain portions of the map ?

In that case you need to put the map in a panel eg. and use the onmousedown/up events to capture the mouse coordinates, with that you can determin where on the map, the user click. Additionally you can use the mouseover event to highlight that region with a circle, rectangle, ,... or something. (Use the Graphics object and pass the panel's handle)

It is really not that hard as it sounds and MSDN has some good examples.

Hope this helps.


There isn't the direct equivalent of an image map in winforms. There is a workaround that lets you emulate it:


  1. Hook into the control hosting the image's MouseUp event
  2. In the event handler, get the position of the mouse and react to it/li>



e.g. something like:

private void whatever_MouseUp(Object sender, MouseEventArgs e)
{
    Rectangle rect = new Rectangle(0, 0 50, 50);  //this is rectangle you want clickable 
    if (rect.Contains(e.Location))
    {
        //Do whatever
    }
}




You are probably going to have multiple rectangles - I suggest storing them in a Dictionary objects (to prevent duplicates - you will have to do extra work to avoid overlapping areas), the value would be a the delegate method that is called. If you are going to use more complex shapes then you can't use the Rectangle object - the principle stands, but you'll have to come up with something yourself, though I dare say a quick Google would help.


There are several ways you could approach this, but you need to clarify your goals.

0. is the structure of the zones you want "active:" i.e., to behave like a "button," regular/tiled so that you can easily calculate which area is "hit" using mathematics based on the Point where the MouseDown event occurs ? Example: a structure like a chessboard.

1. is there more than one area in the map you want active ?

2. are the areas you want active rectangular, or circular, or, complex areas defined by irregular paths (regions) ?

3. do you want some kind of visible indicator to appear on the map when the mouse moves over an active area ?

4. do you want the end-user at run-time to create/edit active zones, or, are they all pre-calculated in code in your application ?

Depending on your answer to these questions, solutions range from simple to complex.

Why don't you get some ideas by studying this article on CP: "C# Windows Forms ImageMap Control:" [^]. And, search CP for "ImageMap" to get other ideas.

If all your active areas are rectangular, and you do not need some form of overlay to pop-up when the mouse is over an active area, and all active areas are created by you at design-time, I have a simple "quick and dirty" solution I'm willing to post ... if you clarify your goals by answering the questions above.


这篇关于使用iamge作为c#形式的按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆