c# - Use image Button with transparent color area -
i have picture in png transparent , normal color.
i use 1 button:
this.button1.backcolor = system.drawing.color.transparent; this.button1.backgroundimage = image; this.button1.backgroundimagelayout = system.windows.forms.imagelayout.none; this.button1.flatappearance.bordersize = 0; this.button1.flatappearance.mousedownbackcolor = system.drawing.color.transparent; this.button1.flatappearance.mouseoverbackcolor = system.drawing.color.transparent; this.button1.flatstyle = system.windows.forms.flatstyle.flat; this.button1.location = new system.drawing.point(0, 0); this.button1.margin = new system.windows.forms.padding(0); this.button1.name = "skin"; this.button1.size = new system.drawing.size(294, 194); this.button1.tabindex = 7; this.button1.usevisualstylebackcolor = false;
and when click @ transparent area, still counts click on button.
how can make button call click event when click @ colored area?
and when click @ transparent area, want count click on object behind button.
you have 2 options:
use
button
region
.use
button
backgroundimage
, check user hits eachclick
option 1 feasible if can create region
, takes graphicspath
, means need create shape need graphics
primitves lines , curves etc..
if have bitmap
transparency, better off not using button
region
.
instead can use button1
, on every click check transparency of clicked pixel.
if transparent call click event of control below it..
private void button1_mouseclick(object sender, mouseeventargs e) { size r = button1.backgroundimage.size; // check have hit image , hit non-transparent pixel if ((e.x < r.width && e.y < r.height) && ((bitmap)button1.backgroundimage).getpixel(e.x, e.y).a != 0) { console.writeline("button clicked"); // test // or call click actions here! } // else pass click on.. else { // create valid mouseeventargs mouseeventargs ee = new mouseeventargs(e.button, e.clicks, e.x + button1.left, e.y + button1.top, e.delta); // pass on stuff below picturebox1_mouseclick(picturebox1, ee); console.writeline("button not clicked"); // test } }
note check assumes have normal layout, button image @ top left , no scaling. if need scale image should keep scaled bitmap checks on.. if can use unscale image should so, better.
note how create correct mouseeventargs
parameter control below, can access button or location of mouse there well..
also note easier use mouseclick
event instead of click
event has mouse location already..
if need/want use click
event instead, can skip creating eventargs
, doesn't have meaningful data; pass out e
click..
here how click
event start:
private void button1_click(object sender, eventargs e) { // need location of clicked pixel: point clicklocation = button1.pointtoclient(control.mouseposition); // rest can proceed above, simple eventargs..
if want check on mouse clicking event , pass each of them down parent have code them all.
first let's @ order of events on msdn
- mousedown event.
- click event.
- mouseclick
- mouseup event.
so need start @ mousedown
. can test in helper function hittest
, can re-use it..:
button clickedbutton = null; mouseeventargs ee = null; void hittest(button btn, mouseeventargs e) { size r = btn.backgroundimage.size; // check have hit image , hit non-transparent pixel if ((e.x < r.width && e.y < r.height) && ((bitmap)btn.backgroundimage).getpixel(e.x, e.y).a != 0) { clickedbutton = btn; ee = new mouseeventargs(e.button, e.clicks, e.x + btn.left, e.y + btn.top, e.delta); } else clickedbutton = null; }
now code 4 events. need call hittest
once , pass simple, unmodified e
parameter in click
event:
private void button1_mousedown(object sender, mouseeventargs e) { hittest(sender button, e); if (sender != clickedbutton) yourparent_mousedown((sender button).parent, ee); else // stuff } private void button1_click(object sender, eventargs e) { if (sender != clickedbutton) yourparent_click((sender button).parent, e); else // stuff } private void button1_mouseclick(object sender, mouseeventargs e) { if (sender != clickedbutton) yourparent_mouseclick((sender button).parent, ee); else // stuff } private void button1_mouseup(object sender, mouseeventargs e) { if (sender != clickedbutton) yourparent_mouseup((sender button).parent, ee); else // stuff }
of course need code 4 events yourparent
also..
Comments
Post a Comment