Posts Tagged ‘Programming’
Team Foundation Service is live! Up to 5 users are free, and for a limited time all use is free!
Go to http://tfs.visualstudio.com/.
I want to read, write (modify) and save Unicoded document with TMemo component in Delphi. And failed.
On the Internet, I had found a procedure to load Unicode files into TMemo, and it worked. But Unicoded string shows like this “???????” (without quotation marks). No, it didn’t work. It seems to be quite difficult to do so.
Here is the code I used. I named TMemo to “Doc”.
Be sure, it didn’t work!
procedure TForm1.LoadUnicodeFile(const filename: String; strings: TStrings);
procedure SwapWideChars(p: PWideChar);
begin
while p^ <> #0000 Do
begin
p^ := WideChar(Swap(Word(p^)));
Inc(p);
end;
end;
var
ms: TMemoryStream;
wc: WideChar;
pWc: PWideChar;
begin
ms:= TMemoryStream.Create;
try
ms.LoadFromFile(filename);
ms.Seek(0, soFromend);
wc := #0000;
ms.Write(wc, sizeof(wc));
pWC := ms.Memory;
if pWc^ = #$FEFF then
Inc(pWc)
else if pWc^ = #$FFFE then
begin
SwapWideChars(pWc);
Inc(pWc);
end
else
;
strings.Text := WideChartoString(pWc);
finally
ms.free;
end;
end;
// This function is necessary to validate the file.
function IsTextFileUnicode(const TextFilename: string): boolean;
var
FS : TFileStream;
w : word;
begin
result := false;
if FileExists(TextFilename) then
begin
FS := TFileStream.Create(TextFilename, fmOpenRead or fmShareDenyNone);
try
if FS.Read(w, SizeOf(w)) = SizeOf(w) then
result := (w = $FEFF) or
(w = $FFFE);
finally
FS.Free;
end;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var OD: TOpenDialog;
begin
Doc.Lines.Clear;
OD := TOpenDialog.Create(Self);
with OD do
begin
Filter := 'Text Files (*.txt)|*.txt';
if Execute then
begin
if IsTextFileUnicode(FileName) then
LoadUnicodeFile(FileName, Doc.Lines) // Load Unicoded text
else
Doc.Lines.LoadFromFile(FileName); // Load ANSI text
end;
end;
end;
I had been looking for C# source code that can be used to learn. Something simple program I wanted. Also I’m interested in images and graphics, I want to have something like that.
Luckily, I found the source code but Visual Basic .Net. And there was something wrong with the code and I could not compile it. It was done by the older version of Visual Studio and it works fine with it, I think. But not with mine (Visual Studio 2008). Since I don’t speak VB.Net, it was a big problem. But I’ve found a good translator on the Internet. There you can translate different languages as well.
http://www.developerfusion.com/tools/convert/vb-to-csharp/
It is so clean, nice and simple.
Finally I could make graphics resizer with C#. It works, but not correctly. The height and the width looks strange. Distorted. Apparently I did something wrong, but where? I do not know. I had been working on the problem, but I did not find the solution. So I ended up with this.
It did not work and it is absolutely not author’s fault but mine.
New Feature
- URI Grabbing function. You are able to grab links from the HTML files.
You may have found a webpage that contains many links with link text and you do not want to copy the texts but links. Just save page as a HTML file and load it into MEMO URI Grabber!
Fixed bug
- When you modify saved documents and select a new document, it showed “File Untitled is modified…” – has been fixed.
Now it shows the document path and name. - When you hit a “Delete” key, it does not happen anything – has been solved. You just need to press “Shift” key when you want to use “Delete” key to delete right side of the cursor. Then it acts as Notepad.
Changes
- Current line color has changed to gray instead of yellow.
This change was necessary, for those who use the “Twilight” or things like dark background and light text, you cannot see the text otherwise. - And some small changes have been made.
People want something to happen. And me too.
So I made the MP3 player. And it really can play MP3.
With the “MediaElement”, you can play video files too.
“MainPage.xaml” looks like this.
<UserControl x:Class="MP3MediaTest.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480">
<Grid x:Name="LayoutRoot">
<StackPanel>
<MediaElement AutoPlay="False" x:Name="myMP3" Source="Etudo.mp3" />
<TextBlock FontSize="20" x:Name="myStatus" />
<StackPanel Orientation="Horizontal">
<Button x:Name="myPlayButton" Width="100" Content="Play" Click="OnPlay" />
<Button x:Name="myPauseButton" Width="100" Content="Pause" Click="OnPause" />
<Button x:Name="myStopButton" Width="100" Content="Stop" Click="OnStop" />
</StackPanel>
</StackPanel>
</Grid>
</UserControl>
And “MainPage.xaml.cs” looks like this.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
namespace MP3MediaTest
{
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
this.myMP3.CurrentStateChanged += new RoutedEventHandler(OnCurrentStateChanged);
}
void OnCurrentStateChanged(object sender, RoutedEventArgs e)
{
bool isPlaying = (this.myMP3.CurrentState == MediaElementState.Playing);
this.myPauseButton.IsEnabled = isPlaying && this.myMP3.CanPause;
this.myPlayButton.IsEnabled = !isPlaying;
this.myStatus.Text = this.myMP3.CurrentState.ToString() + " - " + this.myMP3.Source.ToString();
}
private void OnPlay(object sender, RoutedEventArgs e)
{
this.myMP3.Play();
}
private void OnPause(object sender, RoutedEventArgs e)
{
this.myMP3.Pause();
}
private void OnStop(object sender, RoutedEventArgs e)
{
this.myMP3.Stop();
}
}
}
<UserControl x:Class="ImageBrushTest.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480">
<Grid x:Name="LayoutRoot">
<Canvas>
<Rectangle RadiusX="30" RadiusY="30" Fill="AliceBlue" Stroke="Gray" StrokeThickness="5" Canvas.Left="5" Canvas.Top="5" Width="360" Height="360" />
<Ellipse Width="330" Height="330" Canvas.Left="20" Canvas.Top="20" Stroke="Coral" StrokeThickness="10">
<Ellipse.Fill>
<ImageBrush Stretch="Uniform" ImageSource="bird.png" />
</Ellipse.Fill>
</Ellipse>
</Canvas>
</Grid>
</UserControl>
Image file should be in the same folder as the XAP file (“ClientBin” folder). If it is not the case, you must specify the path to the file (ie http://www.yoursite.com/images/bird.png).
Actually, the XAP file is a ZIP file with different extension. You can change its extension and unzip it.
As I wrote, it did not work at first.
I then changed it this and that … and now it works. I’ll explain how to do.
My system is as follows:
Vista x64
Visual Studio 2008
If you have trouble using the “IEvolution” with windows 7 x64, you should try this too.
- You must copy the “iecore.dll” to the Window\System
(NOT sysWOW64, Nor System32) - In Visual Studio, “Tools -> Choose Toolbox Items” and on “. NET Framework Components” tab, browse “IEvolution2.dll”.
(You can do the same with right-click the Toolbox and select “Choose items …”) - When you open the Demo Project, you must add “references” to IEvolution, and then right-click the project name and select “Properties”, then “Build” tab.
You must change the “Platform Target – Any CPU” to “x86 “.
That’s it!
When you create your own project, you need to make it No.3.
Now everything works!
This way, you can work with the latest version of IEvolution3.12.
IEvolution is freeware and you can download it from the following URI:
HiComponents
I installed IEvolution by HiComponents (http://www.hicomponents.com/), but I cannot even compile the demo program included with the DLLs.
I installed IEvolution.DLL and copied IECore.DLL in the Windows System folder. But it does not work.
When I compile demo project, I get only the following exception.
Exception
Make sure the file image is a valid managed assembly or module.
This exception is thrown when unmanaged code is passed to Load for loading.
Actually I cannot convert demo projects to Visual Studio 2008 projects, not so easy either. XML error occurs!
Can someone tell me how to do it?
In addition to the game, I read “Microsoft Visual C# 2008 Express Edition: Build a Program Now!“, and made this program (see above).
This book is for beginners, and I am a beginner on C#.
It is a good book. I recommend this book for anyone who wants to start C#. Not a lot of theories but lots of code. Quite fun to program. I have Visual Studio, but it was made for the Express edition to compile the example.
This is the final chapter “Weather Tracker”, and it can retrieve weather from MSN. It feels a bit buggy but it works!
I could not display the splash screen in the beginning. I read many times if I missed something, but did not. So I looked at included source code. And “Ha ha!”. It lacked something, of course.
splashScreen.Show();
Oh, yes! This is very important! You should set Main.cs design properties “ShowInTaskbar” – “False”.
Some “namespaces” also have to add yourself. In the last chapter you will suddenly pop up to higher ground – so I felt.
I did a little extra with that program.
If you have the book, and you want users to be able to use pop-up menu from the Main form, just change its properties “ContextMenuStrip” – “None” to “cmsNotify”.
Good luck!
I wanted to make a small game with C# because I got a book, and was looking for a program that could convert the font to BMP or something like that.
I found both shareware and freeware on the Internet, and I was not happy with them. Especially shareware. I did not want to pay for just that. I thought almost do it myself, but I happened to find a good one. Actually on Microsoft’s website. Even better, it’s free. Moreover, with source code in C#. Source is slightly older and you have to convert them but it went well for me.
The text below is from the included HTML file with the source code.
This utility converts Windows TrueType fonts into .bmp files that can be imported using the XNA Framework Content Pipeline, and converted into SpriteFont objects by BitmapFontProcessor.
OverviewWhen you add a new font description to an XNA Framework project, this creates an XML description file describing what font you want to use. The Content Pipeline reads this description, renders out the appropriate characters using the Windows Font API, and converts them into a format that can be efficiently rendered using the SpriteFont class.
You may find “ttf2bmp.exe” on the web, but I would say that it is only the source code. Anyway what I found on Microsoft’s website.
Sorry I forgot the address. Go to the http://www.microsoft.com/express/ and look for ttf2bmp.exe.






