| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1468 人关注过本帖, 1 人收藏
标题:【介紹如何用代碼編寫表單】百錢買百雞
只看楼主 加入收藏
TonyDeng
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:贵宾
威 望:304
帖 子:25859
专家分:48889
注 册:2011-6-22
结帖率:100%
收藏(1)
 问题点数:0 回复次数:26 
【介紹如何用代碼編寫表單】百錢買百雞
以下代碼,顯示如何創建一個表單,以及在用戶調整窗體尺寸時,内部控件如何隨之變化,拖拉窗口邊框或點擊右上角最大化、恢復按鈕,即可看到效果。

程序代码:
&&百钱买百鸡.prg

CLEAR ALL 
oForm = CREATEOBJECT("C_fm1")
WITH oForm
    .Caption = "百钱买百鸡(举解法)"
    .AutoCenter = .T.
    .Show
ENDWITH 
READ EVENTS
CLEAR ALL 
RETURN 

DEFINE CLASS C_fm1 AS Form

    ADD OBJECT cmdExit AS CommandButton WITH Caption = "退出", Height = 25, Width = 85
    ADD OBJECT pgfContent AS PageFrame WITH PageCount = 2
    
    PROCEDURE Arrange
        WITH This.cmdExit
            .Top = ThisForm.Height - .Height - 5
            .Left = ThisForm.Width - .Width - 6
        ENDWITH 
        WITH This.pgfContent
            .Top = 5
            .Left = 5
            .Height = This.cmdExit.Top - 5
            .Width = This.Width - .Left - 5
        ENDWITH 
    ENDPROC 
    
    PROCEDURE Activate
        This.Arrange
    ENDPROC 
    
    PROCEDURE Resize
        This.Arrange
    ENDPROC 
    
    PROCEDURE Destroy
        CLEAR EVENTS
    ENDPROC 
    
    PROCEDURE pgfContent.Init
        WITH This.Pages(1)
            .Caption = "问题描述"
        ENDWITH 
        WITH This.Pages(2)
            .Caption = "解答"
        ENDWITH 
    ENDPROC 
    
    PROCEDURE cmdExit.Click
        RELEASE ThisForm
    ENDPROC 

ENDDEFINE 

*!*    &&_screen.Visible= .F.
*!*     formHeight=280
*!*     formWidth=375

*!*     objForm=CREATEOBJECT("fm1")
*!*     objForm.show
*!*    READ EVENTS

*!*     DEFINE CLASS fm1 as Form
*!*         windowType=1
*!*         showWindow=2
*!*         Desktop=.t.
*!*         autoCenter=.t.
*!*         caption="百钱买百鸡(举解法)"
*!*         height=formHeight
*!*         width=formWidth
*!*         minHeight=280
*!*         minWidth=375
*!*         
*!*         ADD OBJECT cmdExit as myCmd1 WITH top=formHeight-5-25,left=formWidth-5-85
*!*         
*!*         ADD OBJECT pageFrame1 as myPf WITH top=5,left=5
*!*         
*!*         PROCEDURE UnLoad
*!*             CLEAR EVENTS
*!*         ENDPROC
*!*         
*!*    *!*        PROCEDURE resize
*!*    *!*            &&
*!*    *!*        ENDPROC 
*!*     ENDDEFINE

*!*     DEFINE CLASS myPf as PageFrame
*!*         visible=.t.
*!*         &&pageCount=2
*!*         height=formHeight-10-25
*!*         width=formWidth-5
*!*         anchor=15
*!*         
*!*         ADD OBJECT page1 as myPage1
*!*         ADD OBJECT page2 as myPage2
*!*         
*!*     ENDDEFINE

*!*     DEFINE CLASS myPage1 as Page
*!*         visible=.t.
*!*         caption="问题描述"
*!*         
*!*         ADD OBJECT p1Lbl1 as qdLbl WITH top=15,left=10,caption="百钱买百鸡,"+CHR(13)+"公鸡、母鸡、小鸡都要买,各能几只?"
*!*         ADD OBJECT p1Lbl2 as qdLbl WITH top=45,left=10,caption="铜钱一百"
*!*         ADD OBJECT p1Lbl3 as qdLbl WITH top=75,left=10,caption="公鸡一只五钱"
*!*         ADD OBJECT p1Lbl4 as qdLbl WITH top=105,left=10,caption="母鸡一只三钱"
*!*         ADD OBJECT p1Lbl5 as qdLbl WITH top=135,left=10,caption="小鸡三只一钱"
*!*         
*!*     ENDDEFINE 

*!*     DEFINE CLASS qdLbl as Label
*!*         fontsize=10
*!*         autoSize=.t.
*!*     ENDDEFINE

*!*     DEFINE CLASS myPage2 as Page
*!*         visible=.t.
*!*         caption="解答"
*!*         
*!*         ADD OBJECT p2Edit as qaEdit WITH top=5,left=5,anchor=15
*!*     ENDDEFINE 

*!*     DEFINE CLASS qaEdit as EditBox
*!*         visible=.t.
*!*         readOnly=.t.
*!*         
*!*         myHeight=thisform.myPf.height/4  &&??这里怎么算?
*!*         Width=thisform.pageFrame1.width-10
*!*         text="解答说明"
*!*         
*!*     ENDDEFINE 

*!*     DEFINE CLASS myCmd1 as CommandButton
*!*         visible=.t.
*!*         caption="退出"
*!*         height=25
*!*         width=85
*!*         anchor=12
*!*         
*!*         PROCEDURE click
*!*             thisform.release
*!*         ENDPROC
*!*     ENDDEFINE 
*!*     


[此贴子已经被作者于2015-11-20 19:58编辑过]

收到的鲜花
  • tlliqi2015-11-22 11:06 送鲜花  50朵   附言:向你学习
搜索更多相关主题的帖子: 如何 color 
2015-11-20 19:56
TonyDeng
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:贵宾
威 望:304
帖 子:25859
专家分:48889
注 册:2011-6-22
收藏
得分:0 
程序代码:
&&百钱买百鸡.prg

CLEAR ALL 
oForm = CREATEOBJECT("C_fm1")
WITH oForm
    .Caption = "百钱买百鸡(举解法)"
    .AutoCenter = .T.
    .Show
ENDWITH 
READ EVENTS
CLEAR ALL 
RETURN 

DEFINE CLASS C_fm1 AS Form

    ADD OBJECT cmdExit AS CommandButton WITH Caption = "退出", Height = 25, Width = 85
    ADD OBJECT pgfContent AS PageFrame
    
    PROCEDURE Arrange
        LOCAL lnIndex
        
        WITH This.cmdExit
            .Top = ThisForm.Height - .Height - 5
            .Left = ThisForm.Width - .Width - 6
        ENDWITH 
        
        WITH This.pgfContent
            .Top = 5
            .Left = 5
            .Height = This.cmdExit.Top - 5
            .Width = This.Width - .Left - 5
            .Pages[1].Arrange
            .Pages[2].Arrange
        ENDWITH 
        
    ENDPROC 
    
    PROCEDURE Init
        WITH This.pgfContent
            .AddObject("Page1", "C_Page1")
            .AddObject("Page2", "C_Page2")
        ENDWITH 
    ENDPROC 
    
    PROCEDURE Activate
        This.Arrange
    ENDPROC 
    
    PROCEDURE Resize
        This.Arrange
    ENDPROC 
    
    PROCEDURE Destroy
        CLEAR EVENTS
    ENDPROC 
    
    PROCEDURE cmdExit.Click
        RELEASE ThisForm
    ENDPROC 

ENDDEFINE 

DEFINE CLASS C_Page1 AS Page
    Caption = "问题描述"
    
    ADD OBJECT Label1 AS Label WITH Caption = "百钱买百鸡,"
    ADD OBJECT Label2 As Label WITH Caption = "公鸡、母鸡、小鸡都要买,各能几只?"
    ADD OBJECT Label3 AS Label WITH Caption = "铜钱一百"
    ADD OBJECT Label4 AS Label WITH Caption = "公鸡一只五钱"
    ADD OBJECT Label5 AS Label WITH Caption = "母鸡一只三钱"
    ADD OBJECT Label6 AS Label WITH Caption = "小鸡三只一钱"
    
    PROCEDURE Init
        WITH This
            .SetAll("WordWrap", .T., "Label")
            .SetAll("FontSize", 10, "Label")
            .SetAll("AutoSize", .T., "Label")
        ENDWITH 
    ENDPROC 
    
    PROCEDURE Arrange
        LOCAL lnIndex
        FOR lnIndex = 1 TO This.ControlCount
            WITH This.Controls[lnIndex]
                .Top = 15 + (lnIndex - 1) * (.Height + 5)
                .Left = 10
                .Width = This.Parent.Width - .Left - 10
            ENDWITH 
        NEXT 
    ENDPROC 

ENDDEFINE 

DEFINE CLASS C_Page2 AS Page
     Caption = "解答"
     
     ADD OBJECT Edit1 AS EditBox WITH Top = 5, Left = 5, Value = "解答说明"
     
     PROCEDURE Arrange
         WITH This.Edit1
             .Height = .Parent.Parent.PageHeight / 2
             .Width = .Parent.Parent.PageWidth / 2
         ENDWITH 
     ENDPROC 
     

 ENDDEFINE 

授人以渔,不授人以鱼。
2015-11-20 20:54
TonyDeng
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:贵宾
威 望:304
帖 子:25859
专家分:48889
注 册:2011-6-22
收藏
得分:0 
如何做成脫離vfg _Screen的獨立窗體程序,搜索我以前發過的帖子,專帖。

授人以渔,不授人以鱼。
2015-11-20 20:56
TonyDeng
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:贵宾
威 望:304
帖 子:25859
专家分:48889
注 册:2011-6-22
收藏
得分:0 
注意:這個程序中,衹有在PageFrame中用AddObject()方法加載的兩個Page,才是動態加載控件,其餘的都是靜態設計。也就是說,用ADD OBJECT指令寫的那些,是靜態控件,在程序加載到内存時就已經創建好並存在,那就是scx中繪畫控件的部分;用AddObject()寫的,是動態控件,在程序需要的時候由程序員決定加載(相當於C的malloc()動態分配内存函數),這是用scx拖拉不出來的,必須用代碼寫(因此代碼必須學,在某個方法或事件内自己寫這樣的代碼,否則你用所謂的“可視化”編程做不出這樣的動態效果)。學代碼編程,兩方面都會,光會拖拉,還得再學,這就是會與不會的差別,真當拖拉設計可以解決問題呀?

對比一下,用scx寫同樣的東西,你得用鼠標鍵盤輸入多少東西?其操作量比這裏多還是少?修改程序起來,哪個方便?我這樣寫出來的組件,隨時可以挪到別的地方用,用scx,當你要把一個容器或一堆控件從某處搬到別處的時候,看你怎麽做!

[此贴子已经被作者于2015-11-20 21:18编辑过]


授人以渔,不授人以鱼。
2015-11-20 21:16
hu9jj
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:红土地
等 级:贵宾
威 望:400
帖 子:11857
专家分:43421
注 册:2006-5-13
收藏
得分:0 
普及基本技能

活到老,学到老!http://www.(该域名已经被ISP盗卖了)E-mail:hu-jj@
2015-11-21 12:18
tlliqi
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
等 级:贵宾
威 望:204
帖 子:15453
专家分:65956
注 册:2006-4-27
收藏
得分:0 
学习
2015-11-21 12:26
不懂才问
Rank: 15Rank: 15Rank: 15Rank: 15Rank: 15
来 自:大草原
等 级:贵宾
威 望:29
帖 子:1503
专家分:6593
注 册:2010-7-5
收藏
得分:0 
图片附件: 游客没有浏览图片的权限,请 登录注册

图片附件: 游客没有浏览图片的权限,请 登录注册

图片附件: 游客没有浏览图片的权限,请 登录注册

图片附件: 游客没有浏览图片的权限,请 登录注册

报告老师,我低头不是因为我在装低调,是你问的问题,我真的不会答,,,
2015-11-21 13:08
不懂才问
Rank: 15Rank: 15Rank: 15Rank: 15Rank: 15
来 自:大草原
等 级:贵宾
威 望:29
帖 子:1503
专家分:6593
注 册:2010-7-5
收藏
得分:0 
图片附件: 游客没有浏览图片的权限,请 登录注册

图片附件: 游客没有浏览图片的权限,请 登录注册

图片附件: 游客没有浏览图片的权限,请 登录注册

报告老师,我低头不是因为我在装低调,是你问的问题,我真的不会答,,,
2015-11-21 13:09
不懂才问
Rank: 15Rank: 15Rank: 15Rank: 15Rank: 15
来 自:大草原
等 级:贵宾
威 望:29
帖 子:1503
专家分:6593
注 册:2010-7-5
收藏
得分:0 
有人发答案   那我就发题目吧 

报告老师,我低头不是因为我在装低调,是你问的问题,我真的不会答,,,
2015-11-21 13:09
不懂才问
Rank: 15Rank: 15Rank: 15Rank: 15Rank: 15
来 自:大草原
等 级:贵宾
威 望:29
帖 子:1503
专家分:6593
注 册:2010-7-5
收藏
得分:0 
看了T版的代码,果然觉得条理清晰  比我的强太多了

一会写一个代码对比分析   总结一下这两天学的东西

程序代码:
&&百钱买百鸡.prg

&&_screen.Visible= .F.
formHeight=350
formWidth=375

*!*    CREATE CURSOR tempBqbj;
*!*        (gongji i,muji i,xiaoji i,)

objForm=CREATEOBJECT("fm1")
objForm.show
READ EVENTS
&&CLOSE ALL
IF _VFP.StartMode=4
    QUIT
ENDIF 

DEFINE CLASS fm1 as Form
    windowType=1
    showWindow=2
    Desktop=.t.
    autoCenter=.t.
    caption="百钱买百鸡(穷解法)"
    height=formHeight
    width=formWidth
    minHeight=formHeight
    minWidth=formWidth
    
    ADD OBJECT cmdExit as myCmd1 WITH top=formHeight-5-25,left=formWidth-5-85
    
    ADD OBJECT pageFrame1 as myPf WITH top=5,left=5
    
    PROCEDURE UnLoad
        CLEAR EVENTS
    ENDPROC
    
*!*        PROCEDURE resize
*!*            &&
*!*        ENDPROC 
ENDDEFINE

DEFINE CLASS myPf as PageFrame
    visible=.t.
    &&pageCount=2
    height=formHeight-11-25
    width=formWidth-5
    anchor=15
    
    PROCEDURE init
        this.AddObject("page1","myPage1")
        this.AddObject("page2","myPage2")
        this.Resize
    ENDPROC 
    &&ADD OBJECT page1 as myPage1  &&这样是不科学的
    &&ADD OBJECT page2 as myPage2
    
    PROCEDURE resize
        this.page2.myResize
    ENDPROC 
    
ENDDEFINE

DEFINE CLASS myPage1 as Page
    visible=.t.
    caption="问题描述"
    
    
    PROCEDURE init
        this.addObject("p1Lbl1","qdLbl",15,10,"百钱买百鸡,公鸡、母鸡、小鸡都要买,各能几只?")
        this.addObject("p1Lbl2","qdLbl",45,10,"钢钱一百")
        this.addObject("p1Lbl3","qdLbl",75,10,"公鸡一只五钱")
        this.addObject("p1Lbl4","qdLbl",105,10,"母鸡一只三钱")
        this.addObject("p1Lbl5","qdLbl",135,10,"小鸡三只一钱")
    ENDPROC 
    
ENDDEFINE 

DEFINE CLASS qdLbl as Label
    visible=.t.
    fontsize=10
    autoSize=.t.
    
    PROCEDURE init(nTop,nLeft,cCaption)
        this.Top=nTop
        this.Left=nLeft
        this.Caption=cCaption
    ENDPROC
ENDDEFINE

DEFINE CLASS myPage2 as Page
    visible=.t.
    caption="解答"
    
    PROCEDURE init
        this.AddObject("p2Lbl","qaLbl",5,5,15)
        this.AddObject("p2Grid1","qaGrid1")
    ENDPROC
    &&ADD OBJECT p2Edit as qaEdit WITH top=5,left=5,anchor=15   &&这样是不科学的
    
    PROCEDURE myResize
        this.p2Lbl.height=this.Parent.height/4
        this.p2Lbl.width=this.Parent.width-10
        this.p2Grid1.height=this.Parent.height/4
        this.p2Grid1.top=this.Parent.height/4+15
        this.p2Grid1.width=this.Parent.width-10
        
    ENDPROC 
    
ENDDEFINE 

DEFINE CLASS qaLbl as Label 
    visible=.t.
    readOnly=.t.
    &&Height=thisform.myPf.height/4  &&??这里怎么算?
    &&Width=fm1.width-20
    caption="说明一二三四。"
    
    PROCEDURE init(nTop,nLeft,aAnchor)
        this.Top=nTop
        this.Left=nLeft
        this.Anchor=aAnchor
              
    ENDPROC 
    
ENDDEFINE 

DEFINE CLASS qaGrid1 as Grid
    visible=.t.
    left=5
    columnCount=3    
    
    PROCEDURE init
        anchor=15
    ENDPROC 
ENDDEFINE 

DEFINE CLASS myCmd1 as CommandButton
    visible=.t.
    caption="退出"
    height=25
    width=85
    anchor=12
    
    PROCEDURE click
        thisform.release
    ENDPROC
ENDDEFINE 

报告老师,我低头不是因为我在装低调,是你问的问题,我真的不会答,,,
2015-11-21 13:31
快速回复:【介紹如何用代碼編寫表單】百錢買百雞
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.038331 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved