Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

display an image in a dialog box

jaeger0
Beginner
1,020 Views
I'm trying to display an image in a dialog. I have a specific image typ which I read from a file.
So I tryed to make a dialog by the use of a the "Picture Control", as type I guess to use "Bitmap" or is it "owner draw"

Have I to convert my image to bitmap, and generate a bitmap handle (somehow)
However I have no Idea how I can do that, I looked for several examples but could not find one.

Any Idea wwhere I can find information about that problem ?
0 Kudos
5 Replies
Paul_Curtis
Valued Contributor I
1,020 Views
Here is a code fragment extracted from the WM_PAINT message handler within a window's proc function, which shows how to read a background image from a windows bitmap (*.bmp) file and display the image centered in the window:

[cpp]INTEGER                        :: rval, background_color
TYPE(T_PAINTSTRUCT)    :: ps
TYPE(T_RECT)                :: clientRect
INTEGER                        :: WndWidth, WndHeight
INTEGER                        :: imageWidth, imageHeight
INTEGER(HANDLE)           :: hbitmapImage
INTEGER(HANDLE)           :: hdcBitmap
INTEGER(HANDLE)           :: hgdiobjectOld
CHARACTER(LEN=256)    :: fname


fname = 'c:somepathsomefile.bmp'//CHAR(0)
hbitmapImage = LoadImage (ghInstance, fname, IMAGE_BITMAP, 0, 0, &
LR_LOADFROMFILE) rval = GetClientRect(hwnd, clientRect) WndWidth = clientRect%right - clientRect%left WndHeight = clientRect%bottom - clientRect%top
! create a device context for the current window
rval = BeginPaint (hwnd, ps) ! Erase the background CALL fill_rectangle (ps%hdc, background_color, clientRect) ! Draw the image CALL GetBitmapSize (hbitmapImage, ps%hdc, imageWidth, imageHeight) hdcBitmap = CreateCompatibleDC (ps%hdc) hgdiobjectOld = SelectObject (hdcBitmap, hbitmapImage) rval = BitBlt (ps%hdc, & (WndWidth-imageWidth)/2, & (WndHeight-imageHeight)/2, & imageWidth, & imageHeight, & hdcBitmap, 0, 0, SRCCOPY) ! clean up rval = SelectObject (hdcBitmap, hgdiobjectOld) rval = DeleteDC (hdcBitmap) rval = DeleteObject (hbitmapImage) rval = EndPaint (hwnd, ps) !==== seperate utility used in the above code ==== ! Fills width and height with the dimensions, in pixels, of the ! bitmap specified by the bitmap handle, using the metrics of the ! provided device context. SUBROUTINE GetBitmapSize (hbitmap, hDC, width, height) IMPLICIT NONE INTEGER(HANDLE), INTENT(IN) :: hbitmap INTEGER(HANDLE), INTENT(IN) :: hDC INTEGER, INTENT(OUT) :: width INTEGER, INTENT(OUT) :: height TYPE(T_BITMAPINFO) :: bitmapInfo INTEGER :: rval bitmapInfo%bmiHeader%biBitCount = 0 bitmapInfo%bmiHeader%biSize = SIZEOF(bitmapInfo%bmiHeader) rval = GetDIBits (hDC, hbitmap, 0, 0, 0, LOC(bitmapInfo), &
DIB_RGB_COLORS) IF (rval == 0) THEN ! error ELSE width = bitmapInfo%bmiHeader%biWidth height = bitmapInfo%bmiHeader%biHeight END IF END SUBROUTINE GetBitmapSize[/cpp]

0 Kudos
onkelhotte
New Contributor II
1,020 Views
To display a Bitmap on a button, you have to do the following:

1.) Import this bitmap to your resource file (name is IDB_BITMAP)
2.) Set your IDC_BUTTON bitamp true
3.) Use the following code

[cpp]hInst=GetModuleHandle(NULL)
hBitmap=LoadBitmap(hInst,IDB_BITMAP)
l=SendMessage(GetDlgItem(dlg%hwnd,IDC_BUTTON),BM_SETIMAGE,IMAGE_BITMAP,hBitmap)

[/cpp]
Markus
0 Kudos
jaeger0
Beginner
1,020 Views
I could not find where
fill_rectangle is defined,

in which module is this subroutine defined ?


Here is a code fragment extracted from the WM_PAINT message handler within a window's proc function, which shows how to read a background image from a windows bitmap (*.bmp) file and display the image centered in the window:

[cpp]INTEGER                        :: rval, background_color
TYPE(T_PAINTSTRUCT) :: ps
TYPE(T_RECT) :: clientRect
INTEGER :: WndWidth, WndHeight
INTEGER :: imageWidth, imageHeight
INTEGER(HANDLE) :: hbitmapImage
INTEGER(HANDLE) :: hdcBitmap
INTEGER(HANDLE) :: hgdiobjectOld
CHARACTER(LEN=256) :: fname


fname = 'c:somepathsomefile.bmp'//CHAR(0)
hbitmapImage = LoadImage (ghInstance, fname, IMAGE_BITMAP, 0, 0, &
LR_LOADFROMFILE)

rval = GetClientRect(hwnd, clientRect)
WndWidth = clientRect%right - clientRect%left
WndHeight = clientRect%bottom - clientRect%top

! create a device context for the current window
rval = BeginPaint (hwnd, ps)

! Erase the background
CALL fill_rectangle (ps%hdc, background_color, clientRect)

! Draw the image
CALL GetBitmapSize (hbitmapImage, ps%hdc, imageWidth, imageHeight)
hdcBitmap = CreateCompatibleDC (ps%hdc)
hgdiobjectOld = SelectObject (hdcBitmap, hbitmapImage)
rval = BitBlt (ps%hdc, &
(WndWidth-imageWidth)/2, &
(WndHeight-imageHeight)/2, &
imageWidth, &
imageHeight, &
hdcBitmap, 0, 0, SRCCOPY)

! clean up
rval = SelectObject (hdcBitmap, hgdiobjectOld)
rval = DeleteDC (hdcBitmap)
rval = DeleteObject (hbitmapImage)
rval = EndPaint (hwnd, ps)



!==== seperate utility used in the above code ====
! Fills width and height with the dimensions, in pixels, of the
! bitmap specified by the bitmap handle, using the metrics of the
! provided device context.
SUBROUTINE GetBitmapSize (hbitmap, hDC, width, height)
IMPLICIT NONE
INTEGER(HANDLE), INTENT(IN) :: hbitmap
INTEGER(HANDLE), INTENT(IN) :: hDC
INTEGER, INTENT(OUT) :: width
INTEGER, INTENT(OUT) :: height
TYPE(T_BITMAPINFO) :: bitmapInfo
INTEGER :: rval

bitmapInfo%bmiHeader%biBitCount = 0
bitmapInfo%bmiHeader%biSize = SIZEOF(bitmapInfo%bmiHeader)
rval = GetDIBits (hDC, hbitmap, 0, 0, 0, LOC(bitmapInfo), &
DIB_RGB_COLORS)

IF (rval == 0) THEN
! error
ELSE
width = bitmapInfo%bmiHeader%biWidth
height = bitmapInfo%bmiHeader%biHeight
END IF
END SUBROUTINE GetBitmapSize[/cpp]


0 Kudos
mavlik
Beginner
1,020 Views
There is no function "fill_rectangle" but in Win32 SDK (or in MSDN) you can find
function:
int FillRect(
HDC hDC, // handle to device context
CONST RECT *lprc, // pointer to structure with rectangle
HBRUSH hbr // handle to brush
);
May be Mr. Paul Curtis meant exactly this function?
And in unit "user32.f90" (user32.lib) you can find it declaration
[cpp]interface !lib=user32.lib
integer(4) function  FillRect (hDC ,lprc ,hbr ) 
!DEC$ IF DEFINED(_X86_)
!DEC$ ATTRIBUTES STDCALL, ALIAS : '_FillRect@12' :: FillRect
!DEC$ ELSE
!DEC$ ATTRIBUTES STDCALL, ALIAS :  'FillRect'    :: FillRect
!DEC$ ENDIF
!DEC$ ATTRIBUTES REFERENCE :: lprc
use dfwinty
integer         hDC
type(T_RECT)    lprc
integer         hbr
end function FillRect
end interface[/cpp]
0 Kudos
Paul_Curtis
Valued Contributor I
1,020 Views
Sorry; using the Win32 API is facilitated by making a set of wrapper functions for common operations. Here is how you fill a rectangle with a color:

[cpp]	RECURSIVE SUBROUTINE fill_rectangle (hdc, color, rect, edge)
		IMPLICIT NONE
		INTEGER(HANDLE), INTENT(IN)	:: hdc
		INTEGER, INTENT(IN)                   :: color
		TYPE(T_RECT), INTENT(IN)		:: rect
		INTEGER, INTENT(IN), OPTIONAL	:: edge
		INTEGER(HANDLE)			:: hbrush, hold
		INTEGER                                    :: rval 

		!	these statements always go together,
		!	and this routine ensures that a brush
		!	resource is always destroyed after use

		hbrush = CreateSolidBrush (color)
		hold    = SelectObject     (hdc, hbrush)
		rval     = FillRect		   (hdc, rect, hbrush)
		rval     = SelectObject     (hdc, hold)
                rval     = DeleteObject     (hbrush)

		IF (PRESENT(edge)) THEN
			IF (edge == 1) rval = DrawEdge (hdc, rect, EDGE_SUNKEN, BF_RECT)
		END IF

	END SUBROUTINE fill_rectangle[/cpp]

0 Kudos
Reply