By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
460,028 Members | 1,062 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 460,028 IT Pros & Developers. It's quick & easy.

reflect mouse on label to button

P: 92
first, i don't know what could i possibly write for the "question" field...

i have a button, and i put a label over it, i did that, and transparency and other things are not a problem for me, the problem is, when i take mouse over the label, the button no more has the "effect" which comes when mouse is over it (blue colored in vista/7) so what i wanna do is, somehow make button think the mouse is over it when the mouse is actually over the label (that label is over button), and make it blue..., sorry if you don't get my english :( i don't know how to explain this.
Dec 30 '09 #1
Share this Question
Share on Google+
59 Replies


P: 92
forgot to add, i cannot use draw (OnPaint) text over button cause it looks something like this:

Dec 30 '09 #2

tlhintoq
Expert 2.5K+
P: 3,525
Why are you using a label at all?
Why are you not using the text feature of the button?
Dec 31 '09 #3

P: 92
i want to place the text at different locations, not just in middle..., and it will NOT have only a single line of text (like OK, Cancel or anything else).
Dec 31 '09 #4

tlhintoq
Expert 2.5K+
P: 3,525
Change the TextAlignment property. You can place it in 9 different positions, not just the middle.

You can put a newline in your text to give it line breaks.

I just think your approach of a label over the button is a hack that is already causing you issues. Either use it as-is, or use a custom-draw function.

forgot to add, i cannot use draw (OnPaint) text over button cause it looks something like this:
Then make your custom draw routine more elaborate to do what you want.
Dec 31 '09 #5

P: 92
the line breaking is a little complexed, i have seen every possibility i could use, and the only way i see is labels..., i cannot draw(OnPaint), cause as you can see in above post, the draw'ed text dose not look good, while label is good..., so......
Dec 31 '09 #6

P: 92
okay, so i did some tweaking in custom drawing, the problem in drawing is it draws the text more than just 1 time, thats why it looks ugly... so i need a way to clear the old written text when its writing it again, i tried the Clear method in e.Graphics of button OnPaint event, it clears all the graphics but also put a static backround (that it asks in args), but as you can see the button is not only single colored, so... i dont know what to do :| help?
Dec 31 '09 #7

tlhintoq
Expert 2.5K+
P: 3,525
OnPaint should only occur when an area needs to be refreshed/redrawn, such as when an object has been obscured by another window, then returns to visibility.

It really shouldn't be drawing more than once. Sounds like you may be calling it manually as well as the OS calling it - or some other logic problem.
Dec 31 '09 #8

P: 92
well i don't know, i never call the Refresh or raise event manually in code, the problem what i see is, it is not erasing the previous written text on the button...

Expand|Select|Wrap|Line Numbers
  1. e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
  2. e.Graphics.DrawString("Sample Drawing...", Font, Brushes.Navy, new PointF(0, 0));
  3. //TextRenderer.DrawText(e.Graphics, "Sample Drawing...", Font, new Point(0, 0), Color.Navy);
this is the code i write in OnPaint, the g.DrawString, or TextRenderer both work, and i dont see any difference in both...
Dec 31 '09 #9

tlhintoq
Expert 2.5K+
P: 3,525
But this isn't liquid ink that soaks in and bleeds. As long as the location is the same, and the font is the same, you are drawing the exact same pixels over each other. You could draw it 100 times and it wouldn't become a thicker line.

As a test put in a property that you can test for. The string itself perhaps. If that string is already written, then don't write it again.
Dec 31 '09 #10

P: 92
If that string is already written, then don't write it again.
How can i check if the string is already written there....

But this isn't liquid ink that soaks in and bleeds. As long as the location is the same, and the font is the same, you are drawing the exact same pixels over each other. You could draw it 100 times and it wouldn't become a thicker line.
I understand but i dont know why and how this is happening, i managed to somehow print text just once it looked good, but when i take mouse over button, it then flickers, so the way i managed to print once was not good...
Dec 31 '09 #11

tlhintoq
Expert 2.5K+
P: 3,525
If that string is already written, then don't write it again.
How can i check if the string is already written there....
Add a property to check. A button already has text property. You can check to see what the .Text of a button is. But if you set it then that becomes the text on the button through the built-in Text writing methods.

You don't like the built-in text writing methods.

So you could *override* the .Text property to use your methods.
Or create a new property so as to not interfer with the .Text property.
Maybe make a "Caption" property for example.

Then you check the new Caption property to see if it is already the same as the text being passed in. If it is, just return without doing anything.
Dec 31 '09 #12

tlhintoq
Expert 2.5K+
P: 3,525
I'm just curious... What is it that you want to do better/different than the built-in text of a button? The built-in features let you change font, color, size, direction, placement. What does the built-in feature lack that you need?

Additionally the built-in features are aware of system wide themes and preferences. If a user has set their system preferences to larger fonts because they have poor vision then a button already reacts to that. Are you going to write your custom methods to do this as well, or are you going to have your program ignore system-wide user preferences?
Dec 31 '09 #13

P: 92
i didnt understand what u meant by doing the property stuff...

Additionally the built-in features are aware of system wide themes and preferences. If a user has set their system preferences to larger fonts because they have poor vision then a button already reacts to that.
No, i m not going to take that in account, i will ignore "system-wide user prefences'. Actually, i m developing a application sort of Organizer, it has a calander-type interface, when u click on a date it shows events on the date, i wanna display the events in like.. a button for a event... now i have to display the time of event, reoccurence, title and discription, but i cant use label cause then the "effect" of button when mouse is over it is not working..., i use button cause i want that "glow" effect of button.
Dec 31 '09 #14

tlhintoq
Expert 2.5K+
P: 3,525
Sounds like a very ambitious project to tackle this early in your learning.

i didnt understand what u meant by doing the property stuff...
Do you know what a property is?
Have you ever written something that looks like this?
Expand|Select|Wrap|Line Numbers
  1. private string _caption;
  2. public string Caption
  3. {
  4.     get
  5.     {
  6.          return _caption;
  7.     }
  8.     set
  9.     {
  10.           if (value != _caption)
  11.           {
  12.               // Then it is a new value
  13.               DoCustomDraw(value);
  14.           }
  15.           _caption = value;          
  16.     }
  17. }
By the way, that was a hint about your need for a caption property.
Dec 31 '09 #15

P: 92
i m not doing "early" learning, i know what property is, but i didnt understand what u meant by Text property for textbox, i draw text manually OnPaint event, how can it be in Text property =_=
Dec 31 '09 #16

tlhintoq
Expert 2.5K+
P: 3,525
i didnt understand what u meant by doing the property stuff...
i m not doing "early" learning, i know what property is
Please forgive me for misinterpreting your remark of "I don't understand what you mean by property" as thinking you didn't understand what a property was. Silly me.

So you could *override* the .Text property to use your methods.
... i draw text manually OnPaint event, how can it be in Text property
If the help you are receiving isn't worth reading in it's entirety or thoroughly then I'm done wasting my time by writing it and spelling everything out including the code that you couldn't figure out by description, and guiding you away from bad ideas like labels on top of buttons.

I wish you the best of luck with your current project, and any future projects in the new year.
Dec 31 '09 #17

P: 92
argh, okay thanks for your help, happy new year btw. Still i think you dont get what problem i have here. I cannot use a variable/property to check if the text is drawn there..., or maybe i am missing something.
Dec 31 '09 #18

tlhintoq
Expert 2.5K+
P: 3,525
or maybe i am missing something
You are missing something.
It's simple. Maybe you are just over thinking it.

You are going to set the property 'Caption' to the text you want on the button. When you set that with a new string that triggers the custom draw. The custom draw references the Caption property to know what text to put on the button.

When you custom draw your text you set a property equal to that string.
That property now holds the last known string drawn on the face of the button.

At the beginning of your custom draw function you check this property to see if it is the string you are about to draw. If the last string drawn is the same as the string you are about to draw then don't draw it.

Just look at the code I gave you
Expand|Select|Wrap|Line Numbers
  1. private string _caption;
  2. public string Caption
  3. {
  4.     get
  5.     {
  6.          return _caption;
  7.     }
  8.     set
  9.     {
  10.           if (value != _caption)
  11.           {
  12.               // Then it is a new value
  13.               DoCustomDraw(value);
  14.           }
  15.           _caption = value;          
  16.     }
  17. }
I have no idea what your custom draw routine looks like.
If you need to, add a variable to it of "LastDrawnText".
Set LastDrawnText after drawing the text

At the start of drawing check to see if LastDrawnText == Caption.
If it is then return without drawing.
Jan 1 '10 #19

P: 92
now i understand, see the code i wrote:
Expand|Select|Wrap|Line Numbers
  1.  
  2.         private void task_show_Load(object sender, EventArgs e)
  3.         {
  4.             _brush = Brushes.Navy;
  5.             Date = "Sample Drawing...";
  6.         }
  7.         #endregion
  8.  
  9.         #region properties
  10.         private string Date
  11.         {
  12.             get { return _date; }
  13.             set
  14.             {
  15.                 if (value.Equals(_date)) return;
  16.  
  17.                 DrawDate(value);
  18.                 _date = value;
  19.             }
  20.         }
  21.         #endregion
  22.  
  23.         #region text painting and stuff
  24.         private void DrawDate(string date)
  25.         {
  26.             Graphics g = back.CreateGraphics();
  27.             g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
  28.             g.DrawString(date, Font, Brush, new PointF(0, 0));
  29.             //TextRenderer.DrawText(g, "Sample Drawing...", Font, new Point(0, 0), Color.Navy);
  30.             g.Dispose();
  31.         }
  32.        #endregion
  33.  
but now the problem is, as you can see i give Date the value on Load event, but the button redraws immediately after drawing the text.. and... the text disappears..
Jan 1 '10 #20

GaryTexmo
Expert 100+
P: 1,501
I had a thought, hopefully this helps...

Can you just override the paint method on a button to do what you want? I've only been loosely following this thread but looking at the image you posted, the following short snippet of code should give you want you want, and let you set it up through the Designer. Feel free to modify in whatever way you like.

Expand|Select|Wrap|Line Numbers
  1.     class LButton : Button
  2.     {
  3.         [Category("Appearance")]
  4.         public string LabelText { get; set; }
  5.  
  6.         [Category("Appearance")]
  7.         public Point LabelLocation { get; set; }
  8.  
  9.         protected override void OnPaint(PaintEventArgs pevent)
  10.         {
  11.             base.OnPaint(pevent);
  12.  
  13.             if (pevent != null)
  14.             {
  15.                 pevent.Graphics.DrawString(LabelText, this.Font, Brushes.Black, LabelLocation);
  16.             }
  17.         }
  18.     }
Note: I just used auto-implemented properties to save space in this example... again, you can do what you like in there.
Jan 1 '10 #21

P: 92
the result is same as previous image...

Jan 1 '10 #22

GaryTexmo
Expert 100+
P: 1,501
Oh, haha, well serves me right for not reading the whole thing. I thought you photoshopped that as a demo of what you *wanted*, my mistake!

Let me see if I've understood this correctly, when you mouse over the label (or text on the image), you want the whole button to appear as if it's been moused over? Or just blue?
Jan 1 '10 #23

P: 92
yes sir, you got it, when i take the mouse over label (that is on button) i want the button to appear like mouse overed.. the "yellow" glow for this button
Jan 1 '10 #24

GaryTexmo
Expert 100+
P: 1,501
I've gotta run, but I played around a bit and have something that may help you. I'm trying to take a different approach than you and Thlintoq have been so far, in hopes that a different viewpoint may help out :)

Expand|Select|Wrap|Line Numbers
  1.         protected override void OnMouseMove(MouseEventArgs mevent)
  2.         {
  3.             base.OnMouseMove(mevent);
  4.  
  5.             Graphics g = this.CreateGraphics();
  6.             SizeF strSize = g.MeasureString(m_labelText, this.Font);
  7.  
  8.             RectangleF labelRect = new RectangleF(m_labelLocation, strSize);
  9.  
  10.             if (labelRect.Contains(new Point(mevent.X, mevent.Y)))
  11.             {
  12.  
  13.             }
  14.         }
I tried setting the background colour when I was over the label and it worked, but it was just colour, not changing the button's state to whatever it would be when the mouse is down. I was looking around via google but ran outta time.

If that was all that's holding you back, sorry... the thread up until now is a little disjointed, at least to me.

Good luck!
Jan 1 '10 #25

tlhintoq
Expert 2.5K+
P: 3,525
I had a thought, hopefully this helps...

Can you just override the paint method on a button to do what you want?
When Vishal said he had already tried a custom draw routine for the button I thought he had done just that: A custom draw routine in a button (derived) control. I had no idea that he was actually trying to fake it by drawing some text from the main part of his application. Since he started out by trying to fake it with a label placed on top of the button I guess I shouldn't have made the assumption.

I said it before and I'll say it again... This project is more than he should be doing at this early stage of learning.

Vishal: I admire that you want to produce something worthwhile, meaningful, productive. It's good to aim big. But we all have to walk before we can run. I don't want to see you get so frustrated that you give up all together.

Vishal: You are missing several basic concepts in this style of programming.
  • The one I feel very strongly about is that each object whether it be a button, or a form, or a custom class that contains data about your dog... each object is responsible for itself and ONLY itself. It is a black box with inputs and outputs but what is inside and how it works is SUPPOSED to be a mystery to every other object. It is NOT the responsibility of the form to change how the button draws or looks.
  • Next comes reusability. If your form is doing all the heavy lifting for your fancy button what happens when you want to use 5 or 6 of those buttons on a form? What happens when you want to use that same kind of button in a different project? Your custom button should be a control in your toolbox that you can drag and drop on any form: Just exactly the way you do with a standard button, or a textbox or a label. If you have to keep doing copy/paste/alter from project to project to project you are never going to be able to create a uniform look, or produce a finished product that just stands on its own without continuous adjustment and fixes.
  • Then comes inheritance. You can create a "mammal" class, then a "canine" class that inherits from 'mammal', then a 'poodle' class that inherits from 'canine'. With each inheritance down the line the offspring become more and more specialized. The same is done with controls You can create your own button that INHERITS from the standard button, then make small changes to have to do what you want.

I am NOT suggesting that you give up on this project. But I am suggesting that you not see it as one bit project. You can use this a learning project that can teach you many things if you just carve it up into lots of smaller projects. It helps if you can envision this as a lot of little projects put together. Its like driving from the East Coast of a country to the West Coast. You don't have just point 'A' and point 'B'. You have to break up the trip with lots of smaller milestones. Stop every 500 km for fuel and look at a map. Am I still on course? Do I have everything I need for the next 500 km? But every stop is still pointing at the final goal. Same thing in programming. Learning to make properties is the first stop along the way to making a custom control. The custom control is a component inside one of many larger components. Those larger components come together to make a GUI for one mode of the application. Several GUIs come together to be the entire program.

If you continue to try to attack this program as one bit monster it's going to eat you alive.

Try this:
Make a new project whose sole purpose is to be a custom control, derived from a Button. Make it what you want it to be. Learn about properties. Make this custom button control everything you want it to be.

Then you can use that button in a larger project.
Here is a shot from my own Toolbox. You can plainly see custom controls such as an IPAddressControl whose purpose and behaviour is to handle IP addresses in their proper form like 123.456.789.001 This isn't done by a form Or an LEDclock control. Or a SerialCtrl that handles all of my serial port needs. I promise I' not trying to discourage you or belittle you. I'm just trying to guide you in a direction that is proven and works through the coding community.

Jan 1 '10 #26

P: 92
tlhintoq, you dont know my programe structure.. i used the "custom" control for button, i used inheritance , its in the toolbox.. and i m NOT in early stage of learning =_=, i been doing all this from past 2 years, i guess early is the first 6 months. But then again, you never stop learning in this vast programming language, and if you ever finish a language you got more of em coming...
Jan 1 '10 #27

tlhintoq
Expert 2.5K+
P: 3,525
Please don't be offended. "Early" to me has nothing to do with time. I certainly don't know near as much as I would like. A person can take 2 years and complete 1 project. Or the same 2 years and complete 100. 1 project might have taught a person 1 thing, or it can teach them 1,000. Some people write a program for the purpose of making mistakes through experience - then re-write it based on that experience - then re-write that. Others keep massaging the original project. Everyone has their own style.

You might be a whiz-bang at databases... or TCP/ip protocol. I don't know. But in *this* area it is fair to say 'early' in your learning based on the approach you took to *this* problem.

I am now bowing out of this thread.
Jan 1 '10 #28

P: 92
i m not really offended, i agree.. infact.. i did that ... i made a quiz for the re-write... there are like 20 versions of it re-written :P, and i still want a way to draw the text (not ugly way) or make button "think" the mouse is over it while its over label
Jan 1 '10 #29

P: 92
gary, i didnt really understood your code?
Jan 2 '10 #30

GaryTexmo
Expert 100+
P: 1,501
Which part didn't you understand? We'll start there :)
Jan 2 '10 #31

P: 92
Expand|Select|Wrap|Line Numbers
  1. SizeF strSize = g.MeasureString(m_labelText, this.Font);
  2.  
  3.             RectangleF labelRect = new RectangleF(m_labelLocation, strSize);
  4.  
  5.             if (labelRect.Contains(new Point(mevent.X, mevent.Y)))
  6.             {
  7.  
  8.             }
what are you exactly trying to do here?
Jan 2 '10 #32

GaryTexmo
Expert 100+
P: 1,501
line numbers per code block above...

* (Line 1) g.MeasureString gets the size of the string, in pixels, and stores it in a SizeF object, which is basically the width and the height of the text.

* (Line 3) I create a rectangle starting at the location where I draw my label, and having the width and the height of my text.

* (Line 5) I check to see if my rectangle contains the current mouse location, which is basically the same as saying...
Expand|Select|Wrap|Line Numbers
  1. if (mevent.X >= labelRect.Left && mevent.X <= labelRect.Right && mevent.Y >= labelRect.Top && mevent.Y <= labelRect.Bottom)
Jan 3 '10 #33

GaryTexmo
Expert 100+
P: 1,501
I forgot to add when I posted this yesterday... In the future, when you're uncertain on things, try MSDN. Usually they can provide a pretty good explanation of what the methods and classes in the .NET Framework do. These ones were pretty basic though, so I wrote 'em out.
Jan 3 '10 #34

P: 92
how is your code supposed to check if the text is already drawn, i dont get it :S, i just want something that draws clear text like a label control, or some way to make the button think mouse is over it (so it makes that yellow glow) while the mouse actually is over label.., btw! my application's all codes are 99% done (just need some little polishing and optimization), only GUI part is left, and thats... this button thing i am stucked at..
Jan 4 '10 #35

GaryTexmo
Expert 100+
P: 1,501
Oh, it doesn't check if the text is already drawn. I'm trying to show you a different approach than what you had.

I'm drawing the text after the button itself draws, every time. As for the glow, I don't know that either, I've been trying to find it but haven't had much time to search. You'd think there would be a way to tell the button which state it's in for drawing, but maybe it just handles that in the base event handlers.

Out of curiosity, does it have to look like it's clicked/highlighted, or can you do something else? The code I gave you up there will detect when the mouse is over the label, so you could put pretty much whatever you want in there.
Jan 4 '10 #36

P: 92
its like this, its a popup window that comes when you click on a date (its like a calendar interface), this window has all the events listed on that date, now everything is done (the behind-the-scene codes) just "displaying" it is where i m stuck for now, i want to display it like a button for each event, also make it like a button, so when someone clicks it, it opens edit window, just the problem is, i wanna show title, description, time, and reoccurrence of the event on that "button", i made a custom control but as you can see.. i cant find any way to make the button "look/glow" like it would when mouse would be over it instead of label, and when i draw text manually OnPaint it just looks ugly... if you have any other idea of what should i use instead of a button + label (that also looks good) then speak it up! :P. The code you gave.. i don't think it have any use of it here in this situation.
Jan 4 '10 #37

GaryTexmo
Expert 100+
P: 1,501
So far as I can tell, text drawn manually looks the same as if you used a label, I'm not sure what you're seeing that's different.

Anyway, I think the only thing I can tell you at this point is to google around, maaaaaybe you'll find something out there. My solution, and I think thlintoq's too, involves making your own custom control. If you don't want to do that, you're going to have to come up with something fairly sneaky and/or elaborate, which means it's your own creation and people can't really help you with it.

Good luck though!
Jan 4 '10 #38

P: 92
you can see the difference here...


Sample Drawing.. is the drawn text OnPaint
and Sample Label is the label..
Jan 4 '10 #39

GaryTexmo
Expert 100+
P: 1,501
Sample drawing looks like it's being drawn in bold... is the font you're giving the Graphics.DrawString method a bold font?

In the example I gave you, the drawn text and the button text looked the same and the Graphics.DrawString method took the font of the button.
Jan 4 '10 #40

P: 92
i use the Font property of label to draw the text, so the Sample Drawing... is same font as Label, for Brush i use Brushes.Navy.
Jan 5 '10 #41

GaryTexmo
Expert 100+
P: 1,501
Then that's very, very odd. I'm definitely not seeing that on my end.
Jan 5 '10 #42

P: 92
well then what to do now.. i cant make my own "completely" custom draw routine (that is remove button and draw button my own completely), i m screwed that way..
Jan 5 '10 #43

P: 92
i sent u a private message with link to the .rar containing a sample exe of the application :)
Jan 5 '10 #44

GaryTexmo
Expert 100+
P: 1,501
You don't have the custom draw on there, so I can't see the difference. All that's there is the label method.

Like I said though, I honestly don't know how to programmatically set the button's state for drawing, if you even can. The only way I could imagine how to do this would be to overload that draw method to do something different based on external parameters.

You can still use the label if you want, but you're going to have to set a flag on the button when the mouse is over the label and change the way it draws. Or you can sort out why the Graphics.DrawString text comes out different than your button.

This thread aside, it's a nice looking app :) Is this .NET Forms or WPF?
Jan 5 '10 #45

Plater
Expert 5K+
P: 7,872
I am late to the party, did someone already suggest firing the mouseover event for the button?
Jan 5 '10 #46

GaryTexmo
Expert 100+
P: 1,501
I just tried and it's not working for me, did you try it, Plater? I admit I might be doing it wrong...

Cool idea though!
Jan 5 '10 #47

P: 92
@gary sent you another private message, with the exe that also draws the text.., and its a .Net Form application :)
@plater welcome to the party (lol)
Jan 6 '10 #48

GaryTexmo
Expert 100+
P: 1,501
It's gotta be something you're doing... attached is a screenshot of my button with the graphics drawing the text over a normal button.

As it happens, when I mouse over the button it appears moused over, so no trickery needed :)

How'd you colour your buttons and whatnot? Are you just setting foreground colours, or did you do anything special?
Attached Images
File Type: jpg compare.jpg (22.4 KB, 304 views)
Jan 6 '10 #49

P: 92
i use these :P
Jan 6 '10 #50

59 Replies

Post your reply

Sign in to post your reply or Sign up for a free account.