|
|
4月20日 Partial classes is a nice feature coming with .NET 2.0. With partial classes, it is possible to split one bigger class into separate files. For instance, Visual Studio 2005 automatically separates the business and design logic of a control class into to files: filename.cs and filename.designer.cs.
For some purposes it could be very neat to separate parts of a class that belong to a group of functionality into some more files. To do this, you simply need to create a new file, name it ClassName.addition.cs. Visual Studio 2005 treats all file names before the first point as one, so you only need to define in both files the class with the partial keyword e.g.:
public partial class MyClass
{
…
}
As I mentioned, it really makes sense, for instance, I got a form that has an edit and a display mode, so I separated the business logic of these modes into separate files, so I don't lose track of it.
4月17日 Note: The vista complete pc restore is a functionality which is present on windows vista ultimate and windows vista business but not home premium or home basic
Today, when I came home from work, I had a unpleasant surprise when I was synchronizing my laptop with my pc: At the tray, I noticed a new tray icon, so I clicked on it to find out what it is for. It opened a dialog that informed me that my Intel Raid 0 with 2 hd has detected an error on one disk and I should perform a complete backup asap. I'm using 2x250 hds and although I got a usb hd for backup purpose, this backup disk would never fit the required size. So I removed all temporary files and deleted all restore points except the latest to make size. From 260 gb of used bytes I could shrink it to 180 gb! Nice work, but still to much for my 80 gb backup drive. So I decided to abuse my windows xp computer with a 2x80 raid 0 system which I only use occasionally for windows xp testings and plugged them onto my vista computer and made my first complete backup. Other than a standard backup, the complete backup does not compress the files, so the size is indeed the same as the hd requires. It creates a *.vhd file that possibly can be used on a virtual machine like virtual pc 2007.
Now I could spend my time to attempt to solve the disk problem. What I did was simply delete the raid and recreate it again. Unfortunately, this also caused the contents of the raid getting lost, but no problem I got a backup! I restarted my pc with my windows vista ultimate dvd and instead of installing I selected the restore option, loaded the drivers for the backup raid (which was not an intel driver) from a usb stick that I've loaded from the internet with my laptop, and it detected the backup. Currently the computer is still restoring.
Finally, I got to say that this functionality is a very nice feature, because it saved me lots of time reinstalling vista and all applications again, which would take at least one day, maybe too. 4月14日 Here is a code example for an application that converts toolbar images by size and compression. I pulish this application for the sake of offering hints and ideas of how to develop software with .NET 2.0.
Interesting might be the IsImage() method where I check if a file name has an extension of an image. Instead of checking each possible extension separately I use a loop and a string[] collection that is initialized staticly which makes this method easy to read, small in code and easily to extend.
Also interesting might be how to implement drag & drop functionality to an application and read the items that where dropped on it, eq. from the windows explorer.
Now here comes the code:
SourceImage.cs
namespace ImageConverter { public class SourceImage { public SourceImage(string path) : base() { this.path = path; } internal string path; public string Path { get { return path; } } public string Name { get { return System.IO.Path.GetFileNameWithoutExtension(path); } } internal string RenameExt(string extension) { string ext = System.IO.Path.GetExtension(this.path); string name = Name; { name = name + "_copy"; } string path = System.IO.Path.GetDirectoryName(this.path); path = System.IO.Path.Combine(path, name) + extension; return path; } }
public class SourceImageList : List<SourceImage> { public SourceImage Add(string path) { SourceImage image = new SourceImage(path); Add(image); return image; } }
ImageFormats.cs
namespace ImageConverter { public sealed class ImageFormatInfo { public ImageFormatInfo(ImageFormat format, string extension, string description) : base() { this.format = format; this.description = description; this.extension = extension; } private string extension; private string description; private ImageFormat format; public string Extension { get { return extension; } } public string Description { get { return description; } } public ImageFormat Format { get { return format; } } } public sealed class ImageFormatList : List<ImageFormatInfo> { public ImageFormatList() : base() { Add(ImageFormat.Bmp, ".bmp", "Bitmap"); Add(ImageFormat.Jpeg, ".jpg", "Jpeg"); Add(ImageFormat.Gif, ".gif", "Gif"); Add(ImageFormat.Png, ".png", "Png"); } private void Add(ImageFormat format, string extension, string description) { Add(new ImageFormatInfo(format, extension, description)); } } }
Form1.cs:
namespace ImageConverter { { Size imageSize = new Size(16, 16); private Color imageBackground = Color.Fuchsia; public Form1() { InitializeComponent(); ImageBackGround = Color.Fuchsia; cbTarget.DataSource = formats; } private ImageFormatList formats = new ImageFormatList(); private SourceImageList sources = new SourceImageList(); public Color ImageBackGround { get { return imageBackground; } set { imageBackground = value; tbColor.BackColor = value; } } private void listView1_DragOver(object sender, DragEventArgs e) { e.Effect = e.AllowedEffect; } private void imageList_DragDrop(object sender, DragEventArgs e) { string[] images = (string[])e.Data.GetData(DataFormats.FileDrop, false); foreach (string image in images) { if (IsImage(image)) { SourceImage sourceImage = sources.Add(image); if (image!=null) { imageList.Items.Add(sourceImage.Name).Tag = sourceImage; } } } } private bool IsImage(string image) { string ext = System.IO.Path.GetExtension(image); string[] extensions = { ".bmp", ".gif", ".jpg", ".jpeg", ".png" }; foreach (string extension in extensions) { if (ext.Equals(extension, StringComparison.OrdinalIgnoreCase)) return true; } return false; } private void btConvert_Click(object sender, EventArgs e) { ImageFormatInfo format = cbTarget.SelectedItem as ImageFormatInfo; foreach (SourceImage image in sources) { using (Bitmap targetBm = new Bitmap(imageSize.Width, imageSize.Height)) { Graphics g = Graphics.FromImage(targetBm); g.Clear(imageBackground); using (Bitmap sourceBm = new Bitmap(image.Path)) { sourceBm.MakeTransparent(); int x = (imageSize.Width-sourceBm.Width)/2; int y = (imageSize.Height-sourceBm.Height)/2; g.DrawImageUnscaled(sourceBm, x, y); string fileName = image.RenameExt(format.Extension); targetBm.MakeTransparent(ImageBackGround); targetBm.Save(fileName, format.Format); } } } } private void button1_Click(object sender, EventArgs e) { colorDialog.Color = ImageBackGround; if (colorDialog.ShowDialog() == DialogResult.OK) { ImageBackGround = colorDialog.Color; } } private void btClear_Click(object sender, EventArgs e) { sources.Clear(); imageList.Clear(); } private void numericUpDown1_ValueChanged(object sender, EventArgs e) { imageSize.Width = (int)((NumericUpDown)sender).Value; } private void numericUpDown2_ValueChanged(object sender, EventArgs e) { imageSize.Height = (int)((NumericUpDown)sender).Value; } } }
4月13日 Since Windows Vista, you're familiar with this new kind of control. The breadcrumb bar was introduced with the windows explorer and replaces the combo box for the path. But the breadcrumb offers much more than only showing the path. It separates each sub folder as a breadcrumb so the user can go back with ease by just clicking the appropriate breadcrumb or selects another sibling of a breadcrumb which represents another folder inside a folder. The breadcrumb bar can also include some image buttons on the right and an icon on the left as well as a dropdown button that behaves like the dropdown button of a combo box. Even a progress bar is included in the breadcrumb bar!
You can use a breadcrumb bar not only for paths but also for other purposes. For instance in my profession I'm developing an application that shows the medical history of a patient. In the breadcrumb bar, I offer the name of the patient and also the available health insurance voucher of the patient, followed by its type, and the date separated by year, quarter or month, day that may look like this:
Miller, Max (2003-03-24) à Emergency à AOK Berlin à 2007 à November à 15
This is just one other example for what a breadcrumb bar can be used for, there are so many other possibilities. As I'm developing .NET applications, I wrote a breadcrumb bar as part of my jedi.dll assembly which also contains the previously mentioned JediGrid. Soon the jedi.dll will be available for free private use on www.tomssoftware.net, just be patient.
Here is a brief scheme of the breadcrumb bar:

4月11日 .NET 2.0 offers an easy way to find any item of a generic list that is sorted by the item to get using a binary search that has an order of O(ln N). But what if you need to find an item that has not the exact value and you want to find the item that is lower/equal or higher/equal? Fortunately, the BinarySearch method also supports this situation:
BinarySearch returns the item index that matches the item to find, otherwise the return value is less than zero. But this value is a negative value that represents the nearest item that is lower than the item to search minus 2.
The following example uses this feature to find an item that is either equal or less than the index to search for:
First, a comparer class is derived that compares the items. The constructor has a parameter for the index to find instead of specifying a class for the BinarySearch method. As the BinarySearch will be parameter less, the second parameter for the Compare method will always be null and can be ignored:
/// <summary> /// A comparer class to compare the groups list to find a item faster using binary search. /// </summary> class MyComparer : IComparer<MyClass>:IDisposable { public MyComparer(int index) : base() { this.index = index; } private int index; public int Compare(MyClass x, MyClass y) { return x.index - index; } }
The following method LowerOrEqualValue finally performs the search by creating a MyComparer class. If the result value of BinarySearch is positive or null, an item with exact the required index exists, otherwise the index is a negative value of the nearest index that is less than the specified index, so we make it positive with the formula
resultIndex = -2 – resultIndex
Note that, if there is really no item, the return value would be -1 so the formula finally would also be -1.
private int LowerOrEqualValue(int index) { using (MyComparer comparer = new MyComparer (index)) { int resultIndex = items.BinarySearch(null, comparer); if (resultIndex < 0) resultIndex = -2 - resultIndex; if (resultIndex >= 0 && resultIndex < resultIndex.Count) { return resultIndex; } return -1; } }
If you don't need the next lower item but the next higher value, you just need to replace the formula by
resultIndex = -1-resultIndex
But then you must explicitly check the situation of no result when BinarySearch returns -1!
4月9日 Soon I will offer a free version of the Jedi Library for download with the following contents:
The library will be free of charge for private use though it is not allowed to use it for professional applications without permission!
XML Documentation on VisualStudio 2005 is a very neat thing that offers online help of the classes and its members without the need of opening the source file. But as soon as you distribute an assembly without source code, the documentation is not available when you use a class of the assembly by default. To have the documentation available that you (hopefully) created, you need to add an xml file the contains the documentation. Fortunately, to create this xml file is a piece of cake as Visual Studio 2005 does it for you. You simply need to do the following steps to enable xml documentation:
- Open the project properties
- At the Build page ensure XML Documentation file is checked:
-
Finally simply recompile your project. But if you not have already documented all public and protected methods and classes, you will get additional warnings for those methods and classes. It's a little bit annoying to document every method even commonly known derived methods like OnPaint, but some tools like GhostDoc help to minimize the steps.
4月1日 There are certain rumors about this issue that claim that as soon as ready boost is installed, the cpu raises from 1% idle time to constantly 30-40%, like the following article:
- how serious are these reports?
- what is fact?
The truth is, the reports a right, but:
- as soon as you install a new medium for ready boost, vista analyzes the system to determine what can be extended to the external medium. This analysis requires a view minutes and does indeed consume resources so that your computer can become unhandsome.
- Also, when you reboot, this anayzing process starts when you log in, but
- Vista is designed not to shut dow when you press the exit button, but rather going in standbye or sleep mode due to the fact that meanwhile all main boards and cards supports standbye and against common opinion that standbye consumes more power thant to shut down the computer, the truth is, that there is not difference in power consumption whether you are in sleep mode or you have indeed shut down your computer. Only when you switch the button on the back of your computer, it is really off.
So I come to the result, that ready boost does what it's supposed to do, to increase the performance of your computer as long as you use you're vista computer like it was designed to be used.
Rumors that claim to say the oposite are either mistaken because of the analyzis process, or just propaganda from tux landers.
|