![]() |
| ||
|
|
Новости |
Поиск |
FAQ |
Статьи |
Книги |
Новости VCL
| | Форумы ЧАТ |
Орешник |
|
| |||
| |||
« предыдущая ветвь | форум | следующая ветвь »
Написание программ в Delphi на WinApi [D7, WinXP]
yahoo © (2008-04-20 20:45:00)Решил попробовать писать программы на WinApi,пока только получилось создать саму форму ,батоны и едиты...Решил сделать калькулятор...
Сначало все работало , хз из за чего потом перестало норм брать текст из едита...Вообщем вопрос по существу где тут ошибка?
Код урезал) только кнопка "0" и едит
program Project1;
uses
windows,
messages,sysutils;
const
id_Button0 = 100;
id_edit = 110;
var
cymm:string;
function MainWndProc(hWindow: HWND; Msg: UINT; wParam: wParam;
lParam: lParam): LRESULT; stdcall; export;
var
Rect: TRect;
ps: TPaintStruct;
s:Pansichar;
s2:string;
begin
cymm:='0';
Result := 0;
case Msg of
wm_Create: begin
CreateWindowEx (0, // extended styles
'BUTTON', // predefined class
'&0', // caption
ws_Child or ws_Visible or ws_Border
or bs_PushButton, // styles
25, 110, // position: see wm_Size
45, 20, // size
hWindow, // parent
id_Button0, // identifier (not a menu handle)
hInstance, // application id
nil); // init info pointer
CreateWindowEx (0, // extended styles
'EDIT', // predefined class
'', // text
ws_Child or ws_Visible or ws_Border
or ES_RIGHT, // styles
10, 5, // position: see wm_Size
120, 20, // size
hWindow, // parent
id_edit, // identifier (not a menu handle)
hInstance, // application id
nil); // init info pointer
end;
wm_Size:
begin
GetClientRect (hWindow, Rect);
end;
WM_PAINT:
begin
BeginPaint(hWindow, ps);
SetBkMode(ps.hdc, TRANSPARENT);
EndPaint(hWindow, ps);
end;
WM_DESTROY: PostQuitMessage(0);
wm_Command:begin
case LoWord (wParam) of
id_Button0:
if HiWord (wParam) = bn_Clicked then begin
GetWindowText(GetDlgItem (hWindow, id_edit),s,50);
s2:= s;
s2:=s2+'0';
s:=Pchar(s2);
SetWindowText(GetDlgItem (hWindow, id_edit),s);
end end
end
else
begin
Result := DefWindowProc(hWindow, Msg, wParam, lParam);
Exit;
end;
end;
end;
var
wc: TWndClass;
hWindow: HWND;
Msg: TMsg;
begin
wc.lpszClassName := 'YourAppClass';
wc.lpfnWndProc := @MainWndProc;
wc.Style := CS_VREDRAW or CS_HREDRAW;
wc.hInstance := hInstance;
wc.hIcon := LoadIcon(0, IDI_APPLICATION);
wc.hCursor := LoadCursor(0, IDC_ARROW);
wc.hbrBackground := (COLOR_WINDOW + 1);
wc.lpszMenuName := nil;
wc.cbClsExtra := 0;
wc.cbWndExtra := 0;
RegisterClass(wc);
hWindow := CreateWindowEx(WS_EX_CONTROLPARENT or WS_EX_WINDOWEDGE,
'YourAppClass',
'API',
WS_VISIBLE or WS_CLIPSIBLINGS or
WS_CLIPCHILDREN or WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
150, 200 ,
0,
0,
hInstance,
nil);
ShowWindow(hWindow, CmdShow);
UpDateWindow(hWindow);
while GetMessage(Msg, 0, 0, 0) do
begin
TranslateMessage(Msg);
DispatchMessage(Msg);
end;
Halt(Msg.wParam);
end.
P.S. дайте плиз линки на норм инфу об WinApi и примеры создания компанентов.
Германн (2008-04-21 02:07:00) [1]
> yahoo © (20.04.08 23:34) [35]
>
> *ушел учить аглицкий*
>
Поздравляю! Ты очень быстро схватываешь что именно нужно для программирования. Я серьезно.
yahoo (2008-04-20 23:34:00) [2]*ушел учить аглицкий*
Dmitry S (2008-04-20 22:48:00) [3]
> а есть справочник по WinApi для Delphi?
WinApi на то и WinApi, что он одинаковый для всех:)
А справочник находится в Help -> Windows SDK
DVM (2008-04-20 22:47:00) [4]
> а есть справочник по WinApi для Delphi?
msdn.microsoft.com
yahoo (2008-04-20 22:45:00) [5]хмм...спасибо ) чет сразу сам не подумал ))...а есть справочник по WinApi для Delphi?
Dmitry S (2008-04-20 22:39:00) [6]GetWindowText(ch,s,sl+1);
SetWindowText(ch,PChar(s+'0'), sl+1);
Dmitry S (2008-04-20 22:38:00) [7]
> ch := GetDlgItem (hWindow, id_edit);
> sl := GetWindowTextLength(ch);
> getMem(s, sl+1);
> ZeroMemory(s, sl+1);
> GetWindowText(ch,s,50);
> s2:= s;
> s2:=s2+'0';
> s:=Pchar(s2);
> SetWindowText(GetDlgItem (hWindow, id_edit),
> s);> freeMem(s)
ндаааа...
замени на
GetWindowText(ch,s,50);
SetWindowText(ch,PChar(s+'0'), sl+1);
DVM (2008-04-20 22:37:00) [8]
> sl := GetWindowTextLength(ch);
> getMem(s, sl+1);
> ZeroMemory(s, sl+1);
> GetWindowText(ch,s,50);
тогда уж 50 уберите.
Сергей М, (2008-04-20 22:36:00) [9]
> Halt(Msg.wParam);
Это что такое ?
yahoo (2008-04-20 22:33:00) [10]
> Dmitry S © (20.04.08 22:20) [23]
> http://argi.ru/heart3-w.rar
спасибо
yahoo (2008-04-20 22:32:00) [11]
program Project1;
uses
windows,
messages,sysutils;
const
id_Button0 = 100;
id_edit = 110;
function MainWndProc(hWindow: HWND; Msg: UINT; wParam: wParam;
lParam: lParam): LRESULT; stdcall; export;
var
Rect: TRect;
ps: TPaintStruct;
s2:string;
ch: Thandle;
s: PChar;
sl: Integer;
begin
Result := 0;
case Msg of
wm_Create:
begin
CreateWindowEx (0,'BUTTON','&0',ws_Child or ws_Visible
or ws_Border or bs_PushButton,25, 110,45, 20,hWindow,
id_Button0, hInstance, nil);
CreateWindowEx (0,'EDIT','',
ws_Child or ws_Visible or ws_Border or ES_RIGHT,
10, 5,120, 20,hWindow,id_edit, hInstance,nil);
end;
wm_Size:
begin
GetClientRect (hWindow, Rect);
end;
WM_PAINT:
begin
BeginPaint(hWindow, ps);
SetBkMode(ps.hdc, TRANSPARENT);
EndPaint(hWindow, ps);
end;
WM_DESTROY: begin freeMem(s);PostQuitMessage(0);end;
wm_Command:begin
case LoWord (wParam) of
id_Button0:
if HiWord (wParam) = bn_Clicked then begin
ch := GetDlgItem (hWindow, id_edit);
sl := GetWindowTextLength(ch);
getMem(s, sl+1);
ZeroMemory(s, sl+1);
GetWindowText(ch,s,50);
s2:= s;
s2:=s2+'0';
s:=Pchar(s2);
SetWindowText(GetDlgItem (hWindow, id_edit),s);
freeMem(s)
end;
end;
end;
else
begin
Result := DefWindowProc(hWindow, Msg, wParam, lParam);
Exit;
end;
end;
end;
var
wc: TWndClass;
hWindow: HWND;
Msg: TMsg;
begin
wc.lpszClassName := 'YourAppClass';
wc.lpfnWndProc := @MainWndProc;
wc.Style := CS_VREDRAW or CS_HREDRAW;
wc.hInstance := hInstance;
wc.hIcon := LoadIcon(0, IDI_APPLICATION);
wc.hCursor := LoadCursor(0, IDC_ARROW);
wc.hbrBackground := (COLOR_WINDOW + 1);
wc.lpszMenuName := nil;
wc.cbClsExtra := 0;
wc.cbWndExtra := 0;
RegisterClass(wc);
hWindow := CreateWindowEx(WS_EX_CONTROLPARENT or WS_EX_WINDOWEDGE,
'YourAppClass',
'API',
WS_VISIBLE or WS_CLIPSIBLINGS or
WS_CLIPCHILDREN or WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
150, 200 ,
0,
0,
hInstance,
nil);
ShowWindow(hWindow, CmdShow);
UpDateWindow(hWindow);
while GetMessage(Msg, 0, 0, 0) do
begin
TranslateMessage(Msg);
DispatchMessage(Msg);
end;
Halt(Msg.wParam);
end.
Dmitry S (2008-04-20 22:22:00) [12]дай весь код
yahoo (2008-04-20 22:21:00) [13]вообще когда доходит прога до freeMem(s); выдает "invalid pointer operation" ... =(
Dmitry S (2008-04-20 22:20:00) [14]http://argi.ru/heart3-w.rar
> freeMem(s)
после того, как s уже не нужно.
yahoo (2008-04-20 22:19:00) [15]
> Dmitry S © (20.04.08 21:56) [16]
> ch: Thandle;s: PChar;sl: Integer;ch := GetDlgItem (hWindow,
> id_edit);sl := GetWindowTextLength(ch);getMem(s, sl+1);
> ZeroMemory(s, sl+1);GetWindowText(ch,s,50);...freeMem(s)
freeMem(s) при закрытии программы что ли? а то если делать после ее использования ,то выдает "invalid pointer operation"
Dmitry S (2008-04-20 22:17:00) [16]
> ! ...\heart3.rar: Ошибка CRC в Heart3.exe, файл повреждён
> ! ...\heart3.rar: Неожиданный конец архива
>
> *.res нету ) и ехе не обезательно в архив
ща перезалью
DVM (2008-04-20 22:00:00) [17]
> уже сам нашел в чем баг, надо просто объявить не в функции
> а выше...
баг ты не нашел, ты его переместил.
yahoo (2008-04-20 22:00:00) [18]
> DVM © (20.04.08 21:58) [17]
> > yahoo © (20.04.08 21:54) [15] > полный код в первом
> посте... в полном коде я тоже не вижу ни выделения памяти
> под S ни даже определения того сколько ее (памяти) надо
> выделять.
спасибо, буду знать
yahoo (2008-04-20 21:58:00) [19]! ...\heart3.rar: Ошибка CRC в Heart3.exe, файл повреждён
! ...\heart3.rar: Неожиданный конец архива
*.res нету ) и ехе не обезательно в архив
DVM (2008-04-20 21:58:00) [20]
> yahoo © (20.04.08 21:54) [15]
> полный код в первом посте...
в полном коде я тоже не вижу ни выделения памяти под S ни даже определения того сколько ее (памяти) надо выделять.