Example program - Using OCX controls


This example program shows how to use OBJECT notation to manipulate an OCX control embedded on a form. It uses the Microsoft web browser control that forms the heart of Internet Explorer. To use this OCX in your own application you should use the Install OCX option on the file menu of the forms designer and check the Microsoft Web Browser entry. It should be present on any client that has Internet Explorer installed.

The program gets an object reference to the OCX in the Show() event handler and uses it to load the page whose URL is secified in the edit box. It does this in the Validate() event handler for the edit box by calling the Navigate() method and passing it the text entered.

The program also traps the OnDocumentLoad() event which is triggered when a document has loaded. This is an event because the OCX loads asynchronously and when the Navigate() method completes the page may not be visible. Once loaded the event handler will use properties to update the title and status bars with the document title and its URL.

Notice that the edit box has its ReturnEvent property set so that pressing RETURN will trigger the validate as well as tabbing off the control. There is also a toolbar with some buttons that call other methods to move back and forwards on the history list and to refresh the page. These all have error traps as there will be an O30 COM error thrown if the user tabs off the end of the list or tries to refresh a page before one is loaded (a more sophisticated program could track loads and disable these buttons when they did not apply, just as the real IE5 does.)

// Demonstrates the use of OBJECT notation to access an embedded OCX on a form.
// From KCML 6.00 on this is the only way to manipulate an OCX.
// Uses the Microsoft "Web Browser control" which is the main component of IE4+
DEFFORM Form1()={.form,.form$,.Style=0x50c400c4,.Width=474,.Height=321,.Text$="KCML Browser",.Id=1024,.Menu=.mnuControl1},{.editControl1,.kcmldbedit$,.Style=0x50810088,.Left=29,.Top=10,.Width=339,.Height=13,.__Anchor=7,.Id=1000,.ValidateSelChange=1,.DropDownFilled=1},{.cancel,.button$,.Style=0x50010000,.Left=402,.Top=9,.Width=50,.Height=14,.Text$="Close",.__Anchor=5,.Id=2},{.ocxControl1,.kcmlocx$,.Style=0x50010000,.Left=14,.Top=44,.Width=447,.Height=247,.__Anchor=15,.Id=1001,.Definition=.ocxdef1},{.mnuControl1,.menu$,.Id=1002,.prev={.Text$="&Prev",.Picture=.VCRLeft,.Select()#},.next={.Text$="&Next",.Picture=.VCRRight,.Select()#},.refresh={.Flag=128,.Text$="&Refresh",.Picture=.Redo,.Select()#}},{.pane1,.status$,.Style=0x50000000,.Width=100},{.ocxdef1,.ocxdef$,.OCX$="Shell.Explorer",0x66.StatusTextChange(.Text$),0x6c.ProgressChange(.Progress,.ProgressMax),0x69.CommandStateChange(.Command,.Enable),0x6a.DownloadBegin(),0x68.DownloadComplete(),0x71.TitleChange(.Text$),0x70.PropertyChange(.szProperty$),0xfa.BeforeNavigate2(.pDisp,.URL$,.Flags$,.TargetFrameName$,.PostData$,.Headers$,.cancel),0xfb.NewWindow2(.ppDisp,.cancel),0xfc.NavigateComplete2(.pDisp,.URL$),0x0103.DocumentComplete(.pDisp,.URL$),0xfd.OnQuit(),0xfe.OnVisible(.Visible),0xff.OnToolBar(.ToolBar),0x0100.OnMenuBar(.MenuBar),0x0101.OnStatusBar(.StatusBar),0x0102.OnFullScreen(.FullScreen),0x0104.OnTheaterMode(.TheaterMode),0x0106.WindowSetResizable(.Resizable),0x0108.WindowSetLeft(.Left),0x0109.WindowSetTop(.Top),0x010a.WindowSetWidth(.Width),0x010b.WindowSetHeight(.Height),0x0107.WindowClosing(.IsChildWindow,.cancel),0x010c.ClientToHostWindow(.CX,.CY),0x010d.SetSecureLockIcon(.SecureLockIcon),0x010e.FileDownload(.cancel),0x010f.NavigateError(.pDisp,.URL$,.Frame$,.StatusCode$,.cancel),0xe1.PrintTemplateInstantiation(.pDisp),0xe2.PrintTemplateTeardown(.pDisp),0xe3.UpdatePageStatus(.pDisp,.nPage$,.fDone$),0x0110.privacyStateChange(.privacyState)}
LOCAL DIM OBJECT h
DEFEVENT Form1.Show()
	// Get a handle on the OCX.  This must be here and not in the Enter()
	// event as the OCX is instantiated between these two events
	// It should be a local variable for the form so we release it
	// when the form is exited
	OBJECT h = .ocxControl1
END EVENT
DEFEVENT Form1.editControl1.Validate()
	// new URL, load it into the OCX
	h.navigate(..ValidateText$)
END EVENT
DEFEVENT Form1.mnuControl1.next.Select()
	h.goforward()
	ERROR DO
	REM ignore attempts to go beyond the end
	END DO
END EVENT
DEFEVENT Form1.mnuControl1.prev.Select()
	h.goback()
	ERROR DO
		REM ignore attempts to go beyond the start
	END DO
END EVENT
	
DEFEVENT Form1.mnuControl1.refresh.Select()
	h.refresh()
	ERROR DO
		REM ignore errors
	END DO
END EVENT
DEFEVENT Form1.ocxControl1.DownloadComplete()
	// update titlebar with the new page title
	.pane1.Text$ = h.locationurl$
	.form.Text$ = h.locationname$
END EVENT
FORM END Form1
Form1.Open()

See also

Using SVG for graphics