可以根据这个思路把背景封装为表单的属性的
授人以渔,不授人以鱼。
Main() RETURN PROCEDURE Main LOCAL loForm loForm = CREATEOBJECT("_Form") WITH loForm .Caption = "桌面背景测试程序" .Background = "Z:\test\VFP\China 06.JPG" *.BackgroundStretch = 2 .Show ENDWITH READ EVENTS ENDPROC DEFINE CLASS _Form AS Form * 自定义属性 Background = "" && 表单背景图片文件名,可带路径 BackgroundStretch = 1 && 背景图的拉伸属性,传递给Image控件 * 属性Background的关联事件,属性值发生变化即触发 PROCEDURE Background_Assign(tcFileName) ThisForm.Background = IIF((VARTYPE(tcFileName) == "C") .AND. !EMPTY(tcFileName), tcFileName, "") IF !EMPTY(ThisForm.Background) * 检查背景图控件是否存在,若不存在方创建 IF VARTYPE(ThisForm.imgBackground) != "O" ThisForm.AddObject("imgBackground", "Image") ENDIF WITH ThisForm.imgBackground .Picture = ThisForm.Background * 取图片的长宽比备用,在图片中创建自定义属性Scale保存这个比例数值 .Stretch = 0 .AddProperty("Scale", .Width / .Height) * 设置图片的拉伸与表单的自定义属性相同 .Stretch = ThisForm.BackgroundStretch .Visible = .T. ENDWITH ELSE * 撤销背景图 WITH ThisForm.imgBackground .Picture = "" .Visible = .F. ENDWITH ENDIF ENDPROC PROCEDURE BackgroundStretch_Assign(tnValue) WITH ThisForm .BackgroundStretch = IIF((VARTYPE(tnValue) == "N") .AND. BETWEEN(tnValue, 0, 2), tnValue, 1) .imgBackground.Stretch = .BackgroundStretch ENDWITH ENDPROC PROCEDURE Activate This.Resize ENDPROC PROCEDURE Resize IF VARTYPE(ThisForm.imgBackground) == "O" WITH ThisForm.imgBackground .Top = 0 .Left = 0 .Height = ThisForm.Height .Width = ThisForm.Width ENDWITH ENDIF ENDPROC PROCEDURE Destroy CLEAR EVENTS ENDPROC ENDDEFINE
PROCEDURE Activate This.Resize ENDPROC PROCEDURE Resize IF VARTYPE(ThisForm.imgBackground) == "O" WITH ThisForm.imgBackground .Top = 0 .Left = 0 .Height = ThisForm.Height .Width = ThisForm.Width ENDWITH ENDIF ENDPROC
* 自定义属性 Background = "" && 表单背景图片文件名,可带路径 BackgroundStretch = 1 && 背景图的拉伸属性,传递给Image控件