2012-08-17 48 views
5

有没有人知道如何禁用从C程序创建的.exe可执行文件在Windows控制台窗口上的关闭按钮?如何禁用C中的关闭按钮?

+2

你不能。控制台窗口是一个独立的组件。唯一的办法是在你有自己的窗口控制的地方建立GUI程序。 – Mysticial 2012-08-17 08:07:44

+0

另外,你为什么要这样做? – Mysticial 2012-08-17 08:08:45

+0

我只是想对我的兄弟做一个恶作剧。但我必须确保他会吓坏了。你知道我可以用其他方式吗?顺便说一句,我唯一的编程语言是c,不幸的是 – user1590836 2012-08-17 08:12:14

回答

0

如果你想禁用正在运行的程序中的按钮,有方法可以做到这一点。

原理是找到窗口,然后找到窗口内的按钮,然后发送一个WM_DISABLE消息给按钮。

5

here:

#define _WIN32_WINNT 0x0500 
#include <stdio.h> 
#include <windows.h> 

int main(int argc, _TCHAR* argv[]){ 
    HWND h; 
    HMENU sm; 
    int i, j, c; 
    LPTSTR buf; 
    // get the handle to the console 
    h = GetConsoleWindow(); 
    // get handle to the System Menu 
    sm = GetSystemMenu(h, 0); 
    // how many items are there? 
    c = GetMenuItemCount(sm); 
    j = -1; 
    buf = (TCHAR*) malloc (256 *sizeof(TCHAR)); 
    for (i=0; i<c; i++) { 
    // find the one we want 
    GetMenuString(sm, i, buf, 255, MF_BYPOSITION); 
    if (!strcmp(buf, "&Close")) {   
     j = i;   
     break;  
    } 
    } 
    // if found, remove that menu item 
    if (j >= 0) 
    RemoveMenu(sm, j, MF_BYPOSITION); 
    return 0; 
} 
+0

我没有设置测试这个,所以+1假设它有效。我不知道你可以像那样抓住控制台窗口的手柄。 – Mysticial 2012-08-17 08:17:33

+0

没有工作,请尝试在线编译器:cmpe150-1.cmpe.boun.edu.tr/ – user1590836 2012-08-17 08:17:41

+1

对不起,它的工作原理。我只是写了其他代码befor此 – user1590836 2012-08-17 08:20:10