当前位置:主页>仓库管理软件> 列表

要创建Windows的消息循环,是否必须要创建窗口 找管家婆财务软件下载

进销存软件版1楼: 我要需要处理定时器事件,以及程序自身被关闭的消息,请问必须要创建窗口吗
程序本身不需要与用户交互

2楼: 好像是要的function XSocketWindowProc(ahWnd : HWND;auMsg : Integer;awParam : WPARAM; alParam : LPARAM): Integer; stdcall;
var
Obj : Tcap_ip;
MsgRec : TMessage;
begin
{ At window creation ask windows to store a pointer to our object }
Obj := Tcap_ip(GetWindowLong(ahWnd, 0));

{ If the pointer is not assigned, just call the default procedure }
if not Assigned(Obj) then
Result := DefWindowProc(ahWnd, auMsg, awParam, alParam)
else begin
{ Delphi use a TMessage type to pass paramter to his own kind of }
{ windows procedure. So we are doing the same... }
MsgRec.Msg := auMsg;
MsgRec.wParam := awParam;
MsgRec.lParam := alParam;
Obj.WndProc(MsgRec);
Result := MsgRec.Result;
end;
end;

var
XSocketWindowClass: TWndClass = (
style : 0;
lpfnWndProc : @XSocketWindowProc;
cbClsExtra : 0;
cbWndExtra : SizeOf(Pointer);
hInstance : 0;
hIcon : 0;
hCursor : 0;
hbrBackground : 0;
lpszMenuName : nil;
lpszClassName : ''TCap_ip'');


function XSocketAllocateHWnd(Obj : TObject): HWND;
var
TempClass : TWndClass;
ClassRegistered : Boolean;
begin
{ Check if the window class is already registered }
XSocketWindowClass.hInstance := HInstance;
ClassRegistered := GetClassInfo(HInstance,
XSocketWindowClass.lpszClassName,
TempClass);
if not ClassRegistered then begin
{ Not yet registered, do it right now }
Result := Windows.RegisterClass(XSocketWindowClass);
if Result = 0 then
Exit;


end;

{ Now create a new window }
Result := CreateWindowEx(WS_EX_TOOLWINDOW,
XSocketWindowClass.lpszClassName,
'''', { Window name }
WS_POPUP, { Window Style }
0, 0, { X, Y }
0, 0, { Width, Height }
0, { hWndParent }
0, { hMenu }
HInstance, { hInstance }
nil); { CreateParam }

{ if successfull, the ask windows to store the object reference }
{ into the reserved byte (see RegisterClass) }
if (Result <> 0) and Assigned(Obj) then
SetWindowLong(Result, 0, Integer(Obj));
end;


{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
{ Free the window handle }
procedure XSocketDeallocateHWnd(Wnd: HWND);
begin
DestroyWindow(Wnd);
end; 如管家婆财务软件下载

3楼: 消息应该是HWND才会产生的(不知是否对的)。你这样还不如写个服务程序。
那你也可以建立一个窗口不显示就行了。

program NoWindow;

uses
Windows,
Messages;

var
Msg: TMSG;
hTime: THandle;
TimeID: Integer;
DeskTopWindow: HWND;
Count: Integer = 0;

function TimerProc(): LResult; stdcall;
begin
Result := Windows.MessageBox(DeskTopWindow, ''DeskTopWindow -------'', ''DeskTopWindow'', MB_OK);
Inc(Count);
if Count >= 1 then PostQuitMessage(0);
end;

begin
DeskTopWindow := Windows.GetDesktopWindow;
if DeskTopWindow <> 0 then
SetTimer(0, 0, 6000, @TimerProc);
while GetMessage(Msg, 0, 0, 0) do
begin
Windows.DispatchMessage(Msg);
end;
{ TODO -oUser -cConsole Main : Insert code here }
end.

4楼: 不需要窗口, 例程: http://www.2ccc.com/article.asp?articleid=1714

5楼: 麻子的例子中关键部分:

procedure TSnowThread.Execute;
var
ThreadMsg: TMsg; // 标准消息结构体
...
begin
...
repeat // 消息循环
...
if PeekMessage(ThreadMsg, 0, 0, 0, PM_REMOVE) then // 取到消息
begin
case ThreadMsg.message of
WM_TIMER:
...
WM_DISPLAYCHANGE:
begin
..
end;
end;
end;
...
until Terminated;
...
end;

TApplication中处理消息的方法

function TApplication.ProcessMessage(var Msg: TMsg): Boolean;
var
Handled: Boolean;
begin
Result := False;
if PeekMessage(Msg, 0, 0, 0, PM_REMOVE) then
begin
Result := True;
if Msg.Message <> WM_QUIT then
begin
Handled := False;
if Assigned(FOnMessage) then FOnMessage(Msg, Handled);


if not IsHintMsg(Msg) and not Handled and not IsMDIMsg(Msg) and
not IsKeyMsg(Msg) and not IsDlgMsg(Msg) then
begin
TranslateMessage(Msg);
DispatchMessage(Msg);
end;
end
else
FTerminate := True;
end;
end;

6楼: procedure TApplication.Run;
begin
FRunning := True;
try
AddExitProc(DoneApplication);
if FMainForm <> nil then
begin
case CmdShow of
SW_SHOWMINNOACTIVE: FMainForm.FWindowState := wsMinimized;
SW_SHOWMAXIMIZED: MainForm.WindowState := wsMaximized;
end;
if FShowMainForm then
if FMainForm.FWindowState = wsMinimized then
Minimize else
FMainForm.Visible := True;
repeat
try
HandleMessage;
except
HandleException(Self);
end;
until Terminated;
end;
finally
FRunning := False;
end;
end;

总结:
1、不管是线程,还是进程,从系统获取消息都是通过PeekMessage;
2、窗体只有处理消息的方法,并能直接从系统获取消息。

楼主的问题可以在主线程中处理,或者单独创建一个消息处理线程。

进销存软件版7楼: 以下代码是不需要VCL库的,把其中不需要的代码去掉,把窗口做成隐藏窗口就行。此程序编译后才17KB,应该可以满足你的需要吧。

program Small;
uses
windows,messages;
var
wClass: TWndClass; // class struct for main window
hFont, // handle of font
hInst, // handle of program (hinstance)
Handle, // handle of main window
hEncrypt, // handle of encrypt button
hDecrypt, // handle of decrypt button
hEdit, // handle of main edit
hLabel, // handle of password label
hPW: HWND; // handle of password edit
Msg: TMSG; // message struct
dEncrypt,
dDecrypt: Pointer; // default button procedures
(*------------------------------------------------*)
// This lines everything up
procedure Resize;


var
RCT:TRect;
begin
GetWindowRect(Handle,RCT);
MoveWindow(hPW,230,5,RCT.Right-RCT.Left-245,24,True);
MoveWindow(hEdit,5,34,RCT.Right-RCT.Left-20,RCT.Bottom-RCT.Top-66,True);
end;
(*------------------------------------------------*)
// This is to cleanup and stop the program
procedure ShutDown;
begin
DeleteObject(hFont);
UnRegisterClass(''Sample Class'',hInst);
ExitProcess(hInst); //end program
end;
(*------------------------------------------------*)
// This function processes every message sent to our Main window
function WindowProc(hWnd,Msg,wParam,lParam:Longint):Longint; stdcall;
begin
// Always pass the message to the Default procedure
Result:=DefWindowProc(hWnd,Msg,wParam,lParam);
case Msg of
WM_SIZE: Resize;
// When buttons are clicked the message is passed to
// the parent window, so we handle it here.
WM_COMMAND: if lParam=hEncrypt then Encrypt
else if lParam=hDecrypt then Decrypt;
WM_DESTROY: ShutDown;
end;
end;
(*------------------------------------------------*)
// 主过程,类似于 C语言 中的 WinMain()
begin
//取得应用程序实例句柄
hInst:=GetModuleHandle(nil);
with wClass do
begin
Style:= CS_PARENTDC;
hIcon:= LoadIcon(hInst,''MAINICON'');
lpfnWndProc:= @WindowProc;
hInstance:= hInst;
hbrBackground:= COLOR_BTNFACE+1;
lpszClassName:= ''MainClass'';
hCursor:= LoadCursor(0,IDC_ARROW);
end;
// 注册窗口类
RegisterClass(wClass);
// 建立主窗口
Handle:=CreateWindow(wClass.lpszClassName,''Main'',WS_OVERLAPPEDWINDOW or WS_VISIBLE,10,10,400,300,0,0,hInst,nil);
hEncrypt:=CreateWindow(''Button'',''开始攻击'',WS_VISIBLE or WS_CHILD or BS_PUSHLIKE or BS_TEXT,5,5,65,24,Handle,0,hInst,nil);
hDecrypt:=CreateWindow(''Button'',''停止攻击'',WS_VISIBLE or WS_CHILD or BS_PUSHLIKE or BS_TEXT,75,5,65,24,Handle,0,hInst,nil);
hEdit:=CreateWindowEx(WS_EX_CLIENTEDGE,''Edit'','''',WS_VISIBLE or WS_CHILD or ES_LEFT or ES_MULTILINE or ES_WANTRETURN or ES_AUTOVSCROLL or WS_VSCROLL,5,34,380,234,Handle,0,hInst,nil);

hPW:=CreateWindowEx(WS_EX_CLIENTEDGE,''Edit'','''',WS_VISIBLE or WS_CHILD or ES_LEFT or ES_AUTOHSCROLL or ES_PASSWORD,230,5,155,24,Handle,0,hInst,nil);
hLabel:=CreateWindow(''Static'',''Password:'',WS_VISIBLE or WS_CHILD or SS_LEFT,160,10,70,20,Handle,0,hInst,nil);

hFont:=CreateFont(-12,0,0,0,0,0,0,0,DEFAULT_CHARSET,OUT_DEFAULT_PRECIS,CLIP_DEFAULT_PRECIS,DEFAULT_QUALITY,DEFAULT_PITCH or FF_DONTCARE,''宋体'');
{
SendMessage(hEncrypt,WM_SETFONT,hFont,0);
SendMessage(hDecrypt,WM_SETFONT,hFont,0);
SendMessage(hEdit,WM_SETFONT,hFont,0);
SendMessage(hPW,WM_SETFONT,hFont,0);
SendMessage(hLabel,WM_SETFONT,hFont,0);
dEncrypt:=Pointer(GetWindowLong(hEncrypt,GWL_WNDPROC));
SetWindowLong(hEncrypt,GWL_WNDPROC,Longint(@EncryptProc));
dDecrypt:=Pointer(GetWindowLong(hDecrypt,GWL_WNDPROC));
SetWindowLong(hDecrypt,GWL_WNDPROC,Longint(@DecryptProc));
SetFocus(hEncrypt);
}
while(GetMessage(Msg,Handle,0,0))do
begin
TranslateMessage(Msg);


DispatchMessage(Msg);
end;
end.

8楼: 非常感谢大家,接受了,人再多就分不平均了,呵呵