Jumat, 29 Agustus 2008

...check if I am connected to the internet ?

...check if I am connected to the internet ?

interface
uses
Windows, SysUtils, Registry, WinSock, WinInet;

type
TConnectionType = (ctNone, ctProxy, ctDialup);

function ConnectedToInternet : TConnectionType;
function RasConnectionCount : Integer;


implementation

//For RasConnectionCount =======================
const
cERROR_BUFFER_TOO_SMALL = 603;
cRAS_MaxEntryName = 256;
cRAS_MaxDeviceName = 128;
cRAS_MaxDeviceType = 16;
type
ERasError = class(Exception);

HRASConn = DWord;
PRASConn = ^TRASConn;
TRASConn = record
dwSize: DWORD;
rasConn: HRASConn;
szEntryName: Array[0..cRAS_MaxEntryName] Of Char;
szDeviceType : Array[0..cRAS_MaxDeviceType] Of Char;
szDeviceName : Array [0..cRAS_MaxDeviceName] of char;
end;

TRasEnumConnections =
function (RASConn: PrasConn; { buffer to receive Connections data }
var BufSize: DWord; { size in bytes of buffer }
var Connections: DWord { number of Connections written to buffer }
): LongInt; stdcall;
//End RasConnectionCount =======================


function ConnectedToInternet: TConnectionType;
var
Reg : TRegistry;
bUseProxy : Boolean;
UseProxy : LongWord;
begin
Result := ctNone;
Reg := TRegistry.Create;
with REG do
try
try
RootKey := HKEY_CURRENT_USER;
if OpenKey('\Software\Microsoft\Windows\CurrentVersion\Internet settings',False) then begin
//I just try to read it, and trap an exception
if GetDataType('ProxyEnable') = rdBinary then
ReadBinaryData('ProxyEnable', UseProxy, SizeOf(LongWord) )
else begin
bUseProxy := ReadBool('ProxyEnable');
if bUseProxy then
UseProxy := 1
else
UseProxy := 0;
end;
if (UseProxy <> 0) and ( ReadString('ProxyServer') <> '' ) then Result := ctProxy;
end;
except
//Obviously not connected through a proxy
end;
finally
Free;
end;

//We can check RasConnectionCount even if dialup networking is not installed
//simply because it will return 0 if the DLL is not found.
if Result = ctNone then begin
if RasConnectionCount > 0 then Result := ctDialup;
end;
end;

function RasConnectionCount : Integer;
var
RasDLL : HInst;
Conns : Array[1..4] of TRasConn;
RasEnums : TRasEnumConnections;
BufSize : DWord;
NumConns : DWord;
RasResult : Longint;
begin
Result := 0;

//Load the RAS DLL
RasDLL := LoadLibrary('rasapi32.dll');
if RasDLL = 0 then exit;

try
RasEnums := GetProcAddress(RasDLL,'RasEnumConnectionsA');
if @RasEnums = nil then
raise ERasError.Create('RasEnumConnectionsA not found in rasapi32.dll');

Conns[1].dwSize := Sizeof (Conns[1]);
BufSize := SizeOf(Conns);

RasResult := RasEnums(@Conns, BufSize, NumConns);

If (RasResult = 0) or (Result = cERROR_BUFFER_TOO_SMALL) then Result := NumConns;
finally
FreeLibrary(RasDLL);
end;
end;

...change screen resolution ?

{
The function NewRes can have the following result:

DISP_CHANGE_SUCCESSFUL The settings change was successful.
DISP_CHANGE_RESTART The computer must be restarted in order for the graphics mode to work.
DISP_CHANGE_BADFLAGS An invalid set of flags was passed in.
DISP_CHANGE_FAILED The display driver failed the specified graphics mode.
DISP_CHANGE_BADMODE The graphics mode is not supported.
DISP_CHANGE_NOTUPDATED Windows NT only: Unable to write settings to the registry.
}

{
Folgene Rückgabewerte sind für NewRes möglich:

DISP_CHANGE_SUCCESSFUL Auflösung geändert
DISP_CHANGE_RESTART Computer muss neugestartet werden
DISP_CHANGE_BADFLAGS Falsche Flags
DISP_CHANGE_FAILED Fehler in Grafiktreiber
DISP_CHANGE_BADMODE Auflösung nicht unterstützt
DISP_CHANGE_NOTUPDATED Windows NT: Einstellungen konnten nicht in die Registry geschrieben werden
}

function NewRes(XRes,YRes:DWord):integer;
var
DevMode:TDeviceMode;
begin
EnumDisplaySettings(nil, 0, DevMode);
DevMode.dmFields:=DM_PELSWIDTH or DM_PELSHEIGHT;
DevMode.dmPelsWidth:=XRes;
DevMode.dmPelsHeight:=YRes;
Result:=ChangeDisplaySettings(DevMode, 0);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
if NewRes(1024,768)=DISP_CHANGE_SUCCESSFUL then
begin
ShowMessage('Resolution changed.');
end;
end;

Delphi 5 Easter Egg

Are there any easter eggs in Delphi?
Answer:


I know that this isn't the kind of technical article you would expect to see from delphi3000.com, but many people asked if there aren't any 'easter eggs' in Delphi. Here goes:

In Delphi 5 (I've seen it work in unpatched-Enterprise version, but I think it's the same for all) open the About box...
Hold Alt and type in JEDI or DEVELOPERS or TEAM or QUALITY.

After the large DELPHI word scrolls off the screen you will see:
- a praise to the Joint Endeavour of Delphi Innovators (and a link to their site in the About box) or
- the names of the developers / team / QA that made Delphi such a great product

While the "Star Wars" type scrolling in still on you can use the down (or the up) key to turn the scrolling text until it is
upside down, and now you should now see the message:
"Use the Source, Luke"

...disable / enable / hide / show taskbar ?



This is very easy. All that you have to do is obtain the window handle
of the taskbar window and then you can disable or hide it.
The window handle is obtained like this :

var
HTaskbar : HWND;
begin
HTaskBar:=FindWindow('Shell_TrayWnd',nil);

Now you have the window handle, you can use it to disable or hide the taskbar :

EnableWindow(HTaskBar,False); //Disable the taskbar
EnableWindow(HTaskBar,True); //Enable the taskbar
ShowWindow(HTaskbar,SW_HIDE); //Hide the taskbar
ShowWindow(HTaskbar,SW_SHOW); //Show the taskbar

Tidak ada komentar: