// Our first DialogBox sample
#include "FiveWin.ch"
#include "constant.ch"
#include "FiveWin.ch"
#define HKEY_CLASSES_ROOT 2147483648 // 0x80000000
Function Main ()
local ownd
local oimage
local oPreview
DEFINE WINDOW oWnd TITLE "Testing hicon to bmp " FROM 5,5 TO 400, 600 PIXEL
@ 20,2 button "crear" size 40,20 pixel Action GetFiletoview( oImage ) //imagenfromico( oimage )
@ 20 ,130 Button "salir" Action ownd:end() size 40,20 pixel
@ 13, 200 IMAGE oimage FILE "" size 256,256 of oWnd pixel
ACTIVATE WINDOW oWnd
Return (nil)
Function GetFiletoview( oImage )
local oPreview
local cFile:= cGetfile("coge","*.*")
local cExt:= cFileExt( cfile)
if cExt == "pdf"
oImage:Hide()
oPreview := TPreviewControl():New(13,200,oImage:oWnd, 255,255)
// oPreview:AcceptDropFile()
oImage:oWnd():refresh()
oPreview:SetFile( cfile )
else
if !Empty(oPreview)
oPreview:hide()
oPreview:clear()
oPreview:destroy()
oPreview:end()
oPreview:= nil
endif
oImage:show()
oImage:oWnd():refresh()
imagenfromico( oImage, cfile )
endif
Return nil
Function imagenfromico( oImage, cfile )
//local cFile:= cGetfile("coge","*.*")
local ico
ico := ICON_EXEREAD( cFile, 4 )
// msginfo(ico)
gdiBmpFromHIcon( ico, oImage , oImage:Super:nWidth(), oImage:Super:nHeight() )
DestroyIcon( ico )
Return nil
Function GDIBmpFromHIcon( hIcon , oImage , nWidth, nHeight )
//local hBmp := BmpFromIcon( hIcon )
local oGBmp := GdiBmp():new()
oGBmp:hBmp:= GDIPLUSIMAGEFROMICO( hIcon )
DeleteObject( oImage:hBitmap )
oGbmp:Resize( nWidth, nHeight )
// DeleteObject( hBmp )
oImage:hBitmap:= oGBmp:GetGDIHbitmap()
oImage:HasAlpha()
oImage:Refresh()
oGBmp:end()
Return nil
//--------------------------------------------------------------------
CLASS TPreviewControl FROM TControl
CLASSDATA lRegistered AS LOGICAL
DATA pPreview INIT 0
DATA pStream INIT 0
DATA cID
DATA cFile
METHOD New( nRow, nCol, oWnd, nWidth, nHeight ) CONSTRUCTOR
METHOD Destroy()
METHOD SetFile(cFile)
METHOD AcceptDropFile()
METHOD Clear()
METHOD UnloadPreview INLINE UnloadPreviewC ( ::pPreview )
METHOD SeekCLSString( cFile )
METHOD CreatehPreview() INLINE ( ::pPreview := CreatehPreviewC( ::cID ) )
METHOD AddPreview() INLINE AddPreviewC( ::cFile , ::pPreview )
METHOD Show()
METHOD DropFiles( hDrop )
METHOD Resize()
METHOD Prev_DoPreview() INLINE PrevDoPreview( ::pPreview )
METHOD Prev_SetWindow() INLINE PrevSetWindow( ::hWnd, GetClientRect( ::hWnd ), ::pPreview )
METHOD Display() INLINE ::BeginPaint(), ::Paint(), ::EndPaint(), 0
ENDCLASS
METHOD New( nRow, nCol, oWnd, nWidth, nHeight ) CLASS TPreviewControl
DEFAULT nRow := 10, nCol := 10, oWnd := GetWndDefault(), nWidth := 64, nHeight := 64
CoInitialize()
::oWnd := oWnd
::nId := ::GetNewId()
::nStyle := nOR( WS_CHILD, WS_VISIBLE, WS_TABSTOP, WS_CLIPCHILDREN )
::nTop := nRow
::nLeft := nCol
::nBottom := ::nTop + nHeight - 1
::nRight := ::nLeft + nWidth
::Register( nOR( CS_VREDRAW, CS_HREDRAW ) )
::Create()
oWnd:AddControl( Self )
return Self
//-------------------------------------------------------
METHOD Destroy() CLASS TPreviewControl
::Clear()
return ::Super:Destroy()
//-------------------------------------------------------
METHOD Show() CLASS TPreviewControl
::AddPreview()
::Prev_SetWindow()
::Prev_DoPreview()
Return nil
//-------------------------------------------------------
METHOD Clear() CLASS TPreviewControl
if valtype(::pPreview) == "O"
::UnloadPreview()
endif
if Valtype(::pStream) == "P"
// DestroyStream(::pStream)
endif
::pPreview := 0
::pStream := 0
return nil
*/
METHOD SetFile( cFile ) CLASS TPreviewControl
::Clear()
::cFile := cFile
::SeekCLSString( cFile )
::CreatehPreview()
::show()
return self
//----------------------------------------------------------------------------
METHOD SeekCLSString( cFile ) CLASS TPreviewControl
local cExtension
local cKey := ""
local oReg
local cShellExe := "\ShellEx\{8895b1c6-b41f-4c1c-a562-0d564250836f}"
local cValue
if !Empty( cFile )
cExtension := cFileExt( cFile )
if Empty( cExtension )
MsgInfo( "not Extension File" )
Return nil
endif
cExtension := "."+ cExtension
cKey := cExtension + cShellExe
// msginfo(cKey)
oReg := TReg32():New( HKEY_CLASSES_ROOT, ckey )
cValue := oReg:Get()
if Empty( cValue )
oreg:Close()
// msginfo(cKey)
oReg := TReg32():New( HKEY_CLASSES_ROOT, cExtension )
cValue := oReg:Get()
if Empty( cValue )
oReg:Close()
MsgInfo( "Clave no encontrada" )
Return nil
endif
cKey := cValue + cShellExe
// msginfo(cKey)
oReg := TReg32():New( HKEY_CLASSES_ROOT, cKey )
cValue := oReg:Get()
if Empty( cValue )
MsgInfo( "Clave no encontrada" )
Return nil
endIf
endif
endif
::cID := cValue
Return nil
METHOD Resize()
SETRECTPREVIEWC( ::pPreview , GetClientRect( ::hWnd ) )
::refresh()
return self
METHOD AcceptDropFile() CLASS TPreviewControl
DragAcceptFiles( ::hWnd, .T. )
return Self
METHOD DropFiles( hDrop ) CLASS TPreviewControl
LOCAL files
files = DragQueryFiles( hDrop )
::SetFile( files[1])
return Self
//----------------------------------------------------------------------------
#pragma BEGINDUMP
#define CINTERFACE
#define COBJMACROS
#if ( __BORLANDC__ <= 0x582 )
#include <Shextra.h>
#endif
#include <windows.h>
#include <hbapi.h>
#include <CommCtrl.h>
#include <ShObjIdl.h>
//#include <Shlwapi.h>
//#include <ShlGuid.h>
LPWSTR UTF8toUTF16( LPCSTR utf8 );
LPWSTR AnsiToWide( LPSTR cAnsi );
const IID IID_IPreviewHandler = {0x8895b1c6, 0xb41f, 0x4c1c, {0xa5, 0x62,
0x0d, 0x56, 0x42, 0x50, 0x83, 0x6f}};
EXTERN_C const IID IID_IInitializeWithFile = {0xb7d14566, 0x0509, 0x4cce, {0xa7,
0x1f, 0x0a, 0x55, 0x42, 0x33, 0xbd, 0x9b}};
EXTERN_C const IID IID_IInitializeWithStream =
{ 0xB824B49D, 0x22AC, 0x4161, { 0xAC, 0x8A, 0x99, 0x16, 0xE8, 0xFA, 0x3F, 0x7F } };
//------------------------------------------------
HB_FUNC( CREATEHPREVIEWC ) // clsStr
{
CLSID cls;
IPreviewHandler* pRet=0;
CLSIDFromString( AnsiToWide(( char * ) hb_parc( 1 )),&cls);
if( CoCreateInstance( &cls, 0, CLSCTX_INPROC_SERVER |CLSCTX_LOCAL_SERVER, &IID_IPreviewHandler, ( void ** ) &pRet ) ==S_OK )
{
hb_retptr(pRet);
return ;
}
// MessageBoxW(0,cKey,cExtension,MB_OK);
hb_retptr( 0 );
return ;
}
//---------------------------------------------------
HB_FUNC( ADDPREVIEWC ) // cFileName,pPreview
{
IPreviewHandler* pPreview = ( IPreviewHandler* )hb_parptr(2);
LPWSTR cFileName = AnsiToWide(( char * ) hb_parc( 1 ));
LPCWSTR cExtension = wcsrchr(cFileName,L'.');
IInitializeWithFile* pFile;
IInitializeWithStream* pIStream;
if(pPreview==0)
{
MessageBoxW(0,L"no pPreview",cExtension,MB_OK);
hb_retptr( pPreview );
return;
}
IPreviewHandler_QueryInterface(pPreview, &IID_IInitializeWithFile,(void**)&pFile );
IPreviewHandler_QueryInterface(pPreview, &IID_IInitializeWithStream,(void**)&pIStream );
if(pFile==0 && pIStream==0)
{
MessageBoxW(0,L"no pfile no pStream",cExtension,MB_OK);
IPreviewHandler_Unload(pPreview);
pPreview = 0;
hb_retptr( pPreview );
return;
}
if(pFile!=0)
{
IInitializeWithFile_Initialize(pFile, cFileName, STGM_READ);
} else
{ //pIStream!=0
HANDLE hFile = CreateFileW(cFileName,FILE_READ_DATA,FILE_SHARE_READ,NULL,OPEN_EXISTING,0,NULL );
if( INVALID_HANDLE_VALUE != hFile )
{
DWORD dwSize = GetFileSize( hFile, NULL );
HGLOBAL hGlobal= GlobalAlloc(GPTR, dwSize );
BYTE * pByte = (BYTE *)GlobalLock(hGlobal);
if( pByte )
{
LPSTREAM pStream;
ReadFile(hFile,pByte,dwSize,&dwSize,NULL);
GlobalUnlock(hGlobal);
CreateStreamOnHGlobal(hGlobal, TRUE, &pStream);
IInitializeWithStream_Initialize(pIStream, pStream, STGM_READ);
}
CloseHandle( hFile );
}
}
hb_xfree( cFileName );
hb_retptr( pPreview );
return;
}
//----------------------------------------------------------------
HB_FUNC( PREVSETWINDOW ) //pPreview
{
IPreviewHandler* pPreview = ( IPreviewHandler* )hb_parptr(3);
#ifndef _WIN64
HWND hWnd = ( HWND ) hb_parnl( 1 );
#else
HWND hWnd = ( HWND ) hb_parnll( 1 );
#endif
RECT rectPreview;
if( pPreview )
{
rectPreview.top = hb_parvni(2,1);
rectPreview.left = hb_parvni(2,2);
rectPreview.bottom = hb_parvni(2,3);
rectPreview.right = hb_parvni(2,4);
IPreviewHandler_SetWindow( pPreview, hWnd, &rectPreview );
}
}
//----------------------------------------------------------------
HB_FUNC( PREVDOPREVIEW ) //pPreview
{
IPreviewHandler* pPreview = ( IPreviewHandler* )hb_parptr(1);
if( pPreview )
{
IPreviewHandler_DoPreview( pPreview );
}
}
//----------------------------------------------------------------
HB_FUNC( UNLOADPREVIEWC ) //pPreview
{
IPreviewHandler* pPreview = ( IPreviewHandler* )hb_parptr(1);
if( pPreview )
{
IPreviewHandler_Unload(pPreview);
}
}
//----------------------------------------------------------------
HB_FUNC( SETRECTPREVIEWC ) //pPreview
{
IPreviewHandler* pPreview = ( IPreviewHandler* )hb_parptr(1);
RECT rectPreview;
if( pPreview )
{
rectPreview.top = hb_parvni(2,1);
rectPreview.left = hb_parvni(2,2);
rectPreview.bottom = hb_parvni(2,3);
rectPreview.right = hb_parvni(2,4);
IPreviewHandler_SetRect( pPreview , &rectPreview );
}
}
//------------------------------------------------------------------
HB_FUNC( GETSTREAM ) // cFileName
{
wchar_t* cFileName = UTF8toUTF16(hb_parc(1));
HANDLE hFile = CreateFileW(cFileName,FILE_READ_DATA,FILE_SHARE_READ,NULL,OPEN_EXISTING,0,NULL );
DWORD dwSize = GetFileSize( hFile, NULL );
HGLOBAL hGlobal= GlobalAlloc(GPTR, dwSize );
BYTE * pByte = (BYTE *)GlobalLock(hGlobal);
LPSTREAM pStream;
ReadFile(hFile,pByte,dwSize,&dwSize,NULL);
GlobalUnlock(hGlobal);
CloseHandle( hFile );
CreateStreamOnHGlobal(hGlobal, TRUE, &pStream);
hb_xfree( (void*) cFileName);
hb_retptr(pStream);
}
HB_FUNC( DESTROYSTREAM ) // stream
{
LPSTREAM pStream = hb_parptr(1);
HGLOBAL hGlobal;
if( pStream )
{
GetHGlobalFromStream(pStream,&hGlobal);
if(hGlobal)
GlobalFree(hGlobal);
IUnknown_Release(pStream);
}
hb_ret();
}
#pragma ENDDUMP