How to disable the close button in C?

1.2k views Asked by At

Does anyone know how to disable the close button on a windows console window with an .exe executable that was created from a C program?

2

There are 2 answers

0
AndersK On

If you want to disable a button in a running program there are methods to do this.

The principle is to find the window, then find the button inside the window and then send a WM_DISABLE message to the button.

5
weston On

From 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;
}