我只知道,系统在关机时,会给每一个程序发送WM_CLOSE消息

解决方案 »

  1.   

    System Shutdown Messages
    The following messages are used with system shutdown. WM_ENDSESSION
    WM_QUERYENDSESSION 
    WM_QUERYENDSESSION
    The WM_QUERYENDSESSION message is sent when the user chooses to end the session or when an application calls the ExitWindows function. If any application returns zero, the session is not ended. The system stops sending WM_QUERYENDSESSION messages as soon as one application returns zero. After processing this message, the system sends the WM_ENDSESSION message with the wParam parameter set to the results of the WM_QUERYENDSESSION message. A window receives this message through its WindowProc function. LRESULT CALLBACK WindowProc(
      HWND hwnd,       // handle to window
      UINT uMsg,       // WM_QUERYENDSESSION
      WPARAM wParam,   // not used
      LPARAM lParam    // logoff option
    );
    Return Values
    If an application can terminate conveniently, it should return TRUE; otherwise, it should return FALSE. Windows NT/2000: When an application returns TRUE for this message, it receives the WM_ENDSESSION message and it is terminated, regardless of how the other applications respond to the WM_QUERYENDSESSION message. Windows 95/98: After all applications return TRUE for this message, they receive the WM_ENDSESSION and they are terminated. 
      

  2.   

    ExitWindowEx函数可以关机,
    它首先发出WM_QUERYENDSESSION 消息,返回的结果用于设置WM_ENDSESSION消息
    如果所有的程序都关闭了,则执行关机
    也可以在这个函数中使用EWX_FORCE参数,则系统不会发出WM_QUERYENDSESSION 
    或者WM_ENDSESSION消息,而直接关机。
      

  3.   

    jiangsheng(蒋晟) :我想问问,这个windowproc怎麽用呢,我没有看台明白!谢谢了
      

  4.   

    agree with jiangsheng(蒋晟) & richi_(跑) 
    CWnd类中已经封装该两个消息,查一下MSDN
      

  5.   

    以前我也问过,现在解决了,就像jiangsheng说的一样
      

  6.   

    不行呀,谁给我一个具体的源代码,就是windowproc怎麽用呀,他返回的值在那个函数里,如何用!谢谢了
      

  7.   

    如果是SDK,回去看看教材吧
    MFC,使用消息映射,在WizardBar || ClassWizard
      

  8.   

    拜托,MSVCer(家宝) 你不要在这里写一些,很让人迷惑的东西了,我要的是消息跟mfc和sdk有什麽关系,我想要的是一个可以区分这两个消息的函数,或者有什麽不同的消息!
      

  9.   

    急>>>>>>>>>>>>>>>如何实现直接关机???????????而不是调出"关闭Windows"对话框
    急>>>>>>>>>>>>>>>如何实现直接关机???????????而不是调出"关闭Windows"对话框
    急>>>>>>>>>>>>>>>如何实现直接关机???????????而不是调出"关闭Windows"对话框
    急>>>>>>>>>>>>>>>如何实现直接关机???????????而不是调出"关闭Windows"对话框
      

  10.   

    保你满意的答案:自动关机:
     ::ExitWindowsEx(EWX_POWEROFF|EWX_FORCE,NULL);  //关机在NT或2000下,需在此之前得到一定的权限方可:
    OSVERSIONINFO osinfo;
    osinfo.dwOSVersionInfoSize=sizeof(OSVERSIONINFO);
    ::GetVersionEx(&osinfo);
    if(osinfo.dwPlatformId==VER_PLATFORM_WIN32_NT)
    {//系统是NT,则有必要获取关机权限
    HANDLE hToken;
    TOKEN_PRIVILEGES tkp;
    //SeShutDownPrivilege must be enabled
    if (!OpenProcessToken(GetCurrentProcess(),
    TOKEN_ADJUST_PRIVILEGES|TOKEN_QUERY,&hToken))
    return ;
    //Get a LUID for SeShutdownPrivilege
    LookupPrivilegeValue(NULL,TEXT("SeShutdownPrivilege"),
    &tkp.Privileges[0].Luid); //PrivilegeCount enables  more than one privilege to bo set
    tkp.PrivilegeCount=1;
    tkp.Privileges[0].Attributes=SE_PRIVILEGE_ENABLED; AdjustTokenPrivileges(hToken,false,&tkp,0,(PTOKEN_PRIVILEGES)NULL,0);
    //确定AdjustTokenPrivileges执行是否成功。
    if (GetLastError() != ERROR_SUCCESS) 
    AfxMessageBox("权限调整失败.");
    }
          2. 如何知道要关机了?
    BOOL CYourApp::PreTranslateMessage(MSG* pMsg) 
    {
    // TODO: Add your specialized code here and/or call the base class
    if(pMsg->message==WM_QUERYENDSESSION||pMsg->message==WM_ENDSESSION)
    {
    AfxMessageBox("要关机了,请作好准备");
    return CWinApp::PreTranslateMessage(pMsg);
    }
      

  11.   

    如果使用MFC,一般在主窗口中捕获这两个消息并且完成程序的退出。
    如果使用SDK,则在那个处理消息的switch里面添加这两个消息的处理。
      

  12.   

    上面最后一段代码拷贝时出了点小错:BOOL CYourApp::PreTranslateMessage(MSG* pMsg) 
    {
    // TODO: Add your specialized code here and/or call the base class
    if(pMsg->message==WM_QUERYENDSESSION¦¦pMsg->message==WM_ENDSESSION)
    {
    AfxMessageBox("要关机了,请作好准备");
    }//if
    return CWinApp::PreTranslateMessage(pMsg);
    }//PreTranslateMessage
      

  13.   

    拿你这个原理就是判断两个消息都有的情况下事吗!
    我用的是delphi所以可能不好做到两个消息同时比较,但是你的方法,我会参考的,我试通了就给你加分!
      

  14.   

    CSDN这个编辑器老出毛病,该改进改进了!if语句应该是这样的:
    if(pMsg->message==WM_QUERYENDSESSION¦¦pMsg->message==WM_ENDSESSION)也就是说,只要判断到一个消息就行了。
      

  15.   

    天哪,我要找人抨命,写的时候好好的,一按按钮,出来的就变样,真要命!
    我不相信了,if(pMsg->message==WM_QUERYENDSESSION||pMsg->message==WM_ENDSESSION)
      

  16.   

    ::ExitWindowsEx(EWX_SHUTDOWN);或用参数EWX_POWEROFF,重启系统用参数EWX_REBOOT.
    很简单嘛!
      

  17.   

    我不多说了,免得又...
    用程序说话吧,在关机时程序会详细告诉你问题答案(附源码)
    http://www.csdn.net/filebbs/read_topic.asp?id=528
    56K
      

  18.   

    MSVCer(家宝) :不好意思,我可能有些急出口过重了,不过我看了你给我的软件,好像并不是我要的,我知道可以用你的软件来实现重新启动,可是我要的是如何对windows的重新启动和关机加以分析
      

  19.   

    关机和重新启动对于应用程序来说应该是一样的,都是操作系统会重新启动。不过重新启动会在最后执行一条 汇编指令:int 19h (软复位)而已!
      

  20.   

    如果你真的必须区别对待的话,你就得要像在 DOS 下一样截获 int 19h 。Windows下具体实现相当困难!!