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: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177:
| Unit Control;
Interface
Uses Windows, Messages;
Type ponMessage = Function(vWindowHandle: hWnd; vMsg: uInt; wParam: wParam; lParam: lParam): lResult; pControl = ^tControl; tControl = Class Constructor Create; Destructor Destroy; Override; Private Handle: hWnd; Procedure WNDProc(Var vMessage: tMessage); Public onMessage: ponMessage; Published End;
Implementation
Const InstanceCount = 313;
Type tWndMethod = Procedure(Var vMessage: TMessage) of Object;
Type pObjectInstance = ^tObjectInstance; tObjectInstance = Packed Record Code: Byte; Offset: Integer; Case Integer of 0: (Next: pObjectInstance); 1: (Method: tWndMethod); End;
Type pInstanceBlock = ^tInstanceBlock; tInstanceBlock = Packed Record Next: pInstanceBlock; Code: Array[1..2]of Byte; WndProcPtr: Pointer; Instances: Array[0..InstanceCount]of tObjectInstance; End;
Var InstBlockList: pInstanceBlock; InstFreeList: pObjectInstance;
Function StdWndProc(vWindow: hWnd; vMessage, wParam, lParam: LongInt): LongInt; StdCall; Assembler; Asm Xor EAX, EAX; Push EAX; Push lParam; Push wParam; Push vMessage; Mov EDX, ESP; Mov EAX, [ECX].LongInt[4]; Call [ECX].Pointer; Add ESP, 12; Pop EAX; End;
Function CalcJmpOffset(vSrc, vDest: Pointer): LongInt; Begin Result:=LongInt(vDest) - (LongInt(vSrc) + 5); End;
Function MakeObjectInstance(vMEthod: tWndMethod): Pointer; Const BlockCode: Array[1..2]of Byte = ($59, $E9 ); Var Block: pInstanceBlock; Instance: pObjectInstance; Begin If (InstFreeList = NIL) Then Begin Block:=VirtualAlloc(NIL, 4, mem_Commit, Page_Execute_ReadWrite); Block^.Next:=InstBlockList; Move(BlockCode, Block^.Code, SizeOf(BlockCode)); Block^.WndProcPtr:=Pointer(CalcJmpOffset(@Block^.Code[2], @StdWndProc)); Instance:=@Block^.Instances; Repeat Instance^.Code:=$E8; Instance^.Offset:=CalcJmpOffset(Instance, @Block^.Code); Instance^.Next:=InstFreeList; InstFreeList:=Instance; Inc(LongInt(Instance), SizeOf(tObjectInstance)); Until (LongInt(Instance) - LongInt(Block) >= SizeOf(tInstanceBlock)); InstBlockList:=Block; End; Result:=InstFreeList; Instance:=InstFreeList; InstFreeList:=Instance^.Next; Instance^.Method:=vMethod; End;
Procedure FreeObjectInstance(vObjectInstance: Pointer); Begin If (vObjectInstance <> NIL) Then Begin pObjectInstance(vObjectInstance)^.Next:=InstFreeList; InstFreeList:=vObjectInstance; End; End;
Var UtilWindowClass: tWndClass = (Style: 0; lpfnWndProc: @DefWindowProc; cbWndExtra: 0; hInstance: 0; hIcon: 0; hCursor: 0; hbrBackground: 0; lpszMenuName: NIL; lpszClassName: 'NoName' );
Function CreateWindow(vWndMethod: tWndMethod; vWindowClassName, vWindowCaption: pChar): hWnd; Var TempClass: tWndClass; ClassRegisted: Boolean; Begin UtilWindowClass.hInstance:=hInstance; UtilWindowClass.lpfnWndProc:=@DefWindowProc; UtilWindowClass.lpszClassName:=vWindowClassName; ClassRegisted:=GetClassInfo(hInstance, UtilWindowClass.lpszClassName, TempClass); If Not(ClassRegisted) or (TempClass.lpfnWndProc<>@DefWindowProc) Then Begin If (ClassRegisted) Then Windows.UnregisterClass(UtilWindowClass.lpszClassName, hInstance); Windows.RegisterClass(UtilWindowClass); End; Result:=CreateWindowEx(ws_Ex_ToolWindow, UtilWindowClass.lpszClassName, vWindowCaption, ws_Popup , 0, 0, 0, 0, 0, 0, hInstance, NIL); If (Assigned(vWndMethod)) Then SetWindowLong(Result, gwl_WndProc, LongInt(MakeObjectInstance(vWndMethod))); End;
Procedure DestroyWindow(vWindowHandle: hWnd); Var Instance: Pointer; Begin Instance:=Pointer(GetWindowLong(vWindowHandle, gwl_WndProc)); Windows.DestroyWindow(vWindowHandle); If (Instance <> @DefWindowProc) Then FreeObjectInstance(Instance); End;
Constructor tControl.Create; Begin Inherited; Handle:=CreateWindow(Self.WNDProc, 'TestClass', 'Test Fenster'); End;
Destructor tControl.Destroy; Begin DestroyWindow(Handle); Inherited; End;
Procedure tControl.WNDProc(Var vMessage: tMessage); Begin Exit; End;
END. |