Coding

All things computer programming related...

Import a font using c-sharp

- Posted in Coding by
using System.Runtime.InteropServices;

      //interop to allow import of game font
      [System.Runtime.InteropServices.DllImport("gdi32.dll")]
      private static extern IntPtr AddFontMemResourceEx(IntPtr pbFont, uint cbFont,
          IntPtr pdv, [System.Runtime.InteropServices.In] ref uint pcFonts);

Also before the form's constructor, add a form-level variable (but make sure it comes after the Win32 function import):

Font myFont;

If you have the desired font as a .resx resource in your Visual Studio project, you'll use the following code inside the form's constructor:

//Form1 constructor code that pulls game font into memory
          byte[] fontData = Properties.Resources.brushstr;
          IntPtr fontPtr = System.Runtime.InteropServices.Marshal.AllocCoTaskMem(fontData.Length);
          System.Runtime.InteropServices.Marshal.Copy(fontData, 0, fontPtr, fontData.Length);
          uint dummy = 0;
          fonts.AddMemoryFont(fontPtr, Properties.Resources.brushstr.Length);
          AddFontMemResourceEx(fontPtr, (uint)Properties.Resources.brushstr.Length, IntPtr.Zero, ref dummy);
          System.Runtime.InteropServices.Marshal.FreeCoTaskMem(fontPtr);

          myFont = new Font(fonts.Families[0], 16.0F);

Tweego

- Posted in Coding by

At the time of this writing, I couldn't find a Windows binary release of the Tweego compiler for Twee source code files (Tweego is a program written in Go that can compile Twee source code files into HTML games that can be played in a web browser).

I didn't make a careful record of the perambulating required to obtain a copy of the Tweego executable. It had to be built from source, which I recall involved building it with Node package manager. I managed to accomplish this, however, and installed Tweego.exe at C:MyTweego on my Win10 PC, then added that path to my PATH environment variable.

Sweetness after that.

Play .wav or .mp3 media using c-sharp

- Posted in Coding by
using System;

namespace PlayWav{    

    class Program {                

        static void Main(string[] args)
        {

            Console.Clear();            

            System.Media.SoundPlayer player = new System.Media.SoundPlayer();
            player.SoundLocation = "upbeat.wav";
            player.Play();
            Console.WriteLine("nn Press any key to exit...");
            Console.ReadKey();
        }

    }

}

Now, this is quite handy for small .wav files used as sound effects. But if you're going to play songs, you wanna use .mp3 format. That's not quite as simple. After some research, I found it's not difficult if you use a freely available library called NAudio. Your code must reference this library, whose code is contained in file NAudio.dll. I should note that the following code will suffice equally well whether you wish to play .wav or .mp3 files:

using System;
using NAudio;
using NAudio.Wave;

namespace PlayMp3{   

    class Program {                

        static void Main(string[] args)
        {

            Console.Clear(); 
            Console.WriteLine("n Press a key to play a 7-second segment from 'Somewhere Out There' (a 1986 song)");
            Console.ReadKey();
            using(var audioFile = new AudioFileReader("somewhere.mp3"))
            using(var outputDevice = new WaveOutEvent())
            {
                outputDevice.Init(audioFile);
                outputDevice.Play();
                while (outputDevice.PlaybackState == PlaybackState.Playing)
                {
                    System.Threading.Thread.Sleep(1000);
                }
            }
            Console.WriteLine("nn Press any key to exit...");
            Console.ReadKey();
        }

    }

}

Some of my programming creations

- Posted in Coding by
C# Programming

I've dabbled in C# since this programming language was first introduced. Here I provide links to useful tips and snippets, source code, and related thoughts. And let me assure you, these source code files represent a lot of time and learning on my part.

My C# Projects (you're welcome to use/modify the source code of any of these projects, even for commercial purposes) ↓

Found Money: A GUI for tracking piggy-bank deposits over time → | on box.com | on GDrive

Sudoku: My take on the Sudoku number grid gameon box.com | on GDrive

Proofy: My proofreading jobs / words-proofed tracker → | on box.com | on GDrive

KyrCrypt: My files and folders encrypter → | on box.com | on GDrive

<

p class="tab40">KyrHangman: a game I wrote for a donationcoder NANY → | on box.com | on GDrive

SLOC: My counter of source code lines → | on box.com | on GDrive

How Long Since?: Calculates elapsed time between any two dates → | on box.com | on GDrive

KyrPrayerMinder: Prayer journal or use as diary; searchable → | on box.com | on GDrive

Basic BlackJack; decent implementation of a card Deck class → | on box.com | on GDrive

Example of Square Proximity Algorithm: demonstrates detection of square proximity/overlap → | on box.com | on GDrive

How to install nuget on Windows

- Posted in Coding by

Open a PowerShell prompt with elevated privilege (as Administrator)

Get nuget.exe command line wget https://dist.nuget.org/win-x86-commandline/latest/nuget.exe -OutFile nuget.exe

Download the C# Roslyn compiler (just a few megs, no need to 'install') .nuget.exe install Microsoft.Net.Compilers

Note: as of date 21 Aug 2018, this installs the Roslyn compiler here:

C:WindowsSystem32Microsoft.Net.Compilers.2.9.0tools

Count top-level subdirectories

- Posted in Coding by

C# get number of top-level subdirectories in a directory.

Here's a brief program that demonstrates counting the number of top-level subdirectories that reside within a given directory:

https://gist.github.com/kyrathasoft/1dc1a1eb4d6a2a56827dcb5f43840afe

Compress to archive using c-sharp

- Posted in Coding by

I've hosted the source code on GitHub demonstrating using .NET to compress a folder and its contents into a single .zip archive. It also demonstrates decompressing such an archive into its component directory, subdirectories, and files.

To compile, you'll need response file resp.rsp in the same directory as simpleCompExt.cs. The contents of that response file should be:

/r:System.IO.Compression.dll
/r:System.IO.Compression.FileSystem.dll
/out:simple.exe
simpleCompExt.cs

To compile from the command line, execute the following from the source code directory:

csc @resp.rsp

If you want to experiment with this, be sure to add a few small files to the /start subdirectory (so that there's something to compress and extract).

Copy one directory to another

- Posted in Coding by

Introducing the CopyTo method, which faithfully copies one directory's contents to another directory. The method listing is on Github here.

And here is a sample program demonstrating its usage.

Moving a form programmatically using C#

- Posted in Coding by

Shows how we can access Form1's default TextBox (textBox1) from within another class, by passing into that other class's constructor a refence to Form1. We create public properties for the form's 'textBox1' TextBox, because the textbox is private to Form1 (see the declaration in Form1Designer.cs, which looks like this):

private System.Windows.Forms.TextBox textBox1;

By passing a reference to Form1 into the other class's constructor, and then utilizing the properties we created, we can alter Form1's textbox's .Width and .Text properties from another class:

Properly capitalize proper nouns with c-sharp

- Posted in Coding by

C# properly capitalize proper nouns

Here is a method that, if passed a proper noun, such as a person's name or a street address, will return a properly formatted version of that string.

Page 6 of 7