Cinco dicas de Delphi - parte 3

1. Desabilita o botão Fechar do Formulário (topo)

procedure TForm1.FormCreate(Sender: TObject);
var
  hwndHandle : THANDLE;
  hMenuHandle : HMenu;
begin
  hwndHandle := Self.Handle;
  if (hwndHandle <> 0) then
  begin
    hMenuHandle := GetSystemMenu(hwndHandle, FALSE);
    if (hMenuHandle <> 0) then
      DeleteMenu(hMenuHandle, SC_CLOSE, MF_BYCOMMAND);
  end;
end;

2. Mudar a cor da linha de um DBGrid (topo)

procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; .....);
begin
  if odd(field.DataSet.RecNo) then
    DBGrid1.Canvas.Font.Color := clBlue
  else DBGrid1.Canvas.Font.Color := clWhite;
  DBGrid1.DefaultDrawDataCell(Rect, dbgrid1.columns[datacol].field, state);
end;

3. Inicializar vários EDITs em um formulário (topo)

procedure TForm1.Button1Click(Sender: TObject);
var contador : integer;
begin
  for contador := 0 to (Form1.ControlCount - 1) do
    if Form1.Controls[contador].ClassName = 'TEdit' then
      (Form1.Controls[contador] as TEdit).Text := '';
end;

4. Nome do computador (topo)

function NomeComputador : String;
var
  lpBuffer : PChar;
  nSize : DWord;
const Buff_Size = MAX_COMPUTERNAME_LENGTH + 1;
begin
  nSize := Buff_Size;
  lpBuffer := StrAlloc(Buff_Size);
  GetComputerName(lpBuffer,nSize);
  Result := String(lpBuffer);
  StrDispose(lpBuffer);
end;

5. Trocar a resolução de vídeo (topo)

function TrocaResolucao(X, Y: word): Boolean;
var lpDevMode: TDeviceMode;
begin
  if EnumDisplaySettings(nil, 0, lpDevMode) then
  begin
    lpDevMode.dmFields := DM_PELSWIDTH Or DM_PELSHEIGHT;
    lpDevMode.dmPelsWidth := X;
    lpDevMode.dmPelsHeight:= Y;
    Result := ChangeDisplaySettings(lpDevMode, 0) = DISP_CHANGE_SUCCESSFUL;
  end;
end;