This is a preview version of Cyotek's blog and may be missing functionality and/or unstable. Please visit https://www.cyotek.com/blog for the current version of the blog.
If you encounter any problems using this preview site, please contact us with the details.

Posts tagged with 'c#'

Creating a custom type converter part 3: Types to string

c# TypeConverter ComponentModel struct 2 Comments Downloads

I have discussed creating type converters a few times on this blog, and recently came across another use for them. I have a .NET Standard library that uses a variety of struct instances that are sometimes properties of concrete classes. I also have a .NET Framework 4.7.2 demonstration application to work with the library that uses the PropertyGrid control to provide easy editing facilities.

Note: While in this article I'm talking about using type converters purely in relation for use with the PropertyGrid control, this isn't their only purpose (just as well otherwise they wouldn't be a part of .NET Standard I assume). You can learn more about the TypeConverter class on MSDN.

Read More

Setting tab stops in a Windows Forms TextBox control

c# winforms SendMessage pinvoke textbox EM_SETTABSTOPS 2 Comments Downloads

I was adding a Wizard to one of my applications, and the final screen of this Wizard was a summary of the user's choices. I wanted the user to be able to copy this to the Clipboard if required, and so I'd used a TextBox rather than the ListView I might have otherwise used. However, this presented a minor issue as I'd chosen to use tabs to delimit the information, and the varying length of text meant that this wasn't aligned as expected.

Previously I have "dealt" with this issue by cheating - I'd just add extra tabs to force everything to line up. However, I do plan on fully localising this application at some point, not to mention that even using a different font could potentially trigger the text to misalign once more. And so I decided to do it properly this time.

Read More

Migrating from Azure translation API version 2 to 3

c# api azure rest translate localization 0 Comments Downloads

Almost two years ago I wrote a post describing how to translate text using Azure cognitive services, however the API it uses is to be switched off and so I needed to migrate from the version 2 API to version 3.

Whilst most of the code I post on this blog is used in one form or another, I've been using the TranslationClient client presented in that article as-is for the past two years. OK, I changed the namespace. But otherwise it's identical.

Read More

Handling the orientation EXIF tag in images using C#

c# exif jpg orientation 0 Comments Downloads

Two weeks ago I received an interesting support ticket from a user of our ImageBox control, stating that when being used for .png files, the control was absolutely file but when used with .jpg files that had been rotated using Windows Explorer shell context menus, they were displayed in the original orientation.

As soon as I read the ticket I had a hunch what was going to be the ultimate cause, and was able to quickly reproduce the problem and then confirm my hunch.

Read More

Reading cookies from Internet Explorer

c# interop webbrowser InternetGetCookieEx cookie 0 Comments Downloads

In order to work around cases where it wasn't possible automatically authenticate with a website, I wanted the ability to use an embedded Internet Explorer window for manual authentication and then reuse the cookies. This article describes how to read cookies indirectly using InternetGetCookieEx or directly from a WebBrowser control.

The InternetGetCookieEx Win32 API can be used to read cookie name value pairs for a given URI. You can use it to read all cookies or a named cookie. You can also specify which type of cookies to include.

Read More

Using message filters in Windows Forms applications

c# winforms pinvoke IMessageFilter 0 Comments Downloads

For a utility application, I wanted to add an item in the system menu. It's been quite a long time since I last did this (and was in VB6), so I decided to find some ready made source code. This class provides a nice little helper for wrapping the system menu to add new commands to it, but it requires the owner form to hook into its window procedure and forward messages on which I felt was an awkward design.

The code snippets below should illustrate my point - first we initialise the instance of the SystemMenu class, but in order for custom commands to be processed we have to override to override the form's WndProc and pass any messages received into the SystemMenu class.

Read More

Working with CorelDRAW Palettes part 2, writing .pal files

c# color rgb color palette cmyk coreldraw 0 Comments Downloads

In my previous article, I described how to read an archaic CorelDRAW! 3.0 palette file. This continuation covers how to write files in this format.

Just like reading the file, writing is also a simple enough process.

Read More

Working with CorelDRAW Palettes part 1, reading .pal files

c# color rgb color palette cmyk coreldraw 0 Comments Downloads

I recently picked up a copy of CorelDRAW! 3.0 from eBay which came on two CD's with a different version on each. That gave me two different surprises, the first in that 3.0A wasn't an improved version of 3.0, and secondly instead of the .cpl format I was expecting to find, there were two different .pal formats, one text based (for CorelDRAW!) and one binary (for PHOTO-PAINT! (very shouty this software!)). This first article covers reading the text based palette format.

The palette format itself is simple enough, from the example colours below we can infer that each colour entry is in CMYK format with the range 0-100. Although it looks as if it is a fixed width format, when looking at other palettes using this format this isn't the case and columns can be of differing widths.

Read More

Converting colours between RGB and CMYK in C#

c# color rgb cmyk 2 Comments Downloads

In my previous articles on reading and writing colours from various palette/swatch formats, I left CMYK conversion as an exercise for the reader and only demonstrated RGB aspects. This article demonstrates how to convert colours in CMYK format to RGB and vice versa.

Unlike RGB which generally uses the range 0-255, most examples of CMYK I've seen use percentages instead, e.g. 37% cyan, 18% magenta, no yellow and 31% black. In this article I'm using the range 0-1 to describe the colours. (Just to be awkward, the demonstration front end uses 0-100 to make it more user friendly!)

Read More

Displaying text in an empty ListBox

c# winforms WM_PAINT listbox wm_erasebkgnd 3 Comments Downloads

While looking at ways of improving the UI of a dialog in an application, I wanted to display some status text in a ListBox control that was empty. The default Windows Forms ListBox (which uses the underlying native Win32 control) doesn't support this, but with a little effort we can extend the control.

When a Control receives either the WM_PAINT or WM_ERASEBKGND messages, it will check to see if the ControlStyles.UserPaint style is set. If set then the WM_PAINT message will cause the Paint event to be raised, and for WM_ERASEBKGND the PaintBackground event - but only if the the AllPaintingInWmPaint style is not set.

Read More