How can I detect that the left mouse button is pressed when over an ownerdrawn button, so as to change the icon? Tried DRAWITEMSTRUCT.itemaction = ODA_SELECT and DRAWITEMSTRUCT.itemstate = ODS_SELECTED without luck. Or is it necessary to do it under WM_LBUTTONDOWN?
Announcement
Collapse
No announcement yet.
ownerdraw button icon detect selected
Collapse
X
-
How can I detect that the left mouse button is pressed when over an ownerdrawn button
"Pressed" as in the mouse had been captured and is in a drag operation, or "clicked" as in "depressed when located over the control?"
If the latter, you should get a WM_COMMAND/BN_CLICK notification... but I think you should also be getting a WM_DRAWITEM at that time.
But if you are getting WM_DRAWITEM, you can use GetCursorPos() to find the mouse position. I can't find a function like GetKeyState for the mouse ("is mouse button down?") but I have to believe there is one, so then you would know where the mouse is and if it's depressed on each WM_DRAWITEM and draw accordingly.
Or maybe you can hot-track it so in your DRAWITEMSTRUCT you can query:
ODS_HOTLIGHT
Windows 98/Me, Windows 2000/XP: The item is being hot-tracked, that is, the item will be highlighted when the mouse is on the item
There's also a bunch of other mouse functions which might be handy eg getMouseMovePointsEx, TrackMouseEvent.
MCMMichael Mattias
Tal Systems (retired)
Port Washington WI USA
[email protected]
http://www.talsystems.com
-
Well I had the right conditions after all, but was confusing myself nicely by using a transparent icon.
Here is some code which works with PBWin v9 (and probably v8 too). It includes BINBAS statements to put the icons into code, rather than using a resource file or loading icons from disc. But you ignore all that stuff and load the icon any way you want to, the WM_DRAWITEM handler will be the same -unless you can improve on it, which would be even better!
Just don't mention the icon quality.
Code:' ' DDT example using an icon on an ownerdrawn button. ' no image files, no resource! ' Chris Holbrook Oct 2008 ' ' uses Edwin Knoppert's BINBAS to store the images in code ' you can download it from http://www.hellobasic.com/download.aspx ' see examples in PowerBASIC source code forum ' #COMPILE EXE #DIM ALL #INCLUDE "WIN32API.INC" %IDC_DONE_BN = 1001 %IDC_LABEL1 = 1002 '--------------------------------------------------------------- '////////////////////////////////////////////////////////////////////////// '// Icon loading '////////////////////////////////////////////////////////////////////////// TYPE TAGICONDIR idReserved AS WORD '// Reserved (must be 0) idType AS WORD '// Resource Type (1 For icons) idCount AS WORD '// How many images? END TYPE TYPE TAGICONDIRENTRY bWidth AS BYTE '// Width, In Pixels, of the Image bHeight AS BYTE '// Height, In Pixels, of the Image bColorCount AS BYTE '// Number of colors In Image (0 If >=8bpp) bReserved AS BYTE '// Reserved ( must be 0) wPlanes AS WORD '// Color Planes wBitCount AS WORD '// Bits per pixel dwBytesInRes AS DWORD '// How many bytes In this resource? dwImageOffset AS DWORD '// Where In the file is this image? END TYPE '// Creates an icon using plain filedata, like the 766 Bytes .ICO files. '// Returns a iconhandle. FUNCTION SetIconFileBits( BYVAL lpMem AS LONG ) AS LONG DIM pIconDir AS TAGICONDIR PTR DIM IconDirEntry AS TAGICONDIRENTRY PTR pIconDir = lpMem IF @pIconDir.idCount < 1 THEN EXIT FUNCTION IconDirEntry = pIconDir + LEN( @pIconDir ) FUNCTION = CreateIconFromResource( _ BYVAL pIconDir + @IconDirEntry.dwImageOffset _ , @IconDirEntry.dwBytesInRes _ , @pIconDir.idType _ , &H30000& _ ) END FUNCTION '------------------------------------------------------------ MACRO mBinDataStuff LOCAL a AS LONG LOCAL t, t2 AS STRING FOR a = 1 TO DATACOUNT: T = T & READ$( a ): NEXT a FOR a = 1 TO LEN( T ) STEP 2 T2 = T2 & CHR$( VAL( "&H" & MID$( T, a , 2 ) ) ) NEXT a FUNCTION = STRPTR(T2) END MACRO '------------------------------------------------------------------------------- ' X icon for DONE button via BINBAS - NORMAL FUNCTION BinBasnormal( ) AS DWORD mBinDataStuff DATA 0000010001001010100001000400280100001600000028000000100000002000000001 DATA 000400000000000000000000000000000000000000000000000000FFFFFF000000FF00 DATA 0000000000000000000000000000000000000000000000000000000000000000000000 DATA 0000000000000000000000000000000000000000000000000000000000010000000000 DATA 0010001000000000010000010000000010000000100000010000000001000010000000 DATA 0000100100000000000001100000000000000110000000000000100100000000000100 DATA 0010000000001000000100000001000000001000001000000000010001000000000000 DATA 1000000000000000000000000000000000000000000000000000000000000000000000 DATA 0000000000000000000000000000000000000000000000000000000000000000000000 DATA 000000 END FUNCTION '---------------------------------------------------------------------- ' X icon for DONE button via BINBAS - SELECTED FUNCTION BinBasSelected( ) AS DWORD mBinDataStuff DATA 0000010001001010100001000400280100001600000028000000100000002000000001 DATA 0004000000000000000000000000000000000000000000000000000000FF00FFFFFF00 DATA 0000000000000000000000000000000000000000000000000000000000000000000000 DATA 0000000000000000000000000000000000000000000000000000000000010000000000 DATA 0010001000000000010000010000000010000000100000010000000001000010000000 DATA 0000100100000000000001100000000000000110000000000000100100000000000100 DATA 0010000000001000000100000001000000001000001000000000010001000000000000 DATA 1000000000000000000000000000000000000000000000000000000000000000000000 DATA 0000000000000000000000000000000000000000000000000000000000000000000000 DATA 000000 END FUNCTION '////////////////////////////////////////////////////////////////////////// '-------------------------------------------------------------------- CALLBACK FUNCTION CB AS LONG LOCAL pDIS AS DRAWITEMSTRUCT PTR STATIC hiconnormal, hiconSelected AS LONG ' icon for the IDC_DONE button ' SELECT CASE CBMSG CASE %WM_INITDIALOG ' get icon for the "done" button, both states hIconNormal = SetIconFileBits( Binbasnormal ) hIconSelected = SetIconFileBits( Binbasselected ) ' CASE %WM_DRAWITEM pDIS = CB.LPARAM IF @pDIS.CTlID <> %IDC_DONE_BN THEN EXIT SELECT IF (@pDIS.itemaction = %ODA_SELECT) AND (@pDIS.itemstate AND %ODS_SELECTED) THEN DrawIconEx @pDIS.hDc, 0 , 0, hIconSelected, 16, 16, 0, 0, %DI_NORMAL ELSE DrawIconEx @pDIS.hDc, 0 , 0, hIconNormal, 16, 16, 0, 0, %DI_NORMAL END IF CASE %WM_COMMAND IF CBCTL = %IDC_DONE_BN THEN DIALOG END CBHNDL, 0 END SELECT END FUNCTION '---------------------------------------------------------------------------- FUNCTION PBMAIN () AS LONG LOCAL hdlg AS DWORD DIALOG NEW 0, "test ownerdrawn button with icon", 0, 0, 150, 50 TO hdlg CONTROL ADD BUTTON, hdlg, %IDC_DONE_BN, "",20, 20, 12, 12, %BS_OWNERDRAW OR %WS_CHILD CONTROL ADD LABEL, hdlg, %IDC_LABEL1, "CLOSE", 35, 20, 30, 12 DIALOG SHOW MODAL hdlg CALL cb END FUNCTION
Comment
-
Originally posted by Michael Mattias View PostIf the latter, you should get a WM_COMMAND/BN_CLICK notification... but I think you should also be getting a WM_DRAWITEM at that time.
Comment
Comment