28 December 2015

C++ - Write a number to a file.

For debugging a C++ DLL, I needed a way to write the value of a numeric variable to a file. This is how:

Includes needed

#include <sstream>
#include <fstream>

Convert the number

You can convert the number to a string using a stringstream.
std::stringstream convert;
convert << 305.24;

Output to a file

The result of the following is a text file at the DLL's location containing the number.
std::ofstream out("Output.txt");
out << "Value: ";
out << convert.str();
out.close();

27 August 2015

JS - Convert a (nested) JSON object to HTML

For testing purposes I wanted to display the JSON retrieved from an API in a HTML page. I used Fiddler for that, but after some security was included that became impossible. The following Javascript function converts the JSON to a ordered list tree:
function jsonToHtml(data) {
    var result = "<ol>";

    for (var key in data) {
        if (typeof (data[key]) == 'object' && data[key] != null) {
            result += "<li><span class='key'>" + key + "</span>:<ul>";
            result += jsonToHtml(data[key]);
            result += '</ul></li>';
        }
        else {
            result += "<li><span class='key'>" + key + '</span>: ';
            result += '<span class="string">' + data[key] + '</span></li>';
        }
    };

    result += '</ol>';
    return (result);
}

05 August 2015

.NET - What is a GUID?

I found out something interesting today. I always thought a GUID was a string, but as it turns out, it's 128 bits of binary data that is represented in hexadecimal numbers, making it appear like a string. That makes the question of whether a GUID is case-insensitive irrelevant. It would also be better to store GUID's in a binary field rather than a text field.

21 July 2015

Windows Store App - Saving data on suspending

In my app I have a method WriteBinaryDataFile, that is called when the app is suspended. First it writes Started writing to the output window, then does it's work, and finishes by outputting Finished writing. Interestingly, Started writing appeared, but Finished writing didn't, exept when I terminated the app manually. It turned out that the method needs to be async, and had to be awaited. The code is quite simple:
public async Task WriteBinaryDataFile()
{
    //Do what needs to be done.
}

private async void OnSuspending(object sender, SuspendingEventArgs e)
{
    var deferral = e.SuspendingOperation.GetDeferral();
    await MainPage.WriteBinaryDataFile(); //Await and in the middle.
    deferral.Complete();
}

22 April 2015

.NET - Using a font file in a custom control

For my text editor project Filepad I came up with the idea to use a custom control that would use an icon font, like we often do these days in web projects. The obvious font choice is of course FontAwesome. I found initial assistance at this project, and some at MSDN. It's actually pretty easy. Include the font file in the project and set it to copy to the output directory. The following is the relevant parts of the code of my control to make the font work. I provide the code point of the icon I want through a property that is then converted to a string. The code points for FontAwesome are listed here.
private int mIconInt = 0;
private string mIconString = string.Empty;

[Category("Appearance")]
public int Icon {
  get {
    return mIconInt;
  }
  set {
    if (value != mIconInt) {
      mIconInt = value; //In FontAwesome, the code point "0xf0c7" is the "save" icon.
      mIconString = char.ConvertFromUtf32(value);
    }
  }
}

[Category("Appearance")]
public float IconSize { get; set; } //Unit = em

private static PrivateFontCollection Fonts { get; set; } //This object must live as long as it's fonts are in use.
private static Font IconFont { get; set; } //Shared among all font icon buttons.

private Font GetFont() {
  if (IconFont == null) {
    Fonts = new PrivateFontCollection();
    if (File.Exists("FontIconAwesome.ttf")) {
      Fonts.AddFontFile("FontIconAwesome.ttf");
    }

    if (Fonts.Families.Length > 0) {
      IconFont = new Font(Fonts.Families[0], IconSize > 0f ? IconSize : this.Font.Size);
    }
  }

  return IconFont;
}

protected override void OnPaint(PaintEventArgs e) {
  Font font = GetFont(); //Needs to be done before any painting goes on.
  if (font == null) {
    font = this.Font;
  }

  base.OnPaint(e);

  string text = IconFont != null && mIconInt > 0 ? mIconString : this.Text;
  Rectangle textArea = new Rectangle(1, 1, this.Width, this.Height);
  Brush textColor = ...;
  e.Graphics.DrawString(text, font, textColor, textArea);
}

18 April 2015

.net - Keyboard input in custom controls

I'm working on a custom control in .net WinForms that allows the user to input characters when it is focused. So far so good, but there's also a button on the form that has a hotkey assigned. It's label is New &chapter, so whenever I press "ALT+C", it's OnClick event fires. However, I noticed that it also fires when I just press "C", and that of course conflicts with my custom control, which doesn't even receive the keystroke for "C" anymore. I also noticed this doesn't happen when I'm typing in a regular text box. After a lengthy search I found this hotkey behavour is by design, so I needed to find a workaround for my control. After some more searching I finally found this event that does te trick:
protected override bool IsInputChar(char charCode) {
  if (this.Focused) {
    return true;
  }
  else {
    return base.IsInputChar(charCode);
  }
}
I put this in my custom control, and all works well. "ALT+C" still works on the button, but "C" goes to my custom control.

04 April 2015

Windows - The behaviour of the standard textbox

For a custom textbox project, I observed the behaviour of the standard Windows textbox control, as to replicate this in the custom one. If you think of a behaviour not listed, please leave a comment below.

Terms

  • The caret represents the focussed character in the text. It is marked by displaying a blinking bar behind it.
  • The selection represents a range of characters from the caret until a second character. This character may occur before or after the caret.

Keyboard navigation

  • Shift left/right: the selection is expanded/contracted by one character.
  • Shift control left/right: the selection is expanded/contracted by one word.
  • Shift up/down: the selection is expanded/contracted to the character above/below the caret.
  • Home: the newline character of the line that contains the caret becomes the caret.
  • End: the last character of the line that contains the caret becomes the caret.
  • Page up/down: the text is scrolled so that the first/last visible becomes the last/first visible line. The character that occupies the screen space that the caret occupied becomes the caret.

Keyboard other

  • A key or key combination that represents a glyph; no selection: the character is inserted behind the caret, and becomes the caret.
  • A key or key combination that represents a glyph; with selection: the selection is removed. The first character before it becomes the caret. The character is inserted behind the caret, and becomes the caret.
  • Backspace; no selection: the caret is removed. It's predecessor becomes the caret.
  • Backspace; with selection: the selection is removed. The first character before it becomes the caret.
  • Delete; no selection: the character following the caret is removed.
  • Delete; with selection: the selection is removed. The first character before it becomes the caret.
  • Control+A: the selection is expanded, spanning from the first until the last character of the text.
  • Control+X: if there is a selection, the characters in it are copied to the clipboard. The selection is then removed.
  • Control+C: if there is a selection, the characters in it are copied to the clipboard.
  • Control+V: if the clipboard contains text, each character in that text is inserted behind the caret. The last one then becomes the caret.

Mouse

  • Click: if there is a selection, the selection is cleared. The clicked character becomes the caret. If before a line, the newline character becomes the caret. If after a line, the last character of the lines becomes the caret.
  • Double click: the clicked word becomes the selection. The last character of this selection becomes the caret.
  • Shift click: the range between the caret and the clicked character becomes the selection. The last character of this selection becomes the caret.
  • Click and drag: the caret follows the mouse. The selection expands and contracts from the clicked character to the caret, as long as the mouse button is not released. When the caret crosses the edge of the text area, it will scroll as well.

06 February 2015

Visual Studio - Use a theme with Windows high contrast

When you set Windows in a high contrast theme, Visual Studio follows suit, adapts the same colors, and does not allow you to pick another theme anymore. To make matters worse, it shows all the code in the same color; orange in my case. You could customize it, and set all the colors yourself, but that's a lot of work. There is a registry trick however, to fool the IDE and use a theme anyway. I did this for Visual Studio 2013, but it should work in 2012 and probably other versions as well.
  • So open up RegEdit and navigate to the key HKEY_CURRENT_USER\Software\Microsoft\VisualStudio\12.0_Config\Themes. If you use VS 2012, that would be 11.0_Config.
  • You see a list of GUID's there. When you click one, you can see the theme's name. Select the theme you want, right-click it's key and export it.
  • {a5c004b4-2d4b-494e-bf01-45fc492522c7} is the high contrast theme. Rename the key to the GUID of the theme you want after renaming that theme first. I simply swapped there names.
  • Finally, choose File > Import, and import the .reg file you just exported.
  • Now if you open Visual Studio, it should be using your theme.

08 January 2015

HTML5 - Wheel of fortune

For a small project I needed a wheel of fortune – also known as prize wheel, winning wheel and roulette wheel, but I couldn't find anything that did it the way I wanted it. So I made my own with these specifications:
The result is shown here to the right. It has only two dependancies:
  • jQuery: who can live without it...
  • Please: to generate random colors for the sections.
Their's not much HTML needed. There's just:
  • A button called btnSpin that calls the function spin(),
  • A canvas called Wheel,
  • An arrow left of the canvas to mark the selected name.
Note that the code is part of an ASP.net MVC project. The first script line gets the names for the wheel from the model. The script:
var names = @Html.Raw(Json.Encode(Model.Names)); //Creates a javascript array from a C# list.
var n = names.length;
var colors = []; //Randomized based on 'n' further on.
var fps = 1000 / 30; //Animation speed.
var angle = 0;
var velocity = 1; //For the slowing down of the rotation.
var variation = 0; //So it doesn't always stop at the same name.
  
$(function () {
  colors = Please.make_color({ colors_returned: n });
  draw(angle);
  $('#btnSpin').removeAttr('disabled');
})

function random(min, max) {
  return Math.random() * (max - min) + min;
}

function getFontSize() { //The more names, the smaller they are displayed.
  if (n < 10)
    return 28;
  else if(n < 20)
    return 18;
  else
    return 10;
}

function spin() {
  variation = random(0.15, 0.25);
  window.setTimeout(animate, fps); //Begin the animation.
  $('#btnSpin').attr('disabled', 'disabled');
}

function animate() { //Continue spinning until the wheel slowed down enough. Higher velocity means slower rotation.
  velocity += variation * (velocity < 40 ? 1 : (velocity / 40));
  draw(angle += Math.PI / velocity);
  if (velocity > 200) {
    velocity = 1;
    $('#btnSpin').removeAttr('disabled');
  }
  else {
    window.setTimeout(animate, fps);
  }
}

function draw(angle) {
  var canvas = document.getElementById("Wheel");
  var ctx = canvas.getContext('2d');
  var cx = canvas.width / 2;
  var cy = canvas.height / 2;
  var radius = cy - 5;
  var range = Math.PI * 2;
  var a = angle;

  ctx.clearRect(0, 0, canvas.width, canvas.height);
  ctx.strokeStyle = '#222';
  ctx.lineWidth = 3;

  for (var i = 0; i < n; i++) {
    ctx.save(); //Save the current state of the canvas.
    ctx.translate(cx, cy);
    ctx.rotate((a + (a + range / n)) / 2 + (range / 2) + (range / n / 2));
    ctx.translate(-cx, -cy);
    ctx.beginPath();
    ctx.arc(2, cy, 6, 0, range);
    ctx.closePath();
    ctx.shadowColor = '#fff';
    ctx.shadowBlur = 10;
    ctx.shadowOffsetX = 0;
    ctx.shadowOffsetY = 0;
    ctx.fillStyle = '#222';
    ctx.fill(); //Draws the handles on the outside of the wheel.
    ctx.restore();

    ctx.save();
    ctx.beginPath();
    ctx.moveTo(cx, cy);
    ctx.arc(cx, cy, radius, a, a + range / n);
    ctx.closePath();
    ctx.fillStyle = colors[i];
    ctx.fill(); //Draws the filled area of the sections.
        
    ctx.beginPath();
    ctx.moveTo(cx, cy);
    ctx.arc(cx, cy, radius, a, a + range / n);
    ctx.closePath();
    ctx.stroke(); //Draws the outline of the sections.
        
    ctx.translate(cx, cy);
    ctx.rotate((a + (a + range / n)) / 2 + (range / 2));
    ctx.translate(-cx, -cy);
    ctx.beginPath();
    ctx.rect(0, cy - 40, cx - 40, 80);
    ctx.closePath();
    ctx.clip(); //Makes sure the names don't draw outside their section.
    ctx.fillStyle = '#222';
    ctx.font = getFontSize() + 'px Verdana';
    ctx.fillText(names[i], 20, cy + 10);
    ctx.restore(); //Start fresh every time.

    a += range / n;
  }

  ctx.beginPath();
  ctx.arc(cx, cy, radius / 10, 0, range);
  ctx.stroke();
  ctx.fillStyle = '#444';
  ctx.fill();
}

Windows - Hide drives in File Explorer

My administrator mapped some network folders as drives on everyone's PC that I don't have any use for. Getting rid of them is accomplished through the registry:

The registry key

  • Open RegEdit, and open the subkey HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer.
  • If it doesn't exist yet, add a new DWORD value called NoDrives.
  • Put a decimal value in it, as specified below.

The value

Each drive letter has a value double that of the previous letter. So A=1, B=2, C=4 and so on until X=8388608, Y=16777216, Z=33554432. Take the drives you want to hide, add the values of their letters together and that's the value you need to put in the registry.