VC创建定时关闭的MessageBox
- 格式:doc
- 大小:109.50 KB
- 文档页数:11
c#⾃动关闭MessageBox弹出的窗⼝⼀:我们都知道,MessageBox弹出的窗⼝是模式窗⼝,模式窗⼝会⾃动阻塞⽗线程的。
所以如果有以下代码:MessageBox.Show("内容',"标题");则只有关闭了MessageBox的窗⼝后才会运⾏下⾯的代码。
⽽在某些场合下,我们⼜需要在⼀定时间内如果在⽤户还没有关闭窗⼝时能⾃动关闭掉窗⼝⽽避免程序⼀直停留不前。
这样的话我们怎么做呢?上⾯也说了,MessageBox弹出的模式窗⼝会先阻塞掉它的⽗级线程。
所以我们可以考虑在MessageBox前先增加⼀个⽤于“杀”掉MessageBox窗⼝的线程。
因为需要在规定时间内“杀”掉窗⼝,所以我们可以直接考虑使⽤Timer类,然后调⽤系统API关闭窗⼝。
核⼼代码如下:[DllImport("user32.dll", EntryPoint = "FindWindow", CharSet=CharSet.Auto)]private extern static IntPtr FindWindow(string lpClassName, string lpWindowName);[DllImport("user32.dll", CharSet=CharSet.Auto)]public static extern int PostMessage(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam);public const int WM_CLOSE = 0x10;private void StartKiller(){Timer timer = new Timer();timer.Interval = 10000; //10秒启动timer.Tick += new EventHandler(Timer_Tick);timer.Start();}private void Timer_Tick(object sender, EventArgs e){KillMessageBox();//停⽌计时器((Timer)sender).Stop();}private void KillMessageBox(){//查找MessageBox的弹出窗⼝,注意MessageBox对应的标题IntPtr ptr = FindWindow(null,"标题");if(ptr != IntPtr.Zero){//查找到窗⼝则关闭PostMessage(ptr,WM_CLOSE,IntPtr.Zero,IntPtr.Zero);}}在需要的地⽅先调⽤ StartKiller ⽅法再使⽤Messagebox.Show()⽅法即可达到⾃动关闭 MessageBox 的效果。
messageboxtimeout的用法access在Access中,想要创建一个MessageBox而不是使用VBA语言编写代码,可以使用MsgBox函数,该函数的参数中有一个可选参数MsgBoxTimeout。
MsgBoxTimeout参数使得可以在指定的时间内设置MessageBox的超时。
以下是其用法的详细解释。
MsgBoxTimeout是一个可选的参数,它是用于设置MessageBox超时的时间的。
其默认值为0,表示不设置超时时间。
如果在MessageBox超时之前用户没有对其进行任何操作,该MessageBox将会自动关闭。
MsgBox函数的完整语法如下:MsgBox(Prompt[, Buttons] [, Title] [, HelpFile] [, Context]) ,其中方括号表示可选参数。
首先,我们需要知道MsgBox函数中的Buttons参数。
在此参数中,可以使用常量来设置MessageBox的按钮组合,以确定用户可以点击的选项。
常见的常量有:vbOKOnly(0):只显示一个“确定”按钮。
vbOKCancel(1):显示“确定”和“取消”按钮。
vbAbortRetryIgnore(2):显示“中止”、“重试”和“忽略”按钮。
vbYesNoCancel(3):显示“是”、“否”和“取消”按钮。
vbYesNo(4):显示“是”和“否”按钮。
vbRetryCancel(5):显示“重试”和“取消”按钮。
下面是一个示例代码,展示了如何使用MsgBoxTimeout参数:```Sub ShowMessageBoxWithTimeoutDim result As LongSelect Case resultCase vbOKMsgBox "You clicked OK."Case vbCancelMsgBox "You clicked Cancel."Case ElseEnd SelectEnd Sub```在上述示例中,我们调用了MsgBoxTimeout函数,并将超时时间设置为5000毫秒(即5秒)。
c语言自动关机程序代码
1.#include<stdlib.h>和#include<stdio.h>分别是C标准库文件,分别包含了系统函数和输入输出函数。
2. #include <windows.h> 是Windows API的头文件,包含了系统调用的函数和数据类型。
3. int main() 是程序的入口函数。
4. int delay; 定义了一个整型变量 delay。
5. printf('请输入关机延迟时间(单位:秒):'); 输出提示信息,让用户输入延迟时间。
6. scanf('%d', &delay); 从键盘读入一个整数,存放在变量delay 中。
7. printf('%d秒后将自动关机!', delay); 输出提示信息,告诉用户将在多少秒后关机。
8. Sleep(delay * 1000); 延迟指定的时间,此处单位为毫秒,需要将秒转换为毫秒。
9. system('shutdown -s -t 0'); 调用系统命令,执行关机操作。
其中,-s 表示关机,-t 0 表示立即关机。
10. return 0; 结束程序。
- 1 -。
AfxMessageBox的标题就是字符串资源中的AFX_IDS_APP_TITLEVC中MessageBox的常见用法一、关于MessageBox消息框是个很常用的控件,属性比较多,本文列出了它的一些常用方法,及指出了它的一些应用场合。
1.MessageBox("这是一个最简单的消息框!");2.MessageBox("这是一个有标题的消息框!","标题");3.MessageBox("这是一个确定取消的消息框!","标题", MB_OKCANCEL );4.MessageBox("这是一个警告的消息框!","标题", MB_ICONEXCLAMATION );5.MessageBox("这是一个两种属性的消息框!","标题", MB_ICONEXCLAMATION|MB_OKCANCEL );6.if(MessageBox("一种常用的应用","标题",MB_ICONEXCLAMATION|MB_OKCANCEL)==IDCANCEL)return;附其它常用属性:系统默认图标,可在消息框上显示X错误MB_ICONHAND, MB_ICONSTOP, and MB_ICONERROR?询问MB_ICONQUESTION!警告MB_ICONEXCLAMATION and MB_ICONWARNINGi信息MB_ICONASTERISK and MB_ICONINFORMATION按钮的形式MB_OK 默认MB_OKCANCEL 确定取消MB_YESNO 是否MB_YESNOCANCEL 是否取消返回值IDCANCEL 取消被选IDNO 否被选IDOK 确定被选IDYES 是被选注意!!!:以上消息框的用法是在CWnd的子类中的应用,如果不是,则要MessageBox(NULL,"ddd","ddd",MB_OK); 或MessageBox(hWnd,"ddd","ddd",MB_OK); hWnd为某窗口的句柄,或者直接用AfxMessageBox。
关机代码(C语言)在计算机编程中,关机代码是一种能够让计算机自动关机的程序。
这种程序通常用于在特定条件下自动关闭计算机,比如在完成某项任务后自动关闭或者在系统出现故障时自动关闭。
在C语言中,编写关机代码并不复杂。
下面我们将介绍如何使用C语言编写一个简单的关机程序。
首先,我们需要包含Windows系统的头文件,因为关机功能是由操作系统提供的。
在C语言中,可以使用windows.h头文件来调用Windows系统的API函数。
接下来,我们需要定义一个主函数main(),在这个函数中我们将调用Windows系统的API函数来实现关机功能。
我们可以使用ExitWindowsEx函数来实现关机功能。
这个函数的原型如下:BOOL ExitWindowsEx(UINT uFlags, DWORD dwReason);其中,uFlags参数用于指定关机的方式,比如立即关机、重启、注销等。
dwReason参数用于指定关机的原因,比如系统故障、应用程序要求等。
下面是一个简单的关机程序的示例代码:```c。
#include <windows.h>。
int main() {。
// 关机。
ExitWindowsEx(EWX_SHUTDOWN, 0);return 0;}。
```。
在这个示例代码中,我们调用ExitWindowsEx函数并传入EWX_SHUTDOWN参数来实现关机功能。
当程序执行到这一行时,计算机将会自动关机。
除了简单的关机功能,我们还可以根据需要编写更复杂的关机程序。
比如可以添加判断条件,只有在特定的条件下才执行关机操作。
或者可以添加日志功能,记录每次关机的原因和时间。
总的来说,使用C语言编写关机代码并不复杂。
只需要了解Windows系统的API函数和C语言的基本语法,就可以轻松实现关机功能。
当然,在实际编写关机程序时,还需要考虑到系统稳定性和安全性等因素,确保关机操作不会对系统造成损害。
在实际应用中,关机代码可以用于自动化任务的执行。
c语言关闭运行窗口的函数C语言是一种广泛应用于程序开发领域的编程语言,具有灵活、高效的特点。
在C语言中,有时候我们需要通过关闭运行窗口的方式来结束程序的运行。
本文将介绍C语言中关闭运行窗口的函数。
在C语言中,关闭运行窗口的函数是`exit()`函数。
该函数的原型如下:```cvoid exit(int status);````exit()`函数可以终止程序的执行,并返回一个状态码给操作系统。
通过调用`exit()`函数,我们可以在程序执行的任意位置终止程序的运行。
在调用`exit()`函数时,我们可以传递一个整数值作为参数,这个整数值可以作为程序的返回状态码。
在大多数操作系统中,返回状态码为0表示程序正常结束,非0值表示程序异常结束。
下面是一个使用`exit()`函数关闭运行窗口的简单示例:```c#include <stdio.h>#include <stdlib.h>int main() {printf("程序开始执行...\n");// 执行一些操作printf("程序将要结束...\n");exit(0); // 使用exit()函数结束程序// 下面的代码不会被执行printf("程序已经结束...\n");return 0;}```上述示例中,程序会先输出"程序开始执行...",然后执行一些操作,最后输出"程序将要结束..."。
在调用`exit(0)`后,程序会立即终止运行,不会执行后面的代码。
所以最后的输出语句"程序已经结束..."不会被执行。
需要注意的是,`exit()`函数不会立即关闭运行窗口,而是通知操作系统程序已经结束。
最终关闭运行窗口的动作由操作系统负责。
在调用`exit()`函数后,程序会返回到操作系统,然后操作系统会关闭运行窗口。
[转]VC中如何让⼦对话框响应最⼤化、最⼩化、关闭等系统消息我们知道实现捕获最⼤化、最⼩化、关闭、⼤⼩变化等系统消息主要有如下两种⽅法:1、重写PreTranslateMessage函数BOOL CSuperSpeedDlg::PreTranslateMessage(MSG* pMsg){// TODO: Add your specialized code here and/or call the base classif ( pMsg->message == WM_SYSCOMMAND ){if ( ( pMsg->wParam & 0xFFF0) == SC_MINIMIZE ){AfxMessageBox(_T("捕获到窗⼝最⼩化消息"));}else if ( ( pMsg->wParam & 0xFFF0) == SC_MAXIMIZE ){AfxMessageBox(_T("捕获到窗⼝最⼤化消息"));}else if ( ( pMsg->wParam & 0xFFF0) == SC_CLOSE ){AfxMessageBox(_T("捕获到窗⼝关闭消息"));}}return CDialog::PreTranslateMessage(pMsg);}注:此⽅法⽆法捕获对话框程序程序中的系统按钮消息2、响应WM_SIZE消息(OnSize函数)void CSuperSpeedDlg::OnSize(UINT nType, int cx, int cy){CDialogEx::OnSize(nType, cx, cy);switch( nType){case SIZE_RESTORED:AfxMessageBox( L"捕获到窗⼝还原消息" );break;case SIZE_MINIMIZED:AfxMessageBox( L"捕获到窗⼝最⼩化消息" );break;case SIZE_MAXIMIZED:AfxMessageBox( L"捕获到窗⼝最⼤化消息" );break;}}注:此⽅法不但可以捕获对话框程序程序中的系统按钮消息,同时还可以获取客户区⾼度与宽度。
winform关闭当前窗口触发方法在WinForms中,如果您想在关闭当前窗口时触发一个方法,您可以使用窗口的 `FormClosing` 事件。
这个事件在窗口正在关闭之前发生,允许您执行一些清理操作,比如保存数据或询问用户是否真的要退出。
以下是如何在WinForms中为窗口添加 `FormClosing` 事件的示例:1. 在Visual Studio中打开您的WinForms项目。
2. 选择您想要添加事件的窗口,在属性窗口中找到事件(闪电标志)并展开。
3. 在展开的事件列表中找到 `FormClosing` 事件。
4. 双击 `FormClosing` 事件旁边的空白区域,这将在您的代码中自动生成一个事件处理方法。
5. 在自动生成的事件处理方法中编写您希望在窗口关闭之前执行的代码。
例如:```csharpprivate void YourForm_FormClosing(object sender, FormClosingEventArgs e){// 这里是您希望在窗口关闭时执行的代码("窗口即将关闭");}```在这个示例中,当窗口关闭时,会弹出一个消息框显示“窗口即将关闭”。
您可以根据需要替换这个消息框,以执行任何其他操作,比如保存数据、询问用户确认等。
请注意,如果您不希望窗口被关闭(比如用户可能取消了关闭操作),您可以在事件处理方法中检查 `` 属性并将其设置为 `true`。
例如:```csharpprivate void YourForm_FormClosing(object sender, FormClosingEventArgs e){if (("确定要关闭窗口吗?", "提示", ) == ){// 用户选择“否”,取消关闭操作= true;}} ```。
CMFC中定时器的使用MFC(Microsoft Foundation Class)是一套面向Windows平台的C++类库,它简化了Windows API的使用,并提供了一些常用的界面和功能组件。
MFC中的定时器允许开发人员在应用程序中设置一定的时间间隔,以执行特定的任务或更新界面。
本文将详细介绍MFC中定时器的使用方法。
在MFC中,定时器的创建和管理是通过CWnd类来实现的。
CWnd类是MFC中所有窗口类的基类,它封装了一些与窗口有关的函数和属性。
首先,需要在窗口类的头文件中添加响应的定时器消息映射:```cppclass CMyWnd : public CWndDECLARE_MESSAGE_MAPafx_msg void OnTimer(UINT_PTR nIDEvent);};```然后,在窗口类的源文件中添加消息映射的处理函数:```cppBEGIN_MESSAGE_MAP(CMyWnd, CWnd)ON_WM_TIMEREND_MESSAGE_MAPvoid CMyWnd::OnTimer(UINT_PTR nIDEvent)switch (nIDEvent)case 1://处理定时器1的逻辑break;case 2://处理定时器2的逻辑break;default:break;}CWnd::OnTimer(nIDEvent);```在窗口类的构造函数中,可以通过调用CWnd类的SetTimer函数来创建定时器:```cppCMyWnd::CMyWndSetTimer(1, 1000, NULL); // 创建ID为1的定时器,时间间隔为1000msSetTimer(2, 2000, NULL); // 创建ID为2的定时器,时间间隔为2000ms```在窗口类的析构函数中,需要调用CWnd类的KillTimer函数来销毁定时器:```cppCMyWnd::~CMyWndKillTimer(1); // 销毁ID为1的定时器KillTimer(2); // 销毁ID为2的定时器```通过上述代码,我们可以在窗口类中创建和管理多个定时器,并且可以在OnTimer函数中根据定时器的ID来处理相应的逻辑。
1、第一种方法:用微软提供的官方文档From : /kb/181934/en-us/Generally, when you want to display a message box for a limited amount of time, you must implement a regular dialog box that closes itself after a specified amount of time. The problem with this method is that you lose the standard message box functionality that Windows provides.The following example shows how to use the MessageBox function to cre ate a message box that automatically closes after a specified amount of ti me. Note the following about the example:∙The example uses a Windows timer that fires an event after the spe cified amount of time has elapsed.∙When the timer event occurs, the PostQuitMessage API is used to break out of the modal message loop that MessageBox uses.∙Note The WM_QUIT message must be removed from the message queue to prevent it from being retrieved in the main message queue.view plaincopy to clipboardprint?1./***********************************************************************2. THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF3. ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO4. THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A5. PARTICULAR PURPOSE.6.7. Copyright 1998 Microsoft Corporation. All Rights Reserved.8.***********************************************************************/9.10./***********************************************************************11.*12.* MsgBox.c13.*14.* Abstract:15.*16.* Sample program to demonstrate how a program can display a17.* timed message box.18.*19.***********************************************************************/20.21.#define STRICT22.#include <windows.h>23.24./***********************************************************************25.*26.* Overview27.*28.* The key to creating a timed message box is exiting the dialog29.* box message loop internal to the message box. Because the30.* message loop for a message box is part of USER, you cannot31.* modify the message loop without using hooks and other such methods.32.*33.*34.* However, all message loops exit when they receive a35.* WM_QUIT message. Additionally, if a nested message loop36.* receives a WM_QUIT message, the nested message loop must break37.* the loop and then re-post the quit message so that the next38.* outer layer can process it.39.*40.* Therefore, you can make the nested message loop exit by41.* calling the PostQuitMessage function. The nested message loop will42.* clean up and post a new quit message. When the MessageBox43.* returns, you peek to see if there is a quit message. If so,44.* it means that the message loop was abnormally terminated.45.* You also consume the WM_QUIT message instead of re-posting it46.* so that the application continues to run.47.*48.* Essentially, you have "tricked" the nested message loop into49.* determining that the application is terminating. When the quit message50.* returns, you consume the quit message. This method effectively cancels51.* the fake quit message that you generated.52.*53.***********************************************************************/54.55./***********************************************************************56.*57.* Global variables58.*59.***********************************************************************/60.61.HWND g_hwndTimedOwner;62.BOOL g_bTimedOut;63.64./***********************************************************************65.*66.* MessageBoxTimer67.*68.* The timer callback function that posts the fake quit message.69.* This function causes the message box to exit because the message box70.* has determined that the application is exiting.71.*72.***********************************************************************/73.void CALLBACK MessageBoxTimer(HWND hwnd,74.UINT uiMsg,75.UINT idEvent,76.DWORD dwTime)77.{78. g_bTimedOut = TRUE;79. if (g_hwndTimedOwner)80. EnableWindow(g_hwndTimedOwner, TRUE);81. PostQuitMessage(0);82.}83.84./***********************************************************************85.*86.* TimedMessageBox87.*88.* The same as the standard MessageBox, except that TimedMessageBox89.* also accepts a timeout. If the user does not respond within the90.* specified timeout, the value 0 is returned instead of one of the91.* ID* values.92.*93.***********************************************************************/94.int TimedMessageBox(HWND hwndOwner,95.LPCTSTR pszMessage,96.LPCTSTR pszTitle,97.UINT flags,98.DWORD dwTimeout)99.{100.UINT idTimer;101.int iResult;102.103. g_hwndTimedOwner = NULL;104. g_bTimedOut = FALSE;105.106. if (hwndOwner && IsWindowEnabled(hwndOwner))107. g_hwndTimedOwner = hwndOwner;108.109. // Set a timer to dismiss the message box.110. idTimer = SetTimer(NULL, 0, dwTimeout, (TIMERPROC)MessageBoxT imer);111.112. iResult = MessageBox(hwndOwner, pszMessage, pszTitle, flags); 113.114. // Finished with the timer.115. KillTimer(NULL, idTimer);116.117. // See if there is a WM_QUIT message in the queue if we timed out.118. // Eat the message so we do not quit the whole application.119. if (g_bTimedOut)120. {121. MSG msg;122. PeekMessage(&msg, NULL, WM_QUIT, WM_QUIT, PM_REMOVE); 123. iResult = -1;124. }125.126. return iResult;127.}128.129./******************************************************** ***************130.*131.* WinMain132.*133.* Program entry point. Demonstrate TimedMessageBox().134.*135.******************************************************** ***************/136.int WINAPI WinMain(HINSTANCE hinst,137.HINSTANCE hinstPrev,138.LPSTR pszCmdLine,139.int nCmdShow)140.{141.142.UINT uiResult;143.144. // Ask the user a question. Give the user five seconds to 145. // answer the question.146. uiResult = TimedMessageBox(NULL,147. "Does a triangle have three s ides?",148. "Quiz",149. MB_YESNO,150. // NULL first parameter is important.151. 5000);152.153. switch (uiResult) {154. case IDYES:155. MessageBox(NULL,156. "That's right!",157. "Result",158. MB_OK);159. break;160.161. case IDNO:这种山寨版的效果,确实看起来有些猥琐....3、源码下载(SDK、MFC共两种)官方下载:/source/1808835网盘下载:/files/c07e4600-ce99-11de-82ad-001422 1b798a/。