VC创建定时关闭的MessageBox

合集下载

c#自动关闭MessageBox弹出的窗口

c#自动关闭MessageBox弹出的窗口

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

messageboxtimeout的用法access

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语言自动关机程序代码

c语言自动关机程序代码

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 -。

VC显示,关闭,销毁模态与非模态对话框技巧

VC显示,关闭,销毁模态与非模态对话框技巧
hWnd :将被销毁的窗口的句柄。
返回值:如果函数成功,返回值为非零:如果函数失败,返回值为零。若想获得更多错误信息,请调用GetLastError函数。
备注:一个线程不能使用本函数销毁别的线程创建的窗口。如果这个窗口是一个不具有WS_EX_NOPARENTNOTIFY 样式的子窗口,则销毁窗口时将发WM_PARENTNOTIFY 消息给其父窗口。
dlg->Create(IDD_GENERAL_CONTROL);
dlg->ShowWindow(SW_SHOW);
二、关闭对话框:
1.CDialog::OnOK(); //确定按钮按下 CDialog::OnCancel(); //取消按钮被按下
2.CDialog::DestoryWindow();
Windows CE: 本函数将不发送 WM_NCDESTROY 消息.
EndDialog
函数功能:该函数清除一个模态对话框,并使系统中止对对话框的任何处理。
函数原型:BOOL EndDialog(HWND hDlg,int nResult);
参数:
hDlg:表示要被清除的对话框窗口。
NResult:指定从创建对话框函数返回到应用程序的值。
返回值:如果函数调用成功,则返回值为非零值;如果函数调用失败则返回值为零。若想获得错误信息请调用GetLastError函数。
备注:由DialogBox,DialogBoxParam、DialogBoxlndirect和DialogBoxlndirectParam函数创建的对话框一定要用EndDialog函数来清除。应用程序从对话框应用程序内部调用EndDialog函数,该函数不能为其他目的而供使用。对话框应用程序可以在任何时间调用EndDialog函数;甚至在WM_INITDIALOG消息处理过程中。如果应用程序在WM_INTDIALOG消息处理过程中调用该函数,则对话框在显示和输入焦点被设置之前对话框被清除。EndDialog函数并不立即清除对话框。而是设置一个标志,并且允许对话框应用程序把控制权返回系统。系统在试图从应用程序队列检索下一个消息之前检测标志。如果已经设置了标志则系统中止消息循环,清除对话框,且用nResUlt中的值作为从创建对话框的函数中返回的值。

VS中MessageBox的常见用法

VS中MessageBox的常见用法

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语言)在计算机编程中,关机代码是一种能够让计算机自动关机的程序。

这种程序通常用于在特定条件下自动关闭计算机,比如在完成某项任务后自动关闭或者在系统出现故障时自动关闭。

在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语言中,有时候我们需要通过关闭运行窗口的方式来结束程序的运行。

本文将介绍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中如何让子对话框响应最大化、最小化、关闭等系统消息

[转]VC中如何让子对话框响应最大化、最小化、关闭等系统消息

[转]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;}}注:此⽅法不但可以捕获对话框程序程序中的系统按钮消息,同时还可以获取客户区⾼度与宽度。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

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/。

相关文档
最新文档