![]() |
Chapter 1 –Creating the Save DialogOk, let’s start creating the dll. Fire up your Delphi version and press File->New->Other… In the listview quite far down you can see an item called “DLL Wizard”, select it and hit Ok. Now you get the source code for an empty dll, with no methods at all. But before we begin adding methods we should save the project, so lets just hit “File->Save As…” and save the project in any folder you like, and name it “dialog.dpr”. Now locate the section that says:And change the section so it looks like this: The reason we added “dialogs” is because the methods we need to create the dialogs are stored in another delphi unit called dialogs. Anyway, the only thing missing in the dll right now is two methods. One for displaying the open dialog and one for showing the save dialog. Here’s the full code for the save dialog, I will decribe the code a bit more in detail later. The first line, starting with “function “ is the declaration of the method, and as you have noticed we didn’t use the string data type, instead we used the Pchar to manage parameters. Now why did we do that? Well, if you use strings in delphi dlls you need to distribute another dll called borlandmm.dll wich is a memory manager that makes it possible to exchange strings using the Delphi type of string. In windows it’s actually Pchar that is used as strings, a Pchar is simply an array of characters, ending with a null character to show that it’s the end of the string. Another thing to note is that the line is ending with “stdcall;”, that is to tell wich calling convention the dll uses, so that the calling application handles the in and out data the same way. Ok, enough of that, the parameters we’ll be accepting is title wich is simply the title of the save dialog. Defaultext is the default file name extension and finally the filter is the type of files to show in the savedialog. Next thing we did on line two and three was declaring a variable called dlg of type TsaveDialog. Then after the begin line we created the TsaveDialog and assigned the parameters to the different properties of the dlg variable. What we do next is simply showing the dialog, and if the user presses the Ok button we return the filename, else we simply return an empty string. The last thing we do is freeing the memory used by the dlg variable. So, that’s all we need to do to create a nice little savedialog. Chapter 2 –Creating the Open DialogThis code is actually quite similliar to the save dialog and it will accept the same parameters. I will present the code below, followed by a more detailed description.As you can see the first line is completely identical as the savedialog method, except is has a different name. An other difference it that instead of declaring a variable of type Tsavedialog we here have the variable as a type Topendialog. Other than that theese two methods are very identical to eachother. Chapter 3 –Finnishing the dllWell, allthough this code compiles fine with no errors there are three more lines that must be inserted in the code before we can call it from a Blitz program. We need to tell wich methods are exported and usable outside the dll, this is done using the keyword exports. So what we should do is to place the cursor above the “begin…end.” Section, but below the “executeOpendialog” method we wrote, and insert the code below:All you need to do now is to build your project by selecting Project -> Build project and copy the resulting dialog.dll file to the Blitz userlib folder (usually c:\program files\blitz3D\userlibs or something like that). The full source of the dllIf I haven’t made myself clear on how the source of the dll file should look when it’s done, here it is, all you have to do is to paste it in a delphi code editor.Chapter 4 – The declaration fileThe dll is finnished and placed in the correct folder, all that’s missing now for the possibility of calling it from your Blitz program is a declaration file containing the name of the dll and descriptions of the methods that we should be able to call. Open up Notepad and add theese lines:Then save the plain text file in the same folder as you placed the dll in (the userlib folder, remember?) and name it “dialog.decls”. If you look at the text you can see it’s rather simple, the first line tells the name of the dll, and the following two lines shows the way the methods are called from within Blitz. Now close notepad. Chapter 5 – TestrunEverything is done, and all we need to do is to test it to make sure it works well. Fire up Blitz and create a new file. Simply add theese lines to test the newly created userlib:The code above is quite self explanatory, the only thing you need to know is the way to pass the filter parameter. In the standard open and save dialog you can select a file type, that’s the filter. The way to pass this parameter is to separate the different filters with “|”, to the following format: “file type description|file mask|file type description|file mask” e.t.c. Chapter 6 – ConclusionAs you can see, it’s very easy to create userlibraries with Delphi, and it’s even easier to call them from Blitz, so why haven’t I seen many more usefull libraries in the showcase? I hope that will change soon. And I hope this article have given you an usefull introduction on how to create your own userlibraries. Feel free to contact me at metallicas_world@hotmail.com Cheers /MetalManFor a printable copy of this article, please click HERE.
This site is Copyright© 2000-2004, BlitzCoder. All rights reserved.
|