怎么取无窗口程序句柄(200分)

  • 主题发起人 无疯无禄
  • 开始时间

无疯无禄

Unregistered / Unconfirmed
GUEST, unregistred user!
program test; <br><br>uses <br>&nbsp; SysUtils, <br>&nbsp; Windows; <br>var <br>&nbsp; HMutex: THandle; <br>&nbsp; h:THandle; &nbsp;//应用程序句柄 <br><br>begin <br>&nbsp; <br>&nbsp; //程序只执行一次 <br>&nbsp; HMutex := CreateMutex(nil, False, '54BAB749-13BA-4468-84D6-08ABC2A6F837'); <br>&nbsp; if WaitForSingleObject(hMutex, 0) = wait_TimeOut then <br>&nbsp; begin <br>&nbsp; &nbsp; Exit; <br>&nbsp; end; <br>&nbsp; <br>&nbsp; h := //这里怎么取应用程序句柄 <br>&nbsp; //这里不用Application.Handle; 这种方式。因为有些原因需要应用程序比较小。用 Application 对象的话,就可引用Forms单元,应用程序就会变大了很多。 <br>&nbsp; //没有引用Forms时只有40K多,用了Forms后就变成360多K了~~想看这里有没有直接用API函数来取得的 <br><br>end.
 
你要这个 handle 主要为了于外界消息通讯 ?<br>用这个:<br><br>uses<br>&nbsp; SysUtils, Messages, Windows;<br><br>type<br>&nbsp; TWndMethod = procedure(var Message: TMessage) of object;<br><br>////////////////////////////////////////////////////////////////////////////////<br>//设计: Lsuper 2005.07.16<br>//功能: 内部创建一个窗体<br>//参数:<br>////////////////////////////////////////////////////////////////////////////////<br>function AllocateHWndEx(Method: TWndMethod; const AClassName: string): HWND;<br>&nbsp; ////////////////////////////////////////////////////////////////////////////////<br>&nbsp; //设计: Lsuper 2005.07.16<br>&nbsp; //功能: 窗体回调运行函数<br>&nbsp; //参数:<br>&nbsp; ////////////////////////////////////////////////////////////////////////////////<br>&nbsp; function StdWndProc(Window: HWND; Message, WParam: WPARAM;<br>&nbsp; &nbsp; LParam: LPARAM): LRESULT; stdcall;<br>&nbsp; var<br>&nbsp; &nbsp; Msg: Messages.TMessage;<br>&nbsp; &nbsp; WndProc: TWndMethod;<br>&nbsp; begin<br>&nbsp; &nbsp; TMethod(WndProc).Code := Pointer(GetWindowLong(Window, 0));<br>&nbsp; &nbsp; TMethod(WndProc).Data := Pointer(GetWindowLong(Window, 4));<br>&nbsp; &nbsp; if Assigned(WndProc) then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; Msg.Msg := Message;<br>&nbsp; &nbsp; &nbsp; Msg.WParam := WParam;<br>&nbsp; &nbsp; &nbsp; Msg.LParam := LParam;<br>&nbsp; &nbsp; &nbsp; Msg.Result := 0;<br>&nbsp; &nbsp; &nbsp; WndProc(Msg);<br>&nbsp; &nbsp; &nbsp; Result := Msg.Result;<br>&nbsp; &nbsp; end<br>&nbsp; &nbsp; else Result := DefWindowProc(Window, Message, WParam, LParam);<br>&nbsp; end;<br>var<br>&nbsp; TempClass: TWndClass;<br>&nbsp; UtilWindowExClass: TWndClass;<br>&nbsp; ClassRegistered: Boolean;<br>begin<br>&nbsp; FillChar(UtilWindowExClass, SizeOf(TWndClass), #0);<br>&nbsp; with UtilWindowExClass do<br>&nbsp; begin<br>&nbsp; &nbsp; cbWndExtra := SizeOf(TMethod);<br>&nbsp; &nbsp; hInstance := SysInit.HInstance;<br>&nbsp; &nbsp; lpfnWndProc := @DefWindowProc;<br>&nbsp; &nbsp; lpszClassName := PChar(AClassName)<br>&nbsp; end;<br><br>&nbsp; ClassRegistered := Windows.GetClassInfo(HInstance, UtilWindowExClass.lpszClassName,<br>&nbsp; &nbsp; TempClass);<br>&nbsp; if not ClassRegistered or (TempClass.lpfnWndProc &lt;&gt; @DefWindowProc) then<br>&nbsp; begin<br>&nbsp; &nbsp; if ClassRegistered then<br>&nbsp; &nbsp; &nbsp; Windows.UnregisterClass(UtilWindowExClass.lpszClassName, HInstance);<br>&nbsp; &nbsp; Windows.RegisterClass(UtilWindowExClass);<br>&nbsp; end;<br>&nbsp; Result := Windows.CreateWindowEx(Windows.WS_EX_TOOLWINDOW, UtilWindowExClass.lpszClassName,<br>&nbsp; &nbsp; '', Windows.WS_POPUP, 0, 0, 0, 0, 0, 0, HInstance, nil);<br><br>&nbsp; if Assigned(Method) then<br>&nbsp; begin<br>&nbsp; &nbsp; Windows.SetWindowLong(Result, 0, Longint(TMethod(Method).Code));<br>&nbsp; &nbsp; Windows.SetWindowLong(Result, SizeOf(TMethod(Method).Code), Longint(TMethod(Method).Data));<br>&nbsp; &nbsp; Windows.SetWindowLong(Result, GWL_WNDPROC, Longint(@StdWndProc));<br>&nbsp; end;<br>end;<br><br>////////////////////////////////////////////////////////////////////////////////<br>//设计: Lsuper 2005.07.16<br>//功能: 释放窗口过程一个窗体<br>//参数:<br>////////////////////////////////////////////////////////////////////////////////<br>procedure DeallocateHWndEx(Wnd: HWND);<br>begin<br>&nbsp; Windows.DestroyWindow(Wnd);<br>end;
 
路过....学习..
 
顶部