Mega Code Archive

 
Categories / Delphi / Examples
 

Howtouseauserdefinedresource

Cursors in resources See 'DIY Example by By Siamak Shams below' ***************************************************** As discussed in the previous article Screen.Cursors[] is an array of cursors supplied by Delphi. By using resource files, we can add custom cursor to the Cursors property. Unless we wish to replace any of the defaults, the best strategy is to use cursor numbers starting from 1. procedure TfrMain.btnUseCursorClick(Sender: TObject); const NewCursor = 1; begin Screen.Cursors[NewCursor] := LoadCursor(hInstance,'CURHAND'); Image1.Cursor := NewCursor; end; ********************************************************** Borland Technical Article: How to use a user defined resource. - by Borland Developer Support Staff Technical Information Database TI1537D.txt How to use a user defined resource. Category :General Programming Platform :All Product :Delphi All Description: (* The following demonstrates linking a user defined resource. Resources are compiled into a ".res" file that is attached to your application's .exe file at build time. Even after you distribute your application, the resources contained in your application's .exe file can be edited with a resource editor. The resource editor in Borland's Resource Workshop, that ships with the RAD pack, can produce and edit both 16 and 32 bit resources that are self contained, standalone, or embedded in a .exe or .dll in full WYSIWYG fashion. It's worth noting that all versions of Delphi ship with the Borland Resource Command Line Compiler (BRCC.EXE and BRCC32.EXE), and can be found in Delphi's Bin directory. We first must create a text file containing our resource definitions in the application's build directory. You may name the file anything you wish, so long as it has the file extension ".rc" and the filename without the extension is not the same as any unit or project filename. This is very important, as Delphi also will create a number of resource files for your project automatically. Here is the contents of the .rc file for our example: MYUSERDATA MYDATATYPE TEST.TXT MYUSERDATA is the resource name, MYDATATYPE is the resource type and TEST.TXT is the name of the file continuing the user data we will link in. We must next create the TEST.TXT file, using any ASCII editor (notepad will do fine). For our example, the file needs to contain one single line: Hello! To compile the .rc file to a .res file that can be linked with your application, simply type on the dos command line the full path to the resource compiler, and the full path to the name of the .rc file to compile. Here is an example: c:\Delphi\Bin\brcc32.exe c:\Delphi\MYRES.RC When the compiler is finished, you should have a new file with the same name as the .rc file you've compiled, only with an extension of ".res". You can link the resource file with your application simply by adding the following statement to your application's code, substituting the name of your resource file: {$R ResFileName.RES} where ResFileName.RES is the actual name of the compiled .res file. Once the .res file is linked to your program, you can load the resource from any module, even if you specified the $R directive in a different unit. To actually use the resource, you must make a few Windows API calls. First you will call the FindResource() function, passing the instance handle of your application, the name of the resource to load, and the resource type. If FindResource is successful, the function will return a handle to the unloaded resource. Next, you can call the SizeOfResource() function to find the aligned size of the resource, if you want to know the actual size of the loaded resources memory block. Be forewarned that this not the actual size of the data, but rather the size of the memory block that will be allocated when you load the resource. If you want to know the actual size of the data, you will need to embed that information in the data itself. To load the resource, we will call the LoadResource() function, passing the handle returned to us when we called FindResource(). This function will return a handle to the loaded resource that we can pass to the LockResource() function to retrieve a pointer to the resource that we use to actually work with the data. In our example, we will loop through the data, adding the data to a string, until we find the embedded '!' character, signaling that the end of the data has been found. Finally, we will free the resource by first calling UnLockResource() and then FreeResource(), passing the handle of the loaded resource. Example Code: *) implementation {$R *.DFM} {$R MYRES.RES} procedure TForm1.Button1Click(Sender: TObject); var hRes : THandle; {handle to the resource} pRes : pointer; {pointer to the resource} ResSize : longint; {aligned size of the resource} i : integer; {counting variable} {$IFDEF WIN32} s : shortstring; {a string to play with} {$ELSE} s : string; {a string to play with} {$ENDIF} begin {find the resource} hRes := FindResource(hInstance, 'MYUSERDATA', 'MYDATATYPE'); if hRes = 0 then begin ShowMessage('Could not find the resource'); exit; end; {get the aligned size of the resource} ResSize := SizeOfResource(hinstance, hRes); if ResSize = 0 then begin ShowMessage('Nothing to load - size = 0'); Exit; end; {load the resource} hRes := LoadResource(hInstance, hRes); if hRes = 0 then begin ShowMessage('Resource Load Failure'); Exit; end; {Get a pointer to the resource} pRes := LockResource(hRes); if pRes = nil then begin ShowMessage('Resource Lock Failure'); FreeResource(hRes); Exit; end; {convert the resource pointer to a string} s:=''; i := 0; while pChar(pRes)[i] <> '!' do begin s := s + pChar(pRes)[i]; inc(i); end; {prove it works} ShowMessage(s); {unlock and free the resource} UnLockResource(hRes); FreeResource(hRes); end; end. ************************************************ DIY Example by By Siamak Shams Source Code for Custom Cursor ==================PROJECT1.PAS========================= program Project1; uses Forms, Unit1 in 'UNIT1.PAS' {Form1}; {$R *.RES} {$R MYCUR.RES} begin Application.CreateForm(TForm1, Form1); Application.Run; end. ===================UNIT1.PAS=========================== unit Unit1; interface uses SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls, Forms, Dialogs; type TForm1 = class(TForm) procedure FormCreate(Sender: TObject); procedure FormClose(Sender: TObject; var Action: TCloseAction); private { Private declarations } public { Public declarations } end; var Form1 : TForm1; OldCursor : HCursor; implementation {$R *.DFM} procedure TForm1.FormCreate(Sender: TObject); var NewCursor : HCursor; begin Form1.Show; OldCursor := GetClassWord(Form1.Handle, GCW_HCURSOR); if OldCursor = 0 then MessageDlg('Could not get the old cursor', mtError,[mbOK], 0) else begin NewCursor := LoadCursor(HInstance, 'CURSOR_1'); if SetClassWord(Form1.Handle, GCW_HCURSOR, NewCursor) = 0 then MessageDlg('Could not change to new cursor', mtError,[mbOK], 0); end; end; procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction); begin if OldCursor <> 0 then if SetClassWord(Form1.Handle, GCW_HCURSOR, OldCursor) = 0 then MessageDlg('Could not restore cursor', mtError,[mbOK], 0); end; end.