Thursday, April 30, 2009

[Delphi] Changing Printer Paper Size


var
Device, Driver, Port: array[0..80] of Char;
DevMode: THandle;
pDevmode: PDeviceMode;
...
...
// Get printer device name etc.
Printer.GetPrinter(Device, Driver, Port, DevMode);
// force reload of DEVMODE
Printer.SetPrinter(Device, Driver, Port, 0) ;
// get DEVMODE handle
Printer.GetPrinter(Device, Driver, Port, DevMode);
If Devmode <> 0 Then Begin
// lock it to get pointer to DEVMODE record
pDevMode := GlobalLock( Devmode );
If pDevmode <> Nil Then
try
With pDevmode^ Do Begin
// modify paper size
DEBUG('Old dmPapersize = ' + IntToStr(dmPapersize) );

if Unit2.ActiveAccount.clPaperSize = 0 then // Letter
dmPaperSize := DMPAPER_LETTER
else
dmPapersize := DMPAPER_A4;

DEBUG('New dmPapersize = ' + IntToStr(dmPapersize) );
// tell printer driver that dmPapersize field contains
// data it needs to inspect.
dmFields := dmFields or DM_PAPERSIZE;
End;
finally
// unlock devmode handle.
GlobalUnlock( Devmode );
end;
End; { If }

source: http://www.efg2.com/Lab/Library/UseNet/2000/0314.txt

Tuesday, April 7, 2009

[Delphi] I always forgot, the Exception

var
number1, number0 : Integer;
begin
try
number0 := 0;
number1 := 1;
number1 := number1 div number0;
ShowMessage('1 / 0 = '+IntToStr(number1));
except
on E : Exception do
begin
ShowMessage('Exception class name = '+E.ClassName);
ShowMessage('Exception message = '+E.Message);
end;
end;
end;

Monday, April 6, 2009

[Delphi] Using Ampersands inside Delphi

How can I make a Window control's text property contain an ampersand character?

Use two ampersand characters. The Windows default text handling for controls interprets a single Ampersand character as meaning the next character is a hot key. Example: Button .Caption := 'This && That';

Source: http://www.delphi32.com/info_facts/faq/faq_184.asp

Wednesday, January 21, 2009

[Java] Creating JTable from Resultset

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import javax.swing.table.AbstractTableModel; 

public
class ResultTableModel extends AbstractTableModel {
      /**
       *
       */
      private static final long serialVersionUID = 1L;
      boolean DEBUG = false;
      private String[] columnNames;
      private Object[][] data;

      
public ResultTableModel(ResultSet rs, String[] columnNames) {
            ResultSetMetaData meta;
            if (rs != null) {
                  this.columnNames = columnNames;
                  try {
                        meta = rs.getMetaData();
                        int rowcount = 0, colcount = 0;
                        if (rs.last()) {
                              rowcount = rs.getRow();
                              rs.first();
                        }
                        colcount = meta.getColumnCount();
                        data = new Object[rowcount][colcount];
                        int i = 0;
                        do {
                              for (int j = 0; j < colcount; j++) {
                                    data[i][j] = rs.getObject(j + 1);
                              }
                              i++;
                        } while (rs.next());
                  } catch (SQLException e) {
                        e.printStackTrace();
                  }
            }
      }

      
public int getColumnCount() {
            return columnNames.length;
      }

      
public int getRowCount() {
            return data.length;
      }

      
public String getColumnName(final int col) {
            return columnNames[col];
      }

      
public Object getValueAt(final int row, final int col) {
            return data[row][col];
      }

      
/*
       * JTable uses this method to determine the default renderer/ editor for
       * each cell. If we didn't implement this method, then the last column would
       * contain text ("true"/"false"), rather than a check box.
       */
      public Class getColumnClass(final int c) {
            return getValueAt(0, c).getClass();
      }

      
/*
       * Don't need to implement this method unless your table's editable.
       */
      public boolean isCellEditable(final int row, final int col) {
            // Note that the data/cell address is constant,
            // no matter where the cell appears onscreen.
            if (col < 2) {
                  return false;
            } else {
                  return true;
            }
      }

      
/*
       * Don't need to implement this method unless your table's data can change.
       */
      public void setValueAt(final Object value, final int row, final int col) {
            data[row][col] = value;
            fireTableCellUpdated(row, col);
      }
}

Monday, January 12, 2009

[Delphi] String Split

function TForm1.Split(s:String): TStringList;
var
sl : TStringList;
i : Integer;
tmp : String;
begin
sl := TStringList.Create;
for i := 1 to Length(s) do
begin
if s[i] <> ',' then
begin
if s[i] <> '"' then // skip "
begin
tmp := tmp + s[i];
end;
end else
begin
sl.Add(tmp);
tmp := '';
end;
end;
sl.Add(tmp);
Result := sl;
end;

Thursday, January 8, 2009

Windows XP Service Shortcut

Do you know that Windows XP has a shortcut which can be activated by put it into “Run” menu.
for the example press “Start” and Then Press “Run“. and the dialog box will show up.

and then fill it with the service that you want “perfmon.msc”, press OK

” The Performance Monitor ” will comes up to your windows

as easy as that.

hereunder other, dissimilar example of service name which can be activated by through “Run” menu.
* Component Services - comexp.msc
* Computer Management - compmgmt.msc
* Device Manager - devmgmt.msc
* Disk Defrag - dfrg.msc
* Disk Managment - diskmgmt.msc
* Event Viewer - eventvwr.msc
* Group Policies - gpedit.msc
* Local Security Settings - secpol.msc
* Local Users and Groups - lusrmgr.msc
* Performance Monitor - perfmon.msc
* Resultant Set of Policies - rsop.msc
* Services - services.msc
* Shared Folders - fsmgmt.msc


Original from: Windows XP service shortcut: Posted by: trueheart77 | September 25, 2007

Sunday, January 4, 2009

[Delphi] Canvas Print Right Aligned Text


As you know, TCanvas don't have a method to print right aligned text. So what to do?

If the text is left aligned, it should start at X1. Now we want to start from X2, so we use the calculation above to get our X2.

procedure TForm1.Button3Click(Sender: TObject);
var
  X1,X2,W1,W2,W3: Integer;
  _text: String;
begin
  W1 := 100;
  X1 := 5;   // Original X
  _text := '12345.78';
  with Image1.Canvas do
  begin
    W2 := TextWidth(_text);
    W3 := W1 - W2;
    X2 := X1 + W3;
    TextOut( X2, 5, _text);   // 5 = Y
  end;
end;