• Welcome to PlanetSquires Forums.
 

CImageCtx Class

Started by Bumblebee, June 24, 2020, 08:32:02 AM

Previous topic - Next topic

Bumblebee

How do I create an image control in an existing form?
I need an image control whose contents I can change programmatically.
The Picturebox control does not allow this, as far as I can tell.
My intention is to include the CImageCtx class as part of a visual designer project.
Failed pollinator.

Paul Squires

Use the PictureBox and change the Image property. Add the images to your project via the "Image Library" and ensure that the picture format is RCDATA (even for icons). The images will all have names that are prefixed by "IMAGE_".

For example, I added an icon called "WinFBE" (basically rhe blue star icon that you see as WinFBE's exe icon). The name that is assigned to that image is then:  IMAGE_WINFBE 

In a Button Click event I change the PictureBox image to use this new icon as follows:

frmMain.Picture1.Image = "IMAGE_WINFBE"

Paul Squires
PlanetSquires Software
WinFBE Editor and Visual Designer

Bumblebee

In this program, the image will be loaded from a file, when an associated item is selected in a list box. Can I do this with the picture box control?
For example:
a = "C:\My files\test.jpg"
pImageCtx.LoadImageFromFile a 'displays the image

With the picture box, I can select among images added via the Image Manager, using the names as you describe.
How do I add an image from a file at runtime, so the picture box can display it?
Failed pollinator.

Bumblebee

#3
In the Ctx image demo, a Cwindow class named pWindow was created.
So if Form1 is a CWindow, can this control be added to a form at runtime?

Application.Run(Form1)

#INCLUDE ONCE "Afx/CImageCtx.inc"

'Add an image control
'DIM pImageCtx AS CImageCtx = CImageCtx(@pWindow, IDC_IMAGECTX, "", 0, 0, pWindow.ClientWidth, pWindow.ClientHeight)
DIM pImageCtx AS CImageCtx = CImageCtx(@Form1, IDC_IMAGECTX, "", 0, 0, Form1.Width, Form1.Height)


C:\Decoy\Software\fb\WinFBE_VD_MAIN.bas(61) warning 3(1): Passing different pointer types, at parameter 1 (pWindow) of AFX.CIMAGECTX.constructor(as AFX.CWINDOW ptr, as long, as wstring, as long, as long, as long, as long, as ulong, as ulong, as long)
C:\Decoy\Software\fb\WinFBE_VD_MAIN.bas(61) error 24: Invalid data types, at parameter 1 (pWindow) of AFX.CIMAGECTX.constructor(as AFX.CWINDOW ptr, as long, as wstring, as long, as long, as long, as long, as ulong, as ulong, as long)


Original constructor as per the help file:
CONSTRUCTOR CImageCtx (BYVAL pWindow AS CWindow PTR, BYVAL cID AS INTEGER, _
   BYREF wszTitle AS CONST WSTRING = "", BYVAL x AS LONG = 0, BYVAL y AS LONG = 0, _
   BYVAL nWidth AS LONG = 0, BYVAL nHeight AS LONG = 0, BYVAL dwStyle AS DWORD = 0, _
   BYVAL dwExStyle AS DWORD = 0, BYVAL lpParam AS LONG_PTR = 0)
Failed pollinator.

Paul Squires

In theory, you would do like the code below to add the image control at runtime. (I put "ImageFromFile" on my to do list for an additional method for the PictureBox):

The problem is that when I run the code below I am not getting the image to display. I will troubleshoot it tomorrow as I am on very limited time this evening.



#INCLUDE ONCE "Afx/CImageCtx.inc"

dim shared pImageCtx AS CImageCtx PTR

const IDC_IMAGECTX = 200


''
''
Function frmMain_Button1_Click( ByRef sender As wfxButton, ByRef e As EventArgs ) As LRESULT

   ' // Load an image from disk
   if pImageCtx then
      pImageCtx->LoadImageFromFile(".\WinFBE.ico")
   end if

   Function = 0
End Function

''
''
Function frmMain_Load( ByRef sender As wfxForm, ByRef e As EventArgs ) As LRESULT
   
   ' // Add an image control
   dim pWindow as CWindow ptr = AfxCWindowPtr(sender.hWindow)
   if pWindow then
      pImageCtx = @CImageCtx( pWindow, IDC_IMAGECTX, , 10, 10, 200, 200 )
      if pImageCtx then
         pImageCtx->LoadImageFromFile(".\LeftArrow.png")
         pImageCtx->SetImageAdjustment(GDIP_IMAGECTX_AUTOSIZE, true)
      end if
   end if
   
   Function = 0
End Function


Paul Squires
PlanetSquires Software
WinFBE Editor and Visual Designer

Paul Squires

Here you go. Fully working now. I forgot to instantiate the control with NEW and then DELETE it when the Form destroys.


#INCLUDE ONCE "Afx/CImageCtx.inc"

dim shared pImageCtx AS CImageCtx PTR

const IDC_IMAGECTX = 200


''
''
Function frmMain_Button1_Click( ByRef sender As wfxButton, ByRef e As EventArgs ) As LRESULT

   ' // Load an image from disk
   if pImageCtx then
      pImageCtx->LoadImageFromFile(".\WinFBE.ico")
   end if

   Function = 0
End Function

''
''
Function frmMain_Load( ByRef sender As wfxForm, ByRef e As EventArgs ) As LRESULT
   
   ' // Add an image control
   dim pWindow as CWindow ptr = AfxCWindowPtr(sender.hWindow)
   if pWindow then
      pImageCtx = new CImageCtx( pWindow, IDC_IMAGECTX, , 10, 10, 200, 200 )
      if pImageCtx then
         pImageCtx->LoadImageFromFile(".\LeftArrow.png")
         pImageCtx->SetImageAdjustment(GDIP_IMAGECTX_AUTOSIZE, true)
      end if
   end if
   
   Function = 0
End Function

''
''
Function frmMain_FormClosed( ByRef sender As wfxForm, ByRef e As EventArgs ) As LRESULT
   if pImageCtx then
      delete pImageCtx
      pImageCtx = 0
   end if
   Function = 0
End Function


Paul Squires
PlanetSquires Software
WinFBE Editor and Visual Designer

Bumblebee

Thank-you.
I look forward to the era when I understand what's going on with this code.
I really need to study the .inc and .bi files that are being referenced.
Failed pollinator.

Paul Squires

I have added the 'ImageFromFile' property to the PictureBox control so you should be able to change the picture via external disk files at runtime. Will be in the next update.
Paul Squires
PlanetSquires Software
WinFBE Editor and Visual Designer

Bumblebee

I'll be sure to try it out.
Question: Does this control receive focus when the tab key is pressed?
Failed pollinator.

José Roca

> Question: Does this control receive focus when the tab key is pressed?

Not by default, but you can specify the window control styles when you create it and add WS_TABSTOP.

Bumblebee

I've attached a zipped project in the WinFBE version thread. Default behavior is unusual.
Failed pollinator.