April 24, 2024, 05:30:16 PM

News:

IonicWind Snippit Manager 2.xx Released!  Install it on a memory stick and take it with you!  With or without IWBasic!


Bitmap on a static

Started by Andy, July 05, 2018, 03:38:50 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Andy

Hi,

Has anyone tried adding a bitmap to a static control?

MSDN says you can do it with SS_BITMAP, there are one or two listed, but nothing that helps me in IWB.

Thanks,
Andy.
Day after day, day after day, we struck nor breath nor motion, as idle as a painted ship upon a painted ocean.

Egil

Funny you should ask this.. I was just peeking into MSDN and printed to paper the page about Tab Control styles.

About your problem: MSDN also say that the bitmap must be defined in a resource file.
( https://msdn.microsoft.com/en-us/library/ms933002.aspx )

Hope you haven't missed out that one.


Good luck!

Egil

Support Amateur Radio  -  Have a ham  for dinner!

Andy

Egil,

Thanks for that, but even with it in the resource file, how do I load it into a static?

And what's the hex value for SS_BITMAP?

Andy.
Day after day, day after day, we struck nor breath nor motion, as idle as a painted ship upon a painted ocean.

Brian

July 05, 2018, 05:17:10 AM #3 Last Edit: July 05, 2018, 05:18:49 AM by Brian Pugh
CONST SS_BITMAP = 0xE

Specifies that a bitmap is to be displayed in the static control. The text is the name of a bitmap (not a filename) defined elsewhere in the resource file

Brian

fasecero

Good morning. Here's MSDN style. Just edit the file name and adjust the static dimension to match the image size.


$INCLUDE "windowssdk.inc"

CONST STATIC_1 = 1
INT myBMP
WINDOW w1

OPENWINDOW w1,0,0,600,400,@MINBOX|@MAXBOX|@SIZE,NULL,"Simple Window",&w1_handler
WAITUNTIL w1 = 0
END

SUB w1_handler
SELECT @MESSAGE
CASE @IDCREATE
CENTERWINDOW w1

' load the image - create the static - set the image
myBMP = LOADIMAGE (GETSTARTPATH() + "myimage.bmp", @IMGBITMAP)
StaticBitmapCreate(w1.hwnd, 100, 40, 256, 256, STATIC_1)
StaticBitmapSetImage(w1.hwnd, STATIC_1, myBMP)

CASE @IDCLOSEWINDOW
' delete the image
DELETEIMAGE(myBMP, @IMGBITMAP)

CLOSEWINDOW w1
ENDSELECT
ENDSUB

SUB StaticBitmapCreate(HWND parent, int x, int y, int dimx, int dimy, int id), INT
INT dwStyles = WS_CHILD | WS_VISIBLE | SS_LEFT | SS_BITMAP
    RETURN CreateWindowEx(0, "STATIC", "", dwStyles, x, y, dimx, dimy, parent, id, GetModuleHandle(0), 0)
ENDSUB

SUB StaticBitmapSetImage(HWND parent, INT id, HBITMAP imagen)
HWND handle = GetDlgItem(parent, id)
_SendMessage(handle, STM_SETIMAGE, IMAGE_BITMAP, imagen)
ENDSUB


Andy

Brian and Fasecero

That's brilliant guys!!!!!!!!

So here we have a static, loaded with a bitmap, and acts like a button when you click on it!



$INCLUDE "windowssdk.inc"

CONST SS_NOTIFY=0x100
CONST STATIC_1 = 1
INT myBMP
WINDOW w1

OPENWINDOW w1,0,0,600,400,@MINBOX|@MAXBOX|@SIZE,NULL,"Simple Window",&w1_handler
WAITUNTIL w1 = 0
END

SUB w1_handler
SELECT @MESSAGE
CASE @IDCREATE
CENTERWINDOW w1

' load the image - create the static - set the image
myBMP = LOADIMAGE (GETSTARTPATH() + "tabclose.bmp", @IMGBITMAP)
StaticBitmapCreate(w1.hwnd, 100, 40, 256, 256, STATIC_1)
StaticBitmapSetImage(w1.hwnd, STATIC_1, myBMP)

      CASE @IDCONTROL

         SELECT @CONTROLID

CASE STATIC_1
IF @NOTIFYCODE=0
openconsole
closeconsole
endif

         endselect


CASE @IDCLOSEWINDOW
' delete the image
DELETEIMAGE(myBMP, @IMGBITMAP)

CLOSEWINDOW w1
ENDSELECT
ENDSUB

SUB StaticBitmapCreate(HWND parent, int x, int y, int dimx, int dimy, int id), INT
INT dwStyles = WS_CHILD | WS_VISIBLE | SS_LEFT | SS_BITMAP | SS_NOTIFY
    RETURN CreateWindowEx(0, "STATIC", "", dwStyles, x, y, dimx, dimy, parent, id, GetModuleHandle(0), 0)
ENDSUB

SUB StaticBitmapSetImage(HWND parent, INT id, HBITMAP imagen)
HWND handle = GetDlgItem(parent, id)
_SendMessage(handle, STM_SETIMAGE, IMAGE_BITMAP, imagen)
ENDSUB


Fantastic!!!
8)
Day after day, day after day, we struck nor breath nor motion, as idle as a painted ship upon a painted ocean.

fasecero

Well, you guys gave me something play with. I'm assuming that the image content is not rectangular and the image background color matches the window back color (white). The following code informs the user that something will happens when clicking by changing the cursor to a HAND, and only when the cursor is not over the image empty area.



$INCLUDE "windowssdk.inc"

CONST STATIC_1 = 1
INT staticBackColor = RGB(255,255,255) ' image background color
INT myBMP
WINDOW w1

OPENWINDOW w1,0,0,600,400,@MINBOX|@MAXBOX|@SIZE,NULL,"Simple Window",&w1_handler
WAITUNTIL w1 = 0
END

SUB w1_handler
SELECT @MESSAGE
CASE @IDCREATE
CENTERWINDOW w1

' load the image - create the static - set the image
myBMP = LOADIMAGE (GETSTARTPATH() + "myimage.bmp", @IMGBITMAP)
StaticBitmapCreate(w1.hwnd, 100, 40, 256, 256, STATIC_1)
StaticBitmapSetImage(w1.hwnd, STATIC_1, myBMP)

      CASE @IDCONTROL
         SELECT @CONTROLID
CASE STATIC_1
IF @NOTIFYCODE=0
openconsole
closeconsole
endif
         endselect

CASE WM_SETCURSOR
INT hwndST = GetDlgItem(w1.hwnd, STATIC_1)

IF @WPARAM = hwndST THEN
HDC hdc = GetDC(hwndST)

POINT p
GetCursorPos(p)
ScreenToClient(hwndST, p)

COLORREF colour = _GetPixel(hdc, p.x, p.y)

IF colour <> staticBackColor THEN
SetMainCursor(LoadCursor(NULL, IDC_HAND))
ReleaseDC(hwndST, hdc)
RETURN TRUE
ENDIF

ReleaseDC(hwndST, hdc)
ENDIF

CASE @IDCLOSEWINDOW
' delete the image
DELETEIMAGE(myBMP, @IMGBITMAP)

CLOSEWINDOW w1
ENDSELECT
ENDSUB

SUB StaticBitmapCreate(HWND parent, int x, int y, int dimx, int dimy, int id), INT
INT dwStyles = WS_CHILD | WS_VISIBLE | SS_LEFT | SS_BITMAP | SS_NOTIFY
    RETURN CreateWindowEx(0, "STATIC", "", dwStyles, x, y, dimx, dimy, parent, id, GetModuleHandle(0), 0)
ENDSUB

SUB StaticBitmapSetImage(HWND parent, INT id, HBITMAP imagen)
HWND handle = GetDlgItem(parent, id)
_SendMessage(handle, STM_SETIMAGE, IMAGE_BITMAP, imagen)
ENDSUB

DECLARE SetCursorTemplate(HCURSOR cursor), HCURSOR

SUB SetMainCursor(HCURSOR cursor)
UINT hInst=LoadLibrary("user32.dll")

IF hInst THEN
UINT proc = GetProcAddress(hInst, "SetCursor")
IF proc THEN !<SetCursorTemplate>proc(cursor)
FreeLibrary(hInst)
ENDIF
ENDSUB


LarryMc

If I just wanted a static control with a bitmap instead of text and I wanted it to respond to a button click then my code (based on reading the help file) would look like this:
$INCLUDE "windowssdk.inc"

CONST STATIC_1 = 1
WINDOW w1

OPENWINDOW w1,0,0,600,400,@MINBOX|@MAXBOX|@SIZE,0,"Simple Window",&w1_handler
CONTROL w1,@static,"",100, 40, 256, 256,@TABSTOP|@SS_NOTIFY|@CTLSTCBITMAP ,STATIC_1
SETCONTROLTEXT w1,STATIC_1, GETSTARTPATH + "tabclose.bmp"
WAITUNTIL w1 = 0
END

SUB w1_handler(),int
SELECT @MESSAGE
CASE @IDCREATE
CENTERWINDOW w1

      CASE @IDCONTROL

         SELECT @CONTROLID

CASE STATIC_1
IF @NOTIFYCODE =0
messagebox 0,"it works","",0
endif

         endselect

CASE @IDCLOSEWINDOW
CLOSEWINDOW w1
ENDSELECT
return 0
ENDSUB
LarryMc
Larry McCaughn :)
Author of IWB+, Custom Button Designer library, Custom Chart Designer library, Snippet Manager, IWGrid control library, LM_Image control library

Brian

Good stuff, Larry - that's the KISS method!

Brian

Egil

QuoteGood stuff, Larry - that's the KISS method!

I second that!
There is one thing I don't understand though, the Static Control is defined to be 256 by 256 pixels. Therefore I expected the whole Static Control to be "active" when clicking on it. But when I tried  with a much smaller bitmap  (20x16 flag bitmap), only the program only responds when the cursor is placed inside the small bitmap.
ther must be something I have missed.... ???


Egil
Support Amateur Radio  -  Have a ham  for dinner!

Andy

Egil,

I have been playing around with this, and I have found that the control is automatically set to the size of the image.

So it looks like you have to set the image size, and that will make the control the same size - or at least that's how I see it at the moment.

Andy.
Day after day, day after day, we struck nor breath nor motion, as idle as a painted ship upon a painted ocean.

fasecero

July 06, 2018, 12:42:11 PM #11 Last Edit: July 06, 2018, 12:45:16 PM by fasecero
Very good indeed, Larry! Sometimes I'm so deep into the winapi stuff that I tend to forget about the built-in functions a lot. Good call :)

fasecero

Andy, I have looked at msdn and found the following
https://docs.microsoft.com/en-us/windows/desktop/controls/static-control-styles


SS_BITMAP
The style ignores the nWidth and nHeight parameters; the control automatically sizes itself to accommodate the bitmap.