Lời đầu tiên xin kính chào anh chị em bà con cô bác chú dì dượng mợ thím và má của thằng nhỏ ! :gathering:

Để ra mắt ace e xin gửi code một chương trình nhỏ em code buổi sáng :
+ Project : Pascal Simple Calculator
+ Compiler : Freepascal (2.x)
+ Des : Thực ra là cái máy tính nhỏ e viết bằng Win API trên pascal, ace có thể lấy source về rồi chạy bằng free pascal là sẽ thấy


Download : http://www.mediafire.com/download/iugjzj0fg414vpr/PCalculator.zip

Mã:
program PCalculator;

{$APPTYPE GUI}
{$MODE DELPHI}


uses
  Windows,
  SysUtils;


const
  appName = 'PCalculator';


  BUT0 = 100;
  BUT1 = 101;
  BUT2 = 102;
  BUT3 = 103;
  BUT4 = 104;
  BUT5 = 105;
  BUT6 = 106;
  BUT7 = 107;
  BUT8 = 108;
  BUT9 = 109;


  BUTA = 201;
  BUTS = 202;
  BUTM = 203;
  BUTD = 204;
  BUTDOT = 250;
  BUTCAL = 300;


  BUTCLEAR = 260;
  EDITBOX = 500;


var
  wc: WNDClass;
  win, edit: hwnd;


  prev: string;
  buf: string = '';
  OPER: char;


  msg: TMSG;


  function checkSt(st: string): boolean;
  begin
    if st[length(st)] = '.' then
      Result := False
    else
      Result := True;
  end;


  function WindowProc(Window: HWnd; AMessage: UINT; WParam: WPARAM;
    LParam: LPARAM): LRESULT; stdcall;
  var
    n: boolean;
    a, b, c: real;


  begin
    WindowProc := 0;


    case AMessage of
      wm_Destroy:
      begin
        PostQuitMessage(0);
        Exit;
      end;
      WM_COMMAND:
      begin
        n := True;
        case LOWORD(WParam) of
          BUT0: if buf <> '' then
              buf := buf + '0';
          BUT1: buf := buf + '1';
          BUT2: buf := buf + '2';
          BUT3: buf := buf + '3';
          BUT4: buf := buf + '4';
          BUT5: buf := buf + '5';
          BUT6: buf := buf + '6';
          BUT7: buf := buf + '7';
          BUT8: buf := buf + '8';
          BUT9: buf := buf + '9';
          BUTDOT: if (Pos('.', buf) = 0) and (length(buf) <> 0) then
              buf := buf + '.';
          BUTCLEAR:
          begin
            buf := '';
            prev := '';
          end;
          BUTA: if checkSt(buf) then
            begin
              OPER := '+';
              prev := buf;
              buf := '';
            end;
          BUTS: if checkSt(buf) then
            begin
              OPER := '-';
              prev := buf;
              buf := '';
            end;
          BUTM: if checkSt(buf) then
            begin
              OPER := '*';
              prev := buf;
              buf := '';
            end;
          BUTD: if checkSt(buf) then
            begin
              OPER := '/';
              prev := buf;
              buf := '';
            end;
          BUTCAL: if checkSt(buf) and (prev <> '') then
            begin
              a := StrToFloat(prev);
              b := StrToFloat(buf);
              case OPER of
                '+': c := a + b;
                '-': c := a - b;
                '*': c := a * b;
                '/': c := a / b;
              end;
              prev := '';
              buf := FloatToStr(c);
            end;
          else
            n := False;
        end;
        if n then
          SetWindowText(edit, PChar(buf));
      end;
    end;


    WindowProc := DefWindowProc(Window, AMessage, WParam, LParam);
  end;


  procedure addControls();
  var
    i, j, k: byte;
  begin
    //Add editbox
    edit := CreateWindow('edit', '', WS_CHILD or WS_VISIBLE or
      ES_RIGHT, 15, 10, 140, 30, win, HMENU(EDITBOX), HINSTANCE, nil);
    //Add num-button
    k := 0;
    for j := 1 to 3 do
      for i := 1 to 3 do
      begin
        Inc(k);
        CreateWindow('button', PChar(IntToStr(k)), WS_CHILD or
          WS_VISIBLE or BS_DEFPUSHBUTTON, (i - 1) * 50 + 10, j *
          50, 50, 50, win, HMENU(100 + k), HINSTANCE, nil);
      end;
    //Add bot-button
    CreateWindow('button', '0', WS_CHILD or WS_VISIBLE or BS_DEFPUSHBUTTON,
      10, 200, 50, 50, win, hmenu(100), HINSTANCE, nil);
    CreateWindow('button', '.', WS_CHILD or WS_VISIBLE or BS_DEFPUSHBUTTON,
      60, 200, 50, 50, win, hmenu(BUTDOT), HINSTANCE, nil);
    CreateWindow('button', '=', WS_CHILD or WS_VISIBLE or BS_DEFPUSHBUTTON,
      110, 200, 50, 50, win, hmenu(BUTCAL), HINSTANCE, nil);
    //Add operations
    CreateWindow('button', 'Clear', WS_CHILD or WS_VISIBLE or
      BS_DEFPUSHBUTTON, 160, 0, 50, 50, win, HMENU(BUTCLEAR), HINSTANCE, nil);
    CreateWindow('button', '+', WS_CHILD or WS_VISIBLE or BS_DEFPUSHBUTTON,
      160, 50, 50, 50, win, HMENU(BUTA), HINSTANCE, nil);
    CreateWindow('button', '-', WS_CHILD or WS_VISIBLE or BS_DEFPUSHBUTTON,
      160, 100, 50, 50, win, HMENU(BUTS), HINSTANCE, nil);
    CreateWindow('button', '*', WS_CHILD or WS_VISIBLE or BS_DEFPUSHBUTTON,
      160, 150, 50, 50, win, HMENU(BUTM), HINSTANCE, nil);
    CreateWindow('button', '/', WS_CHILD or WS_VISIBLE or BS_DEFPUSHBUTTON,
      160, 200, 50, 50, win, HMENU(BUTD), HINSTANCE, nil);
  end;


begin
  // Register class
  wc.style := CS_HREDRAW or CS_VREDRAW;
  wc.lpfnWndProc := WindowProc;
  wc.hInstance := HINSTANCE;
  wc.hCursor := LoadCursor(0, IDC_ARROW);
  wc.hbrBackground := GetStockObject(GRAY_BRUSH);
  wc.lpszClassName := appName;


  // Check class and create Window
  if RegisterClass(wc) = 0 then
  begin
    raise Exception.Create('Cannot register class');
    exit;
  end;
  win := CreateWindow(appName, 'Pascal simple calculator', WS_OVERLAPPEDWINDOW,
    CW_USEDEFAULT, CW_USEDEFAULT, 235, 300, 0, 0, HINSTANCE, nil);


  // Check window and add control
  if longint(win) = 0 then
  begin
    raise Exception.Create('Cannot create window');
    exit;
  end;
  addControls();
  ShowWindow(win, SW_SHOW);
  UpdateWindow(win);


  // Message loop
  while GetMessage(msg, 0, 0, 0) do
  begin
    TranslateMessage(msg);
    DispatchMessage(msg);
  end;
  Halt(msg.wParam);
end.