UAC shield icon in WPF

Updated on Sunday, May 2, 2021

WPF: When it's good it’s very, very good and when it’s bad it’s like sautéing your own eyeballs.

When you’re about to launch a process that will trigger an elevation prompt it’s polite to decorate it with the little UAC shield so the user knows what to expect. Of course there’s no such capability in WPF, and WPF controls have no handles so you can’t use SendMessage / BCM_SETSHIELD as with Windows Forms.

System.Drawing.SystemIcons.Shield seems promising, but it returns the wrong icon on Windows 7 (at least in .NET 4).

SHGetStockIconInfo will allow you to get the correct icon, but isn’t supported on Windows XP. I’ve just added the necessary interop signatures for SHGetStockIconInfo to pinvoke.net so I won’t duplicate that code here. Once you have the interop you can get the correct icon as a BitmapSource using the following code:

Add your comment...

Related Posts

(All Code Posts)

(Published to the Fediverse as: UAC shield icon in WPF #code #.net #c# #wpf #uac #pinvoke How to add the correct UAC shield icon for each Windows version in .NET WPF using Win32 interop. )

Geotagging posts in BlogEngine.NET

Updated on Sunday, September 30, 2018

Geotagging posts in BlogEngine.NET

I've written an extension for BlogEngine.NET that automatically adds several different geographical tags to blog posts. I knocked this up for my Hikes blog. It might be useful for any blog where some of the posts are related to a real world location.

To get started download GeotagFromKML.zip (2.24 kb) and copy GeotagFromKML.cs to the App_Code\Extensions folder in your BlogEngine.NET instance.

The extension does two things. Firstly it looks for a link to a KML file when post is added or updated (it does this because each of my hike posts includes a Google Earth KML file for the hike). If a KML link is found then a paragraph is added to the post containing the longitude and latitude of the first coordinate in the KML file. The paragraph uses the Geo microformat. You can customize the text in settings for the extension. You can also regenerate by deleting the paragraph and saving the post. 

The second function is to add ICBM and Geo Tag META tags when serving a post that contains the geotagged coordinates. You can take advantage of this without linking to a KML file, just include a location like this in your post:

Once you have geotagging up and running you might also want to add GeoURL to the list of ping services for your site.

Add your comment...

Related Posts

(All Code Posts)

(Published to the Fediverse as: Geotagging posts in BlogEngine.NET #code #blogengine.net #.net #c# #metadata #kml #geo #icbm #geourl #geotag A customer extension that supports geotagging posts in BlogEngine.NET. )

WPF commands with nested focus scope

Updated on Sunday, September 30, 2018

Here's a frustrating WPF scenario — you use the ApplicationCommands class to add Cut, Copy and Paste commands to toolbar and then put a TextBox on another toolbar. Click in the TextBox and the commands remain disabled. WTF, WPF?

The problem is with focus scopes. Your window is a focus scope and so are any menus or toolbars. This has the desirable property of allowing commands to target the control you were in immediately before invoking the command. You want paste to target the text box you're editing, not the menu item or button you clicked to request the paste.

So far so good. The problem is that the commanding system isn't smart enough to target the control with keyboard focus if it's in a nested focus scope. Remember that the window itself is a focus scope so our TextBox in a ToolBar (also a focus scope) is nested and immune to commands from our menu or toolbar.

Here's a simple window that demonstrates the problem:

Ignore the PreviewCanExecute handler for now. If you run this window and click in the main TextBox the paste button and menu item are enabled. Click in the toolbar TextBox and pasting isn't an option. Well, Ctrl-V still works and there's a context menu but you know what I mean.

The problem can be fixed by adding a command binding for ApplicationCommands.Paste and handling the PreviewCanExecute event:

When the window loads we're making note of the focus scopes for the toolbar and menu. Then when PreviewCanExecute fires we check to see if the element with the keyboard focus is in a different focus scope (and also that the window doesn't have keyboard focus). We then set the CommandTarget for the menu item and button to the element that has keyboard focus.

A handler isn't required for CanExecute as the command will take care of this with respect to the new CommandTarget.

Run the window again and you'll see that the paste button is enabled for both of the TextBox controls. When you click the button (or menu item) our PreviewCanExecute handler ignores the new keyboard focus and the command is sent to the desired control. 

One drawback of this approach is that keyboard focus isn't returned to the TextBox after the command executes. The CommandTarget remains in place so you can keep pasting and the command remains enabled but you lose the visual cue that lets you know where the target is. I haven't figured out a clean approach to this yet. When I do, I'll update this post. Better yet, if you've figured it out leave a comment.

Add your comment...

Related Posts

(All Code Posts)

(Published to the Fediverse as: WPF commands with nested focus scope #code #wpf #.net #c# #xaml How to persuade a WPF application to paste into a selected control when the control is in a different focus scope. )

Converting Blogger ATOM export to BlogML

Updated on Thursday, December 26, 2019

I'm slowly converting a number of blogs from Blogger to BlogEngine.NET. The least fun part is dealing with the Blogger export file. For this blog I used a Powershell script but had problems with comments not exporting correctly and it was quite painful to fix everything up. Blogger allows you to export a copy of your blog using ATOM, however BlogEngine.NET (and other tools) speak BlogML.

I've just released a command line tool that takes the ATOM format Blogger export and converts it to BlogML. You can download Blogger2BlogML from GitHub. The tool uses .NET 4.0 (client profile) so you'll need to install this if you don't already have it. If you give Blogger2BlogML a try let me know how you get on. 

Add your comment...

Related Posts

(All Code Posts)

(Published to the Fediverse as: Converting Blogger ATOM export to BlogML #code #blogger #blogml #codeplex #c# #.net Tool that converts Blogger ATOM blog export files to BlogML for importing to a different blog engines. )

ESRI Shapefile Reader in .NET

Updated on Sunday, March 12, 2023

ESRI Shapefile Reader in C#/.NET

Catfood.Shapefile is a .NET read-only, forward-only parser for Esri shapefiles. With Catfood.Shapefile it’s easy to enumerate all the shapes in a shapefile together with their metadata.

From version 2.00 Catfood.Shapefile is available via NuGet. It has also been migrated to .NET Standard 2.0. This means it is compatible with .NET Framework 4.61 or better and .NET Core 2.0 or better. If you use an older version of .NET then you should continue using Catfood.Shapefile 1.60. The source code is on GitHub.

Esri (originally Environmental Systems Research Institute) shapefiles are a GIS (geographic information systems) file format that can store a wide variety of geographic information. I originally started working with these files to add country borders to Catfood Earth.

Shapefiles actually consist of a number of different files with different extensions. There are three mandatory extensions: .shp, .shx and .dbf and so a shapefile containing borders might be distributed as borders.shp, borders.shx and borders.dbf. The .shp file contains the actual shape data in vector format, often lines or polygons. The .shx file is an index to the shapes. Finally the .dbf file is a database of metadata associated with the shapes. Catfood.Shapefile requires these three files.

There are several optional extensions that might be included with a shapefile. .prj contains the geographical projection, .xml is an alternate metadata format, .sbn and .sbx index for spatial queries, and .cpg defines the code page used by the shapefile.

Here is some sample code for a .NET 6 console application to print basic information about a shapefile and the first point in the first polygon:

To use this, create a new console application, paste the code into Program.cs and add Catfood.Shapefile via NuGet.

The default constructor uses a database connection string that uses the Jet driver. This is only available for 32-bit applications and you will need to target Windows x86 to use it. An overload is available that allows you to specify a custom connection string to access metadata.

Catfood.Shapefile supports Null, Point, MultiPoint, PolyLine, PolyLineM and Polygon shapes. Three dimensional shapes (PointZ, PolyLineZ and PolygonZ) are not currently supported. There is also currently no support for writing shapefiles. If you add any of this please send a pull request on GitHub!

I am grateful to the developers who have contributed to Catfood.Shapefile over the years. Most are credited in the release notes below. If you need help or have feedback please leave a comment on this post.

Updates

Catfood.Shapefile 2.00

I just released Catfood.Shapefile 2.00, my .NET parser for ESRI Shapefiles.

The big change is that I have migrated to .NET Standard 2.0. This makes it possible to use from .NET Core as well as classic .NET Framework from 4.6.1 up. If you need to use an older version of .NET Framework then you'll want to stick with Catfood.Shapefile 1.60.

Catfood.Shapefile is now also available via NuGet. This is the recommended way to install. The source code is still available on GitHub.

Catfood.Shapefile 1.60

I just released Catfood.Shapefile 1.60. This contains a fix from Libor Weigl that factors out the enumerator so that you can still access the shapefile after enumeration.

Catfood.Shapefile is a .NET library for parsing ESRI Shapefiles.

(previously)

Shapefile Update

A few people have asked for 3D shape support in my ESRI Shapefile library. I've never got around to it, but CodePlex user ekleiman has forked a version in his ESRI Shapefile to Image Convertor that supports PointZ, PolygonZ and PolyLineZ shapes. If that's what you need please check it out.

Catfood.Shapefile 1.50

I've just released a small update to my C# Shapefile library on Codeplex. Catfood.Shapefile 1.50 fixes a couple of bugs related to metadata and adds the ability to access metadata records directly via IDataRecord. 

Catfood.Shapefile 1.40

I’ve just released a small update to Catfood.Shapefile. Stephan Stapel, who implemented PolyLineM support, has contributed a patch that improves the class hierarchy. CodePlex user originSH suggested supporting the ACE driver for 64-bit systems. I’ve added a constructor overload that allows you to use predefined Jet and ACE connection strings or provide your own templates if necessary. Thanks to Stephan and originSH.

Catfood.Shapefile is a .NET library for enumerating ESRI shapefiles. I originally wrote the library to help me build some complex layers in Catfood Earth. Since then it’s picked up thousands of users and some really valuable suggestions and patches from the CodePlex community. I’m very glad a took a couple of hours to open source the library back in 2009.

PolyLineM support in Catfood.Shapefile

I’ve just updated Catfood.Shapefile, my ESRI Shapefile parser for .NET, with PolyLineM support thanks to a contribution from Stephan Stapel. The solution for the new version has also been updated to Visual Studio 2010.

Download Catfood.Shapefile.dll 1.30 from CodePlex.

ESRI Shapefile Library Update

I've just released a small update for my ESRI Shapefile Reader project on GitHub. The only change is a patch from SolutionMania that fixes a problem when the shapefile name is also a reserved name in the metadata database. The patch escapes the name preventing an exception from being thrown.

Catfood.Shapefile.dll is a .NET 2.0 forward only parser for reading an ESRI Shapefile. Download 1.20 from GitHub.

Catfood.Shapefile.dll 1.10

I've just released v1.10 of my ESRI Shapefile Reader (Catfood.Shapefile.dll). This is a .NET 2.0 forward only parser for reading shapefile content.

Sharon Tickell was kind enough to report two bugs with suggested updates. These have both been fixed in 1.10.

While working on these fixes I also discovered that there are no 64-bit Jet drivers (since releasing the first version I've upgraded to a 64-bit box for development). This is an easy fix, just target any application using Catfood.Shapefile.dll at x86. I've updated the demo application accordingly.

Download Catfood.Shapefile.dll from GitHub.

Add your comment...

Related Posts

(All Code Posts)

(Published to the Fediverse as: ESRI Shapefile Reader in .NET #code #shapefile #esri #github #codeplex #polylinem #c# #.net #nuget Catfood.Shapefile is a .NET library that provides easy access to Esri shapefiles (.shp, .shx, .dbf), available on NuGet and GitHub. )

StackOverflow DevDays

Updated on Saturday, July 18, 2020

Fort Mason, San Francisco

Just got back from the StackOverflow DevDays event in San Francisco. I was a bit worried that this would be overly focused on marketing FogBugz and StackOverflow. There were brief pitches on each (and I learned that FogCreek is launching hosted source control and code reviews called Kiln, now in beta and looking pretty nifty with tight integration with FogBugz) but this wasn't the focus.

I was also a little concerned that I'd be the only one there without a StackOverflow profile t-shirt. Luckily I didn't see any reputation toting pod people at the conference.

Happily the day was very code oriented, and very diverse. Spell checking in Python, smartphone development for iPhone, Nokia (via Qt) and Android, ASP.NET MVC and jQuery. I spend most of my time at the moment in C#/.NET and it was really valuable to spend a day briefly diving into different stacks and platforms.

Joel said that they'd be back next year and I'm hoping that it offers a similar diverse range of topics.

Add your comment...

Related Posts

(All Code Posts)

(Published to the Fediverse as: StackOverflow DevDays #code #stackoverflow #c# #.net Review of the StackOverflow DevDays conference in San Francisco, California. )

MessageInterceptor doesn't always...

Updated on Saturday, October 1, 2022

I've been writing a play by text message version of battleships for Windows Mobile 6 using the MessageInterceptor class to receive messages. This works great on my AT&T Tilt but completely fails on my wife's Pantech device. The MessageInterceptor hooks up fine but never fires.

The 2.0 compact framework SmsAccount lets you send messages but unaccountably doesn't let you read them.

I avoided some unpleasant interop by grabbing the MAPIdotnet library. This implements a NewMessage event for MAPI stores, however as with MessageInterceptor the event hooks up OK but never fires on the damned Pantech.

MAPIdotnet does allow you to read the SMS inbox though, so the final answer is to fall back to looking for, processing and then deleting game related text messages on a timer. This is far from ideal as the game messages arrive and vibrate the phone before getting processed but at least it's now possible to play without buying a new phone.

To add to the frustration during development the Visual Studio 2005 toolbox lost all icons except for the pointer and a custom control that was part of the project. Resetting the toolbox didn't help, nor did restarting VS and the computer. The fix was to exit VS and then delete all the .tbd files from Microsoft\VisualStudio\8.0 in local application data. Sigh.

Add your comment...

Related Posts

(All Code Posts)

WiX Tricks for Screen Savers

Updated on Wednesday, May 5, 2021

I've been migrating my installers over to WiX. My only complaint with the WiX toolkit is that there's no bootstrapper included. This is important for installing any pre-requisites before passing control over to Windows Installer. Hopefully this will come after WiX 3.0 is released. For now, I've rolled my own bootstrapper to install .NET 2.0 if needed.

A couple of tips for screen savers. You can bring up the Windows screen saver settings using the following custom action. This is the same command that is launched when you right-click a screen saver (.scr) file and pick Install:

<CustomAction Id='InstallSS' Directory='SystemFolder' ExeCommand='rundll32.exe desk.cpl,InstallScreenSaver the.scr' Return='asyncNoWait'/>

Sequence this in InstallExecuteSequence after InstallFinalize:

You can also add a shortcut to your Program Files folder to configure the screen saver. This is really helpful for people who aren't sure how to get to the screen saver dialog from Control Panel or right-clicking the Desktop:

Unlike a shortcut to a file that you install you need to specify the Target. The example above assumes that ProgramMenuDir is the Id of your Start Menu folder. The shortcut should be in a Directory but not as a child of a File node.

Updated 2020-05-26 20:22:

It looks like something changed in Windows 10, around build 1903, that stops this from working on 64-bit systems. The screensaver will preview OK but does not start as expected (system screensavers start OK, so it's not some sort of power management problem). I could reproduce this on one box running 1909 but not another so maybe there is something else going on. The snippets above will run rundll32.exe from the SysWOW64 folder and at some point this seems to have stopped working. Changing to [System64Folder]rundll32.exe launches the System32 version which then causes the screensaver to load normally. If your screensaver is 32-bit and installed to SysWOW64 then you need a command that looks like this on a 64-bit system: [System64Folder]rundll32.exe desk.cpl,InstallScreenSaver [SystemFolder]the.scr

Add your comment...

Related Posts

(All Code Posts)

(Published to the Fediverse as: WiX Tricks for Screen Savers #code #c# How to run the screen saver install dialog from Windows Installer XML or WiX and add a program menu shortcut. )

code, c#