Skip to main content

Posts

C# - Taking Some Screenshots with C# [Beginner]

We do some pretty neat things with all sorts of code, and all this requires a ton of screenshots to help explain all the craziness. But besides having that good ol' fashion "Print Screen" button, we can actually use C# to take a screenshot for us. The best part of all is that it is only a couple lines of code. So lets just go ahead and dive right into it. Now, all we are going to do is create a function that you can call whenever you need to take a screenshot. The first thing we need to do is add a using statement, because we are going to use a non-standard namespace. We need to include the System.Drawing.Imaging namespace in order for this to work: Using System.Drawing.Imaging;   That will let us use the basic yet powerful methods that will help take a screenshot. The first thing we need to do is setup an empty method to fill up. To start, our method will take in nothing and return a bitmap, so it will look something like this: public Bitmap Screen...

C# Strategy Pattern - using Interfaces and Delegates [Beginner]

Have you encountered code with a lot of if-else logic? I am sure you have. Most of the time we see that kind of logic, behind the scenes, it's a strategy problem. Someone trying to figure out a strategy to solve the problem using the most primitive language tool - 'if' statement.  However, when it comes to Object Oriented Programming, it's important to understand that the code needs to be reusable, testable and at the same time scalable. That's where a Strategy Pattern comes in handy. Let's take a look at an example. (Examples are in C# but the concept can be applied to any Object-Oriented Language) First, we will see the code for something that is written in the old fashion. public int Calculate ( int x , int y ) { if ( action == "Add" ) { return x + y ;} } else if ( action == "Sub" ) { return x - y ; } else if ...... .... ...

C# Code Generation [Beginner]

Code generation is a big part of most modern frameworks. .NET is full of code generators that you've probably used - maybe without even knowing it. XAML files are converted to .g.cs files that are then fed to the compiler at compilation time. WCF is bundled with a code generator that will convert a WSDL file to a client to easily connect to existing web services. Today's tutorial will cover how to use objects provided by the .NET framework to build your own code generator. The code we're going to generate today won't be especially useful, however it will demonstrate a lot of the available functionality. I'm going to use C# to generate another C# class that can hold part of someone's Twitter feed. The .NET namespace that makes all this possible is System.CodeDom . Much like the name implies, these classes allow you to build a "DOM" of class hierarchies and then dump them out to text. The first thing you should check out before starting...

C# Dialogs, Part 2 - Custom Dialogs [Beginner]

OK, so in Part 1 we went over the MessageBox that C# offers. As simple and easy as it is to use MessageBox, sometimes you need something more customized to your needs. Although MessageBox is extremely simple to use, it is almost as easy to create your very own dialog and use whatever you want. The first thing we must do is create a new project. I named my project "Custom Dialog", but any name will do. Once your project is created and ready, the first step is to create a new windows form to be our dialog, which is a simple process. All you need to do is right-click on your solution in the Solution explorer, go to Add , then Windows Form . The screenshot below shows you exactly how its done. Now we need to work with our new form, first go ahead and put a label on it, representing the question or dialog text. Once the label is all squared away, we need to add the buttons. Adding the button is easy enough, but we need to change them in order for them to wor...

C# Dialogs, Part 1 - MessageBox [Beginner]

We have all seen them, and most of the time they mark something bad. They are dialog boxes and not only have they been the bane of all computer users since the GUI was invented, but they are really a key to any software project. Whether you are making a simple text editor, or you programming a time viewing machine, chances are you at some point going to need a dialog box. Luckily, with C# adding one is super easy. To start off we will make a simple popup that will alert the user of some useful information. To do this all we need to do is use MessageBox.Show() and that will make a super simple popup window that will tell our user whatever we want. So it would look something like this: MessageBox . Show ( "SOTC has the best tutorials!" )   All this will do is bring up a small dialog box, with the message being whatever the text is. Nothing special, but it can get the job done if you need something simple. All the user has to do is either hit OK or the X button ...

C# Snippet Tutorial - Determining if Aero is Enabled [Beginner]

Recently I was working on a project and the UI required minor tweaks depending on whether or not Aero was enabled. Fortunately, I came across an MSDN forum topic with the solution, so I thought I'd share. There's no way to do it within .NET, so you'll have to use DLLImport to bring in the Windows API function, DwmIsCompositionEnabled . [ DllImport ( "dwmapi.dll" , EntryPoint = "DwmIsCompositionEnabled" )] public static extern int DwmIsCompositionEnabled ( out bool enabled );   Once you've got it imported, using it is pretty straight forward. bool aeroEnabled ; DwmIsCompositionEnabled ( out aeroEnabled ); Console . WriteLine ( "Area Enabled: " + aeroEnabled . ToString ());   This function is only supported in versions of Windows starting with Vista, so it might be a good idea to check the OS version before using it. if ( Environment . OSVersion . Version . Major > 5 ) { bool aeroEnabled ; ...

C# Snippet Tutorial - Comparing IP Addresses [Beginner]

Comparing two IP Addresses is something that has caused me frustration on more than one occasion. Simply put, I always assume the default == operator will tell me if the addresses contained in the objects are equal, and as always, it does not. Using == on reference type objects will (almost all of the time) return true if the references point to the same object. This means, comparing two IPAddress objects using == will return true if they are the same object, not if the addresses contained within them are the same. However, the IPAddress object does override the default implementation of the Equals method, which is what you should use when you want to compare IP Addresses. IPAddress addr1 = IPAddress . Parse ( "192.168.1.100" ); IPAddress addr2 = IPAddress . Parse ( "192.168.1.100" ); Console . WriteLine ( addr1 == addr2 ); // False Console . WriteLine ( addr1 . Equals ( addr2 )); // True   Because my curiosity knows no bounds and it'...