ListBox -> sendmessage avec message : LB_INSERTSTRING

coopertel Messages postés 12 Date d'inscription jeudi 29 juillet 2004 Statut Membre Dernière intervention 8 février 2006 - 27 sept. 2004 à 11:36
coopertel Messages postés 12 Date d'inscription jeudi 29 juillet 2004 Statut Membre Dernière intervention 8 février 2006 - 27 sept. 2004 à 16:24
Bonjour,

J'ai mis dans les pièces joints le source de mon programme.

En fait je voudrais tout simplement utiliser la fct sendmessage
avec le message LB_INSERTSTRING (pour apprendre à utiliser l'api).

J'ai déjà trouver comme faire bouger (ou plutôt suivre) un ascenceure d'une ListBox par un ascenceure d'une autre listbox.

Maintenant je voudrais ajouter des chaînes à mes listes (en passant par l'api uniquement).

mais voilà le noeud de mon porblème, la ligne de code suivante ne fonctionne pas :

SendMessage(Form1.List_Box_1.Handle
, LB_INSERTSTRING
, -1
, P);

avec P étant un pointeur sur une chaîne à zéro terminal...

Merci de votre aide.

David

PS : je n'ai pas trouvé le moyen de mettre mon source en pièces jointes donc le voici :

le dpr ne fait rien de particulier et le dfm de l'unité 1 contient un bouton et une editbox. c'est tout.
les listBox sont créés dynamiquement.

******
UNITE 1
******
unit Unit1;

interface

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

type
TList_Box = class(TListBox)
private
{ Déclarations privées }
protected
procedure WndProc(var Message: TMessage); override;
public
{ Déclarations publiques }
constructor Create (AOwner : TComponent); override;
destructor Destroy;override;
end;
type
TForm1 = class(TForm)
Button1: TButton;
Edit1: TEdit;
Button2: TButton;
Edit2: TEdit;
procedure FormCreate(Sender: TObject);
procedure List_Box_1DblClick(Sender : TObject);
procedure List_Box_2DblClick(Sender : TObject);
procedure Button1Click(Sender: TObject);
private
{ Déclarations privées }
public
{ Déclarations publiques }
List_Box_1 : TList_Box;
List_Box_2 : TList_Box;
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
var
I : integer;
begin
List_Box_1:=TList_Box.Create(Self);
with List_Box_1 do
begin
Parent:=Form1;
Setbounds(16, 120, 160, 145);
OnDblClick:=List_Box_1DblClick;
for I:=1 to 100 do
AddItem(inttostr(I), nil);
end;

List_Box_2:=TList_Box.Create(Self);
with List_Box_2 do
begin
Parent:=Form1;
Setbounds(192, 120, 160, 145);
OnDblClick:=List_Box_2DblClick;
for I:=1 to 100 do
AddItem(inttostr(I), nil);
end;
end;

procedure TForm1.List_Box_1DblClick(Sender : TObject);
begin
showmessage('List_Box_1DblClick');
end;

procedure TForm1.List_Box_2DblClick(Sender : TObject);
begin
showmessage('List_Box_2DblClick');
end;

{ TList_Box }

constructor TList_Box.Create(AOwner: TComponent);
begin
inherited;
//
end;

destructor TList_Box.Destroy;
begin
inherited;
//
end;

var
Verrou : boolean;

procedure TList_Box.WndProc(var Message: TMessage);
var
Num_Index : Integer;
begin
if not Verrou then
try
Verrou:=true;
try
if Handle=Form1.List_Box_2.Handle then
begin
Num_Index:=SendMessage(Handle, LB_GETTOPINDEX, 0, 0);
SendMessage(Form1.List_Box_1.Handle, LB_SETTOPINDEX, Num_Index, 0);
if Form1.List_Box_1.ItemIndex<>ItemIndex then
Form1.List_Box_1.ItemIndex:=ItemIndex;
end
else
begin
Num_Index:=SendMessage(Handle, LB_GETTOPINDEX, 0, 0);
SendMessage(Form1.List_Box_2.Handle, LB_SETTOPINDEX, Num_Index, 0);
if Form1.List_Box_2.ItemIndex<>ItemIndex then
Form1.List_Box_2.ItemIndex:=ItemIndex;
end;
except
end;
finally
Verrou:=false;
end;
try
inherited WndProc(Message);
except
end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
TChaine : array[0..255] of Char;
I : Cardinal;
Chaine_avec_zero_terminal : PChar;
P : ^PChar;
begin
try
// LB_INSERTSTRING

for I:=0 to length(Edit1.Text) do
TChaine[I]:=Copy(Edit1.Text, I, 1)[1];

Chaine_avec_zero_terminal:=TChaine;

P:=@Chaine_avec_zero_terminal[0];

// I:=P;

SendMessage(Form1.List_Box_1.Handle, LB_INSERTSTRING, -1, P);
except
end
end;

end.

1 réponse

coopertel Messages postés 12 Date d'inscription jeudi 29 juillet 2004 Statut Membre Dernière intervention 8 février 2006
27 sept. 2004 à 16:24
SendMessage(Form1.List_Box_1.Handle
, LB_INSERTSTRING
, -1
, Cardinal(P)); ca passe.

sinon pour simplier on peut faire directement :

SendMessage(Form1.List_Box_1.Handle
, LB_INSERTSTRING
, round(List_Box_1.Count/2)
, Cardinal(@PChar(Edit2.Text)[0]));

PChar(Edit2.Text) convertie la chaîne en chaîne à zéro terminal.
@PChar(Edit2.Text)[0]

@ (l'adresse de la chaîne)
[0] 1er caractères de la chaîne.
0
Rejoignez-nous