Scott Hanselman

MouseEnter and MouseLeave loops in WPF

January 12, '09 Comments [20] Posted in Windows Client | WPF
Sponsored By

Sorry for the cheesy blur effect, but my buddy hasn't gotten permission from the company he's working with to blog about his project yet. When we get permission I'll blog in more detail about all the fun stuff we've learned.

I'm helping a friend of mine occasionally with WPF-related questions as he works on a really good looking application that features clothing from a large US apparel manufacturer. There's a nice animation as you roll over transparent PNGs of two shirts. The shirt you're rolling over zooms to the forefront with a nice blur and zoom animation. Below the shirts is a styled Radio Button with "Mens" and "Womens" text.

There's MouseEnter and MouseLeave events hooked up

We found ourselves in a really strange situation where there was a tiny strip of pixels that, when moused over, caused an apparently Enter/Leave loop.

The screenshot below shows the System.Diagnostics.Debug.WriteLine() calls showing the output from the application. Notice that the mouse is just approaching the second men's shirt. At this point the application would freak out as it got stuck in a loop until I moved the mouse away.

image 

This was really confusing and my knee-jerk reaction was that was somehow a bug in WPF, but then I remembered something an engineer smarted than I said:

"My code runs exactly as I wrote it."

If I go on the assumption that I haven't found a major bug in WPF, then the problem must be me. The code must be running as I wrote it.

It turns out that if you change the element that is underneath the mouse, like hiding it or changing its Z-order, you'll get a MouseLeave for the changed element and a MouseEnter for the new element that appears.

We'd gotten ourselves in a loop because we'd pushed the elements so close to together that they overlapped. One element would leave, the one underneath would popin, and the swapping would continue.

The solution was as simple as zooming in on the XAML view in the designer and moving the elements away from each other so they didn't overlap.

It was such a frustrating bug and we fought with this for an hour before we stepped back and reminded ourselves that it might just be the way we wrote it.

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Sponsored By
Hosting By
Dedicated Windows Server Hosting by SherWeb

Hanselminutes Podcast 145 - SOLID Principles with Uncle Bob - Robert C. Martin

January 8, '09 Comments [8] Posted in Learning .NET | Podcast | Programming
Sponsored By

photo_martin_r My one-hundred-and-forty-fifth podcast is up. Scott sits down with Robert C. Martin as Uncle Bob (@unclebobmartin) helps Scott understand the SOLID Principles of Object Oriented Design.

Subscribe: Subscribe to Hanselminutes Subscribe to my Podcast in iTunes

Do also remember the complete archives are always up and they have PDF Transcripts, a little known feature that show up a few weeks after each show.

Telerik is our sponsor for this show!

Building quality software is never easy. It requires skills and imagination. We cannot promise to improve your skills, but when it comes to User Interface, we can provide the building blocks to take your application a step closer to your imagination. Explore the leading UI suites for ASP.NET and Windows Forms. Enjoy the versatility of our new-generation Reporting Tool. Dive into our online community. Visit www.telerik.com.

As I've said before this show comes to you with the audio expertise and stewardship of Carl Franklin. The name comes from Travis Illig, but the goal of the show is simple. Avoid wasting the listener's time. (and make the commute less boring)

Enjoy. Who knows what'll happen in the next show?

Technorati Tags: ,,,,

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Sponsored By
Hosting By
Dedicated Windows Server Hosting by SherWeb

Be Aware of DPI with Image PNGs in WPF - Images Scale Weird or are Blurry

January 6, '09 Comments [28] Posted in Windows Client | WPF
Sponsored By

Is that enough TLAs (Three Letter Acronyms) in the title there? I continue to mentally deny the existence of DPI (dots per inch) as a concept. It's my own fault. I have been living on PCs at 96dpi for so many years, I just stopped caring. This problem we ran into is SO obvious after the fact, but it flummoxed us for a half-hour.

I've been helping a buddy on a super-cool super secret WPF application and the window has multiple states - regular, compact, and minimized. We've got three transparent PNGs for these three buttons. The designer created them and sent them along.

However, with one of them, it kept showing up at the wrong size, and was blurry. Even if we set the width and height ourselves, it looked totally wrong.

Here's wrong:

wrong

Unrelated to the scaling issue, I saw that the file was 3099 bytes, which I thought was a little large. I opened it up in the Visual Studio binary hex editor and noticed that I could see strings like "Photoshop ICC profile" in the header. PNGs are lossless (they are like Bitmaps, not JPEGs) and while they compress, there are may ways they can compress, like removing crap from headers.

I like to run PNGOUT on all my PNGs as it'll try different techniques to make the PNG as small as possible without losing any data (without changing the way the PNG looks). I ran PNGOUT on the wrong file and it went from 3099 bytes to 292 bytes. It also just happened it look right afterwards.

Here's right:

right

So why did it look right suddenly? Turns out that PNGOUT also changes the DPI, without asking, to 96dpi. Here's a little C# program I wrote to test:

static void Main(string[] args)
{
Image i = Image.FromFile(@"c:\users\scott\desktop\collapse-wrong.png");
Console.WriteLine(i.HorizontalResolution + " " + i.VerticalResolution);
Console.WriteLine(i.Width + " " + i.Height);

Image i2 = Image.FromFile(@"c:\users\scott\desktop\collapse.png");
Console.WriteLine(i2.HorizontalResolution + " " + i2.VerticalResolution);
Console.WriteLine(i2.Width + " " + i2.Height);
}

The output of this is:

72.009 72.009
56 10
96 96
56 10

Both files are 56x10 in dimension, but the first is 72.009 dpi and the second is 96 dpi. WPF defaults to 96 dpi so when it encounters the 72 dpi image it scales it up. If you can't change the image, there are funky ways around it. Personally, I believe if you are running into this because of a designer/developer mismatch, then just coordinate between each other and decide on a DPI, probably 96. In our case, DPI didn't really matter as the designer was developing pixel-accurate images, so we just run PNGOUT on all the images.

I usually run PNGOUT from Powershell, but there is a nice free .NET app called PNGGaunlet from Ben Hollis that provides a nice GUI frontend.

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Sponsored By
Hosting By
Dedicated Windows Server Hosting by SherWeb

ASP.NET MVC Design Gallery

January 5, '09 Comments [13] Posted in ASP.NET | ASP.NET MVC
Sponsored By

My teammate Stephen Walther launched the ASP.NET MVC Design Gallery a few weeks back. This stemmed from an idea ScottGu had to let the community change the default templates that ship with ASP.NET MVC. I was supposed to work on this but headed to Africa for the holidays so Stephen helped me out and took over the project and did a better job than I would have!

You can vote on the designs that you like and that you don't like. You also can contribute new designs of your own.

image

After you download a design from the gallery, you can use the design by following these steps:

1) Copy the files from DesignTemplateCS or DesignTemplateVB folder into your project (use DesignTemplateCS for C# projects and DesignTemplateVB for VB.NET projects)

2) Do a search and replace the string [YourProjectName]. Replace [YourProjectName] with the actual name of your project as displayed in the Solution Explorer window.

This last step is necessary to get the namespaces right. Most of the templates use helper methods to render the menu items so that the selected menu item is correctly displayed.

Microsoft also is hosting a design competition to encourage members of the community to submit new designs. Submit a new design before January 31, 2009. ScottGu will be selecting the best three designs and announcing them on his blog. Learn more about the ASP.NET MVC Design Competition and submit your designs before January 31st.

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Sponsored By
Hosting By
Dedicated Windows Server Hosting by SherWeb

South Africa 2008: NewsFlash - Turns out eating less and moving more causes weight loss

January 3, '09 Comments [36] Posted in Africa | Musings
Sponsored By

image0-1When I left for South Africa I was 193lbs/87kg, my heaviest ever on a 6 foot/1.8 meter medium frame. This was shocking to me because I could totally see 200lbs coming. It was right there ahead of me. As a Type 1 Diabetic getting fat(ter) is deeply uncool. You can literally be a Type 1 (insulin dependant) and Type 2 (insulin resistant) diabetic at the same time. This would mean I'd need to take MORE insulin to do the SAME amount of work. I'd already started seeing this recently as my total daily insulin dosage, usually under 30U (Units) was creeping up to 40U.

Then we headed to Africa for our semi-yearly sojourn. Fast forward roughly a month and I'm 176lbs/79.8kg. That's 17 pounds, or as a newly svelt person me likes to say "I lost, like, 20 pounds!" The trick will be to keep it off, but here's what I think worked (as I lose nearly 20 pounds on EVERY trip to Africa, and nearly every trip overseas. The tragedy here is the re-gaining and re-losing of that same 20 pounds.)

These are totally obvious observations/tips, but I'm a little dense, so I'm writing them down.

That picture to the right is me when I was thinner. I'm also the short one in the picture.

Don't Eat a Serving Larger than Your Fist

I didn't consciously do this, it just happened. As my wife and I discussed in our recent podcast, folks just show up to visit and food is divided appropriately. This is not to say that I wanted for food in any way. I didn't. It's that my hosts in South Africa gave us a reasonable amount of food, not an American Amount of Food.

This just happened to be about the size of one's fist. Kind of unrelated, I mentioned this to the boy's doctor since the older boy wasn't eating that much and he said that most kids will naturally eat what they need to, no more and no less, and that amount was often about the size of his fist. We'd been wasting a lot of food as I was giving him a plate full of food as big as his head! So, it's a fist-sized serving for me.

Eat Breakfast

I am notorious for eating one giant 1pm meal, ahem, then a giant 6pm meal, then a small (read: almost giant) midnight meal. This didn't fly in South Africa, as there was a lovely light breakfast at 7am, lunch at 1pm, and a dinner around 6pm. Each was appropriately proportioned, especially lunch which went on the assumption that you ate breakfast.

I realized that I've been eaten Epic Lunches to stave off death by starvation from skipping breakfast entirely. While eating breakfast does make my diabetes a little more tricky to manage as it adds a new variable, breakfast does support the next tip...

Eat Only Enough to Get You To the Next Meal

or

You Will Eat Again, In Your Life, You Know

My brother, the Ironman competitor and firefighter, taught me this not-obvious-to-me tip. He says that folks who aren't eating often enough eat like it's the last time they will ever eat. Rather, try eating with the knowledge that you will eat again in a few hours. If you realize that this meal only needs to get you to the next meal. This is the single most powerful dieting tip I've got.

Reset Your Full Indicator

My wife calls overeating "Pushing Through" as in "Oh, I pushed through with that last bite." Not recognizing what full feels like is a big problem for me. Basically you can reset your internal gas tank indicator by just thinking about it. "Am I full?" "Do I really need that next bite?" This has caused me to eat about 1/3 less without actually feeling any less full. Actually, I feel less bloated after meals.

Aside: I actually have personal data (and charts!) that reflects this - while I was in South Africa my daily insulin usage (and hence, carb intake) went from 30-40U a day to 20U. Basically I used 33% to 50% less insulin per day.

Move More

I didn't visit the South Africa that a lot of people do. In my South Africa not everyone has a car. In mine you can easily fit 9 people into a VW Golf. Either way, I walked a lot. If there's no car available and you want to go to the mall that's 3km away, you walk. This, along with sweltering heat and a lot of water, is another nice way to lose weight.

This year I've started working out, six days a week, for eat least an hour. I'm alternating cardio and anaerobic exercise. I'm finding that if I time my workouts with the length of certain television shows, I can make it through without going insane. That means, 40 minutes of cardio while I watch, say, Heroes. Then 22 minutes of weights as I watch Arrested Development. I'm actually watching more TV (and enjoying it) but I don't have to feel bad since I'm moving.

Drink Water

Yes I know if you drink WAY too much water you can mess up your salt/electrolyte balance, but a few liters a day are what I've found is the right amount for me to lose weight. I can't overstate how water intake directly affects weight loss. It's really amazing. Everyone I know with 6-pack abs carries a water bottle around with them. While I was in Africa I really upped my water intake because of the heat, to the point where drinking water at every turn was second nature.

What are your weight loss tips, Dear Reader?

Technorati Tags: ,

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Sponsored By
Hosting By
Dedicated Windows Server Hosting by SherWeb

Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.