Showing posts with label Delphi. Show all posts
Showing posts with label Delphi. Show all posts

Wednesday, 21 December 2011

Comment About Comments

After posting my last tutorial, someone made a criticism, or rather an observation, about the coding style I have been using. I will be the first to admit, they are quite right in what they said, too.

I failed to use any comments in my code. I should have commented. No matter how small the piece of code you are writing, it is always advisable to put comments in your code. This can save you a lot of time in the future, especially if you haven't worked with that piece of code for some time. Even one line of code can cause a programmer to stop and think, if it's not too clear on it's function.

So, my apologies to all. From now on, I shall ensure that I place adequate comments in the code, as well as the explanations of the code function in the blogs.

Wednesday, 24 August 2011

Some old, but useful code

Digging through some archives, I found some useful code, so I thought I would share these.

These snippets are from my Delphi programming days, so the code is in Pascal, but it can easily be converted to whichever language is being used.

Hide an application from the taskbar - Delphi

This is a really simple technique that can hide your application from the taskbar.In your main forms On Create event, type the following code:

SetWindowLong(Application.Handle, GWL_EXSTYLE, GetWindowLong(Application.Handle, GWL_EXSTYLE ) or WS_EX_TOOLWINDOW and not WS_EX_APPWINDOW);

That's all there is to it.

Making a program run at Windows startup - Delphi

On your programs Settings or Preferences form, place a TCheckBox with the caption Run at Windows Startup.

In the forms uses clause, include the unit Registry.

In the TCheckBox On Click event, include the following code:

procedure TForm1.TCheckBox1Click(Sender: TObject); 

     var Registry: TRegistry; 

 begin

      Registry := TRegistry.Create; 

      Registry.RootKey := HKEY_LOCAL_MACHINE;

      Registry.OpenKey('Software\Microsoft\Windows\CurrentVersion\Run', false);

      If TCheckBox1.Checked then 

          Registry.WriteString(Application.Title, Application.ExeName) 

      else Registry.DeleteValue(Application.Title); 

      Registry.CloseKey; 

      Registry.free; 

end; 

To ensure that the TCheckBox displays the current status of the function when the form is displayed, we need to insert the following code into the forms On Show event: 

procedure TForm1.FormShow(Sender: TObject); 

    var Registry : TRegistry; 

begin 

    Registry := TRegistry.Create;

    Registry.RootKey := HKEY_LOCAL_MACHINE;

    Registry.OpenKey('Software\Microsoft\Windows\CurrentVersion\Run', false); 

    CheckBox1.Checked := Registry.ValueExists(Application.Title); 

    Registry.CloseKey; 

    Registry.free; 

end;

This code simply checks to see if the run command for the appllication has been inserted into the registry. If it has, the the TCheckBox is checked, otherwise it is not.