C++ builder 开发图片查看的应用程序 - 无幻の编程 - 对于一个初学者来说,野心也是必须的...
C++ builder 开发图片查看的应用程序
软件开发基础教程(下册)
P505页的实例:创建工具栏、菜单及动作列表对象的使用
本例将创建如图所示的带有工具栏、菜单栏、弹出式菜单和其他控件的主窗体的应用程序,具有打开图形(照片)文件、显示或不显示图形、放大或缩小图形、放大或缩小图形文件名的功能,一些菜单项和工具栏上按钮的响应动作由动作列表(TActionList)对象管理。通过该实例的练习,可以帮助、理解并掌握如何在软件开发中创建工具栏、菜单(包括弹出式菜单)和组件,设置组件属性、定义组件方法及事件响应。操作步骤简易描述如下:
(1)首先准备好素材,图标图形文件等,一并放在本应用程序的同一目录;
(2)添加相应的组件,如下图:
3.相应代码如下:
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
AnsiString fileName;int colorFlag=0;static bool aClick=false;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{ Application->Icon=Form1->Icon;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FileOpen1Accept(TObject *Sender)
{
fileName=FileOpen1->Dialog->FileName;
Label1->Caption=fileName+" ";
Image1->Visible=true;
try{Image1->Picture->LoadFromFile(fileName);}
catch(Exception &e){ShowMessage(e.Message);}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::OpenPicture2Accept(TObject *Sender)
{
fileName=OpenPicture2->Dialog->FileName;
Image1->Picture->LoadFromFile(fileName);
Label1->Caption=fileName+" ";
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FontEdit1Accept(TObject *Sender)
{
Label1->Font=FontEdit1->Dialog->Font;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ColorSelect1Accept(TObject *Sender)
{
TColor color1=ColorSelect1->Dialog->Color;
switch(colorFlag)
{case 11:Label1->Color=color1;break;
case 17:Form1->Color=color1;break;
case 18:Panel1->Color=color1;break;
}
colorFlag=0;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::N6Click(TObject *Sender)
{
colorFlag=11;
ColorSelect1->Execute();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::N8Click(TObject *Sender)
{
if(N8->Checked==true)
{if(N9->Checked==false)
{Panel1->Visible=false;
N8->Checked=false;
}
else
{ShowMessage("工具栏处于可用状态。请先使工具栏不可用!");}
}
else
{Panel1->Visible=true;
N8->Checked=true;
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::N9Click(TObject *Sender)
{
if(N9->Checked==true)
{N9->Checked=false;
Panel1->Enabled=false;}
else
{if(N8->Checked==true)
{N9->Checked=true;Panel1->Enabled=true;}
else
{ShowMessage("工具栏处于不可见状态,请先使工具栏可见!");}
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::N17Click(TObject *Sender)
{
colorFlag=17;
ColorSelect1->Execute();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::N18Click(TObject *Sender)
{
colorFlag=18;
ColorSelect1->Execute();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::N12Click(TObject *Sender)
{
WindowState=wsMinimized;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::N13Click(TObject *Sender)
{
WindowState=wsMaximized;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::N14Click(TObject *Sender)
{
WindowState=wsNormal;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::N16Click(TObject *Sender)
{
Close();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Action1Execute(TObject *Sender)
{
if(Image1->Visible==false)Image1->Visible=true;
else Image1->Visible=false;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Action2Execute(TObject *Sender)
{
static int i=0,flag=0;
Label1->Font->Color=clTeal;
if(flag==0)
{if((i+=2)>=12){flag=1;i=18;}}
else
{if((i-=2)<=0) flag=0;}
Label1->Font->Size=6+i%12;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Action3Execute(TObject *Sender)
{
static int h=250,w=300,flag=0;
if(flag==0)
{if((h-=25)<26)flag=1;
Image1->Height=h;Image1->Width=(w-=30);
}
else
{if((h+=25)>225)flag=0;
Image1->Height=h;Image1->Width=(w+=30);
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
// Label1->Font->Style=TFontStyles()<<fsBold<<fsItalic<<fsUnderline;
Image1->Stretch=true;
SpeedButton1->Caption="";
SpeedButton2->Caption="";
SpeedButton3->Caption="";
SpeedButton4->Caption="";
SpeedButton5->Caption="";
SpeedButton6->Caption="";
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
Action1->Execute();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
Action2->Execute();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::RichEditBold1Execute(TObject *Sender)
{
if(!aClick)
{
Label1->Font->Style=TFontStyles()<<fsBold;
aClick=true;
}
else
{
Label1->Font->Style=TFontStyles()>>fsBold;
aClick=false;
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::RichEditItalic1Execute(TObject *Sender)
{
Label1->Font->Style=TFontStyles()<<fsItalic;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::RichEditUnderline1Execute(TObject *Sender)
{
Label1->Font->Style=TFontStyles()<<fsUnderline;
}
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
AnsiString fileName;int colorFlag=0;static bool aClick=false;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{ Application->Icon=Form1->Icon;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FileOpen1Accept(TObject *Sender)
{
fileName=FileOpen1->Dialog->FileName;
Label1->Caption=fileName+" ";
Image1->Visible=true;
try{Image1->Picture->LoadFromFile(fileName);}
catch(Exception &e){ShowMessage(e.Message);}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::OpenPicture2Accept(TObject *Sender)
{
fileName=OpenPicture2->Dialog->FileName;
Image1->Picture->LoadFromFile(fileName);
Label1->Caption=fileName+" ";
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FontEdit1Accept(TObject *Sender)
{
Label1->Font=FontEdit1->Dialog->Font;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ColorSelect1Accept(TObject *Sender)
{
TColor color1=ColorSelect1->Dialog->Color;
switch(colorFlag)
{case 11:Label1->Color=color1;break;
case 17:Form1->Color=color1;break;
case 18:Panel1->Color=color1;break;
}
colorFlag=0;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::N6Click(TObject *Sender)
{
colorFlag=11;
ColorSelect1->Execute();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::N8Click(TObject *Sender)
{
if(N8->Checked==true)
{if(N9->Checked==false)
{Panel1->Visible=false;
N8->Checked=false;
}
else
{ShowMessage("工具栏处于可用状态。请先使工具栏不可用!");}
}
else
{Panel1->Visible=true;
N8->Checked=true;
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::N9Click(TObject *Sender)
{
if(N9->Checked==true)
{N9->Checked=false;
Panel1->Enabled=false;}
else
{if(N8->Checked==true)
{N9->Checked=true;Panel1->Enabled=true;}
else
{ShowMessage("工具栏处于不可见状态,请先使工具栏可见!");}
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::N17Click(TObject *Sender)
{
colorFlag=17;
ColorSelect1->Execute();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::N18Click(TObject *Sender)
{
colorFlag=18;
ColorSelect1->Execute();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::N12Click(TObject *Sender)
{
WindowState=wsMinimized;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::N13Click(TObject *Sender)
{
WindowState=wsMaximized;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::N14Click(TObject *Sender)
{
WindowState=wsNormal;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::N16Click(TObject *Sender)
{
Close();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Action1Execute(TObject *Sender)
{
if(Image1->Visible==false)Image1->Visible=true;
else Image1->Visible=false;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Action2Execute(TObject *Sender)
{
static int i=0,flag=0;
Label1->Font->Color=clTeal;
if(flag==0)
{if((i+=2)>=12){flag=1;i=18;}}
else
{if((i-=2)<=0) flag=0;}
Label1->Font->Size=6+i%12;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Action3Execute(TObject *Sender)
{
static int h=250,w=300,flag=0;
if(flag==0)
{if((h-=25)<26)flag=1;
Image1->Height=h;Image1->Width=(w-=30);
}
else
{if((h+=25)>225)flag=0;
Image1->Height=h;Image1->Width=(w+=30);
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
// Label1->Font->Style=TFontStyles()<<fsBold<<fsItalic<<fsUnderline;
Image1->Stretch=true;
SpeedButton1->Caption="";
SpeedButton2->Caption="";
SpeedButton3->Caption="";
SpeedButton4->Caption="";
SpeedButton5->Caption="";
SpeedButton6->Caption="";
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
Action1->Execute();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
Action2->Execute();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::RichEditBold1Execute(TObject *Sender)
{
if(!aClick)
{
Label1->Font->Style=TFontStyles()<<fsBold;
aClick=true;
}
else
{
Label1->Font->Style=TFontStyles()>>fsBold;
aClick=false;
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::RichEditItalic1Execute(TObject *Sender)
{
Label1->Font->Style=TFontStyles()<<fsItalic;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::RichEditUnderline1Execute(TObject *Sender)
{
Label1->Font->Style=TFontStyles()<<fsUnderline;
}
//---------------------------------------------------------------------------