I am trying (so far without much luck) to create a drag image from text. ListViews and Treeviews have a command to create the image from an item. I would like to replicate that behavior from a hard-coded text.
This is what I have so far after a few days of working on and off this project:
The user should be able to drag an image from the top left corner. This image is displayed on the right-hand side of the dialog. If you click the button, then the drag image will switch to be a hand icon. The purpose of the hand icon is to show that an image can get there--just not the text image. The purpose of the "hello" image on the right is to show that the image is indeed being drawn to the DC. The problem is that the image always shows up black. If I change:
to
then it will display the transparency I am looking for, but still no text.
Any thought, advice, or prayers would be appreciated. InThanksAdvance!
This is what I have so far after a few days of working on and off this project:
Code:
#PBFORMS CREATED V1.51 #COMPILE EXE #DIM ALL '------------------------------------------------------------------------------ ' ** Includes ** '------------------------------------------------------------------------------ #PBFORMS BEGIN INCLUDES #IF NOT %DEF(%WINAPI) #INCLUDE "WIN32API.INC" #ENDIF #IF NOT %DEF(%COMMCTRL_INC) #INCLUDE "COMMCTRL.INC" #ENDIF GLOBAL SwitchtoHand AS LONG #PBFORMS END INCLUDES '------------------------------------------------------------------------------ '------------------------------------------------------------------------------ ' ** Constants ** '------------------------------------------------------------------------------ #PBFORMS BEGIN CONSTANTS %IDD_DIALOG1 = 101 %IDC_BUTTON1 = 1001 #PBFORMS END CONSTANTS '------------------------------------------------------------------------------ '------------------------------------------------------------------------------ ' ** Declarations ** '------------------------------------------------------------------------------ DECLARE CALLBACK FUNCTION ShowDIALOG1Proc() DECLARE FUNCTION ShowDIALOG1(BYVAL hParent AS DWORD) AS LONG #PBFORMS DECLARATIONS '------------------------------------------------------------------------------ '------------------------------------------------------------------------------ ' ** Main Application Entry Point ** '------------------------------------------------------------------------------ FUNCTION PBMAIN() ShowDIALOG1 %HWND_DESKTOP END FUNCTION '------------------------------------------------------------------------------ '------------------------------------------------------------------------------ ' ** CallBacks ** '------------------------------------------------------------------------------ CALLBACK FUNCTION ShowDIALOG1Proc() STATIC fDrag, hfont, hfont2, hbmp AS LONG LOCAL retval AS LONG LOCAL rc AS RECT STATIC pt AS POINTAPI STATIC himg AS LONG '' STATIC himg2 AS LONG STATIC ptIcon AS POINTAPI STATIC cx AS LONG STATIC cy AS LONG STATIC hdc AS DWORD LOCAL ps AS PAINTSTRUCT LOCAL szText AS ASCIIZ * 50 STATIC memDC, memBMP AS LONG LOCAL oldBMP AS LONG LOCAL dtVal AS LONG LOCAL hBrush AS LONG STATIC imageNumToLoad AS LONG SELECT CASE AS LONG CBMSG CASE %wm_initdialog imageNumToLoad = 0 hBrush = CreateSolidBrush(%WHITE) 'himg = ImageList_Create(100, 16, %ILC_COLOR , 1, 1) himg = ImageList_Create( 64, 64, %ILC_COLOR , 1, 1 ) ImageList_SetBKColor hImg, %CLR_NONE memDC = CreateCompatibleDC(GetDC(CBHNDL)) memBMP = CreateBitmap(64, 64,1,1,BYVAL 0&) SetBKMode memDC, %TRANSPARENT oldBMP = SelectObject(memDC, memBMP) PatBlt memDC, 0, 0, 64, 64, %WHITENESS rc.nLeft = 0: rc.nTop = 0: rc.nRight = 64: rc.nBottom = 64 szText = "Hello" 'this is using drawtext 'rc.nleft = 0 'rc.nTop = 0 'rc.nBottom = 64 'rc.nRight = 64 'dtVal = DrawText(memDC, szText, len(szText),rc, %DT_CENTER) TextOut memDC, 1, 1, szText, LEN(szText) retval = ImageList_Add(himg, memBmp, 0) 'other ways to add this bitmap--none seem to work 'retval = ImageList_AddMasked(himg2, memBmp, hBrush) 'retval = ImageList_AddMasked( hImg, memBMP, GetSysColor(%COLOR_WINDOW)) 'Add the hand icon for testing retval = LoadCursor( %NULL, BYVAL %IDC_HAND ) ImageList_AddIcon himg, retval 'clean up (commented out for now) 'IF SelectObject(memDC, oldBMP) THEN ' DeleteObject hBmp 'END IF CASE %wm_paint 'use this to get the image out of the ImageList for debugging 'retval = ImageList_Draw(himg, 0, memDC,0,0, %ILD_TRANSPARENT ) 'show the image hDC = BeginPaint(CBHNDL, ps) BitBlt hDC, 10, 100, 64, 64, memDC, 0, 0, %SRCCOPY EndPaint CBHNDL, ps FUNCTION = 0 EXIT FUNCTION CASE %wm_destroy ReleaseDC CBHNDL, hDC CASE %wm_ncactivate STATIC hWndSaveFocus AS DWORD IF ISFALSE CBWPARAM THEN ' Save control focus hWndSaveFocus = GetFocus() ELSEIF hWndSaveFocus THEN ' Restore control focus SetFocus(hWndSaveFocus) hWndSaveFocus = 0 END IF CASE %wm_lbuttonup '// release the mouse CALL ReleaseCapture() ImageList_EndDrag() CASE %wm_lbuttondown '// determine the current mouse position pt.x = LOWRD( CBLPARAM) pt.y = HIWRD( CBLPARAM) '// determine the icon rectangle rc.nleft = ptIcon.x rc.ntop = ptIcon.y rc.nright = ptIcon.x + 64 rc.nbottom = ptIcon.y + 64 '// evaluate if we clicked on the icon IF PtInRect( rc, pt.x,pt.y ) = %TRUE THEN '// calculate the offset from the upper left corner cx = pt.x - ptIcon.x cy = pt.y - ptIcon.y '// get the imagelist handle ' himg = GetWindowLong( hwnd, %GWL_USERDATA) '// convert the cursor position in screen coordinates CALL ClientToScreen( CBHNDL, pt) '// define the icon attributes CALL ImageList_BeginDrag( himg, imageNumToLoad, cx, cy) '// enter dragging mode CALL ImageList_DragEnter( %NULL, pt.x, pt.y) '// set the drag flag to TRUE fDrag = 1 '// capture the mouse to drag the icon everywhere on the screen CALL SetCapture( CBHNDL) END IF CASE %wm_mousemove '// skip if we are not dragging the icon IF fDrag = 1 THEN pt.x = LOWRD(CBLPARAM) pt.y = HIWRD(CBLPARAM) '// convert the cursor position in screen coordinates CALL ClientToScreen( CBHNDL, pt) '// drag the icon CALL ImageList_DragMove( pt.x, pt.y) END IF CASE %wm_command ' Process control notifications SELECT CASE AS LONG CBCTL CASE %IDC_BUTTON1 IF CBCTLMSG = %bn_clicked OR CBCTLMSG = 1 THEN IF switchtoHand = 0 THEN CONTROL SET TEXT CBHNDL, %idc_button1, "switch to text" switchtoHand = 1 imageNumToLoad = 1 ELSE CONTROL SET TEXT CBHNDL, %idc_button1, "switch to hand" switchtoHand = 0 imageNumToLoad = 0 END IF END IF END SELECT END SELECT END FUNCTION '------------------------------------------------------------------------------ '------------------------------------------------------------------------------ ' ** Dialogs ** '------------------------------------------------------------------------------ FUNCTION ShowDIALOG1(BYVAL hParent AS DWORD) AS LONG LOCAL lRslt AS LONG #PBFORMS BEGIN DIALOG %IDD_DIALOG1->-> LOCAL hDlg AS DWORD DIALOG NEW hParent, "Dialog1", 239, 270, 200, 120, %ws_popup OR _ %ws_border OR %ws_dlgframe OR %ws_sysmenu OR %ws_clipsiblings OR _ %ws_visible OR %ds_modalframe OR %ds_3dlook OR %ds_nofailcreate OR _ %ds_setfont, %ws_ex_controlparent OR %ws_ex_left OR _ %ws_ex_ltrreading OR %ws_ex_rightscrollbar, TO hDlg CONTROL ADD BUTTON, hDlg, %IDC_BUTTON1, "Switch to hand", 50, 55, 75, 55 #PBFORMS END DIALOG DIALOG SHOW MODAL hDlg, CALL ShowDIALOG1Proc TO lRslt #PBFORMS BEGIN CLEANUP %IDD_DIALOG1 #PBFORMS END CLEANUP FUNCTION = lRslt END FUNCTION
Code:
himg = ImageList_Create( 64, 64, %ILC_COLOR , 1, 1 )
Code:
himg = ImageList_Create( 64, 64, %ILC_MASK, 1, 1 )
Any thought, advice, or prayers would be appreciated. InThanksAdvance!
Comment