******************************************************************************** * Program : TRANSWND.PRG * Launch TRANSWND.EXE * Aim : Show how to program Applications using Windows traditional manner, * ..........: with NTKCORE GUI (low level) set of functions for X/HARBOUR. * ..........: This sample creates a TRANSPARENT WINDOW using the Win32 SDK's way of doing things ... * ..........: It can also easily be adapted to NTKRAD's way of programming... * Make......: Juste type MK TRANSWND * Date : 08/08/06 * Author(s) : Jn DECHEREUX * Copyright : (c) 2006 - Jn DECHEREUX. Tous droits r‚serv‚s/All Rights Reserved. ******************************************************************************** * Remark: * I have to confess that English is not mother tongue. So, in case of * typo or misspelling, don't be too angry - just bear with me. * You have to know that i'll do consider any comment, correction, suggestion * with great pleasure and high kindliness. Just be polite ! ;-) * This remark is also available for bug notifications or any other kind of * contributions that can improve my product... ******************************************************************************** * ********************************************************************************** // Harbour includes #include "cstruct.ch" // NTK includes //#include "wintypes.ch" #include "windows.ch" #include "ntkdll.ch" #include "ntkgdi.ch" #include "ntkmsg.ch" //#include "ntkacc.ch" #include "ntkcctrl.ch" //#include "ntkttips.ch" #include "wNtk.ch" #define CR CHR(13) #define CRLF CHR(13)+CHR(10) #define TAB CHR(9) // Now integrated into Window.ch //#define WS_EX_LAYERED 524288 //#define LWA_COLORKEY 1 //#define LWA_ALPHA 2 //#define RDW_INVALIDATE 1 // 0x1 //#define RDW_ERASE 4 // 0x4 //#define RDW_ALLCHILDREN 128 // 0x80 //#define RDW_FRAME 1024 // 0x400 FUNCTION MAIN() LOCAL cMsg LOCAL NTK_aMSG := { 0,0,0,0,0,0,0 } // 7 elements, see NTKMSG.CH LOCAL hInst := NTK_GetInstance() LOCAL hIcon := NTK_LoadIcon( Nil , IDI_HAND ) // IDI_APPLICATION) LOCAL hCurs := NTK_LoadCursor( Nil , IDC_ARROW) LOCAL hBrush := NTK_GetStockObject(WHITE_BRUSH) //LTGRAY_BRUSH) LOCAL cWinTitle := "Transparent window & child controls with NTKCore for X/HRB !" LOCAL hWndDemo PRIVATE aDemoFont1 PRIVATE hDemoFont1 PRIVATE hMyTB // ----- Ensure that the common control dynamic-link library (DLL) is loaded. // ----- Don't forget to use this command if U plan to use controls such as DatePicker, TRANSWNDs... NTK_InitCommonControlsEx( ICC_DATE_CLASSES + ICC_BAR_CLASSES + ICC_STANDARD_CLASSES + ICC_WIN95_CLASSES ) IF !NTK_RegisterClassEx( CS_HREDRAW + CS_VREDRAW,; // CS_OWNDC + CS_VREDRAW + CS_HREDRAW + CS_DBLCLKS,; hInst,; hIcon,; hCurs,; hBrush,; "NTKFEN",; { |hWnd, message, nwParam, nlParam|; MAINWNDPROC(hWnd, message, nwParam, nlParam) } ) NTK_MsgBox( , "Can't register NTKFEN class..." ) RETURN Nil ENDIF hWndDemo = NTK_CreateWindowEx( WS_EX_WINDOWEDGE, "NTKFEN", cWinTitle, ; F_MIN+F_MAX+WS_SYSMENU, ; 0, 0, 640, 480 ) //, ; //0, 0, hInst ) //hWndMain, 0, hInst ) // NIL, NIL, NIL, NIL, ; // NIL, NIL, hInst ) IF hWndDemo == 0 NTK_MsgBox( , "Can not create MAIN Window..." ) RETURN Nil ENDIF aDemoFont1 := { -48,12,0,0,700, .F., .F., .F., 1, 0, 0, 0, 0, "ARIAL" } hDemoFont1 := NTK_CreateFont( aDemoFont1 ) IF hDemoFont1 == 0 NTK_MsgBox( , "Can not create DemoFont1..." ) RETURN Nil ENDIF NTK_ShowWindow( hWndDemo, SW_SHOW ) NTK_UpdateWindow( hWndDemo ) // ---------------------------------- NTK's Main Events (Windows Msg) Loop DO WHILE NTK_GetMessage( NTK_aMSG, 0 ) //,hWndDemo,0,0 ) NTK_TranslateMessage( NTK_aMSG ) NTK_DispatchMessage( NTK_aMSG ) ENDDO NTK_DeleteObject( hDemoFont1 ) NTK_SelectWindow( hWndDemo ) NTK_DestroyWindow( hWndDemo ) NTK_UnregisterClass( "NTKFEN", hInst ) RETURN ****** ****** ****** FUNCTION MAINWNDPROC( hWnd, message, nwParam, nlParam) LOCAL nI LOCAL nAlpha LOCAL aPS := { 0,.T.,0,0,0,0,.T.,.T.,nil } // 9 elements, see NTKGDI.CH LOCAL hDC := 0 LOCAL nRed := NTK_RGB(255,0,0) LOCAL nBlue := NTK_RGB(0,0,128) LOCAL nSelMin := 15 // minimum value of TrackBar selection. Do not exceed a 15 percents min limit, otherwise you won't see anything... LOCAL nSelMax := 100 // maximum value of TrackBar selection LOCAL nTBPos // current position of slider DO CASE CASE message == WM_CREATE // CreateMyTrackBar(hWndP, aRect, nMin, nMax, nSelMin, nSelMax, nTbStyle ) hMyTB := CreateMyTrackBar(hWnd, {050,160,500,50}, 0, 100, nSelMin, nSelMax, TBS_AUTOTICKS + TBS_ENABLESELRANGE ) nTBPos:= 100 // Set transparency to none NTK_SendMessage(hMyTB, TBM_SETPOS, .T., nTBPos) // Initializes trackbar gauge to the end CASE message == WM_HSCROLL // nwParam // TrackBar Value // nlParam // handle of TrackBar window // HIWORD(wParam) // scroll box position // The following code simply // ensures that the slider remains within the selection range. hWndTrack := nlParam nTBPos := NTK_SendMessage(hwndTrack, TBM_GETPOS, 0, 0) IF NTK_LoWord(nwParam) == TB_ENDTRACK If (nTBPos > nSelMax) NTK_SendMessage(hwndTrack, TBM_SETPOS,; .T. ,; // redraw flag nSelMax) NTK_MessageBeep(MB_ICONEXCLAMATION) **RemoveTransparency(hWnd) ElseIf (nTBPos < nSelMin) NTK_SendMessage(hwndTrack, TBM_SETPOS,; .T. ,; // redraw flag nSelMin) Endif ELSE SetTransparency(hWnd, (255*nTBPos)/100 ) *SetTransparency(hWnd, nTBPos ) ENDIF // Ask the window and its children to repaint NTK_RedrawWindow( hWnd,; NULL,; NULL,; RDW_ERASE + RDW_INVALIDATE + RDW_FRAME + RDW_ALLCHILDREN ) RETURN 0 CASE message == WM_CHAR IF nwParam==27 // K_ESC IF NTK_MsgBox( hWnd,; "Do you really want to quit ?",; "Your Attention Please !",; MB_OKCANCEL+MB_ICONQUESTION ) == IDOK NTK_PostQuitMessage(0) ENDIF ENDIF RETURN(0) CASE message == WM_RBUTTONDOWN RemoveTransparency(hWnd) RETURN(0) CASE message == WM_SYSCOMMAND IF nwParam == SC_CLOSE // system menu double click, or Alt-F4 IF NTK_MsgBox( hWnd,; "Do you really want to quit ?",; "Your Attention Please !",; MB_OKCANCEL+MB_ICONQUESTION ) == IDOK NTK_PostQuitMessage(0) ENDIF RETURN(0) // According to M$ Win32 SDK Doc. We tell Windows we've processed the message ELSEIF nwParam == SC_MAXIMIZE NTK_MsgBox( hWnd,; "Sound like you're not allowed to maximize this window...",; "Hello, my little boy !", MB_OK+MB_ICONASTERISK ) RETURN(0) // Returning other value than DefWinProc means we don't want to maximize the window... ENDIF CASE message == WM_PAINT hDC := NTK_BEGINPAINT( hWnd, aPS ) NTK_TextOut( hDC, 100, 400, "Press Esc or Alt-F4 to quit..." ) hOldObj := NTK_SelectObject( hDC, hDemoFont1 ) NTK_SetTextColor( hDC, nRed ) NTK_SetBkMode( hDC, TRANSPARENT ) NTK_TextOut( hDC, 50, 100, "Transparency Level:" ) NTK_SelectObject( hDC, hOldObj ) nTBPos := NTK_SendMessage(hMyTB, TBM_GETPOS, 0, 0) NTK_TextOut( hDC, 015, 170, STR(nTBPOS,3,0)+"%" ) NTK_SetTextColor( hDC, nBlue ) NTK_TextOut( hDC, 100, 230, "Hit the Right mouse button to stop the translucent effect..." ) NTK_ENDPAINT( hWnd, aPS ) RETURN(0) CASE message == WM_DESTROY NTK_PostQuitMessage(0) RETURN(0) ENDCASE RETURN( NTK_DEFWNDPROC(hWnd, message, nwParam, nlParam) ) //RETURN -1 // OR NTK_DEFWNDPROC( ... ) is the same ! ****** ****************************************************************************************** ****** ****** ****** /* CREATE A WINDOWS STANDARD PROGRESS BAR CONTROL */ FUNCTION CreateMyTrackBar(hWndP, aRect, nMin, nMax, nSelMin, nSelMax, nTbStyle ) LOCAL hwndTB // handle to the TrackBar control LOCAL g_hInst := NTK_GetInstance() // nMin // minimum value in TrackBar range // nMax // maximum value in TrackBar range // nSelMin // minimum value in TrackBar selection // nSelMax // maximum value in TrackBar selection IF ValType( aRect ) != "A" aRect := NTK_GetClientRect(hWndP) /* If aRect is Nil : GET COORDINATES OF THE MAIN CLIENT AREA */ ENDIF IF ValType( nTbStyle ) != "N" nTbStyle := 0 // Windows classic theme : Default segmented bar. ENDIF hwndTB := NTK_CreateWindowEx( 0 ,; TRACKBAR_CLASS ,; "Track Bar Control" ,; WS_CHILD + WS_VISIBLE + nTbStyle ,; aRect[Rect_Left] ,; aRect[Rect_Top] ,; aRect[Rect_Right] ,; aRect[Rect_Bottom] ,; hwndP ,; NULL ,; g_hinst ,; NULL) IF EMPTY(hwndTB) NTK_MsgBox(hWndP, "Cannot create the TrackBar..." ) ENDIF *// Set the Range NTK_SendMessage(hwndTB, TBM_SETRANGE,; .T.,; // redraw flag NTK_MAKELONG(nMin, nMax) ) // min. & max. positions NTK_SendMessage(hwndTB, TBM_SETPAGESIZE, 0, 4) // new page size NTK_SendMessage(hwndTB, TBM_SETSEL,; .F.,; // redraw flag NTK_MAKELONG(nSelMin, nSelMax) ) NTK_SendMessage(hwndTB, TBM_SETPOS,; .T.,; // redraw flag nSelMin) NTK_SendMessage( hWndTB, TBM_SETTICFREQ,; 5,; // nTickFrequency 0 ) // not used, must be zero NTK_SetFocus(hwndTB); RETURN(hwndTB) ****** ****** ****** FUNCTION SetTransparency(hWnd, nAlpha) // Just a little tweak to set WS_EX_LAYERED exStyle to your window (on fly)... LOCAL Param, lRet IF nAlpha < 0 nAlpha := 0 ELSEIF nAlpha > 255 nAlpha := 255 ENDIF Param := NTK_GetWindowLong( hWnd, GWL_EXSTYLE ) // ----------------------- Set WS_EX_LAYERED on this window styles NTK_SetWindowLong( hWnd, GWL_EXSTYLE, NTK_OR(Param, WS_EX_LAYERED) ) lRet := SetLayeredWindowAttributes(hWnd, 0, CHR(nAlpha), LWA_ALPHA) RETURN(lRet) ********** ********** ********* FUNCTION RemoveTransparency(hWnd) LOCAL Param := NTK_GetWindowLong( hWnd, GWL_EXSTYLE ) // ----------------------- Remove WS_EX_LAYERED from this window styles NTK_SetWindowLong(hWnd, GWL_EXSTYLE, NTK_AND(Param, NTK_NOT(WS_EX_LAYERED)) ) // Ask the window and its children to repaint NTK_RedrawWindow( hWnd,; NULL,; NULL,; RDW_ERASE + RDW_INVALIDATE + RDW_FRAME + RDW_ALLCHILDREN ) RETURN(Nil) ********** ********** ********* ****** SetLayeredWindowAttributes WRAPPER. ****** Inluded: In user32.dll ****** The SetLayeredWindowAttributes function sets the opacity and transparency color key of a layered window. DECLARE FUNCTION SetLayeredWindowAttributes LIB user32.SetLayeredWindowAttributes ; ( hWnd as HWND ,; crKey as COLORREF ,; bAlpha as UINT ,; dwFlags as DWORD ) AS BOOL //hwnd [in] Handle to the layered window. //crKey [in] Pointer to a COLORREF value that specifies the transparency color key to be used. // (When making a certain color transparent...). Use the NTK_RGB(). //bAlpha [in] Alpha value used to describe the opacity of the layered window. // 0 = Invisible, 255 = Fully visible //dwFlags [in] Specifies an action to take. This parameter can be LWA_COLORKEY // (When making a certain color transparent...) or LWA_ALPHA. // //If you want to set the window to 70% visibility, here is how to proceed: //SetLayeredWindowAttributes(hWnd, 0, (255 * 70) / 100, LWA_ALPHA)