全國最多中醫師線上諮詢網站-台灣中醫網
發文 回覆 瀏覽次數:2159
推到 Plurk!
推到 Facebook!

使用兩個Image來實現部份放大、拖曳的功能

答題得分者是:arisaka_matsuri
myelf
一般會員


發表:22
回覆:11
積分:6
註冊:2004-12-12

發送簡訊給我
#1 引用回覆 回覆 發表時間:2008-01-13 02:38:37 IP:140.138.xxx.xxx 訂閱
我想要作一個程式
大致內容是這樣
先建立一個Image1放置原始圖片(圖片很大,超過一整個螢幕)
將Image1設定為隱藏

接著建立Image2作為顯示部份區域使用
但是我弄了很久一直搞不定
原因是拖曳的部份有點困難
讓Image2顯示Image1中的一小塊OK
可是要讓他可以以滑鼠移動到不同的區塊顯示我不知道該怎麼作

(想要的功能就有點類似一般圖片瀏覽器中,圖片放大後可以直接用滑鼠拖曳,移動觀看的地方)

我想這個應該算是基礎程式
請問有沒有範例可以參考呢?

搜尋了相關主題好像沒有比較接近的…
請各位指導一下m(_ _)m

附上原始碼
(主要的問題是拖了以後,下次再拖曳他會變回原本位置,我嘗試加入變數卻得不到想要的效果..)

[code cpp]
int mouse_x,mouse_y;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------

void __fastcall TForm1::SpeedButton1Click(TObject *Sender)
{
TRect orig_img, new_img;
orig_img = Rect( 0,0,Image2->Width,Image2->Height);
new_img = Rect( 0,0,Image2->Width,Image2->Height);
Image2->Canvas->CopyRect(new_img,Image1->Canvas,orig_img);
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Image2MouseDown(TObject *Sender,
TMouseButton Button, TShiftState Shift, int X, int Y)
{
mouse_x = X ;
mouse_y = Y ;
Image2->Tag = 1;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Image2MouseMove(TObject *Sender, TShiftState Shift,
int X, int Y)
{
if( Image2->Tag == 1 )
{
int des_x = mouse_x - X, des_y = mouse_y - Y;
TRect orig_img, new_img;
orig_img = Rect( des_x,des_y,des_x Image2->Width,des_y Image2->Height);
new_img = Rect( 0,0,Image2->Width,Image2->Height);
Image2->Canvas->Rectangle(0,0,Image2->Width,Image2->Height);
Image2->Canvas->CopyRect(new_img,Image1->Canvas,orig_img);
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Image2MouseUp(TObject *Sender, TMouseButton Button,
TShiftState Shift, int X, int Y)
{
Image2->Tag = 0;
}

[/code]
編輯記錄
myelf 重新編輯於 2008-01-13 10:15:14, 註解 無‧
arisaka_matsuri
高階會員


發表:25
回覆:205
積分:231
註冊:2003-10-19

發送簡訊給我
#2 引用回覆 回覆 發表時間:2008-01-13 11:14:34 IP:218.166.xxx.xxx 訂閱
你的問題只要多加一個變數紀錄目前的滑鼠位置即可
重點在 TForm1::Image1MouseMove() 裡的 PrevMouseLoc = TPoint(X, Y);

附上我的程式碼讓你參考
不過只用一個TImage當作顯示用(長寬各為400 pixels)
一個TBitmap作為保存影像用

Unit1.h

[code cpp]
#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include
#include
#include
#include <Forms.hpp><br />
#include
#include
#include
#include
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // IDE-managed Components
TImage *Image1;
TButton *Button1;
TOpenPictureDialog *OpenPictureDialog1;
void __fastcall Button1Click(TObject *Sender);
void __fastcall Image1MouseMove(TObject *Sender, TShiftState Shift, int X, int Y);
void __fastcall FormDestroy(TObject *Sender);
private: // User declarations
public: // User declarations
__fastcall TForm1(TComponent* Owner);

Graphics::TBitmap *BmpBuff;
TPoint PrevMouseLoc;
TRect rectDisplay;
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif

[/code]

Unit1.cpp

[code cpp]
//---------------------------------------------------------------------------

#include
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
BmpBuff = new Graphics::TBitmap();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
if (OpenPictureDialog1->Execute())
{
if (FileExists(OpenPictureDialog1->FileName))
{
// create a new jpeg image
TJPEGImage *JPEG = new TJPEGImage();
// load jpeg image from file
JPEG->LoadFromFile(OpenPictureDialog1->FileName);
// decompress jpeg image to BmpBuff
BmpBuff->Assign(JPEG);
// show a init. 400x400 region of image
rectDisplay = TRect(0,0,400,400);
Image1->Canvas->CopyMode = cmSrcCopy;
Image1->Canvas->CopyRect(TRect(0,0,400,400), BmpBuff->Canvas, rectDisplay);
// release jpeg image
delete JPEG;
}
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Image1MouseMove(TObject *Sender, TShiftState Shift, int X, int Y)
{
if (Shift.Contains(ssLeft))
{
// calculate offsets of mouse movement
int iOffsetX = PrevMouseLoc.x - X;
int iOffsetY = PrevMouseLoc.y - Y;
// calculate the region of showing image
rectDisplay.Left = iOffsetX;
rectDisplay.Top = iOffsetY;
rectDisplay.Right = iOffsetX;
rectDisplay.Bottom = iOffsetY;
// show the region of image
Image1->Canvas->CopyRect(TRect(0,0,400,400), BmpBuff->Canvas, rectDisplay);
}

PrevMouseLoc = TPoint(X, Y);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormDestroy(TObject *Sender)
{
delete BmpBuff;
}
//---------------------------------------------------------------------------

[/code]
編輯記錄
arisaka_matsuri 重新編輯於 2008-01-13 11:18:19, 註解 無‧
arisaka_matsuri 重新編輯於 2008-01-13 11:21:03, 註解 無‧
myelf
一般會員


發表:22
回覆:11
積分:6
註冊:2004-12-12

發送簡訊給我
#3 引用回覆 回覆 發表時間:2008-01-13 15:26:22 IP:140.138.xxx.xxx 訂閱
謝謝你,這正式我要的
系統時間:2024-05-20 2:53:23
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!