Friday 26 November 2010

How to copy an HTML content to the Clipboard (C#)

If you want to copy an HTML content to the Clipboard you have to understand the concept of HTML Clipboard Format. When you copy a piece of HTML using Ctrl+C shortcut, OS creates previously mentioned header for you but if you want to achieve the same thing in code you have to prepare the header by yourself.

   1:      Version:0.9
   2:      StartHTML:71
   3:      EndHTML:170
   4:      StartFragment:140
   5:      EndFragment:160
   6:      StartSelection:140
   7:      EndSelection:160
   8:      <!DOCTYPE>
   9:      <HTML>
  10:      <HEAD>
  11:      <TITLE>The HTML Clipboard</TITLE>
  12:      <BASE HREF="http://sample/specs"> 
  13:      </HEAD>
  14:      <BODY>
  15:      <!--StartFragment -->
  16:      <P>The Fragment</P>
  17:      <!--EndFragment -->
  18:      </BODY>
  19:      </HTML>

Header explanation:
  • Version vv - Version number of the clipboard. Starting version is 0.9.
  • StartHTML - Byte count from the beginning of the clipboard to the start of the context, or -1 if no context.
  • EndHTML - Byte count from the beginning of the clipboard to the end of the context, or -1 if no context.
  • StartFragment - Byte count from the beginning of the clipboard to the start of the fragment.
  • EndFragment - Byte count from the beginning of the clipboard to the end of the fragment.
  • StartSelection - Byte count from the beginning of the clipboard to the start of the selection.
  • EndSelection - Byte count from the beginning of the clipboard to the end of the selection.
According to the Microsoft StartSelection and EndSelection attributes are optional because sufficient information is provided in the fragment section. Below you can find an example of method which you can use for coping an HTML content to the Clipboard under the condition that input parameter 'fullHtmlContent' contains all necessary tags (a fully functional HTML document).

   1:          public void CopyToClipboard(string fullHtmlContent)
   2:          {
   3:              System.Text.StringBuilder sb = new System.Text.StringBuilder();
   4:              string header = @"Version:1.0
   5:                              StartHTML:<<<<<<<1
   6:                              EndHTML:<<<<<<<2
   7:                              StartFragment:<<<<<<<3
   8:                              EndFragment:<<<<<<<4";
   9:              sb.Append(header);
  10:              int startHTML = sb.Length;
  11:              sb.Append(fullHtmlContent);
  12:              int endHTML = sb.Length;
  14:              sb.Replace("<<<<<<<1", startHTML.To8CharsString());
  15:              sb.Replace("<<<<<<<2", endHTML.To8CharsString());
  16:              sb.Replace("<<<<<<<3", startHTML.To8CharsString());
  17:              sb.Replace("<<<<<<<4", endHTML.To8CharsString());
  19:              Clipboard.Clear();
  20:              Clipboard.SetText(sb.ToString(), TextDataFormat.Html);
  21:          }

   1:          public static string To8CharsString(this int x)
   2:          {
   3:              return x.ToString("0#######");
   4:          }

Useful links:

How to show a dynamically generated HTML content in a WPF application

If you have to show dynamically generated .html pages in your WPF application you can use a WebBrowser control. NavigateToString("...") method gives you full flexibility and navigates asynchronously to a string which contains the content for a document.

   1:  <Window x:Class="WpfApplication1.MainWindow"
   2:          xmlns=""
   3:          xmlns:x=""
   4:         xmlns:local="clr-namespace:WpfApplication1"
   5:          Title="MainWindow" Height="350" Width="525">
   7:      <Window.Resources>
   8:          <WebBrowser x:Name="browser"/>
   9:      </Grid>
  10:  </Window>

   1:   public partial class MainWindow : Window, IDataErrorInfo, INotifyPropertyChanged
   2:      {
   3:          public MainWindow()
   4:          {
   5:              InitializeComponent();
   6:              browser.NavigateToString("<html><head><h1>Head</h1></head><body>" + DateTime.Now + "</body></html>");
   7:          }
   8:     }

Sunday 21 November 2010

How to show HTML content in a WPF window

There are two ways to show HTML content in a WPF window using Frame or WebBrowser control. If you want to switch between WPF and HTML content seamlessly the Frame control is a better choice for you. If you need more control over HTML content, examine the object model of the page and/or monitor page navigation you should consider the WebBrowser control. Both these controls show standard Internet Explorer window with all additional features like JavaScript, Dynamic HTML, ActiveX controls.

XAML version:

   1:      <Grid x:Name="grid">
   2:          <Frame Source="" />
   3:          <!--or-->
   4:          <WebBrowser Source=""/>
   5:      </Grid>

C# version:

   1:              Frame frame = new Frame();
   2:              grid.Children.Add(frame);
   3:              frame.Navigate(new Uri(""));
   5:              //or
   7:              WebBrowser browser = new WebBrowser();
   8:              grid.Children.Add(browser);
   9:              browser.Navigate(new Uri(""));

Useful links:

Neutral Density Filter (B+W ND10)

Photos were taken with Canon EOS 450D and18-55mm lens.

Useful links:

Saturday 20 November 2010

Close-Up Filter (B+W NL4)

If you are not satisfied with the macro photos taken with your camera and you would like to shorten the distance between a lens and a photographed object, it means that you need a close-up filter. For the following photos I used Canon EOS 450D with 18-55mm lens and optionally B+W NL4 close-up filter. 

Without filter                                       With filter

Useful links:

Wednesday 10 November 2010

WPF App - Virtualized Canvas - 32k nodes + connections

Some time ago I faced the challenge of showing thousands of elements on the canvas at the same time. The list of requirement was short and tidy:
  1. presentation of thousands of elements,
  2. low memory consumption, 
  3. fully responsive UI, 
  4. real-time interaction. 
Showing thousands of elements on the screen is not a problem but if you want to minimize memory consumption and you don't want to wait ages for loading all elements (fully responsive UI) you have to run this  process asynchronously and visualize only necessary elements. For detecting elements belonging to my visibility area (in this particular case screen + 25% margin) I decided to use Quadtree algorithm. On the pictures below you can find ZoomBoxes with all elements (left hand side) and just necessary elements detected by Quadtree (right hand side). ZoomBoxes on the left hand side contain simplified form of nodes (just nodes no connections, about 15k ellipses) loaded upfront to help users to find themselves on this huge canvas 66000 x 66000 pixels.

Tuesday 2 November 2010

Elegant Code - Laborers versus Professionals

I found this very interesting article Laborers versus Professionals. Remember to check  links at the very bottom of the article, especially this one: The surprising truth about what motivates us.