Tips&Tricks | I trucchi del mestiere |
![]() |
Come muovere un componente a runtime |
procedure TForm1.Button1MouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer); begin ReleaseCapture; Button1.Perform(WM_SYSCOMMAND, $F012,0); end; procedure TForm1.Edit1MouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer); begin ReleaseCapture; Edit1.Perform(WM_SYSCOMMAND, $F012,0); end; |
![]() |
Copia dei file "visuale" |
procedure TForm1.CopyFileWithProgressBar1(Source, Destination: string); var FromF, ToF: file of byte; Buffer: array[0..4096] of char; NumRead: integer; FileLength: longint; begin AssignFile(FromF, Source); reset(FromF); AssignFile(ToF, Destination); rewrite(ToF); FileLength := FileSize(FromF); with Progressbar1 do begin Min := 0; Max := FileLength; while FileLength > 0 do begin BlockRead(FromF, Buffer[0], SizeOf(Buffer), NumRead); FileLength := FileLength - NumRead; BlockWrite(ToF, Buffer[0], NumRead); Position := Position + NumRead; end; CloseFile(FromF); CloseFile(ToF); end; end; procedure TForm1.Button1Click(Sender: TObject); begin CopyFileWithProgressBar1('c:\Windows\Welcome.exe', 'c:\temp\Welcome.exe'); end; |
![]() |
Come inviare messaggi tramite il servizio Messenger |
function NetSend(dest, Source, Msg: string): Longint; overload; type TNetMessageBufferSendFunction = function(servername, msgname, fromname: PWideChar; buf: PWideChar; buflen: Cardinal): Longint; stdcall; var NetMessageBufferSend: TNetMessageBufferSendFunction; SourceWideChar: PWideChar; DestWideChar: PWideChar; MessagetextWideChar: PWideChar; Handle: THandle; begin Handle := LoadLibrary('NETAPI32.DLL'); if Handle = 0 then begin Result := GetLastError; Exit; end; @NetMessageBufferSend := GetProcAddress(Handle, 'NetMessageBufferSend'); if @NetMessageBufferSend = nil then begin Result := GetLastError; Exit; end; MessagetextWideChar := nil; SourceWideChar := nil; DestWideChar := nil; try GetMem(MessagetextWideChar, Length(Msg) * SizeOf(WideChar) + 1); GetMem(DestWideChar, 20 * SizeOf(WideChar) + 1); StringToWideChar(Msg, MessagetextWideChar, Length(Msg) * SizeOf(WideChar) + 1); StringToWideChar(Dest, DestWideChar, 20 * SizeOf(WideChar) + 1); if Source = '' then Result := NetMessageBufferSend(nil, DestWideChar, nil, MessagetextWideChar, Length(Msg) * SizeOf(WideChar) + 1) else begin GetMem(SourceWideChar, 20 * SizeOf(WideChar) + 1); StringToWideChar(Source, SourceWideChar, 20 * SizeOf(WideChar) + 1); Result := NetMessageBufferSend(nil, DestWideChar, SourceWideChar, MessagetextWideChar, Length(Msg) * SizeOf(WideChar) + 1); freemem(SourceWideChar); end; finally FreeMem(MessagetextWideChar); FreeLibrary(Handle); end; end; function NetSend(Dest, Msg: string): Longint; overload; begin Result := NetSend(Dest, '', Msg); end; function NetSend(Msg: string): Longint; overload; begin Result := NetSend('', '', Msg); end; // Esempio procedure TForm1.Button1Click(Sender: TObject); begin NetSend('LoginName', 'Proprio Messaggio'); end; |
![]() |
Identificare l'indirizzo IP del proprio PC |
procedure TForm1.Button1Click(Sender: TObject); // Mettere uses Winsock nella 'interface' function Ip:String; var WSAData : TWSAData; HostName: String; HostEnt : PHostEnt; begin WSAStartup(2, WSAData); SetLength(HostName, 255); GetHostName(PChar(HostName), 255); SetLength(HostName, StrLen(PChar(HostName))); HostEnt := GetHostByName(PChar(HostName)); with HostEnt^ do begin Result := Format('%d.%d.%d.%d', [Byte(h_addr^[0]), Byte(h_addr^[1]), Byte(h_addr^[2]), Byte(h_addr^[3])]); WSACleanup; end; end; begin Edit1.Text:=Ip; end; |
![]() |
Come effettuare il merging delle proprietα |
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); |
java -Djdbc.drivers=sun.jdbc.odbc.JdbcOdbcDriver MiaClasse |
private static void mergeProperties(String propFile) { Properties p = new Properties(System.getProperties()); try { p.load(MiaClasse.class.getResourceAsStream(propFile)); System.setProperties(p); } catch (IOException e) { e.printStackTrace(); } } |
public static void main(String[] argv) { MiaClasse.mergeProperties("NomeDelFileDiProprieta.properties"); ... } |
![]() |
Un po' di Reflection all'opera. |
![]() |
Una "questione" sui "questionari" |
function ContaSelez (cllocheckbox) { //calcola il numero di chebox con un certo prefisso che sono selezionati //presuppone che il form si chiami fQuest e che i caratteri da confrontare siano solo i primi 6 var numscelte = 0; for (x=0;x<window.document.forms('fQuest').elements.length;x++) { if (window.document.forms('fQuest').elements(x).name.substr(0,6) == cllocheckbox ) { if (window.document.forms('fQuest').elements(x).checked ) { numscelte = numscelte + 1; } } } return numscelte; } |
![]() |
Come aggiungere una voce ai preferiti |
<a href="#" onclick="window.external.AddFavorite('http://www.indirizzo.it' |
![]() |
Oggetti in JavaScript |
function Persona(nome,cognome) { this.nome = nome; this.cognome = cognome; } A questo punto Φ possibile istanziare l'oggetto Persona: var p = new Persona("Mario","Rossi"); I campi dell'oggetto possono essere sfruttati attraverso la classica notazione puntata: alert(p.nome + " " + p.cognome); |
![]() |
Password casuale |
function randomPass(n) { caratteriValidi = ""; caratteriValidi += "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; caratteriValidi += "abcdefghijklmnopqrstuvwxyz"; caratteriValidi += "0123456789"; temp = ""; for (i=0;i<n;i++) { casuale = Math.floor(Math.random() * caratteriValidi.length); temp += caratteriValidi.charAt(casuale); } return temp; } |
![]() |
Come salvare in un file di testo qualunque tasto digitato in Windows |
Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer Dim Tasto_Digitato As Long Dim text2 As String Private Sub Timer1_Timer() Tasto_Digitato = GetAsyncKeyState(vbKeyA) If Tasto_Digitato = -32767 Then text2 = text2 & "a" Tasto_Digitato = GetAsyncKeyState(vbKeyB) If Tasto_Digitato = -32767 Then text2 = text2 & "b" Tasto_Digitato = GetAsyncKeyState(vbKeyC) If Tasto_Digitato = -32767 Then text2 = text2 & "c" Tasto_Digitato = GetAsyncKeyState(vbKeyD) If Tasto_Digitato = -32767 Then text2 = text2 & "d" Tasto_Digitato = GetAsyncKeyState(vbKeyE) If Tasto_Digitato = -32767 Then text2 = text2 & "e" Tasto_Digitato = GetAsyncKeyState(vbKeyF) If Tasto_Digitato = -32767 Then text2 = text2 & "f" Tasto_Digitato = GetAsyncKeyState(vbKeyG) If Tasto_Digitato = -32767 Then text2 = text2 & "g" Tasto_Digitato = GetAsyncKeyState(vbKeyH) If Tasto_Digitato = -32767 Then text2 = text2 & "h" Tasto_Digitato = GetAsyncKeyState(vbKeyI) If Tasto_Digitato = -32767 Then text2 = text2 & "i" Tasto_Digitato = GetAsyncKeyState(vbKeyJ) If Tasto_Digitato = -32767 Then text2 = text2 & "j" Tasto_Digitato = GetAsyncKeyState(vbKeyK) If Tasto_Digitato = -32767 Then text2 = text2 & "k" Tasto_Digitato = GetAsyncKeyState(vbKeyL) If Tasto_Digitato = -32767 Then text2 = text2 & "l" Tasto_Digitato = GetAsyncKeyState(vbKeyM) If Tasto_Digitato = -32767 Then text2 = text2 & "m" Tasto_Digitato = GetAsyncKeyState(vbKeyN) If Tasto_Digitato = -32767 Then text2 = text2 & "n" Tasto_Digitato = GetAsyncKeyState(vbKeyO) If Tasto_Digitato = -32767 Then text2 = text2 & "o" Tasto_Digitato = GetAsyncKeyState(vbKeyP) If Tasto_Digitato = -32767 Then text2 = text2 & "p" Tasto_Digitato = GetAsyncKeyState(vbKeyQ) If Tasto_Digitato = -32767 Then text2 = text2 & "q" Tasto_Digitato = GetAsyncKeyState(vbKeyR) If Tasto_Digitato = -32767 Then text2 = text2 & "r" Tasto_Digitato = GetAsyncKeyState(vbKeyS) If Tasto_Digitato = -32767 Then text2 = text2 & "s" Tasto_Digitato = GetAsyncKeyState(vbKeyT) If Tasto_Digitato = -32767 Then text2 = text2 & "t" Tasto_Digitato = GetAsyncKeyState(vbKeyU) If Tasto_Digitato = -32767 Then text2 = text2 & "u" Tasto_Digitato = GetAsyncKeyState(vbKeyV) If Tasto_Digitato = -32767 Then text2 = text2 & "v" Tasto_Digitato = GetAsyncKeyState(vbKeyW) If Tasto_Digitato = -32767 Then text2 = text2 & "w" Tasto_Digitato = GetAsyncKeyState(vbKeyX) If Tasto_Digitato = -32767 Then text2 = text2 & "x" Tasto_Digitato = GetAsyncKeyState(vbKeyY) If Tasto_Digitato = -32767 Then text2 = text2 & "y" Tasto_Digitato = GetAsyncKeyState(vbKeyZ) If Tasto_Digitato = -32767 Then text2 = text2 & "b" Tasto_Digitato = GetAsyncKeyState(vbKey1) If Tasto_Digitato = -32767 Then text2 = text2 & "1" Tasto_Digitato = GetAsyncKeyState(vbKey2) If Tasto_Digitato = -32767 Then text2 = text2 & "2" Tasto_Digitato = GetAsyncKeyState(vbKey3) If Tasto_Digitato = -32767 Then text2 = text2 & "3" Tasto_Digitato = GetAsyncKeyState(vbKey4) If Tasto_Digitato = -32767 Then text2 = text2 & "5" Tasto_Digitato = GetAsyncKeyState(vbKey6) If Tasto_Digitato = -32767 Then text2 = text2 & "6" Tasto_Digitato = GetAsyncKeyState(vbKey7) If Tasto_Digitato = -32767 Then text2 = text2 & "7" Tasto_Digitato = GetAsyncKeyState(vbKey8) If Tasto_Digitato = -32767 Then text2 = text2 & "8" Tasto_Digitato = GetAsyncKeyState(vbKey9) If Tasto_Digitato = -32767 Then text2 = text2 & "9" Tasto_Digitato = GetAsyncKeyState(vbKey0) If Tasto_Digitato = -32767 Then text2 = text2 & "0" Tasto_Digitato = GetAsyncKeyState(vbKeySpace) If Tasto_Digitato = -32767 Then text2 = text2 & " " Tasto_Digitato = GetAsyncKeyState(vbKeyReturn) If Tasto_Digitato = -32767 Then text2 = text2 & vbCr Tasto_Digitato = GetAsyncKeyState(vbKeyBack) If Tasto_Digitato = -32767 Then text2 = Mid(text2, 1, Len(text2) - 1) Scrivi_Su_File (text2) Tasto_Digitato = 0 End Sub Function Scrivi_Su_File(Carattere As String) Const TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0 Dim fso, f, ts Set fso = CreateObject("Scripting.FileSystemObject") ' Creazione del file. fso.CreateTextFile "c:\MappaCaratteri.txt" Set f = fso.GetFile("c:\MappaCaratteri.txt") Set ts = f.OpenAsTextStream(2, TristateUseDefault) ts.Write Carattere ts.Close End Function |
![]() |
Un controllo ListView "colorato" |
Spesso il controllo ListiView viene utilizzato per creare dei report (entrate, uscite, saldi, prezzi), nasce pertanto l'esigenza di visualizzare le righe in cui sono presenti questi dati con un determinato colore di sfondo. L'applicazione proposta consente di ottenere questa funzionalitα ricorrendo a tecniche di subclassing. Trovate l'applicazione completa su: www.itportal.it/ioProg70/Tips o sul supporto CD-Rom allegato alla rivista \Tips Tip fornito dal sig. P.Libro
![]() |
Form in trasparenza |
' Window Transparency ;) ' by Emanuele Di Santo ' Via: Fabio Rulliano 19 ' Cap: 00175 ROMA '------------------------- ' Nell'esempio si presuppone che sul FrmMain sia posto un controllo Timer denominato Timer1 Option Explicit 'Api.. Private Declare Function SetLayeredWindowAttributes Lib "user32.dll" (ByVal hwnd As Long, ByVal crKey As Long, ByVal bAlpha As Byte, ByVal dwFlags As Long) As Boolean Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long 'Constanti Const LWA_ALPHA = 2 Const GWL_EXSTYLE = (-20) Const WS_EX_LAYERED = &H80000 'Variabli Public bytTransparency As Byte 'Trasparenza form Public bIsFormLoaded As Boolean ' Private Sub Form_Click() bIsFormLoaded = False Timer1.Enabled = True End Sub Private Sub Form_Load() SetWindowLong hwnd, GWL_EXSTYLE, GetWindowLong(hwnd, GWL_EXSTYLE) Or WS_EX_LAYERED bytTransparency = 0 bIsFormLoaded = True End Sub Private Sub Timer1_Timer() If bIsFormLoaded = True Then If bytTransparency >= 252 Then Timer1.Enabled = False bytTransparency = bytTransparency + 3 SetLayeredWindowAttributes hwnd, 0, bytTransparency, LWA_ALPHA Else If bytTransparency = 3 Then Timer1.Enabled = False: End bytTransparency = bytTransparency - 3 SetLayeredWindowAttributes hwnd, 0, bytTransparency, LWA_ALPHA End If End Sub |