GUI enabling/disabling of controls: Difference between revisions

Content added Content deleted
Line 309: Line 309:
<p>
<p>
[[File:Guienabbc.gif]]
[[File:Guienabbc.gif]]

=={{header|C}}==
{{works with|C (with Win32 API)}}

<pre>file main.c</pre>

<lang c>#include <windows.h>
#include "resource.h"

#define MIN_VALUE 0
#define MAX_VALUE 10

BOOL CALLBACK DlgProc( HWND hwnd, UINT msg, WPARAM wPar, LPARAM lPar );
void Increment( HWND hwnd );
void Decrement( HWND hwnd );
void SetControlsState( HWND hwnd );

int WINAPI WinMain( HINSTANCE hInst, HINSTANCE hPInst, LPSTR cmdLn, int show ) {
return DialogBox( hInst, MAKEINTRESOURCE(IDD_DLG), NULL, DlgProc );
}

BOOL CALLBACK DlgProc( HWND hwnd, UINT msg, WPARAM wPar, LPARAM lPar ) {
switch( msg ) {

case WM_INITDIALOG:
srand( GetTickCount() );
SetDlgItemInt( hwnd, IDC_INPUT, 0, FALSE );
break;

case WM_COMMAND:
switch( LOWORD(wPar) ) {
case IDC_INCREMENT:
Increment( hwnd );
break;
case IDC_DECREMENT:
Decrement( hwnd );
break;
case IDC_INPUT:
// update controls' state according
// to the contents of the input field
if( HIWORD(wPar) == EN_CHANGE ) SetControlsState( hwnd );
break;
case IDC_QUIT:
SendMessage( hwnd, WM_CLOSE, 0, 0 );
break;
default: ;
}
break;

case WM_CLOSE: {
int reply = MessageBox( hwnd,
"Do you really want to quit?",
"Quit confirmation", MB_ICONQUESTION|MB_YESNO );
if( reply == IDYES )
EndDialog( hwnd, 0 );
} break;

default: ;
}

return 0;
}

void Increment( HWND hwnd ) {
UINT n = GetDlgItemInt( hwnd, IDC_INPUT, NULL, FALSE );

if( n < MAX_VALUE ) {
SetDlgItemInt( hwnd, IDC_INPUT, ++n, FALSE );
SetControlsState( hwnd );
}
}

void Decrement( HWND hwnd ) {
UINT n = GetDlgItemInt( hwnd, IDC_INPUT, NULL, FALSE );

if( n > MIN_VALUE ) {
SetDlgItemInt( hwnd, IDC_INPUT, --n, FALSE );
SetControlsState( hwnd );
}
}

void SetControlsState( HWND hwnd ) {
UINT n = GetDlgItemInt( hwnd, IDC_INPUT, NULL, FALSE );
EnableWindow( GetDlgItem(hwnd,IDC_INCREMENT), n<MAX_VALUE );
EnableWindow( GetDlgItem(hwnd,IDC_DECREMENT), n>MIN_VALUE );
}</lang>

<pre>file resource.h</pre>

<lang c>#define IDD_DLG 101
#define IDC_INPUT 1001
#define IDC_INCREMENT 1002
#define IDC_DECREMENT 1003
#define IDC_QUIT 1004</lang>

<pre>file resource.rc</pre>

<lang c>#include <windows.h>
#include "resource.h"

LANGUAGE LANG_NEUTRAL, SUBLANG_NEUTRAL
IDD_DLG DIALOG 0, 0, 154, 46
STYLE DS_3DLOOK | DS_CENTER | DS_MODALFRAME | DS_SHELLFONT | WS_CAPTION | WS_VISIBLE | WS_POPUP | WS_SYSMENU
CAPTION "GUI Component Interaction"
FONT 12, "Ms Shell Dlg" {
EDITTEXT IDC_INPUT, 33, 7, 114, 12, ES_AUTOHSCROLL | ES_NUMBER
PUSHBUTTON "Increment", IDC_INCREMENT, 7, 25, 50, 14
PUSHBUTTON "Decrement", IDC_DECREMENT, 62, 25, 50, 14, WS_DISABLED
PUSHBUTTON "Quit", IDC_QUIT, 117, 25, 30, 14
RTEXT "Value:", -1, 10, 8, 20, 8
}</lang>


=={{header|C++}}==
=={{header|C++}}==