Custom Control - How To

Started by Richard Marchessault, January 15, 2013, 01:53:51 PM

Previous topic - Next topic

Richard Marchessault

Here is my code in Firefly. When run the message pops up indicating that the object has been created but the About box never shows.

Function FORM1_WM_CREATE ( _
                         hWndForm As Dword, _      ' handle of Form
                         ByVal UserData As Long _  ' optional user defined Long value
                         ) As Long

    Dim BCR As EYMBARCODEREADERLibEymBarcodeReader
    BCR = OC_GetDispatch(HWND_FORM1_OCXCONTROL1)
    BCR = NewCom $PROGID_EYMBARCODEREADEREYMBARCODEREADERCTRL1
    If IsObject(BCR) Then MsgBox "Object" Else MsgBox "Not object"
    FF_Control_SetFocus(HWND_FORM1_OCXCONTROL1)
    Object Call BCR.AboutBox     
End Function
Thanks,
Dick

José Roca

Remove BCR = NewCom $PROGID_EYMBARCODEREADEREYMBARCODEREADERCTRL1

Richard Marchessault

I removed that line but now the program indicates that an object has not been created and the about box still does not show up.
Thanks,
Dick

José Roca

Do you have introduced EYMBARCODEREADER.EymBarcodeReaderCtrl.1 in the ProgID field of the properties of the control?

Richard Marchessault

Yes. See the attachment.
Thanks,
Dick

José Roca

The probably the control has not yet been created and HWND_FORM1_OCXCONTROL1 must be 0 (check it). Add a button for testing, move the code to the WM_COMMAND message and test it there.

Richard Marchessault

Unfortunately putting the code in the Command section for a button produces the same result as was obtained with the code in the Create section for the form. The same result is obtained each time the button is clicked. In all cases HWND_FORM1_OCXCONTROL1 comes up with a value of zero.
Thanks,
Dick

José Roca

Then the control is not being created for some reason. Try removing the trial key; it doesn't look like a key for licensed controls, that usually is a GUID.

Richard Marchessault

I removed the trial key and that made a difference. The control now appears on the form and the handle is not zero. I still have the code in the command section for a button on the form. However, the about box does not display when the following code is executed.

Object Call BCR.AboutBox
Thanks,
Dick

Richard Marchessault

The following code in the Command code for a button on the form works in that the About box appears and I can read values from parameters of the OCX control. Now if I can figure out how to load a bitmap into the control I should be well on my way to fully evaluating this control before I purchase it. Thanks to all for your assistance.

    Dim BCR As EYMBARCODEREADERLibEymBarcodeReader
    Dim X As Single, LL As Long     
    BCR = OC_GetDispatch(HWND_FORM1_OCXCONTROL1)
    Object Get BCR.Confidence To X
    MsgBox Str$(X)
    Object Get BCR.ExpectedDpi To X
    MsgBox Str$(X)
    Object Call BCR.AboutBox To LL
    MsgBox Str$(LL) & " " & Hex$(LL)
Thanks,
Dick

José Roca


LOCAL oPicture  AS IDispatch
vPath = <path to picture>
IF OleLoadPictureFile(vPath, oPicture) = %S_OK THEN
  OBJECT SET BCR.Picture = oPicture 
END IF


You must have to add #INCLUDE "OleCtl.inc" to the list of include files to have access to the OleLoadPictureFile function.

Richard Marchessault

#26
I added the include statement for OleCtl.inc and ran the code below for the button on the form. If I click the button once, the "Not OK" message comes up. If I click the button a second time the program crashes after the "Not OK" message appears and is cleared from the screen. If I uncomment the Exit Function line, I can click the button as many times as I like and there is no crash. In each case the About box for the OCX does popup. What might be causing the crash?

Local BCR As EYMBARCODEREADERLibEymBarcodeReader
    Local X As Single, LL As Long
    Local oPicture As IDispatch, vPath As Variant, Z$     
    BCR = OC_GetDispatch(HWND_FORM1_OCXCONTROL1)
    Object Call BCR.AboutBox To LL
    vPath = "C:\Temp\barcode.bmp"
    'Exit Function
    If OleLoadPictureFile(vPath, oPicture) = %S_OK Then
        Object Set BCR.Picture = oPicture
        Object Get BCR.RawData To Z$
        MsgBox Z$
    Else
        MsgBox "Not OK"
    End If

Using the following code eliminates the crash but produces the result of "Other = FFFFFFFF80020008" every time.

    Local BCR As EYMBARCODEREADERLibEymBarcodeReader
    Local X As Single, LL As Long
    Local oPicture As IDispatch, vPath As Variant, Z$, Y$     
    BCR = OC_GetDispatch(HWND_FORM1_OCXCONTROL1)
    Object Call BCR.AboutBox To LL
    vPath = "C:\Users\Dick\Documents\Temp\barcode.bmp"
    LL = OleLoadPictureFile(vPath, oPicture)
    Select Case LL
        Case %S_OK
            MsgBox "Picture OK"
            Object Set BCR.Picture = oPicture
            Object Get BCR.RawData To Z$
            MsgBox Z$
        Case %CTL_E_INVALIDPICTURE             
            MsgBox "Invalid Picture"
        Case Else
            MsgBox "Other = " & Hex$(LL)
    End Select
Thanks,
Dick

José Roca

I think there is a mistake in the declare.

Change:


DECLARE FUNCTION OleLoadPictureFile IMPORT "OLEAUT32.DLL" ALIAS "OleLoadPictureFile" ( _
   BYREF varFileName AS ANY _                           ' __in  VARIANT varFileName
, BYREF lplpdispPicture AS IDispatch _                 ' __out LPDISPATCH *lplpdispPicture
) AS LONG                                              ' HRESULT


to.


DECLARE FUNCTION OleLoadPictureFile IMPORT "OLEAUT32.DLL" ALIAS "OleLoadPictureFile" ( _
   BYVAL varFileName AS VARIANT _                       ' __in  VARIANT varFileName
, BYREF lplpdispPicture AS IDispatch _                 ' __out LPDISPATCH *lplpdispPicture
) AS LONG                                              ' HRESULT


in OleCtl.inc.

Richard Marchessault

#28
I got it working. I entered the message below before I picked up the fact that not only was the change in the include function from "Any" to "Variant" but also from "ByRef" to "ByVal". Now that I made that change, the OCX is working just fine. Pictures are loading without a problem. Barcodes reading is working fine. Thanks for all your help.

If anyone is reading through this, you should note that the string value being picked up by the control is a unicode value and requires a wide string. The code below works best with Z$$ instead of Z$.




Okay. I made the change to variant in the include file. No crashing but the picture is still not being loaded. The return code from OleLoadPictureFile is "Other = 80020008". What else might be the problem?

    Local BCR As EYMBARCODEREADERLibEymBarcodeReader
    Local X As Single, LL As Dword
    Local oPicture As IDispatch, vPath As Variant, Z$, Y$     
    BCR = OC_GetDispatch(HWND_FORM1_OCXCONTROL1)
    Object Call BCR.AboutBox To LL
    vPath = "C:\Users\Dick\Documents\Temp\barcode.bmp"
    LL = OleLoadPictureFile(vPath, oPicture)
    Select Case LL
        Case %S_OK
            MsgBox "Picture OK"
            Object Set BCR.Picture = oPicture
            Object Get BCR.RawData To Z$
            MsgBox Z$
        Case %CTL_E_INVALIDPICTURE             
            MsgBox "Invalid Picture"
        Case Else
            MsgBox "Other = " & Hex$(LL)
    End Select
Thanks,
Dick