请教!!!(50分)

  • 主题发起人 主题发起人 oneyhabc
  • 开始时间 开始时间
O

oneyhabc

Unregistered / Unconfirmed
GUEST, unregistred user!
以下是我照着书本编写的一个小程序(中间有些地方做了一些改动),编译时老出错,请大家帮帮我:)
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Menus, ExtCtrls, ComCtrls, ToolWin, ImgList, NMUDP,
jpeg;

type
TForm1 = class(TForm)
ToolBar1: TToolBar;
GroupBox1: TGroupBox;
StatusBar1: TStatusBar;
Panel1: TPanel;
MainMenu1: TMainMenu;
N1: TMenuItem;
N2: TMenuItem;
N3: TMenuItem;
N11: TMenuItem;
Edit1: TEdit;
Edit2: TEdit;
ComboBox1: TComboBox;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Label4: TLabel;
Label5: TLabel;
Label6: TLabel;
Label7: TLabel;
Label8: TLabel;
Label9: TLabel;
Label10: TLabel;
Label11: TLabel;
ImageList1: TImageList;
NMUDP1: TNMUDP;
Image1: TImage;
Image2: TImage;
Panel2: TPanel;
ListView1: TListView;
Memo1: TMemo;
N10: TMenuItem;
N4: TMenuItem;
N5: TMenuItem;
N9: TMenuItem;
N6: TMenuItem;
N7: TMenuItem;
N8: TMenuItem;
N12: TMenuItem;
N13: TMenuItem;
N14: TMenuItem;
SendBtn: TButton;
ClearBtn: TButton;
PortBtn: TButton;
NameBtn: TButton;
KickBtn: TButton;
AboutBtn: TButton;
QuitBtn: TButton;
procedure FormCreate(Sender: TObject);
procedure ComboBox1KeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
procedure ComboBox1Select(Sender: TObject);
procedure ListView1SelectItem(Sender: TObject; Item: TListItem;
Selected: Boolean);
procedure SendBtnClick(Sender: TObject);
procedure ClearBtnClick(Sender: TObject);
procedure PortBtnClick(Sender: TObject);
procedure NameBtnClick(Sender: TObject);
procedure KickBtnClick(Sender: TObject);
procedure QuitBtnClick(Sender: TObject);
procedure N5Click(Sender: TObject);
procedure N9Click(Sender: TObject);
procedure N10Click(Sender: TObject);
procedure N6Click(Sender: TObject);
procedure N7Click(Sender: TObject);
procedure N8Click(Sender: TObject);
procedure N12Click(Sender: TObject);
procedure N13Click(Sender: TObject);
procedure N14Click(Sender: TObject);
procedure NMUDP1DataReceived(Sender: TComponent; NumberBytes: Integer;
FromIP: String; Port: Integer);
procedure NMUDP1DataSend(Sender: TObject);
procedure NMUDP1InvalidHost(var handled: Boolean);
procedure NMUDP1Status(Sender: TComponent; status: String);
procedure AboutBtnClick(Sender: TObject);
procedure N4Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

uses Unit2;

{$R *.dfm}

function GetMaskString(S,Mask:string;Position:integer):string;
var
Str:string;
i,Len:integer;
begin
Str:='';
for i:=0 to Position -1 do
begin
if (Pos(Mask,S)<=0) then
begin
Str:=S;
Break;
end;
Str:=Copy(S,1,Pos(Mask,S)-1);
Len:=Length(Str);
S:=Copy(S,Len+2,Length(s)-Len-1);
end;
Result:=Str;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
NMUDP1.LocalPort:=6768;
NMUDP1.RemotePort:=6768;
end;

procedure TForm1.ComboBox1KeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
var
MyStream:TMemoryStream;
TmpStr:String;
exist:bool;
ii:Integer;
begin
exist:=false;
if key=vk_return then begin
NMUDP1.ReportLevel:=Status_Basic;
NMUDP1.RemoteHost:=Edit1.Text;
NMUDP1.RemotePort:=StrToInt(Edit2.Text);
for ii:=0 to ComboBox1.Items.Count-1 do begin
if ComboBox1.Text=ComboBox1.Items[ii] then Exist:=true;
end;
if exist=false then
ComboBox1.Items.Add(ComboBox1.Text);
TmpStr:=Label7.Caption+'^'+Label1.Caption+'^'+
ComboBox1.Text+'^'+IntToStr(Image1.Tag)+'^'+Label11.Hint;
MyStream:=TMemoryStream.Create;
try
MyStream.Write(TmpStr[1],Length(TmpStr));
NMUDP1.SendStream(MyStream);
finally
MyStream.Free;
end;
end;
end;

procedure TForm1.ComboBox1Select(Sender: TObject);
var
MyStream:TMemoryStream;
TmpStr:String;
exist:bool;
ii:Integer;
begin
exist:=false;
NMUDP1.ReportLevel:=Status_Basic;
NMUDP1.RemoteHost:=Edit1.Text;
NMUDP1.RemotePort:=StrToInt(Edit2.Text);
for ii:=0 to ComboBox1.Items.Count-1 do begin
if ComboBox1.Text=ComboBox1.Items[ii] then Exist:=true;
end;
if exist=false then
ComboBox1.Items.Add(ComboBox1.Text);
TmpStr:=Label7.Caption+'^'+Label1.Caption+'^'+
ComboBox1.Text+'^'+IntToStr(Image1.Tag)+'^'+Label11.Hint;
MyStream:=TMemoryStream.Create;
try
MyStream.Write(TmpStr[1],Length(TmpStr));
NMUDP1.SendStream(MyStream);
finally
MyStream.Free;
end;
end;

procedure TForm1.ListView1SelectItem(Sender: TObject; Item: TListItem;
Selected: Boolean);
begin
if Listview1.ItemIndex<>-1 then begin
Edit2.Text:=Listview1.Items[Listview1.ItemIndex].Caption;
Edit1.Text:=Listview1.Items[Listview1.ItemIndex].SubItems[1];
end;
end;

procedure TForm1.SendBtnClick(Sender: TObject);
var
MyStream:TMemoryStream;
TmpStr:String;
exist:bool;
ii:Integer;
begin
Exist:=false;
NMUDP1.ReportLevel:=Status_Basic;
NMUDP1.RemoteHost:=Edit1.Text;
NMUDP1.RemotePort:=StrToInt(Edit2.Text);
for ii:=0 to ComboBox1.Items.Count-1 do begin
if ComboBox1.Text=ComboBox1.Items[ii] then Exist:=false;
end;
if exist=false then
ComboBox1.Items.Add(ComboBox1.Text);
TmpStr:=Label7.Caption+'^'+Label1.Caption+'^'+
ComboBox1.Text+'^'+IntToStr(Image1.Tag)+'^'+Label11.Hint;
MyStream:=TMemoryStream.Create;
try
MyStream.Write(TmpStr[1],Length(TmpStr));
NMUDP1.SendStream(MyStream);
finally
MyStream.Free;
end;
end;

procedure TForm1.ClearBtnClick(Sender: TObject);
begin
Memo1.Clear;
end;

procedure TForm1.PortBtnClick(Sender: TObject);
var
tmpstr:string;
portn:integer;
begin
tmpstr:=InputBox('端口输入','请输入本地工作端口:','');
try
portn:=strToInt(tmpstr);
Nmudp1.LocalPort:=portn;
Label7.Caption:=tmpstr;
except
ShowMessage('请输入数字!');
end;
end;

procedure TForm1.NameBtnClick(Sender: TObject);
var
tmpstr:string;
begin
tmpstr:=InputBox('端口输入',
'请输入本地工作端口:','');
if tmpstr<>'' then
Label1.Caption:=tmpstr;
end;

procedure TForm1.KickBtnClick(Sender: TObject);
begin
if Listview1.ItemIndex<>-1 then
begin
listview1.Items.Delete(Listview1.ItemIndex);
end;
end;

procedure TForm1.QuitBtnClick(Sender: TObject);
begin
Close;
end;

procedure TForm1.N5Click(Sender: TObject);
begin
Image1.Picture.Assign(nil);
Image1.Tag:=2;
Imagelist1.GetBitmap(Image1.tag,Image1.Picture.Bitmap);
end;

procedure TForm1.N9Click(Sender: TObject);
begin
Image1.Tag:=0;
Image1.Picture.Assign(nil);
end;

procedure TForm1.N10Click(Sender: TObject);
begin
Close;
end;

procedure TForm1.N6Click(Sender: TObject);
begin
ComboBox1.Text:=copy(TMenuItem(Sender).Caption,0,
length(TMenuItem(Sender).Caption)-4);
SendBtnClick(Sender);
end;

procedure TForm1.N7Click(Sender: TObject);
begin
ComboBox1.Text:=copy(TMenuItem(Sender).Caption,0,
length(TMenuItem(Sender).Caption)-4);
SendBtnClick(Sender);
end;

procedure TForm1.N8Click(Sender: TObject);
begin
ComboBox1.Text:=copy(TMenuItem(Sender).Caption,0,
length(TMenuItem(Sender).Caption)-4);
SendBtnClick(Sender);
end;

procedure TForm1.N12Click(Sender: TObject);
begin
Label11.Caption:=copy(TMenuItem(Sender).Caption,0,
length(TMenuItem(Sender).Caption)-4);
Label11.Hint:='oneyh-abc@163.com';
end;

procedure TForm1.N13Click(Sender: TObject);
begin
Label11.Caption:=copy(TMenuItem(Sender).Caption,0,
length(TMenuItem(Sender).Caption)-4);
Label11.Hint:='explorer.exe';
end;

procedure TForm1.N14Click(Sender: TObject);
begin
Label11.Caption:='无';
Label11.Hint:='无';
end;

procedure TForm1.NMUDP1DataReceived(Sender: TComponent;
NumberBytes: Integer; FromIP: String; Port: Integer);
var
MyStream:TMemoryStream;
TmpStr,command:String;
ii:integer;
exist:bool;
ListItem:TListItem;
Imgsn:Integer;
begin
exist:=false;
MyStream:=TMemoryStream.Create;
try
NMUDP1.ReadStream(MyStream);
SetLength(TmpStr,NumberBytes);
MyStream.Read(TmpStr[1],NumberBytes);
Memo1.Lines.Add(DateToStr(Date)+''+TimeToStr(time)+'==>>'+
getmaskstring(tmpstr,'^',2)+'说:'+
getmaskstring(tmpstr,'^',3));
try
Imgsn:=StrToInt(getmaskstring(tmpstr,'^',4));
if Imgsn>0 then begin
Image2.Tag:=Imgsn;
Image2.Picture.Assign(nil);
Imagelist1.GetBitmap(Imgsn,Image2.Picture.Bitmap);
end else Image2.Picture.Assign(nil);
except
end;

command:=getmaskstring(tmpstr,'^',5);
if command<>'无' Then
ShellExecute(Handle,NiL,pchar(command),NiL,NiL,SW_SHOWNORMAL);

for ii:=0 to ListView1.Items.Count-1 do begin
if getmaskstring(tmpstr,'^',1)=listView1.Items[ii].Caption then
begin
exist:=true;
if getmaskstring(tmpstr,'^',1)=listView1.Items[ii].SubItems[0]
then begin
Memo1.Lines.Add(DateToStr(Date)+''+TimeToStr(time)+'==>>'+
listView1.Items[ii].SubItems[0]+'更名为'+
getmaskstring(tmpstr,'^',2));
listView1.Items[ii].SubItems[0]:=getmaskstring(tmpstr,'^',2);
end;
end;
end;
if exist=false then begin
ListItem:=ListView1.Items.Add;
ListItem.Caption:=IntToStr(Port);
ListItem.SubItems.Add(getmaskstring(tmpstr,'^'2));
ListItem.SubItems.Add(FromIp);
ListItem.SubItems.Add('nothing');
end;
finally
MyStream.Free;
end;
end;

procedure TForm1.NMUDP1DataSend(Sender: TObject);
begin
StatusBar1.SimpleText:='数据发送完毕';
end;

procedure TForm1.NMUDP1InvalidHost(var handled: Boolean);
var
TmpStr:String;
begin
If InputQuery('不正确的主机设置','请重新指定主机:',TmpStr) then
Begin
NMUDP1.RemoteHost:=TmpStr;
Handled:=TRUE;
end;
end;

procedure TForm1.NMUDP1Status(Sender: TComponent; status: String);
begin
If StatusBar1<>nil then
StatusBar1.SimpleText:=status;
end;

procedure TForm1.AboutBtnClick(Sender: TObject);
begin
AboutBox.showmodal;
end;

procedure TForm1.N4Click(Sender: TObject);
begin
Image1.Picture.Assign(nil);
Image1.Tag:=1;
Imagelist1.GetBitmap(Image1.Tag,Image1.Picture.Bitmap);
end;
end.
 
楼主,怎么这么多呀,多也不怕,你把出错的地方标记一下,也方便大家帮你纠错呀,你这样太长了
 
不好意思,想急于知道答案所以遗忘了:)
procedure TForm1.NMUDP1DataReceived(Sender: TComponent;
NumberBytes: Integer; FromIP: String; Port: Integer);
var
MyStream:TMemoryStream;
TmpStr,command:String;
ii:integer;
exist:bool;
ListItem:TListItem;
Imgsn:Integer;
begin
exist:=false;
MyStream:=TMemoryStream.Create;
try
NMUDP1.ReadStream(MyStream);
SetLength(TmpStr,NumberBytes);
MyStream.Read(TmpStr[1],NumberBytes);
Memo1.Lines.Add(DateToStr(Date)+''+TimeToStr(time)+'==>>'+
getmaskstring(tmpstr,'^',2)+'说:'+
getmaskstring(tmpstr,'^',3));
try
Imgsn:=StrToInt(getmaskstring(tmpstr,'^',4));
if Imgsn>0 then begin
Image2.Tag:=Imgsn;
Image2.Picture.Assign(nil);
Imagelist1.GetBitmap(Imgsn,Image2.Picture.Bitmap);
end else Image2.Picture.Assign(nil);
except
end;

command:=getmaskstring(tmpstr,'^',5);
if command<>'无' Then
//在编绎时下面这段老出错
ShellExecute(Handle,NiL,pchar(command),NiL,NiL,SW_SHOWNORMAL);

for ii:=0 to ListView1.Items.Count-1 do begin
if getmaskstring(tmpstr,'^',1)=listView1.Items[ii].Caption then
begin
exist:=true;
if getmaskstring(tmpstr,'^',1)=listView1.Items[ii].SubItems[0]
then begin
Memo1.Lines.Add(DateToStr(Date)+''+TimeToStr(time)+'==>>'+
listView1.Items[ii].SubItems[0]+'更名为'+
getmaskstring(tmpstr,'^',2));
listView1.Items[ii].SubItems[0]:=getmaskstring(tmpstr,'^',2);
end;
end;
end;
if exist=false then begin
ListItem:=ListView1.Items.Add;
ListItem.Caption:=IntToStr(Port);
ListItem.SubItems.Add(getmaskstring(tmpstr,'^'2));
ListItem.SubItems.Add(FromIp);
ListItem.SubItems.Add('nothing');
end;
finally
MyStream.Free;
end;
end;
 
錯在哪個地方,你要指明.
 
procedure TForm1.NMUDP1DataReceived(Sender: TComponent;
NumberBytes: Integer; FromIP: String; Port: Integer);
var
MyStream:TMemoryStream;
TmpStr,command:String;
ii:integer;
exist:bool;
ListItem:TListItem;
Imgsn:Integer;
begin
exist:=false;
MyStream:=TMemoryStream.Create;
try
NMUDP1.ReadStream(MyStream);
SetLength(TmpStr,NumberBytes);
MyStream.Read(TmpStr[1],NumberBytes);
Memo1.Lines.Add(DateToStr(Date)+''+TimeToStr(time)+'==>>'+
getmaskstring(tmpstr,'^',2)+'说:'+
getmaskstring(tmpstr,'^',3));
try
Imgsn:=StrToInt(getmaskstring(tmpstr,'^',4));
if Imgsn>0 then begin
Image2.Tag:=Imgsn;
Image2.Picture.Assign(nil);
Imagelist1.GetBitmap(Imgsn,Image2.Picture.Bitmap);
end else Image2.Picture.Assign(nil);
except
end;

command:=getmaskstring(tmpstr,'^',5);
if command<>'无' Then
//在编绎时下面这一段老出错
ShellExecute(Handle,NiL,pchar(command),NiL,NiL,SW_SHOWNORMAL);

for ii:=0 to ListView1.Items.Count-1 do begin
if getmaskstring(tmpstr,'^',1)=listView1.Items[ii].Caption then
................



 
是编译通不过吗?

有没有uses ShellAPI ?

还是编译通过了提示内存或其它运行期间错误?
 
看了一眼1楼, 果然是没有uses ShellAPI
ShellExecute这个API的声明在ShellAPI.pas里
 
编绎通不过
 
说详细一点行吗?我很菜的,谢谢!!!
 
赶快在uses子句中加ShellAPI, 应该搞定这个编译不通过的问题
 
哦,如果还有问题我再联系你!
 
procedure TForm1.NMUDP1DataReceived(Sender: TComponent;
NumberBytes: Integer; FromIP: String; Port: Integer);
var
MyStream:TMemoryStream;
TmpStr,command:String;
ii:integer;
exist:bool;
ListItem:TListItem;
Imgsn:Integer;
begin
exist:=false;
MyStream:=TMemoryStream.Create;
try
NMUDP1.ReadStream(MyStream);
SetLength(TmpStr,NumberBytes);
MyStream.Read(TmpStr[1],NumberBytes);
Memo1.Lines.Add(DateToStr(Date)+''+TimeToStr(time)+'==>>'+
getmaskstring(tmpstr,'^',2)+'说:'+
getmaskstring(tmpstr,'^',3));
try
Imgsn:=StrToInt(getmaskstring(tmpstr,'^',4));
if Imgsn>0 then begin
Image2.Tag:=Imgsn;
Image2.Picture.Assign(nil);
Imagelist1.GetBitmap(Imgsn,Image2.Picture.Bitmap);
end else Image2.Picture.Assign(nil);
except
end;

command:=getmaskstring(tmpstr,'^',5);
if command<>'无' Then
ShellExecute(Handle,NiL,pchar(command),NiL,NiL,SW_SHOWNORMAL);

for ii:=0 to ListView1.Items.Count-1 do begin
if getmaskstring(tmpstr,'^',1)=listView1.Items[ii].Caption then
begin
exist:=true;
if getmaskstring(tmpstr,'^',1)=listView1.Items[ii].SubItems[0]
then begin
Memo1.Lines.Add(DateToStr(Date)+''+TimeToStr(time)+'==>>'+
listView1.Items[ii].SubItems[0]+'更名为'+
getmaskstring(tmpstr,'^',2));
listView1.Items[ii].SubItems[0]:=getmaskstring(tmpstr,'^',2);
end;
end;
end;
if exist=false then begin
ListItem:=ListView1.Items.Add;
ListItem.Caption:=IntToStr(Port);
//下面这段编绎又出错了5555555555:(
ListItem.SubItems.Add(getmaskstring(tmpstr,'^'2));
ListItem.SubItems.Add(FromIp);
ListItem.SubItems.Add('nothing');
end;
finally
MyStream.Free;
end;
end;
 
ListItem.SubItems.Add(getmaskstring(tmpstr,'^'2));
改成:
ListItem.SubItems.Add(getmaskstring(tmpstr,'^',2));
就可以了.
看看这一句前面的类似句子你就明白了
 
后退
顶部