精华区 [关闭][返回]

当前位置:网易精华区>>讨论区精华>>编程开发>>● Delphi>>Windows API函数>>[转载]一组好文章/English

主题:[转载]一组好文章/English
发信人: delphifan()
整理人: delphifan(2000-12-05 19:49:22), 站内信件
Turn off the monitor

// turn it off:
SendMessage(Application.Handle, WM_SYSCOMMAND, SC_MONITORPOWER, 0); 
// turn it on again:
SendMessage(Application.Handle, WM_SYSCOMMAND, SC_MONITORPOWER, -1); 


Caution: you must ensure the monitor being turned on again after you u
se this command, otherwise the system must be rebooted! 



----------------------------------------------------------------------
----------


Loading your glyphs from resources
If you want static bitmaps in your forms and the users are not allowed
 to change them you can use a RES file instead of loading Bitmaps in t
he Delphi IDE. This also makes your EXE smaller! Example: 

Unit Unit1;
Interface
Uses
x,y,z;
Type
...
Implementation
{$R BitIco.res}
...
procedure ...
begin
// Watch out for upper and lower case
Image1.Picture.Bitmap.Handle := LoadBitmap(HInstance, 'Name of Bitma
p');
// or:
Image1.Picture.Icon.Handle := LoadIcon(HInstance, 'Name of Icon');
...
end;

Don't forget that the names are treated case-sensitive!



----------------------------------------------------------------------
----------


How to display columns in a simple Listbox
The TabWidth property of the TListbox component sets the number of dia
log base units, usually pixels, for each tab character. Set it, to say
, a half of the ListBox' width to display two columns. When adding str
ings to the ListBox, use the tab character (^I) at the desired positio
n: 

ListBox1.Items.Add('Column1'^I'Column2');

The imperfection of such approach is that the width of column is not s
et automatically depending on the width of the string displayed, which
 is simple to improve. Let's look at TextWidth method of TCanvas class
: it returns a width in pixels of a string passed as a parameter. Then we can 
write 
with ListBox do begin
  W := Canvas.TextWidth(Str);
  if W > TabWidth then
    TabWidth := W;
end;


(don't remember where I found this tip, sorry) 

----------------------------------------------------------------------
----------


Preventing Ctrl-Alt-Del
Your program might require to disable Ctrl-Alt-Del key sequence, for e
xample if you don't want your program to be unloaded from memory (it's
 up to you to decide if your users will love you for that): and it is 
possible by using the SystemParametersInfo API function. This function
 is used by Control Panel to customize the Windows environment like se
tting keyboard-, display-, sound setting parameters and others. Its sy
ntax is as follows: 

BOOL SystemParametersInfo(
UINT uiAction,  // system parameter to query or set
UINT uiParam,   // depends on action to be taken
PVOID pvParam,  // depends on action to be taken
UINT fWinIni    // user profile update flag
);
The meaning of each parameter is described in Win32 Developer's Refere
nce (kbase.hlp).
Now, to do what we want, we must call this function like in the follow
ing procedure:
procedure DisableCtrlAltDel;
var
  i : integer;
begin
  i := 0;
  {Disable Ctrl-Alt-Del}
  SystemParametersInfo( SPI_SCREENSAVERRUNNING, 1, @i, 0);
end.

Don't forget to place WinProcs unit in "uses" clause of your unit. Rem
ark: To disable
the Alt-Tab sequence, you must use the first parameter SPI_SETFASTTASK
SWITCH and others are the same as above.

----------------------------------------------------------------------
----------


Bringing Delphi 4 to work under Win2000 Beta

MS ackowledges that the current install problems are their bug and the
y have stated
that it will be addressed in a future beta. All InstallShield programs
 that use password
protected CABs will probably have problems. However, here's a workarou
nd how to
bring Delphi 4 to work under Windows 2000:

install Delphi 4 on Windows 2000 and ignore all error messages
copy the runtime image directly from the CD
go to the command line and change to
Common Files\Borland\Shared\Debugger
From there, type regsvr32 -u bordbk40.dll
and then regevr32 bordbk40.dll

..this should do the trick!

----------------------------------------------------------------------
----------


Preventing task switching

Preventing a user from switching to another application is possible un
der Windows 3.x and 9x
by tricking Windows into thinking a screen saver is running. This meth
od does not work
under Windows NT and is not guaranteed to be available in future versi
ons of Windows.
Many versions of Windows may also respond to a task switching trap ins
talled by a CBT
(Computer Based Training) application hook. To prevent task switching 
under Windows NT,
you will need access to a third party keyboard device device driver.
  VAR
OldValue: Longint;
  [..]
// turns trap on:
SystemParametersInfo(97, True, OldValue, 0);
// turns trap off:
SystemParametersInfo(97, False, OldValue, 0);


----------------------------------------------------------------------
----------


Ensuring your program runs just one instance

Modify your *.DPR project file with the example below:
Program PrevInst;
USES
Forms, Windows, // Add Windows
Unit1 in 'Unit1.pas' {Form1};
{$R *.RES}
VAR // Add Vars
MutexHandle: THandle;
hwind:HWND;
BEGIN
// Add below code
MutexHandle := CreateMutex(nil, TRUE, 'MysampleAppMutex'); // should 
be a unique string
IF MutexHandle <> 0 then
    begin
IF GetLastError = ERROR_ALREADY_EXISTS then
begin
  // MessageBox(0, 'Instance of this application is already running.
',
  // 'Application already running', mb_IconHand);
  CloseHandle(MutexHandle);
  hwind := 0;
  repeat
// The string 'My app' must match your App Title (below)
            hwind:=Windows.FindWindowEx(0,hwind,'TApplication','My app
');
  until (hwind<>Application.Handle);
      IF (hwind<>0) then
          begin
Windows.ShowWindow(hwind,SW_SHOWNORMAL);
Windows.SetForegroundWindow(hwind);
  end;
          Halt;
end
end;
// your/Delphi's window create / run code is below here:
Application.Initialize;
Application.Title := 'My app'; // this matches to above
Application.CreateForm(TForm1, Form1);
Application.Run;
END.

A shorter method (without automatic switching to your app's original i
nstance) would be:
CreateMutex(nil,FALSE,'AnyNameHere');
IF GetLastError = ERROR_ALREADY_EXISTS THEN
begin
  MessageDlg('Program is already running. You can not start more than
 one instance',
    mterror,[mbOK], 0);
Halt(0);
end;
Application.Initialize;




--
=================================================
==          [email protected]                  ==
==    [一个真正有内容、较实用的Delphi网站]       == 
==        http://delphifan.wojia.com           ==
==  http://lmd.yeah.net  http://lmd.126.com    ==
=================================================

※ 来源:.月光软件站 http://www.moon-soft.com.[FROM: 202.100.18.147]

[关闭][返回]