API有隐藏的MessageBoxTimeOut函数可以做计时对话框,缺点是不能显示还剩下多少秒关闭。
- const
- IDTIMEDOUT = 32000;
-
- function MessageBoxTimeOut(hWnd: HWND;
- lpText: PChar; lpCaption: PChar; uType: UINT; wLanguageId: WORD;
- dwMilliseconds: DWORD): Integer; stdcall; external
- user32 name 'MessageBoxTimeoutA';
其实只要获得对话框的句柄就可以用时间器修改窗体或者按钮标题实现倒计时提示。 窗体激活后可以通过Hook CBT捕获HCBT_ACTIVATE 参考如下方法:
- var
- hookHandle: THandle;
- dialogHandle: THandle;
- second: Integer;
-
- function CBTHookCallback(nCode: Integer;
- wParam: WPARAM;
- lParam: LPARAM
- ): Integer; stdcall;
- begin
- case nCode of
- HCBT_ACTIVATE:
- begin
- dialogHandle := wParam;
- second := 5;
- UnhookWindowsHookEx(hookHandle);
- end;
- end;
- Result := CallNextHookEx(hookHandle, nCode, wParam, lParam);
- end;
-
- procedure TForm1.Button1Click(Sender: TObject);
- begin
- Timer1.Enabled := True;
- hookHandle := SetWindowsHookEx(WH_CBT, CBTHookCallback, HInstance, 0);
- MessageBox(Handle, 'Zswang 路过', '倒计时(5)', 0);
- end;
-
- procedure TForm1.Timer1Timer(Sender: TObject);
- begin
- Dec(second);
- if second <= 0 then
- begin
- PostMessage(dialogHandle, WM_CLOSE, 0, 0);
- TTimer(Sender).Enabled := False;
- end;
- SetWindowText(dialogHandle, PChar(Format('倒计时(%d)', [second])));
- end;
相对来说,C#中写得更麻烦一些,因为要自己什么API,如下是修改按钮标题的例子:
- public delegate IntPtr HookProc(int nCode, IntPtr wParam, IntPtr lParam);
-
- [DllImport("user32.dll")]
- public static extern IntPtr SetWindowsHookEx(int hookid,
- HookProc pfnhook, IntPtr hinst, int threadid);
-
- [DllImport("user32.dll")]
- public static extern IntPtr CallNextHookEx(IntPtr hhook,
- int code, IntPtr wparam, IntPtr lparam);
-
- [DllImport("kernel32.dll")]
- public static extern IntPtr GetModuleHandle(string modName);
-
- [DllImport("user32.dll")]
- public static extern bool UnhookWindowsHookEx(IntPtr hhook);
-
- public const int WH_CBT = 5;
- public const int HCBT_ACTIVATE = 5;
- IntPtr hookHandle = IntPtr.Zero;
-
- public delegate bool WNDENUMPROC(IntPtr hwnd, int lParam);
-
- [DllImport("user32.dll")]
- public static extern int EnumChildWindows(IntPtr hWndParent,
- WNDENUMPROC lpEnumFunc, int lParam);
-
- [DllImport("user32.dll")]
- public static extern int GetClassName(IntPtr hWnd, StringBuilder lpClassName,
- int nMaxCount);
-
- [DllImport("user32.dll")]
- public static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString,
- int nMaxCount);
-
- [DllImport("user32.dll")]
- public static extern void SetWindowText(IntPtr hwnd, string lpString);
-
- public bool EnumChild(IntPtr hwnd, int lParam)
- {
- StringBuilder vBuffer = new StringBuilder(256);
- GetClassName(hwnd, vBuffer, vBuffer.Capacity);
- if (vBuffer.ToString().ToLower() == "button")
- {
- StringBuilder vText = new StringBuilder(256);
- GetWindowText(hwnd, vText, vText.Capacity);
- if (vText.ToString().ToLower().IndexOf("&a") >= 0)
- SetWindowText(hwnd, "停不要动");
- if (vText.ToString().ToLower().IndexOf("&r") >= 0)
- SetWindowText(hwnd, "再来一次");
- if (vText.ToString().ToLower().IndexOf("&i") >= 0)
- SetWindowText(hwnd, "就这样吧");
- }
- return true;
- }
-
- private IntPtr CBTHookCallback(int nCode, IntPtr wParam, IntPtr lParam)
- {
- switch (nCode)
- {
- case HCBT_ACTIVATE:
- EnumChildWindows(wParam, new WNDENUMPROC(EnumChild), 0);
- UnhookWindowsHookEx(hookHandle);
- break;
- }
- return CallNextHookEx(hookHandle, nCode, wParam, lParam);
- }
-
-
- private void button1_Click(object sender, EventArgs e)
- {
- hookHandle = SetWindowsHookEx(WH_CBT, new HookProc(CBTHookCallback),
- GetModuleHandle(null), 0);
- MessageBox.Show("Zswang 路过", "提示", MessageBoxButtons.AbortRetryIgnore);
- }
-
http://blog.csdn.net/zswang/article/details/3081063
|
请发表评论