Autor Beitrag
Burgpflanze
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 67

Windows2000 Prof. SP4
Delphi7 Enterprise
BeitragVerfasst: So 15.02.04 07:24 
Warum bekomme ich in meiner SubClass-Function die WM_COMMAND-Message nicht?

ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
program test;

uses
  Windows,
  Messages;

var
  MainWnd, Button: THandle;

function SubClass(hWnd, uMsg: Cardinal; wParam, lParam: Integer): Integer; stdcall;
begin
  Result := 0;

  case uMsg of
    WM_COMMAND: MessageBox(MainWnd, 'klick', 'button', 0);
  else
    Result := CallWindowProc(Pointer(GetProp(Button, 'OrigProc')), hWnd, uMsg, wParam, lParam);
  end;
end;

function MainProc(hWnd, uMsg: Cardinal; wParam, lParam: Integer): Integer; stdcall;
begin
  Result := 0;

  case uMsg of
    WM_CREATE:
      begin
        Button := CreateWindow('Button', 'Test', WS_CHILD or WS_VISIBLE or WS_TABSTOP or BS_PUSHBUTTON,  
            10, 10,  120,  25, hWnd, 101, hInstance, nil);
        SetProp(Button, 'OrigProc', SetWindowLong(Button, GWL_WNDPROC, Integer(@SubClass)));
      end;

    WM_DESTROY:
      begin
        SetWindowLong(Button, GWL_WNDPROC, GetProp(Button, 'OrigProc'));
        PostQuitMessage(0);
      end;
  else
    Result := DefWindowProc(hWnd, uMsg, wParam, lParam);
  end;
end;

var
  Msg: TMsg;
  wc: TWndClassEx;

begin
  wc.cbSize := SizeOf(wc);
  wc.style := CS_HREDRAW or CS_VREDRAW;
  wc.lpfnWndProc := @MainProc;
  wc.cbClsExtra := 0;
  wc.cbWndExtra := 0;
  wc.hInstance := hInstance;
  wc.hIcon := LoadIcon(0, IDI_APPLICATION);
  wc.hCursor := LoadCursor(0, IDC_ARROW);
  wc.hbrBackground := HBRUSH(COLOR_BTNFACE + 1);
  wc.lpszMenuName := nil;
  wc.lpszClassName := 'Window';
  wc.hIconSm := 0;

  RegisterClassEx(wc);

  MainWnd := CreateWindowEx(WS_EX_CONTROLPARENT, 'Window', 'Test', WS_OVERLAPPEDWINDOW, 
      10, 10, 400, 400, 0, 0, hInstance, nil);

  ShowWindow(MainWnd, SW_SHOW);
  UpdateWindow(MainWnd);

  while GetMessage(Msg, 0, 0, 0) do
  begin
    TranslateMessage(Msg);
    DispatchMessage(Msg);
  end;

  ExitCode := Msg.wParam;
  DestroyWindow(MainWnd);
end.


Moderiert von user profile iconDeCodeGuru: Code- durch Delphi-Tags ersetzt

_________________
Gruss, Burgpflanze
Tana´Ri
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 230



BeitragVerfasst: Mo 16.02.04 11:29 
Hi

ich habs so jemacht

ausblenden Delphi-Quelltext
1:
2:
3:
4:
{ zum merken der UrWndProc und }
 SetProp(BUTTONCLOSE,PChar('InhWndProc'),DWord(GetWindowLong(BUTTONCLOSE,GWL_WNDPROC)));
{ zum setzen der eigenen WndProc }
 SetWindowLong(BUTTONCLOSE,GWL_WNDPROC,Integer(@SubClass));


Hast du mal WM_COMMAND nachgelesen, die Nachricht kommt von den Controls selbst,
diese senden sie zum übergeordneten Fenseter (Parent), ich habe es so gelöst:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
function WndProc(hWnd: HWND; uMsg: UINT; wParam: WPARAM; lParam: LPARAM): LRESULT; stdcall;
 begin
  Result := 0;
  case uMsg of
    WM_DESTROY:
      begin
       PostQuitMessage(0);
      end;
    WM_CREATE:
      begin
       BUTTONCLOSE := createwindow('BUTTON', 'Beenden', WS_VISIBLE or WS_CHILD, 10, 50, 100, 30, hwnd, IDCLOSE, hInstance, nil);
       SetProp(BUTTONCLOSE,PChar('InhWndProc'),DWord(GetWindowLong(BUTTONCLOSE,GWL_WNDPROC)));
       SetWindowLong(BUTTONCLOSE,GWL_WNDPROC,Integer(@SubClass));
      end;
    WM_COMMAND:
      begin
       case HiWord(WPARAM) of
            BN_CLICKED : if LoWord(WPARAM) = IDCLOSE then PostMessage(BUTTONCLOSE,WM_COMMAND,0,0); // send back

       end;
      end;
  else
    Result := DefWindowProc(hWnd, uMsg, wParam, lParam);
  end;
end;

_________________
mfg
Tana´Ri