0% found this document useful (0 votes)
47 views

Dialog Box Prog 1

This document contains code for creating dialog boxes in C++. It defines functions for initializing and handling dialog box windows. The code creates dialog resources, shows dialog boxes modally from the main window procedure, and handles user input and updates radio buttons and checkboxes on the dialogs. It demonstrates creating modeless and modal dialog boxes with various controls like lists, radio buttons, and checkboxes.

Uploaded by

Anjani Kunwar
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views

Dialog Box Prog 1

This document contains code for creating dialog boxes in C++. It defines functions for initializing and handling dialog box windows. The code creates dialog resources, shows dialog boxes modally from the main window procedure, and handles user input and updates radio buttons and checkboxes on the dialogs. It demonstrates creating modeless and modal dialog boxes with various controls like lists, radio buttons, and checkboxes.

Uploaded by

Anjani Kunwar
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 15

Dialog Box Prog 1

#include<windows.h>

#include"resource.h"

LRESULT CALLBACK r(HWND,UINT,WPARAM,LPARAM);

BOOL CALLBACK D(HWND,UINT,WPARAM,LPARAM);

int WINAPI WinMain(HINSTANCE i,HINSTANCE j,LPSTR k,int l)

HWND h;

MSG m;

WNDCLASS w;

if(!j)

w.cbClsExtra=0;

w.cbWndExtra=0;

w.hbrBackground=(HBRUSH) GetStockObject(WHITE_BRUSH);

w.hCursor=LoadCursor(NULL,IDC_ARROW);

w.hIcon=LoadIcon (NULL, IDI_APPLICATION) ;

w.hInstance=i;

w.lpfnWndProc=r;

w.lpszClassName=TEXT("Class");

w.lpszMenuName=MAKEINTRESOURCE(IDR_MENU1);

w.style=CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;


if(!RegisterClass(&w))

return 0;

h=CreateWindow(TEXT("Class"),TEXT("Dialog
Box"),WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFA
ULT,NULL,NULL,i,NULL);

ShowWindow(h,l);

while(GetMessage(&m,NULL,0,0))

TranslateMessage(&m);

DispatchMessage(&m);

return(int)( m.wParam);

LRESULT CALLBACK r(HWND h,UINT m,WPARAM w,LPARAM l)

// FARPROC lpfnDlgProc; // Pointer to a callback function.

switch(m)

case WM_COMMAND:

switch(w)

case IDM_DIALOG:

// lpfnDlgProc=MakeProcInstance(DialogProc,
(HINSTANCE) GetWindowLong(h,GWL_HINSTANCE));
//MakeProcInstance function is obsolete. Win32
functions can be called directly.

DialogBox((HINSTANCE)GetWindowLong(h,GWL_HINSTANCE),MAKEINTRESOURCE(FirstDialog),h,D);//Di
alogProc);

// FreeProcInstance(lpfnDlgProc);

// FreeProcInstance function is also obsolete.

// This function is provided only for compatibility with 16-bit versions of


Windows.

//Win32-based applications should not use this function; it has no


meaning in the 32-bit environment.

break;

case IDM_QUIT:

DestroyWindow(h);

break;

break;

case WM_DESTROY:

PostQuitMessage(0);

break;

default:

return DefWindowProc(h,m,w,l);

return 0;

BOOL CALLBACK D(HWND h,UINT m,WPARAM w,LPARAM l)


{

switch(m)

case WM_INITDIALOG:

return TRUE;

case WM_COMMAND:

switch(LOWORD(w))

case DLI_OK:

EndDialog(h,0);

return TRUE;

case DLI_NOTOK:

PostQuitMessage(0);

//MessageBeep(0);

return TRUE;

break;

case WM_DESTROY:

EndDialog(h,0);

//PostQuitMessage(0);

break;

return FALSE;

}
Dialog box Prog 2

#include<windows.h>

#include"resource.h"

int nRadioChoice=0;

int nCheckOnOff=0;

LRESULT CALLBACK r(HWND,UINT,WPARAM,LPARAM);

BOOL CALLBACK D(HWND,UINT,WPARAM,LPARAM);

void SetRadioButtons(HWND, int);

int WINAPI WinMain(HINSTANCE i,HINSTANCE j,LPSTR k,int l)

HWND h;

MSG m;

WNDCLASS w;

if(!j)

w.cbClsExtra=0;

w.cbWndExtra=0;

w.hbrBackground=(HBRUSH) GetStockObject(WHITE_BRUSH);

w.hCursor=LoadCursor(NULL,IDC_ARROW);

w.hIcon=NULL;//LoadIcon (i, MAKEINTRESOURCE (IDI_ICON)) ;

w.hInstance=i;
w.lpfnWndProc=r;

w.lpszClassName=TEXT("Class");

w.lpszMenuName=MAKEINTRESOURCE(IDR_MENU1);

w.style=CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;

if(!RegisterClass(&w))

return 0;

h=CreateWindow(TEXT("Class"),TEXT("Dialog
Box"),WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFA
ULT,NULL,NULL,i,NULL);

ShowWindow(h,l);

while(GetMessage(&m,NULL,0,0))

TranslateMessage(&m);

DispatchMessage(&m);

return( m.wParam);

LRESULT CALLBACK r(HWND h,UINT m,WPARAM w,LPARAM l)

static int nRetVal=0;

PAINTSTRUCT ps;

TCHAR cBuf[128];
switch(m)

case WM_COMMAND:

switch(w)

case IDM_DIALOG:

nRetVal= DialogBox((HINSTANCE)
GetWindowLong(h,GWL_HINSTANCE),MAKEINTRESOURCE(PickDialog),h,D);

InvalidateRect(h,NULL,TRUE);

break;

case IDM_QUIT:

DestroyWindow(h);

break;

break;

case WM_PAINT:

BeginPaint(h,&ps);

TextOut(ps.hdc,0,0,cBuf, wsprintf(cBuf, TEXT("DialogBox returned: %d"), nRetVal));

TextOut(ps.hdc,0,20,cBuf,wsprintf(cBuf,TEXT("RadioChoice=%d, CheckBox On/Off=%d"),


nRadioChoice+1, nCheckOnOff));

EndPaint(h,&ps);

break;

case WM_DESTROY:

PostQuitMessage(0);

break;

default:
return DefWindowProc(h,m,w,l);

return 0;

BOOL CALLBACK D(HWND h,UINT m,WPARAM w,LPARAM l)

switch(m)

case WM_INITDIALOG:

SetRadioButtons(h, nRadioChoice);

SendDlgItemMessage(h,DLI_CHECKBOX,BM_SETCHECK,nCheckOnOff,0);

return(TRUE);

case WM_COMMAND:

switch(w)

case DLI_PICKOK:

EndDialog(h,TRUE);

return (TRUE);

case DLI_PICKCANCEL:

EndDialog(h,FALSE);

return (TRUE);

case DLI_RADIO1:

nRadioChoice=0;

SetRadioButtons(h,nRadioChoice);
return (TRUE);

case DLI_RADIO2:

nRadioChoice=1;

SetRadioButtons(h,nRadioChoice);

return (TRUE);

case DLI_CHECKBOX:

nCheckOnOff=(nCheckOnOff?0:1);

SendDlgItemMessage(h,DLI_CHECKBOX,BM_SETCHECK,nCheckOnOff,0);

return(TRUE);

break;

return FALSE;

void SetRadioButtons(HWND h, int nButtonOn)

if(nButtonOn==1)

SendDlgItemMessage(h,DLI_RADIO1,BM_SETCHECK,0,0);

SendDlgItemMessage(h,DLI_RADIO2,BM_SETCHECK,1,0);

else {

SendDlgItemMessage(h,DLI_RADIO1,BM_SETCHECK,1,0);

SendDlgItemMessage(h,DLI_RADIO2,BM_SETCHECK,0,0);
}

Dialog Box 3

#include<windows.h>

#include"resource.h"

#define NUMMENU 7

#define ARRAYWIDE 15

LRESULT CALLBACK r(HWND,UINT,WPARAM,LPARAM);

BOOL CALLBACK ListDialogProc(HWND, UINT, WPARAM, LPARAM);

typedef struct k {

int nCharWidth;

int nItemCount;

int nSelection;

TCHAR *cBuf;

}CHARARRAY;

int WINAPI WinMain(HINSTANCE i,HINSTANCE j,LPSTR k,int l)

HWND h;
MSG m;

WNDCLASS w;

if(!j)

w.cbClsExtra=0;

w.cbWndExtra=0;

w.hbrBackground=(HBRUSH) GetStockObject(WHITE_BRUSH);

w.hCursor=LoadCursor(NULL,IDC_ARROW);

w.hIcon=NULL;

w.hInstance=i;

w.lpfnWndProc=r;

w.lpszClassName=TEXT("Class");

w.lpszMenuName=MAKEINTRESOURCE(IDR_MENU1);

w.style=CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;

if(!RegisterClass(&w))

return 0;

h=CreateWindow(TEXT("Class"),TEXT("Dialog
Box"),WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFA
ULT,NULL,NULL,i,NULL);

ShowWindow(h,l);

while(GetMessage(&m,NULL,0,0))

TranslateMessage(&m);
DispatchMessage(&m);

return( m.wParam);

LRESULT CALLBACK r(HWND h,UINT m,WPARAM w,LPARAM l)

static CHARARRAY caMenu;

static TCHAR cMenuNames[NUMMENU][ARRAYWIDE]={ TEXT("Abb"), TEXT("Bccc"),


TEXT("Cddd"), TEXT("Effff"), TEXT("Fggg"), TEXT("Ghhhh"), TEXT("Hiiii")};

PAINTSTRUCT ps;

int i;

TCHAR c[256];

switch(m) {

case WM_CREATE:

caMenu.nCharWidth=ARRAYWIDE;

caMenu.nItemCount=NUMMENU;

caMenu.nSelection=-1;

caMenu.cBuf=(TCHAR *)cMenuNames;

break;

case WM_COMMAND:

switch(LOWORD(w)) {

case IDM_LIST:

DialogBoxParam((HINSTANCE)
GetWindowLong(h,GWL_HINSTANCE),MAKEINTRESOURCE(LISTDIALOG),h,ListDialogProc,
(LPARAM)&caMenu);
InvalidateRect(h,NULL,TRUE);

break;

case IDM_QUIT:

DestroyWindow(h);

break;

break;

case WM_PAINT:

BeginPaint(h,&ps);

if(caMenu.nSelection==-1)

TextOut(ps.hdc, 0, 20, TEXT("not chose"), 9);

else

TextOut(ps.hdc,
0,20,cMenuNames[caMenu.nSelection],lstrlen(cMenuNames[caMenu.nSelection]));

EndPaint(h,&ps);

break;

case WM_DESTROY:

PostQuitMessage(0);

break;

default:

return DefWindowProc(h,m,w,l);

return 0;

BOOL CALLBACK ListDialogProc(HWND h, UINT m, WPARAM w, LPARAM l)


{

static CHARARRAY *pcaListArray=NULL;

int i;

LPSTR lpszItem;

TCHAR c[128];

switch(m)

case WM_INITDIALOG:

pcaListArray=(CHARARRAY *)l; //5th argument of DialogBoxParam() is passed as


LPARAM so it is converted here into CHARARRAY type.

lpszItem=(LPSTR)pcaListArray->cBuf;

for(i=0;i<pcaListArray->nItemCount;i++) {

SendDlgItemMessage(h,DLI_LISTBOX, LB_ADDSTRING,0,(LPARAM)lpszItem);

lpszItem+=(pcaListArray->nCharWidth*2);

return(TRUE);

case WM_COMMAND:

switch(LOWORD(w)) {

case DLI_LISTOK:

EndDialog(h,TRUE);

return TRUE;

case DLI_LISTCANCEL:

EndDialog(h,FALSE);

return TRUE;

case DLI_LISTBOX:

if(HIWORD(w)==LBN_SELCHANGE) {
i=(int)SendDlgItemMessage(h,DLI_LISTBOX, LB_GETCURSEL,0,0);

pcaListArray->nSelection=i;

return TRUE;

break;

return FALSE;

You might also like