garfield Site Admin

註冊時間: 2003-01-30 文章: 2160
第 1 樓
|
發表於: 星期四 五月 20, 2004 11:46 am 文章主題: [原創]利用vfp 所做的圖形縮放功能 |
|
|
工作原理: 利用VFP內建的image顯示功能 , 將圖形顯示在form中, 再利用 mydll.dll 的功能, 將form轉存圖形檔, 這樣就可達到圖形縮放的功能.
感謝 mydll.dll 作者: 任明漢 無私的提供這些函數供我們使用.
* 附檔有二種, 內容皆相同, 只差在簡繁體字而已.
代碼: |
*********************************************
*-- Form: resizepic (c:\vfp\picture\resizepic.scx)
*-- ParentClass: form
*-- BaseClass: form
*-- Time Stamp: 05/20/04 11:16:12 AM
*
DEFINE CLASS resizepic AS form
Height = 300
Width = 430
DoCreate = .T.
AutoCenter = .T.
BorderStyle = 0
Caption = "改變圖形size"
TitleBar = 0
Name = "RESIZEPIC"
ADD OBJECT picx AS textbox WITH ;
FontSize = 11, ;
Alignment = 3, ;
BackStyle = 0, ;
BorderStyle = 0, ;
Value = 0, ;
Enabled = .F., ;
Left = 168, ;
TabIndex = 4, ;
TabStop = .F., ;
Top = 72, ;
Width = 48, ;
Name = "picx"
ADD OBJECT label5 AS label WITH ;
AutoSize = .T., ;
FontSize = 11, ;
BackStyle = 0, ;
Caption = "*", ;
Enabled = .F., ;
Left = 228, ;
Top = 79, ;
Width = 9, ;
TabIndex = 5, ;
Name = "Label5"
ADD OBJECT picy AS textbox WITH ;
FontSize = 11, ;
Alignment = 0, ;
BackStyle = 0, ;
BorderStyle = 0, ;
Value = 0, ;
Enabled = .F., ;
Left = 240, ;
TabIndex = 6, ;
TabStop = .F., ;
Top = 72, ;
Width = 48, ;
Name = "picy"
ADD OBJECT label1 AS label WITH ;
AutoSize = .T., ;
FontSize = 11, ;
BackStyle = 0, ;
Caption = "原圖", ;
Left = 36, ;
Top = 36, ;
Width = 32, ;
TabIndex = 1, ;
Name = "Label1"
ADD OBJECT source1 AS textbox WITH ;
FontSize = 11, ;
Height = 24, ;
Left = 72, ;
MaxLength = 250, ;
TabIndex = 2, ;
Top = 36, ;
Width = 264, ;
Name = "source1"
ADD OBJECT getpic1 AS commandbutton WITH ;
Top = 36, ;
Left = 336, ;
Height = 24, ;
Width = 25, ;
FontSize = 11, ;
Caption = "...", ;
TabIndex = 3, ;
Name = "getpic1"
ADD OBJECT size1 AS optiongroup WITH ;
AutoSize = .T., ;
ButtonCount = 5, ;
Value = 1, ;
Height = 98, ;
Left = 72, ;
Top = 96, ;
Width = 76, ;
TabIndex = 8, ;
Name = "size1", ;
Option1.Caption = "自定尺寸", ;
Option1.Value = 1, ;
Option1.Height = 16, ;
Option1.Left = 5, ;
Option1.Top = 5, ;
Option1.Width = 66, ;
Option1.AutoSize = .T., ;
Option1.Name = "Option1", ;
Option2.Caption = "320*240", ;
Option2.Height = 16, ;
Option2.Left = 5, ;
Option2.Top = 23, ;
Option2.Width = 60, ;
Option2.AutoSize = .T., ;
Option2.Name = "Option2", ;
Option3.Caption = "640*480", ;
Option3.Height = 16, ;
Option3.Left = 5, ;
Option3.Top = 41, ;
Option3.Width = 60, ;
Option3.AutoSize = .T., ;
Option3.Name = "Option3", ;
Option4.Caption = "800*600", ;
Option4.Height = 16, ;
Option4.Left = 5, ;
Option4.Top = 59, ;
Option4.Width = 60, ;
Option4.AutoSize = .T., ;
Option4.Name = "Option4", ;
Option5.Caption = "1024*768", ;
Option5.Height = 16, ;
Option5.Left = 5, ;
Option5.Top = 77, ;
Option5.Width = 66, ;
Option5.AutoSize = .T., ;
Option5.Name = "Option5"
ADD OBJECT label2 AS label WITH ;
AutoSize = .T., ;
FontSize = 11, ;
BackStyle = 0, ;
Caption = "轉存尺寸", ;
Left = 12, ;
Top = 96, ;
Width = 62, ;
TabIndex = 7, ;
Name = "Label2"
ADD OBJECT command2 AS commandbutton WITH ;
AutoSize = .T., ;
Top = 228, ;
Left = 276, ;
Width = 109, ;
FontSize = 11, ;
Caption = "\<S.?#125;始轉換", ;
TabIndex = 14, ;
Name = "Command2"
ADD OBJECT sizex AS spinner WITH ;
FontSize = 11, ;
KeyboardLowValue = 1, ;
Left = 168, ;
SpinnerLowValue = 1.00, ;
TabIndex = 9, ;
Top = 96, ;
Width = 60, ;
Value = 640, ;
Name = "sizex"
ADD OBJECT label3 AS label WITH ;
AutoSize = .T., ;
FontSize = 11, ;
BackStyle = 0, ;
Caption = "*", ;
Left = 228, ;
Top = 103, ;
Width = 9, ;
TabIndex = 10, ;
Name = "Label3"
ADD OBJECT sizey AS spinner WITH ;
FontSize = 11, ;
KeyboardLowValue = 1, ;
Left = 240, ;
SpinnerLowValue = 1.00, ;
TabIndex = 11, ;
Top = 96, ;
Width = 60, ;
Value = 480, ;
Name = "sizey"
ADD OBJECT stretch1 AS optiongroup WITH ;
AutoSize = .T., ;
ButtonCount = 3, ;
Value = 2, ;
Height = 62, ;
Left = 168, ;
Top = 132, ;
Width = 88, ;
TabIndex = 16, ;
Name = "stretch1", ;
Option1.Caption = "裁切", ;
Option1.Value = 0, ;
Option1.Height = 16, ;
Option1.Left = 5, ;
Option1.Top = 5, ;
Option1.Width = 42, ;
Option1.AutoSize = .T., ;
Option1.Name = "Option1", ;
Option2.Caption = "按比例縮放", ;
Option2.Value = 1, ;
Option2.Height = 16, ;
Option2.Left = 5, ;
Option2.Top = 23, ;
Option2.Width = 78, ;
Option2.AutoSize = .T., ;
Option2.Name = "Option2", ;
Option3.Caption = "按尺寸縮放", ;
Option3.Height = 16, ;
Option3.Left = 5, ;
Option3.Top = 41, ;
Option3.Width = 78, ;
Option3.AutoSize = .T., ;
Option3.Name = "Option3"
ADD OBJECT target1 AS textbox WITH ;
FontSize = 11, ;
Height = 24, ;
Left = 72, ;
MaxLength = 250, ;
TabIndex = 13, ;
ToolTipText = "不指定檔名, 則由電腦會自動產生一個檔名", ;
Top = 228, ;
Width = 204, ;
Name = "target1"
ADD OBJECT label4 AS label WITH ;
AutoSize = .T., ;
FontSize = 11, ;
BackStyle = 0, ;
Caption = "圖形檔名", ;
Left = 12, ;
Top = 228, ;
Width = 62, ;
TabIndex = 12, ;
Name = "Label4"
ADD OBJECT command3 AS commandbutton WITH ;
AutoSize = .T., ;
Top = 156, ;
Left = 288, ;
Width = 78, ;
FontSize = 11, ;
Cancel = .T., ;
Caption = "\<0.離?#125;", ;
TabIndex = 15, ;
Name = "Command3"
ADD OBJECT image1 AS image WITH ;
Stretch = 2, ;
Height = 24, ;
Left = 0, ;
Top = 0, ;
Visible = .F., ;
Width = 72, ;
Name = "Image1"
PROCEDURE form2jpg
lpara mret
* 將目前active的form, 存成一個 jpg 檔
* 參數: jpg 檔名( 可以指定要存到那個檔案, 不傳這個參數則會傳回隨機給的一個檔名 )
* 傳回值: jpg 檔名 , 如果傳回空值, 則表示不成功.
local mfile
mfile = 'z'+ltrim( right( str( val(sys(3)+'0000000')*rand() , 20,0 ),7))
if type('mret') # 'C'
mret = mfile+'.jpg'
else
mret = iif( lower(right(mret , 4)) # '.jpg' , mret+'.jpg' , mret )
endif
Set LIBRARY TO "C:\Program Files\Microsoft Visual Studio\Vfp98\foxtools.fll"
mHWnd = _WhToHwnd(_WFINDTITL( Thisform.Caption))
* ??? _WFINDTITL 這個函數須要用大寫 ???
*Declare INTEGER GetActiveWindow IN user32
*mHWnd=GetActiveWindow()
Declare Integer formtobmp IN "mydll.dll" integer hwnd,String bmpFileName
if formtobmp(mHWnd ,mfile+'.bmp' ) =0
Declare Integer tojpeg IN "mydll.dll" String bmpfilename, String jpgfilename
if tojpeg(mfile+'.bmp',mret ) # 0
mret = ''
endif
dele file &mfile..bmp
else
mret = ''
endif
return mret
ENDPROC
PROCEDURE Init
* 原創: garfield
* 有任何問題或建議: garfield_s@hotmail.com
* 感謝 mydll.dll 作者任明漢 提供的函數.
if file('resizepic.ini')
thisform.sizex.value = max(1, val( mline( filetostr( 'resizepic.ini') ,1)))
thisform.sizey.value = max(1, val( mline( filetostr( 'resizepic.ini') ,2)))
endif
ENDPROC
PROCEDURE source1.Valid
with thisform
.target1.value = repl( '' , len( .target1.value ))
local nwidth,nheight
*-- Retrieves the width and height of a jpg file
Declare Integer getjpgdimension IN "mydll.dll" string jpgfilename, integer @ nwidth,integer @ nheight
nwidth=0
nheight=0
if getjpgdimension( alltrim(this.value) ,@ nwidth, @ nheight) # 0
*messagebox('無法得知圖片尺寸,無法轉換')
*return .F.
.picx.value = 0
.picy.value = 0
else
.picx.value = nwidth
.picy.value = nheight
endif
endwith
ENDPROC
PROCEDURE source1.DblClick
this.parent.getpic1.click
ENDPROC
PROCEDURE getpic1.Click
with this.parent.source1
.value = getpic('jpg' )
.valid( )
endwith
ENDPROC
PROCEDURE size1.Valid
with thisform
store (this.value=1) to .sizex.enabled , .sizey.enabled
do case
case this.value = 2
.sizex.value = 320
.sizey.value = 240
case this.value = 3
.sizex.value = 640
.sizey.value = 480
case this.value = 4
.sizex.value = 800
.sizey.value = 600
case this.value = 4
.sizex.value = 1024
.sizey.value = 768
other
*
endcase
endwith
ENDPROC
PROCEDURE command2.Click
local mret, moldx,moldy, mrate1, mrate2
with thisform
********************************************************************************
.lockscreen = .T.
.image1.picture = alltrim(.source1.value)
.image1.stretch = .stretch1.value -1
if .image1.stretch = 1 &&--等比例填充
if .picx.value =0 .or. .picy.value=0
messagebox('無法得知圖片尺寸,無法轉換')
return .F.
endif
mrate1 = .picx.value/.picy.value
mrate2 = .sizex.value/.sizey.value
if .picx.value = .picy.value
.sizex.value = min( .sizex.value , .sizey.value)
.sizey.value = .sizex.value
else
if .sizey.value*mrate1 > .sizex.value
.sizey.value = .sizex.value / mrate1
else
.sizex.value = .sizey.value * mrate1
endif
endif
.image1.width = .sizex.value
.image1.height = .sizey.value
else
.image1.width = .sizex.value
.image1.height = .sizey.value
endif
moldx = .width
moldy = .height
.width = .image1.width
.height = .image1.height
.image1.visible = .T.
.lockscreen = .F.
inkey(1)
if empty( .target1.value )
mret = .form2jpg( )
else
mret = .form2jpg( alltrim( .target1.value ))
endif
.image1.visible = .F.
.width = moldx
.height = moldy
if empty( mret )
messagebox( '無法產生圖形檔, 請檢查檔案名是否正確' )
else
.target1.value = padr( mret , 250 )
if .size1.value = 1 &&--記憶自定尺寸
strtofile( ltrim(str( .sizex.value))+chr(13)+chr(10)+ltrim(str( .sizey.value)) , 'resizepic.ini')
endif
endif
endwith
ENDPROC
PROCEDURE command3.Click
thisform.release
ENDPROC
ENDDEFINE
*
*-- EndDefine: resizepic
**************************************************
|
_________________ 利用>>搜尋<<的功能會比問的還要快得到答案. |
|