CTapiz/CTapiz_mm.cpp
2021-09-12 21:58:47 +02:00

465 lines
12 KiB
C++

//---------------------------------------------------------------------------
#include <registry.hpp>
#include <winuser.h>
#include <stdlib.h>
#include <stdio.h>
#include <vcl.h>
#pragma hdrstop
#include "CTapiz_mm.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "mwajpeg"
#pragma resource "*.dfm"
TCTapiz *CTapiz;
//---------------------------------------------------------------------------
#define GetCurrentDir ExtractFileDir(Application->ExeName)
__fastcall TCTapiz::TCTapiz(TComponent* Owner)
: TForm(Owner)
{
WallPaper_Toolbar = true;
TrayMessage(NIM_ADD);
TrayMessage(NIM_MODIFY);
AllowExit = false;
RefreshYescoge(0);
}
//---------------------------------------------------------------------------
AnsiString TCTapiz::EligeBitmap( AnsiString iPath )
{
AnsiString NombreImagen, Path;
if ( WallPaper_Toolbar )
Path = iPath;
else
Path = iPath + "\\Toolbar";
EligeBitmapR( Path + "\\*.jpg" );
EligeBitmapR( Path + "\\*.bmp" );
// EligeBitmapR( Path + "\\*.gif" );
randomize();
if( ListBox1 -> Items -> Count > 0 )
{
ListBox1->ItemIndex = rand() % ( ListBox1->Items->Count );
NombreImagen = ListBox1->Items->Strings[ListBox1->ItemIndex];
return (Path + "\\" + NombreImagen);
} else
return AnsiString("");
}
//---------------------------------------------------------------------------
void TCTapiz::EligeBitmapR( AnsiString PathC )
{
TSearchRec sr;
int iAttributes = 0xFF;
if (FindFirst( PathC, iAttributes, sr) == 0)
{
if (sr.Attr & iAttributes)
ListBox1 -> Items ->Add( sr.Name );
while (FindNext(sr) == 0)
if (sr.Attr & iAttributes)
ListBox1 -> Items ->Add( sr.Name );
}
FindClose(sr);
};
//---------------------------------------------------------------------------
void TCTapiz::PonTapiz( AnsiString Tapiz )
{
TRegistry *Registro;
Registro = new TRegistry;
AnsiString bStyle;
bStyle = EligeStylo( Tapiz );
try {
Registro -> OpenKey( "\\Control Panel\\Desktop", false );
Registro -> WriteString( "Wallpaper", Tapiz );
Registro -> WriteString( "TileWallpaper", (bStyle == "2") ? "0" : "1" );
Registro -> WriteString( "WallpaperStyle", bStyle );
SystemParametersInfo( SPI_SETDESKWALLPAPER, 0, Tapiz.c_str(), SPIF_UPDATEINIFILE );
SystemParametersInfo( SPI_SETDESKWALLPAPER, 0, Tapiz.c_str(), 0 );
}catch(...){
//do nothing
}
delete Registro;
};
//---------------------------------------------------------------------------
AnsiString TCTapiz::EligeStylo( AnsiString IMG )
{
AnsiString dev;
dev = "0";
if ( Image1->Picture->Width >= 600 && Image1->Picture->Height >= 400 ) dev = "2";
return dev;
}
//---------------------------------------------------------------------------
void __fastcall TCTapiz::ListBox1DblClick(TObject *Sender)
{
if ( !WallPaper_Toolbar && !SwapActive->Checked ) return;
AnsiString Tapiz, Where;
Tapiz = GetCurrentDir + "\\";
AnsiString NImage;
if ( WallPaper_Toolbar )
{
Where = "";
NImage = "ctapiz_JPG2BMP.bmp";
} else {
char name[13];
tmpnam(name);
Where = "ToolBar\\";
NImage = AnsiString( name ) + "_ctapiz_JPG2BMP.bmp";
}
// Si es un JPEG lo convertimos a BMP
if ( (ListBox1->Items->Strings[ ListBox1 -> ItemIndex ].UpperCase()).AnsiPos( ".BMP" ) == 0 )
{
Image1->Picture->SaveToFile( Where + NImage );
Tapiz += ( Where + NImage );
} else
Tapiz += (Where + ListBox1 -> Items -> Strings[ ListBox1 -> ItemIndex ]);
if ( WallPaper_Toolbar )
PonTapiz( Tapiz );
else
PonToolBar( Tapiz );
}
//---------------------------------------------------------------------------
void __fastcall TCTapiz::ListBox1KeyPress(TObject *Sender, char &Key)
{
if ( Key == VK_RETURN )
ListBox1DblClick( 0 );
}
//---------------------------------------------------------------------------
void __fastcall TCTapiz::ListBox1Click(TObject *Sender)
{
// Cargamos la imagen
if ( WallPaper_Toolbar )
Image1->Picture->LoadFromFile( ListBox1 -> Items -> Strings[ ListBox1 -> ItemIndex ] );
else
Image1->Picture->LoadFromFile( "ToolBar\\"+ListBox1 -> Items -> Strings[ ListBox1 -> ItemIndex ] );
Image1->Top = 0;
Image1->Left = 140;
// Vemos si es mayor que el recuadro que la contiene...
if ( Image1->Picture->Height > 200 || Image1->Picture->Width > 200 )
{
// Reescalamos en ese caso
if ( Image1->Picture->Height > Image1->Picture->Width )
{
Image1->Height = 200;
Image1->Width = (Image1->Picture->Width * 200 ) / Image1->Picture->Height;
Image1->Left += ( 200 - Image1->Width ) / 2;
} else {
Image1->Width = 200;
Image1->Height = (Image1->Picture->Height * 200 ) / Image1->Picture->Width;
Image1->Top = ( 200 - Image1->Height ) / 2;
}
} else {
Image1->Height = 200;
Image1->Width = 200;
}
}
//---------------------------------------------------------------------------
void __fastcall TCTapiz::FormDestroy(TObject *Sender)
{
TrayMessage(NIM_DELETE);
}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
void __fastcall TCTapiz::DrawItem(TMessage& Msg)
{
IconDrawItem((LPDRAWITEMSTRUCT)Msg.LParam);
TForm::Dispatch(&Msg);
}
//---------------------------------------------------------------------------
void __fastcall TCTapiz::MyNotify(TMessage& Msg)
{
POINT MousePos;
switch(Msg.LParam)
{
case WM_RBUTTONUP:
if ( GetCursorPos(&MousePos) )
{
PopupMenu1->PopupComponent = CTapiz;
SetForegroundWindow(Handle);
PopupMenu1->Popup(MousePos.x, MousePos.y);
PopupMenu1->PopupComponent = 0;
}
break;
case WM_LBUTTONDBLCLK:
Height = 230;
Visible = true;
break;
default:
break;
}
TForm::Dispatch(&Msg);
}
//---------------------------------------------------------------------------
bool __fastcall TCTapiz::TrayMessage(DWORD dwMessage)
{
NOTIFYICONDATA tnd;
PSTR pszTip;
pszTip = TipText();
tnd.cbSize = sizeof(NOTIFYICONDATA);
tnd.hWnd = Handle;
tnd.uID = IDC_MYICON;
tnd.uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP;
tnd.uCallbackMessage = MYWM_NOTIFY;
if (dwMessage == NIM_MODIFY)
{
tnd.hIcon = IconHandle();
if (pszTip)
lstrcpyn(tnd.szTip, pszTip, sizeof(tnd.szTip));
else
tnd.szTip[0] = '\0';
}
else
{
tnd.hIcon = NULL;
tnd.szTip[0] = '\0';
}
return (Shell_NotifyIcon(dwMessage, &tnd));
}
//---------------------------------------------------------------------------
HANDLE __fastcall TCTapiz::IconHandle(void)
{
return Application->Icon->Handle;
}
//---------------------------------------------------------------------------
PSTR __fastcall TCTapiz::TipText(void)
{
return ("CTapiz v2.1");
}
//---------------------------------------------------------------------------
LRESULT IconDrawItem(LPDRAWITEMSTRUCT lpdi)
{
HICON hIcon;
hIcon = (HICON)LoadImage(g_hinst, MAKEINTRESOURCE(lpdi->CtlID), IMAGE_ICON,
16, 16, 0);
if (!hIcon)
return(FALSE);
DrawIconEx(lpdi->hDC, lpdi->rcItem.left, lpdi->rcItem.top, hIcon,
16, 16, 0, NULL, DI_NORMAL);
return(TRUE);
}
void __fastcall TCTapiz::MostarCTapiz1Click(TObject *Sender)
{
Height = 230;
Visible = true;
}
//---------------------------------------------------------------------------
void __fastcall TCTapiz::Cerrar1Click(TObject *Sender)
{
Visible = false;
Close();
}
//---------------------------------------------------------------------------
void __fastcall TCTapiz::Configurar1Click(TObject *Sender)
{
Height = 290;
Visible = true;
}
//---------------------------------------------------------------------------
void __fastcall TCTapiz::CheckBox2Click(TObject *Sender)
{
TRegistry *Registro;
Registro = new TRegistry;
if ( CheckBox2->Checked )
AllowExit = false;
Registro -> OpenKey( "\\Software\\JDsoft", true );
Registro -> OpenKey( "\\Software\\JDsoft\\CTapiz", true );
Registro -> WriteBool( "AutoChange", CheckBox2->Checked );
Timer1->Interval = UpDown1->Position * 60 * 1000;
Timer1->Enabled = true;
delete Registro;
}
//---------------------------------------------------------------------------
void __fastcall TCTapiz::CheckBox1Click(TObject *Sender)
{
TRegistry *Registro;
Registro = new TRegistry;
try {
Registro -> OpenKey( "\\Software\\Microsoft\\Windows\\CurrentVersion\\Run", false );
if ( CheckBox1->Checked )
Registro -> WriteString( "CTapiz", Application->ExeName );
else
Registro -> DeleteValue( "CTapiz" );
}catch(...){
//do nothing
}
delete Registro;
}
//---------------------------------------------------------------------------
void __fastcall TCTapiz::FormCloseQuery(TObject *Sender, bool &CanClose)
{
if ( Visible )
{
Visible = false;
CanClose = false;
}
}
//---------------------------------------------------------------------------
void __fastcall TCTapiz::UpDown1Changing(TObject *Sender,
bool &AllowChange)
{
TRegistry *Registro;
Registro = new TRegistry;
Label1->Caption = UpDown1->Position;
try {
Registro -> OpenKey( "\\Software\\JDsoft\\CTapiz", true );
Registro -> WriteInteger( "DelayTime", UpDown1->Position );
Timer1->Interval = UpDown1->Position * 60 * 1000;
}catch(...){
//do nothing
}
delete Registro;
}
//---------------------------------------------------------------------------
void __fastcall TCTapiz::RefreshYescoge(TObject *Sender)
{
ListBox1->Clear();
if ( ! EligeBitmap( GetCurrentDir ).IsEmpty() )
{
ListBox1Click( 0 );
if ( WallPaper_Toolbar )
ListBox1DblClick( 0 );
}
}
//---------------------------------------------------------------------------
void __fastcall TCTapiz::FormCreate(TObject *Sender)
{
TRegistry *Registro;
Registro = new TRegistry;
try {
Registro -> OpenKey( "\\Software\\Microsoft\\Windows\\CurrentVersion\\Run", false );
CheckBox1->Checked = Registro -> ValueExists( "CTapiz" );
}catch(...){
//do nothing
}
try {
Registro -> OpenKey( "\\Software\\JDsoft\\CTapiz", true );
CheckBox2->Checked = Registro -> ReadBool( "AutoChange" );
UpDown1->Position = Registro->ReadInteger( "DelayTime" );
}catch(...) {
UpDown1->Position = 15;
}
delete Registro;
Label1->Caption = UpDown1->Position;
Timer1->Interval = UpDown1->Position * 60 * 1000;
Timer1->Enabled = CheckBox2->Checked;
if ( ! CheckBox1->Checked )
Visible = true;
}
//---------------------------------------------------------------------------
void __fastcall TCTapiz::SwapActiveClick(TObject *Sender)
{
TRegistry *Registro;
Registro = new TRegistry;
Registro -> OpenKey( "\\Software\\Microsoft\\Internet Explorer\\Toolbar", true );
// Eliminamos la clave
if ( ! SwapActive->Checked )
{
try {
Registro -> DeleteValue( "BackBitmap" );
} catch(...) {
// ------
}
}
delete Registro;
}
//---------------------------------------------------------------------------
void __fastcall TCTapiz::SwapFunctionClick(TObject *Sender)
{
WallPaper_Toolbar = !WallPaper_Toolbar;
if ( WallPaper_Toolbar )
{
SwapActive->Enabled = false;
SwapFunction->Caption = "Barra de Herramientas";
RefreshYescoge(0);
} else {
SwapActive->Enabled = true;
SwapFunction->Caption = "Tapiz del Escritorio";
RefreshYescoge(0);
}
}
//---------------------------------------------------------------------------
void TCTapiz::PonToolBar( AnsiString Tapiz )
{
TRegistry *Registro;
Registro = new TRegistry;
// Borramos la entrada antigua...
Registro -> OpenKey( "\\Software\\JDsoft\\CTapiz", true );
try {
remove( (Registro -> ReadString( "ToolBarBitmap" )).c_str() );
} catch(...) {
/// nothing
}
if ( Tapiz.Pos( "_ctapiz_JPG2BMP.bmp" ) != 0 )
Registro -> WriteString( "ToolBarBitmap", Tapiz );
Registro -> OpenKey( "\\Software\\Microsoft\\Internet Explorer\\Toolbar", true );
Registro -> WriteString( "BackBitmap", Tapiz );
/*
SystemParametersInfo( SPI_SETDESKWALLPAPER, 0, Tapiz.c_str(), SPIF_UPDATEINIFILE );
SystemParametersInfo( SPI_SETDESKWALLPAPER, 0, Tapiz.c_str(), 0 );
*/
delete Registro;
}
//---------------------------------------------------------------------------
void __fastcall TCTapiz::Timer1Timer(TObject *Sender)
{
if ( ! WallPaper_Toolbar )
{
SwapFunctionClick(0);
SwapFunctionClick(0);
} else
RefreshYescoge(0);
}
//---------------------------------------------------------------------------