Notepad API

Posted on March 9, 2010, 10:25 pm UTC by Iago Sousa (about 1 year ago)

Code (highlighted for Delphi)

  1. program notepad;
  2.  
  3. uses
  4.   Windows;
  5.  
  6. const
  7.   clss    = 'notepadAPI001';
  8.   ID_NEW  = $1064;
  9.   ID_OPEN = $1065;
  10.   ID_SAVE = $1066;
  11.   ID_EXIT = $1067;
  12.   ID_ALWA = $1068;
  13.  
  14. var
  15.   WinClss       :TWndClassExA;
  16.   handle, memo  :HWND;
  17.   menu, submenu,
  18.   sysmenu       :Cardinal;
  19.   menut         :TMenuItemInfoA;
  20.   uMsg          :TMsg;
  21.   Inst          :Cardinal;
  22.   Width         :Integer=400;
  23.   Height        :Integer=400;
  24.   Text          :string='Close';
  25.   Always        :Boolean=FALSE;
  26.  
  27. function WindowProc(hwnd, uint, wparam, lparam: Integer): integer; stdcall;
  28. begin
  29.   if (uint=$0010) then
  30.     DestroyWindow(handle);
  31.   if (uint=$0002) then
  32.   begin
  33.     PostQuitMessage(0);
  34.     Halt;
  35.   end;
  36.   if ((uint=$00111) and (uMsg.wParam=ID_NEW)) then
  37.   begin
  38.     Text:='';
  39.     SendMessage(memo, $000C, 0, Integer(Text));
  40.   end;
  41.   if ((uint=$0112) and (uMsg.wParam=ID_SAVE)) then
  42.     SendMessage(handle, $0010, 0, 0);
  43.   if ((uint=$0111) and (uMsg.wParam=ID_ALWA)) then
  44.   begin
  45.     if not Always then
  46.     begin
  47.       menu:=GetMenu(handle);
  48.       FillChar(menut, SizeOf(TMenuItemInfoA), #0);
  49.       menut.cbSize:=SizeOf(TMenuItemInfoA);
  50.       menut.fMask:=MIIM_TYPE or MIIM_ID or MIIM_STATE;
  51.       menut.fType:=MF_STRING;
  52.       menut.fState:=MF_CHECKED;
  53.       menut.wID:=ID_ALWA;
  54.       Text:='&Always on top';
  55.       menut.dwTypeData:=PChar(Text);
  56.       menut.cch:=Length(Text);
  57.       SetMenuItemInfoA(menu, ID_ALWA, false, menut);
  58.       SetWindowPos(Handle, HWND_TOPMOST, CW_USEDEFAULT, CW_USEDEFAULT, Width,
  59.       Height, SWP_NOACTIVATE or SWP_NOMOVE or SWP_NOSIZE);
  60.       Always:= True;
  61.     end
  62.     else
  63.     begin
  64.       menu := GetMenu(handle);
  65.       FillChar(menut, SizeOf(TMenuItemInfoA), #0);
  66.       menut.cbSize := SizeOf(TMenuItemInfoA);
  67.       menut.fMask := MIIM_TYPE or MIIM_ID or MIIM_STATE;
  68.       menut.fType := MF_STRING;
  69.       menut.fState := MF_UNCHECKED;
  70.       menut.wID := ID_ALWA;
  71.       Text := '&Always on top';
  72.       menut.dwTypeData := PChar(Text);
  73.       menut.cch := Length(Text);
  74.       SetMenuItemInfoA(menu, ID_ALWA, false, menut);
  75.       SetWindowPos(Handle, HWND_NOTOPMOST, CW_USEDEFAULT, CW_USEDEFAULT, Width,
  76.       Height, SWP_NOACTIVATE or SWP_NOMOVE or SWP_NOSIZE);
  77.       Always := False;
  78.     end;
  79.   end;
  80.   if ((uint=$0111) and (uMsg.wParam=ID_EXIT)) then
  81.     PostMessage(handle, $0010, 0, 0);
  82.  
  83.   Result := DefWindowProc(hwnd, uint, wparam, lparam);
  84. end;
  85.  
  86. begin
  87.   with WinClss do
  88.   begin
  89.     cbSize        := SizeOf(TWNDCLASSEX);
  90.     style         := CS_DBLCLKS;
  91.     lpfnWndProc   := @WindowProc;
  92.     cbClsExtra    := 0;
  93.     cbWndExtra    := 0;
  94.     hInstance     := Inst;
  95.     hIcon         := LoadIcon(0, IDI_APPLICATION);
  96.     hCursor       := LoadCursor(0, IDC_ARROW);
  97.     hbrBackground := COLOR_BACKGROUND;
  98.     lpszMenuName  := 0;
  99.     lpszClassName := clss;
  100.     hIconSm       := LoadIcon(0, IDI_APPLICATION);
  101.   end;
  102.   RegisterClassEx(WinClss);
  103.  
  104.   handle := CreateWindowEx(WS_EX_LEFT, clss, 'Notepad API',
  105.   WS_CAPTION or WS_VISIBLE or WS_SYSMENU or WS_MINIMIZEBOX or WS_MAXIMIZEBOX,
  106.   CW_USEDEFAULT, CW_USEDEFAULT, Width, Height, HWND_DESKTOP, 0, Inst, nil);
  107.  
  108.   memo   := CreateWindow('edit', '', WS_VISIBLE or WS_CHILD or ES_MULTILINE
  109.   or ES_WANTRETURN or WS_EX_LEFTSCROLLBAR or WS_EX_RIGHTSCROLLBAR or WS_VSCROLL
  110.   or WS_HSCROLL, 0, 0, Width - 7, Width - 48, handle, 0, Inst, nil);
  111.  
  112.   menu    := CreateMenu;
  113.   submenu := CreateMenu;
  114.   sysmenu := GetSystemMenu(handle, false);
  115.   FillChar(menut, SizeOf(TMenuItemInfoA), #0);
  116.   menut.cbSize := SizeOf(TMenuItemInfoA);
  117.   menut.fMask := MIIM_TYPE or MIIM_ID or MIIM_STATE;
  118.   menut.fType := MFT_STRING;
  119.   menut.wID := SC_CLOSE;
  120.   Text := 'Quit';
  121.   menut.dwTypeData := PChar(Text);
  122.   menut.cch := Length(Text);
  123.   SetMenuItemInfoA(sysmenu, SC_CLOSE, false, menut);
  124.   AppendMenuA(submenu, MF_STRING, ID_NEW, '&New');
  125.   AppendMenuA(submenu, MF_STRING, ID_OPEN, '&Open...');
  126.   AppendMenuA(submenu, MF_STRING, ID_EXIT, 'Exit');
  127.   AppendMenuA(menu, MF_STRING or MF_POPUP, submenu, '&File');
  128.   submenu := CreateMenu;
  129.   AppendMenuA(submenu, MF_STRING, ID_ALWA, '&Always on top');
  130.   AppendMenuA(menu, MF_UNCHECKED or MF_POPUP, submenu, '&View');
  131.   SetMenu(handle, menu);
  132.  
  133.   ShowWindow(handle, SW_SHOWNORMAL);
  134.   UpdateWindow(handle);
  135.  
  136.   while (GetMessage(uMsg, handle, 0, 0)) do
  137.   begin
  138.     TranslateMessage(uMsg);
  139.     DispatchMessage(uMsg);
  140.   end;
  141. end.
  142.  

Comments

Bem pequeno rs