关闭

Panel1加载图片

3000人阅读 评论(0) 收藏 举报

.h文件

//---------------------------------------------------------------------------

#ifndef Unt_TextH
#define Unt_TextH
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <ComCtrls.hpp>
#include <ExtCtrls.hpp>
#include <Graphics.hpp>
#include <Menus.hpp>
#include <Dialogs.hpp>
#include <ExtDlgs.hpp>
#include <Jpeg.hpp>
//---------------------------------------------------------------------------
class TStarCraft : public TForm
{
__published:// IDE-managed Components
        TScrollBox *sb_Map;
        TButton *btn_Generator;
        TButton *btn_Export;
        TPopupMenu *pm_Select;
        TStatusBar *sb_State;
        TMenuItem *pm_Solder;
        TMenuItem *pm_Robot;
        TMenuItem *pm_Home;
        TMenuItem *Line;
        TMenuItem *rSolder;
        TMenuItem *rRobot;
        TMenuItem *rHome;
        TPanel *pl_Panel;
        TImage *img_Map;
        TImage *img_Solder;
        TImage *img_Robot;
        TImage *img_Home;
        TSavePictureDialog *spd_Export;
        void __fastcall CaseSelection(TObject *Sender);
        void __fastcall GenerateSolder(TObject *Sender);
        void __fastcall GenerateRobot(TObject *Sender);
        void __fastcall GenerateHome(TObject *Sender);
        void __fastcall RemoveSolder(TObject *Sender);
        void __fastcall RemoveRobot(TObject *Sender);
        void __fastcall RemoveHome(TObject *Sender);
        void __fastcall ExportPictures(TObject *Sender);

       
private:// User declarations
        bool HasSelected,isCon,irCon,ihCon;
        POINT point;

        void __fastcall CustomMD(TObject *Sender, TMouseButton Button,
          TShiftState Shift, int X, int Y)
        {
                if (Sender->InheritsFrom(__classid(TImage)))
                {
                        if (Button == mbLeft)
                        {
                                HasSelected = true;
                                point.x = X;
                                point.y = Y;
                                ((TImage*)Sender)->BringToFront();

                                sb_State->Panels->Items[0]->Text = IntToStr(((TImage*)Sender)->Left) + " , " + IntToStr(((TImage*)Sender)->Top);
                                sb_State->Panels->Items[1]->Text = IntToStr(((TImage*)Sender)->Width) + " × " + IntToStr(((TImage*)Sender)->Height);
                        }
                }
        }
        void __fastcall CustomMM(TObject *Sender, TShiftState Shift,
          int X, int Y)
        {
                if (Sender->InheritsFrom(__classid(TImage)))
                {
                        if (HasSelected)
                        {
                                if (point.x > X)        //Left move
                                        if (((TImage*)Sender)->Left - abs(point.x - X) >= img_Map->ClientRect.Left)
                                                ((TImage*)Sender)->Left = ((TImage*)Sender)->Left - abs(point.x - X);
                                        else
                                                ((TImage*)Sender)->Left = img_Map->ClientRect.Left;
                                if (point.x < X)        //Right move
                                        if (((TImage*)Sender)->Left + abs(X - point.x) + ((TImage*)Sender)->Width <= pl_Panel->ClientWidth)
                                                ((TImage*)Sender)->Left = ((TImage*)Sender)->Left + abs(X - point.x);
                                        else
                                                ((TImage*)Sender)->Left = pl_Panel->ClientWidth - ((TImage*)Sender)->Width;

                                if (point.y > Y)        //Up move
                                        if (((TImage*)Sender)->Top - abs(point.y - Y) >= img_Map->ClientRect.Top)
                                                ((TImage*)Sender)->Top = ((TImage*)Sender)->Top - abs(point.y - Y);
                                        else
                                                ((TImage*)Sender)->Top = img_Map->ClientRect.Top;
                                if (point.y < Y)        //Down move
                                        if (((TImage*)Sender)->Top + abs(Y - point.y) + ((TImage*)Sender)->Height <= pl_Panel->ClientHeight)
                                                ((TImage*)Sender)->Top = ((TImage*)Sender)->Top + abs(Y - point.y);
                                        else
                                                ((TImage*)Sender)->Top = pl_Panel->ClientHeight - ((TImage*)Sender)->Height;

                                sb_State->Panels->Items[0]->Text = IntToStr(((TImage*)Sender)->Left) + " , " + IntToStr(((TImage*)Sender)->Top);
                                sb_State->Panels->Items[1]->Text = IntToStr(((TImage*)Sender)->Width) + " × " + IntToStr(((TImage*)Sender)->Height);
                        }
                }
        }
        void __fastcall CustomMU(TObject *Sender, TMouseButton Button,
          TShiftState Shift, int X, int Y)
        {
                if (Sender->InheritsFrom(__classid(TImage)))
                {
                        if (Button == mbLeft)
                                HasSelected = false;

                        sb_State->Panels->Items[0]->Text = IntToStr(((TImage*)Sender)->Left) + " , " + IntToStr(((TImage*)Sender)->Top);
                        sb_State->Panels->Items[1]->Text = IntToStr(((TImage*)Sender)->Width) + " × " + IntToStr(((TImage*)Sender)->Height);
                }
        }
public:// User declarations
        TImage *is,*ir,*ih;
        __fastcall TStarCraft(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TStarCraft *StarCraft;
//---------------------------------------------------------------------------
#endif
.cpp文件

//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Unt_Text.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TStarCraft *StarCraft;
//---------------------------------------------------------------------------
__fastcall TStarCraft::TStarCraft(TComponent* Owner)
        : TForm(Owner)
{
        DoubleBuffered = true;
}
//---------------------------------------------------------------------------
void __fastcall TStarCraft::CaseSelection(TObject *Sender)
{
        POINT curpos;
        if (GetCursorPos(&curpos))
                pm_Select->Popup(curpos.x,curpos.y);
}
//---------------------------------------------------------------------------
void __fastcall TStarCraft::GenerateSolder(TObject *Sender)
{
        if (!isCon)
        {
                is = new TImage(this);

                is->Parent = sb_Map;
                is = img_Solder;        //Get the address of existing TImage class
                is->BringToFront();     //Display the image over the background map
                is->Visible = true;

                is->OnMouseDown = CustomMD;
                is->OnMouseMove = CustomMM;
                is->OnMouseUp = CustomMU;

                isCon = true;
                pm_Solder->Enabled = !isCon;
        }
}
//---------------------------------------------------------------------------
void __fastcall TStarCraft::GenerateRobot(TObject *Sender)
{
        if (!irCon)
        {
                ir = new TImage(this);

                ir->Parent = sb_Map;
                ir = img_Robot;
                ir->BringToFront();
                ir->Visible = true;

                ir->OnMouseDown = CustomMD;
                ir->OnMouseMove = CustomMM;
                ir->OnMouseUp = CustomMU;

                irCon = true;
                pm_Robot->Enabled = !irCon;
        }
}
//---------------------------------------------------------------------------
void __fastcall TStarCraft::GenerateHome(TObject *Sender)
{
        if (!ihCon)
        {
                ih = new TImage(this);

                ih->Parent = sb_Map;
                ih = img_Home;
                ih->BringToFront();
                ih->Visible = true;

                ih->OnMouseDown = CustomMD;
                ih->OnMouseMove = CustomMM;
                ih->OnMouseUp = CustomMU;

                ihCon = true;
                pm_Home->Enabled = !ihCon;
        }
}
//---------------------------------------------------------------------------
void __fastcall TStarCraft::RemoveSolder(TObject *Sender)
{
        if (isCon)
        {
                is->Visible = false;
                is = NULL;      //Release the address of existing TImage class
                delete is;

                isCon = false;
                pm_Solder->Enabled = !isCon;
        }
}
//---------------------------------------------------------------------------
void __fastcall TStarCraft::RemoveRobot(TObject *Sender)
{
        if (irCon)
        {
                ir->Visible = false;
                ir = NULL;
                delete ir;

                irCon = false;
                pm_Robot->Enabled = !irCon;
        }
}
//---------------------------------------------------------------------------
void __fastcall TStarCraft::RemoveHome(TObject *Sender)
{
        if (ihCon)
        {
                ih->Visible = false;
                ih = NULL;
                delete ih;

                ihCon = false;
                pm_Home->Enabled = !ihCon;
        }
}
//---------------------------------------------------------------------------
void __fastcall TStarCraft::ExportPictures(TObject *Sender)
{
        //Create the TBitmap class and save the background
        Graphics::TBitmap *background_map = new Graphics::TBitmap;
        background_map->Width = img_Map->Width;
        background_map->Height = img_Map->Height;
        background_map->Canvas->CopyRect(img_Map->ClientRect,img_Map->Canvas,img_Map->ClientRect);

        //Redraw the elements above the background
        if (isCon)
                background_map->Canvas->Draw(is->Left,is->Top,img_Solder->Picture->Bitmap);
        if (irCon)
                background_map->Canvas->Draw(ir->Left,ir->Top,img_Robot->Picture->Bitmap);
        if (ihCon)
                background_map->Canvas->Draw(ih->Left,ih->Top,img_Home->Picture->Bitmap);

        //Export the picture file
        spd_Export->Title = "保存StarCraft地图";
        spd_Export->Filter = "Bitmap files (*.bmp)|*.bmp|JPEG files (*.jpg)|*.jpg";
        if (spd_Export->Execute())
        {
                if (spd_Export->FilterIndex == 1)
                        background_map->SaveToFile(spd_Export->FileName + ".bmp");
                if (spd_Export->FilterIndex == 2)
                {
                        TJPEGImage *convert = new TJPEGImage;
                        convert->Assign(background_map);
                        convert->SaveToFile(spd_Export->FileName + ".jpg");
                        delete convert;
                }
        }
        else
                return;

        delete background_map;
}
//---------------------------------------------------------------------------

(1)运行以后,FORM1最小化后,再用MOUSE点击,恢复为最小化前状态,此时,PANEL1上的背景图像会消失?
(2)在编辑状态,用鼠标把PANEL1尺寸扩大以后,此时FORM1上出现流动条,此时运行,点击BUTTON1后,保存图片则不只是PANEL1上的图片?
(3)当IMAGE1拖动到PANEL1边缘(例IMAGE1已经有一半看不到了),如何实现PANEL1尺寸会变大?
1、就这个问题,我觉得,当在组件表面进行重画的时候,会经常出现你说的这种情况。其实,解决这个问题的一种实用的方法就是在Panel上放置两个Image组件。一个用于做背景,另一个用于做前景,也就是你这个例子中的Image。之后,背景图片采用Image->Picture->Bitmap->LoadFromFile的方式,则背景就不会消失了。
2、我不明白你这样做是什么目的,当然,你可以设置Panel->Align为alClient。这样,Panel会自动填充满整个窗体,而不会出现超出边界的情况。
3、针对这种情况,可以通过判断Image的左、右、上、下边界和Panel的CliengRect的关系解决。稍后我会贴出这个解决方法的。

含有了图片保存功能的代码:

//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;

TControlCanvas *cc = new TControlCanvas;
Graphics::TBitmap *bmp = new Graphics::TBitmap;

bool IsOnIt,IsClicking;
TPoint p;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
        DoubleBuffered = true;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
        IsClicking = true;
        cc->Control = Panel1;
        bmp->LoadFromFile("c://windows//Clouds.bmp");
        cc->Brush->Bitmap = bmp;
        cc->FillRect(Panel1->ClientRect);
        //Display the Image's picture
        Image1->Refresh();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Image1MouseDown(TObject *Sender,
      TMouseButton Button, TShiftState Shift, int X, int Y)
{
        if (Button == mbLeft)
        {
                IsOnIt = true;
                p.x = X;
                p.y = Y;
        }
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Image1MouseMove(TObject *Sender, TShiftState Shift,
      int X, int Y)
{
        if (IsOnIt)
        {
                if (p.x > X)
                        Image1->Left = Image1->Left - abs(p.x - X);
                if (p.x < X)
                        Image1->Left = Image1->Left + abs(X - p.x);
                if (p.y > Y)
                        Image1->Top = Image1->Top - abs(p.y - Y);
                if (p.y < Y)
                        Image1->Top = Image1->Top + abs(Y - p.y);
                if (IsClicking)
                        Button1Click(Sender);
        }

}
//---------------------------------------------------------------------------
void __fastcall TForm1::Image1MouseUp(TObject *Sender, TMouseButton Button,
      TShiftState Shift, int X, int Y)
{
        if (Button == mbLeft)
                IsOnIt = false;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormDestroy(TObject *Sender)
{
        delete bmp;
        delete cc;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
        Graphics::TBitmap *b = new Graphics::TBitmap;
        b->Width = Panel1->Width;
        b->Height = Panel1->Height;
        b->Canvas->CopyRect(TRect(Panel1->ClientRect.Left,Panel1->ClientRect.Top,Panel1->ClientWidth,Panel1->ClientHeight),cc,Panel1->ClientRect);
        b->SaveToFile("c://abcde.bmp");
        delete b;
}
//---------------------------------------------------------------------------

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:214241次
    • 积分:3314
    • 等级:
    • 排名:第10853名
    • 原创:84篇
    • 转载:37篇
    • 译文:0篇
    • 评论:26条
    文章分类
    其他blog