Yo guys plz can anyone teach me how to disable a button without using MFC. It would be great for the help

Dani AI

Generated

For : pointed you in the right direction and is also right to nitpick the wording — disabling a button in a non‑MFC C++ program is done with the Win32 API, not an MFC member. The Win32 function is declared roughly as:

// declared in <windows.h>, implemented in user32.dll
BOOL EnableWindow(HWND hWnd, BOOL bEnable);

Example usage in a dialog or any HWND-based code:

HWND hBtn = GetDlgItem(hDlg, IDC_MYBUTTON);
if (hBtn) {
    // disable
    EnableWindow(hBtn, FALSE);

    // later, re-enable
    EnableWindow(hBtn, TRUE);
}

Practical tips and common pitfalls:

  • Always get a valid HWND (GetDlgItem or the handle returned by CreateWindow/CreateWindowEx). Check for NULL before calling.
  • Call EnableWindow after the control exists (e.g., in WM_INITDIALOG / WM_CREATE or after CreateWindow).
  • EnableWindow returns the previous enabled state (nonzero if it was enabled before).
  • UI calls should be made on the GUI thread; if you need to toggle from a worker thread, post a message to the window and do the EnableWindow call in the message handler.
  • To start a control disabled, create it with the WS_DISABLED style; EnableWindow is preferred for runtime changes.
  • Owner-drawn or custom controls may need extra paint handling to show a proper "disabled" appearance.

If using another GUI toolkit (Qt, wxWidgets, etc.), use that framework’s API (for example, Qt::setEnabled(false)) instead of raw Win32 calls.

Recommended Answers

All 2 Replies

Use EnableWindow() Method.

Use EnableWindow() Method.

It's not a "method"...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.