All about Delphi

Tuesday, January 15, 2008

Moving Delphi Form with WMNCHitTest

Now I'd like to share about how to move your Delphi Form without Caption Form which is at the top form as usually.

I use Delphi 7 to test this function, I use Messages Unit and your form doesn;t conaint messages then added at uses. for complete code here :

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, ExtCtrls, StdCtrls, Buttons;

type
  TformMenu = class(TForm)
    Image1: TImage;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    BitBtn1: TBitBtn;
    procedure BitBtn1Click(Sender: TObject);
    //tambahan type
    procedure WMNCHitTest(var msg : TWMNCHitTest); message WM_NCHitTest;
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  formMenu: TformMenu;

implementation

{$R *.dfm}

procedure TformMenu.WMNCHitTest(var msg : TWMNCHitTest);
begin
inherited;
if (msg.Result = htClient) then
if Msg.YPos < Top + 287 then
msg.Result := htCaption;
end;

procedure TformMenu.BitBtn1Click(Sender: TObject);
begin
Application.Terminate;
end;

end.

Enjoy this function.

Sunday, March 11, 2007

Excel in Delphi OLE

The purpose of this document is only to present a way simple to control Excel, while remaining independent of the components of Delphi. The code was written and tested with Delphi 7 and Excel 2000 or later.
The comments with the code are minimal, this not being an explanation of the operation of Excel, but simply the interface with Delphi and the code being, I think, clarify by itself.
The use of Variable is nearly general. For recall, the Variant type is a type of variable which with the characteristic to be able not to be given at the compile time, and to even change in the course of execution of the program. Its greater interest, with my direction, is that it can also contain an interface towards an object OLE and give access to the properties, procedures and functions of this one.
Here is Step by Step to Connect Excel in Delphi :
1. Start Excel with Delphi
Before being able to control Excel, it is necessary to add ComObj to the Uses clauses.
open a new authority

VAr vMSExcel: varying; begin vMSExcel: = CreateOleObject (“Excel.Application”); vMSExcel.Visible: = true;
end;
Connect itself to an existing authority

VAr vMSExcel: varying; begin vMSExcel: = GetActiveOleObject (“Excel.Application”); vMSExcel.Visible: = true;
end;
try vMSExcel: = GetActiveOleObject (“Excel.Application”); except vMSExcel: = CreateOleObject (“Excel.Application”); end;
Close Excel

VAr vMXEcel: varying; begin vMSExcel.Quit; vMSExcel: = unassigned; end;
Create a new sorter

VAr vXLWorkbook, vXLWorkbooks: varying; begin vXLWorkbooks: = vMSExcel.Workbooks; vXLWorkbook: = vXLWorkbooks.Add; end;


Excel holds up to date a list of all the opened sorters, in a collection called WorkBooks.
To open a new sorter simply amounts adding a new element to this collection. The Add function returns Variable referring to the new sorter.

Open Existing simple sorter

VAr vXLWorkbooks, vXLWorkbook: varying; aFileName: AnsiString; begin aFileName: = “c:\Home\Sources\Delphi\classeur1.xls”; vXLWorkbooks: = vMSExcel.Workbooks; vXLWorkbook: = vXLWorkbooks.Open (vFileName); end;
Open a sorter in reading alone

VAr vXLWorkbooks, vXLWorkbook, vReadOnly, vLink: varying; aFileName: AnsiString; begin aFileName: = “c:\Home\Sources\Delphi\classeur1.xls”; vLink: = unassigned; vReadOnly: = true; vXLWorkbooks: = vMSExcel.Workbooks; vXLWorkbook: = vXLWorkbooks.Open (vFileName, vLink, vReadOnly); end;
To open a sorter in reading alone is done while passing the 3rd parameter of the Open function with a value true.
The Open function has a series of parameters whose exhaustive list is in the assistance of VBA for Excel.
It should be noted that the optional parameters must have passed like Variable Unassigned, and not from the Nile, as one could think it of the first access.

Open a file csv

VAr aFileName: AnsiString; vXLWorkbooks, vXLWorkbook, vUnused, vSeparator: varying; begin aFileName: = “c:\Home\Sources\Delphi\Excel\text.csv”; vUnused: = unassigned; vSeparator: = 4; vXLWorkbooks: = vMSExcel.Workbooks; vXLWorkbook: = vXLWorkbooks.Open (aFileName, vUnused, vUnused, vSeparator); end;
vSeparator specifies the character used as separator of fields in the textual file to import.
To know the various possible values, to see the documentation of VBA of Excel.

Record the sorter under the same name

VAr vXLWorkbook: varying; begin vXLWorkbook.Save; end;
Record the sorter under another name

VAr aFileName: AnsiString; vXLWorkbook: varying; begin aFileName: = “c:\Home\Sources\Delphi\Excel\SaveAs.xls”; vXLWorkbook.SaveAs (aFileName); end;

Close a sorter

VAr vSaveChanges: varying; aFileName: AnsiString; begin vSaveChanges: = true; aFileName: = “c:\Home\Sources\Delphi\Excel\SaveAs.xls”; vXLWorkbook.Close (vSaveChanges, aFileName); end;
The 1st parameter of the Close function makes it possible to specify if the modifications are recorded.

  • true: the modifications are unregistered,
  • false: the modifications are not unregistered,
  • Unassigned: ask the user.

The second parameter of the Close function makes it possible to specify the name under which the sorter will be unregistered.
These 2 parameters are optional.

Reach a worksheet

VAr aSheetName: AnsiString; vWorksheet: varying; begin aSheetName: = “Feuil2”; vWorksheet: = vXLWorkbook.WorkSheets [aSheetName]; end;
Reach a cell

VAr aRange: AnsiString; vCell: varying; begin aRange: = “D3”; vCell: = vWorksheet.Range [aRange]; end;
See the value of a cell

VAr aValue, aRange: AnsiString; vCell: varying; begin aRange: = “D3”; vCell: = vWorksheet.Range [aRange]; aValue: = vCell.Value; end;

Modify the value of a cell

VAr aRange, aValue: AnsiString; vCell: varying; begin aRange: = “D3”; vCell: = vWorksheet.Range [aRange]; aValue: = “new”; vCell.Value: = aValue; end;
To update a beach of cells can be made same way, simply by specifying a selection in the place of a cell alone

vRange: = “D3”;
becomes
vRange: = “C3: E5”;

Monday, October 17, 2005

Hiding an Application from Windows

How do I keep an application from displaying a button on the Windows task bar and prevent users from using [Alt-Tab] to switch to it once it executes?

By convention, any application’s window that has its extended window style bit set as a tool window (WS_EX_TOOLWINDOW) will be hidden from the task bar and users will not be able to switch to it. In Delphi, every form has a BorderStyle property of bsToolWindow, so you might think that all you have to do is set the borderstyle of the main form of the application to bsToolWindow and you’ll be able to hide the application from the task bar. Not really. The reason for this is because the main form of the application really isn’t the main form of the application.
The application object that encapsulates all Delphi programs (TApplication) actually has a form of its own. It’s totally transparent, so you can’t really see it. This is the window we need to interact with in order to hide the application. So how do we do it? Well, if you remember what I said at the top of this tip, the Windows convention for “hiding” an application is to set its extended window style bit to WS_EX_TOOLWIN. You can easily do this with a little Windows API trickery. Yes, that’s right, trickery. We trick the application into displaying as a tool window instead of an application window.

Specifically, in order to accomplish this task you have to edit the project’s source code. Here’s a complete listing:
program Project1;

uses
Forms,
Unit1 in ‘Unit1.pas’ {Form1},
Windows;

{$R *.RES}

//Declare a var to retrieve current window information
var
ExtendedStyle : Integer;

begin
Application.Initialize;

//Get the Extended Styles of the Application, by passing its
//handle to GetWindowLong
ExtendedStyle := GetWindowLong(Application.Handle, GWL_EXSTYLE);

//Now, set the Extended Style by doing a bit masking operation.
//OR in the WS_EX_TOOLWINDOW bit, and AND out the WS_EXAPPWINDOW bit
//This effectively converts the application from an App Windows to a
//Tool Window.
SetWindowLong(Application.Handle, GWL_EXSTYLE, ExtendedStyle OR WS_EX_TOOLWINDOW
AND NOT WS_EX_APPWINDOW);
Application.CreateForm(TForm1, Form1);
Application.Run;
end.

Notice that I’ve treated the application object like a window. It’s legal to do that because the application object has its own window—it’s just hidden. So by using a couple of functions to retrieve and then set the window display properties, we can achieve the desired effect. The first function, GetWindowLong, is a Windows API function that takes an application handle and a constant representing a bit offset stored in Windows that describes a particular window. For our purposes, we pass the GWL_EXSTYLE offset value to retrieve the extended window style information. Once we’ve got that, it’s a simple matter of making a call to SetWindowLong to make the change to the application. SetWindowLong takes the application’s handle, the information offset, and what you want to change at that offset. Once called, it changes the window appropriately. The net effect is that no button is displayed for the application on the task bar and you won’t be able to [Alt-Tab] to the application. It’s really hidden.
You should probably study the online documentation regarding the Get- and SetWindowLong Windows API functions to better familiarize yourself with the functions. You’ll see that you can do a lot more with these functions than what I’ve just brushed over here.
Getting Some Use Out of All This…
When I originally started writing this article, I was going to leave it at hiding an application from Windows. But then I thought to myself, “What use is it?” The most obvious use of the technique is for creating system tray applications; that is, applications that add an icon to the system tray and are invoked from there as opposed to the task bar. For example, the Audio Control (the little loudspeaker at the bottom right of your screen) is a system tray application. So how do you create a system tray application? Easy. Just look at the code below:
{This sets up the application to be a system tray application.
This is the main form for the application. It has a popup menu that will be used to display the main form, or close the application.
And using the ShellAPI unit, we can then use a couple of calls to display the application’s icon on the system tray, and make it respond to a right mouse click}
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ShellAPI, ExtCtrls, Menus;

type
TForm1 = class(TForm)
PopupMenu1: TPopupMenu;
ShowMainForm1: TMenuItem;
N1: TMenuItem;
ExitApplication1: TMenuItem;
procedure FormCreate(Sender: TObject);
procedure ShowMainForm1Click(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure ExitApplication1Click(Sender: TObject);
private
procedure WndProc(var Msg : TMessage); override;
public
IconNotifyData : TNotifyIconData;
end;

var
Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.FormCreate(Sender: TObject);
begin
//Set the border icons to have only a system menu. This will
//leave just the close button.
BorderIcons := [biSystemMenu];
//Now set up the IconNotifyData structure so that it receives
//the window messages sent to the application and displays
//the application’s tips
with IconNotifyData do begin
hIcon := Application.Icon.Handle;
uCallbackMessage := WM_USER + 1;
cbSize := sizeof(IconNotifyData);
Wnd := Handle;
uID := 100;
uFlags := NIF_MESSAGE + NIF_ICON + NIF_TIP;
end;
//Copy the Application’s Title into the tip for the icon
StrPCopy(IconNotifyData.szTip, Application.Title);
//Add the Icon to the system tray and use the
//the structure and its values
Shell_NotifyIcon(NIM_ADD, @IconNotifyData);
end;

procedure TForm1.WndProc(var Msg : TMessage);
var
p : TPoint;
begin
case Msg.Msg of
WM_USER + 1:
case Msg.lParam of
WM_RBUTTONDOWN: begin
GetCursorPos(p);
PopupMenu1.Popup(p.x, p.y);
end;
end;
end;
inherited;
end;

procedure TForm1.ShowMainForm1Click(Sender: TObject);
begin
Form1.Show;
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
Action := caNone;
Form1.Hide;
end;

procedure TForm1.ExitApplication1Click(Sender: TObject);
begin
Shell_NotifyIcon(NIM_DELETE, @IconNotifyData);
Application.ProcessMessages;
Application.Terminate;
end;

end.

There’s really not much to this. Just study the code to see what’s going on. But the important thing you should concentrate on is the Create method of the form and what is done to the IconNotifyData structure. This is a record structure declared in the ShellAPI unit that stores information for a tray icon. Notice the flags that were used: NIF_MESSAGE + NIF_ICON + NIF_TIP. These flags tell the icon to process application messages—use the application’s icon and its tip, respectively. Once we’ve set up all that, then it’s a simple matter of creating the window interaction stuff, like we’d normally do at design time. The tray icon doesn’t come into play until run time.
The other thing to look at is the override of the WndProc procedure. WndProc is short for Window Procedure. It intercepts all the messages sent to the window, and acts as the central message dispatcher. In that procedure, you can trap specific Windows messages by overriding the inherited procedure. In our case, we trap two things: the Msg field of the message sent to the application. If it was our custom message (WM_USER + 1) defined for the IconNotifyData variable, then we want to handle a right-click. All other messages sent to the application are handled in their normal fashion.

I realize that this was pretty quick and dirty, so I encourage you to play around with the code. Just keep in mind that you have to do two things, if you’re going to create system tray application:
1. You need to first create the “hiding” mechanism for the application.
2. Then, you need to create the interface so that you can interact with the application when its main form isn’t being displayed.

Have fun!

Wednesday, October 05, 2005

Welcome to my Delphi Blog

This is another blog i have.....
I try to collect many information and it's All about Delphi, because i do like delphi for programming but still not being an expert.
Well let's have some fun with it .. no problemo if you're expert or not, the important thing is trial and error and keep trying.

About Me

My Photo
Andi Eko Nurhadiyanto
I'm an Indonesian blogger and want to share about the latest information about this site.
View my complete profile