Jump to content

A WPF program, all in ONE FILE!


G+_Joe Maruschek
 Share

Recommended Posts

A WPF program, all in ONE FILE! 

And it is all C#, no XAML.

I included a lot of comments in the code (too much, really) so hopefully you can follow along with what is happening.  The program is really simple: it displays some text in a window.  It also shows how to wire up an event handler in C#.  If you click on the window, you get an new random background color.

 

To compile this yourself in Visual Studio:

1. Choose File -> New Project

2. Under Templates, Visual C#, Windows, choose the "Empty Project", give it a name, uncheck "Create Directory for Solution" and click OK.

3. In the Solution Explorer, right-click References under the project name, and choose "Add Reference".  Add these: PresentationCore, PresentationFramework, System, System.Xaml, and WindowsBase.

4. Right-click the project name and choose "Add -> New Item" and choose "Code File".

5. Copy and paste the code linked below from github into your new code file.

6. Click the Start button (or hit F5) and hopefully it works!

 

This example is based on one by Charles Petzold in his book Applications = Code + Markup.  Check it out for more examples of WPF programming. 

https://github.com/joemarus/Test1/blob/master/HandleAnEvent.cs

Link to comment
Share on other sites

Great post! Thanks! This single source example file brings back a lot of fond memories.

 

A long long time ago, back when you could only code a Windows app in C,

and Peter Norton had not yet crossed his arms, I once won a contest for

the world's shortest Windows program.

 

IIRC, the Microsoft team's entry was 88 lines of code.

 

I turned in a single line that looked something like this:

 

include windows.h; {return(MessageBox(0, "Winner?", "Hello World", MB_OK))};

 

It may have been "include user.h", its been decades. I started programing

for Windows back at version Beta 0.8 (wondering if can I disclose that yet).

 

I cannot remember the event, but I want to say it was something called

"Storm the Gates", hosted by Bill Gates, but my memory is not so good

on this one.

 

I will for sure go check out the Petzold book!!! I am very pleased to hear

Charles is still punching the keys. Thanks for the pointer!

 

TJoe

Link to comment
Share on other sites

Thanks for mentioning my program on the show Shannon Morse !  I am really enjoying the show!  The reason you have to set all those references manually with my program is that I have you start with the empty project, which doesn't have any references set.  When you start with the full WPF template, it already has those references set for you.  Your show got me learning WPF which I had been wanting to learn for a while now. I'm following the advice of author Charles Petzold by trying these little self-contained programs written by hand rather than having Visual Studio write a bunch of code that I don't understand yet.

Link to comment
Share on other sites

 Share

×
×
  • Create New...