Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Programming_Windows_95_Part_I.pdf
Скачиваний:
97
Добавлен:
05.06.2014
Размер:
4.61 Mб
Скачать

364

Более сложное окно диалога

В программе ABOUT1 проиллюстрированы основы работы с простым окном диалога; теперь попытаемся сделать нечто более сложное. Программа ABOUT2, представленная на рис. 11.3, показывает, как работать с дочерними окнами элементов управления (в данном случае с группой переключателей) внутри процедуры окна диалога, и как рисовать в рабочей области окна диалога.

ABOUT2.MAK

#----------------------

# ABOUT2.MAK make file

#----------------------

about2.exe : about2.obj about2.res

$(LINKER) $(GUIFLAGS) -OUT:about2.exe about2.obj about2.res $(GUILIBS)

about2.obj : about2.c about2.h $(CC) $(CFLAGS) about2.c

about2.res : about2.rc about2.h about2.ico $(RC) $(RCVARS) about2.rc

ABOUT2.C

/*------------------------------------------

 

 

 

ABOUT2.C

-- About Box Demo Program No. 2

 

 

(c) Charles Petzold, 1996

------------------------------------------

 

 

*/

#include <windows.h>

 

#include "about2.h"

 

LRESULT CALLBACK WndProc

(HWND, UINT, WPARAM, LPARAM);

BOOL

CALLBACK AboutDlgProc(HWND, UINT, WPARAM, LPARAM);

int iCurrentColor = IDD_BLACK, iCurrentFigure = IDD_RECT;

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)

{

 

static char

szAppName[] = "About2";

MSG

msg;

HWND

hwnd;

WNDCLASSEX

wndclass;

wndclass.cbSize

= sizeof(wndclass);

wndclass.style

= CS_HREDRAW | CS_VREDRAW;

wndclass.lpfnWndProc

= WndProc;

wndclass.cbClsExtra

= 0;

wndclass.cbWndExtra

= 0;

wndclass.hInstance

= hInstance;

wndclass.hIcon

= LoadIcon(hInstance, szAppName);

wndclass.hCursor

= LoadCursor(NULL, IDC_ARROW);

wndclass.hbrBackground

=(HBRUSH) GetStockObject(WHITE_BRUSH);

wndclass.lpszMenuName

= szAppName;

wndclass.lpszClassName

= szAppName;

wndclass.hIconSm

= LoadIcon(hInstance, szAppName);

RegisterClassEx(&wndclass);

hwnd = CreateWindow(szAppName, "About Box Demo Program", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL);

365

ShowWindow(hwnd, iCmdShow);

UpdateWindow(hwnd);

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

{

TranslateMessage(&msg);

DispatchMessage(&msg);

}

return msg.wParam;

}

void PaintWindow(HWND hwnd, int iColor, int iFigure)

 

 

{

 

 

 

 

static COLORREF crColor[8] = { RGB(0,

0, 0), RGB(

0,

0, 255),

 

RGB(0,

255, 0), RGB(

0, 255, 255),

 

RGB(255,

0, 0), RGB(255,

0, 255),

 

RGB(255, 255, 0), RGB(255, 255, 255) };

HBRUSH

hBrush;

 

 

 

HDC

hdc;

 

 

 

RECT

rect;

 

 

 

hdc = GetDC(hwnd); GetClientRect(hwnd, &rect);

hBrush = CreateSolidBrush(crColor[iColor - IDD_BLACK]); hBrush =(HBRUSH) SelectObject(hdc, hBrush);

if(iFigure == IDD_RECT)

Rectangle(hdc, rect.left, rect.top, rect.right, rect.bottom);

else

Ellipse (hdc, rect.left, rect.top, rect.right, rect.bottom);

DeleteObject(SelectObject(hdc, hBrush));

ReleaseDC(hwnd, hdc);

}

void PaintTheBlock(HWND hCtrl, int iColor, int iFigure)

{

InvalidateRect(hCtrl, NULL, TRUE);

UpdateWindow(hCtrl);

PaintWindow(hCtrl, iColor, iFigure);

}

LRESULT CALLBACK WndProc(HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)

{

static HINSTANCE hInstance; PAINTSTRUCT ps;

switch(iMsg)

{

case WM_CREATE :

hInstance =((LPCREATESTRUCT) lParam)->hInstance; return 0;

case WM_COMMAND : switch(LOWORD(wParam))

{

case IDM_ABOUT :

if(DialogBox(hInstance, "AboutBox", hwnd, AboutDlgProc))

InvalidateRect(hwnd, NULL, TRUE); return 0;

}

break;

366

case WM_PAINT : BeginPaint(hwnd, &ps); EndPaint(hwnd, &ps);

PaintWindow(hwnd, iCurrentColor, iCurrentFigure); return 0;

case WM_DESTROY : PostQuitMessage(0); return 0;

}

return DefWindowProc(hwnd, iMsg, wParam, lParam);

}

BOOL CALLBACK AboutDlgProc(HWND hDlg, UINT iMsg, WPARAM wParam, LPARAM lParam)

{

static HWND hCtrlBlock; static int iColor, iFigure;

switch(iMsg)

{

case WM_INITDIALOG :

iColor = iCurrentColor; iFigure = iCurrentFigure;

CheckRadioButton(hDlg, IDD_BLACK, IDD_WHITE, iColor);

CheckRadioButton(hDlg, IDD_RECT, IDD_ELL, iFigure);

hCtrlBlock = GetDlgItem(hDlg, IDD_PAINT);

SetFocus(GetDlgItem(hDlg, iColor)); return FALSE;

case WM_COMMAND : switch(LOWORD(wParam))

{

case IDOK :

iCurrentColor = iColor; iCurrentFigure = iFigure; EndDialog(hDlg, TRUE); return TRUE;

case IDCANCEL : EndDialog(hDlg, FALSE); return TRUE;

case IDD_BLACK : case IDD_RED : case IDD_GREEN : case IDD_YELLOW : case IDD_BLUE : case IDD_MAGENTA : case IDD_CYAN : case IDD_WHITE :

iColor = LOWORD(wParam);

CheckRadioButton(hDlg, IDD_BLACK, IDD_WHITE, LOWORD

(wParam));

PaintTheBlock(hCtrlBlock, iColor, iFigure); return TRUE;

case IDD_RECT : case IDD_ELL :

iFigure = LOWORD(wParam);

367

CheckRadioButton(hDlg, IDD_RECT, IDD_ELL, LOWORD

(wParam));

PaintTheBlock(hCtrlBlock, iColor, iFigure); return TRUE;

}

break;

case WM_PAINT :

PaintTheBlock(hCtrlBlock, iColor, iFigure); break;

}

return FALSE;

}

ABOUT2.RC

/*---------------------------

ABOUT2.RC resource script

---------------------------*/

#include <windows.h> #include "about2.h"

about2 ICON about2.ico

About2 MENU

{

POPUP "&Help"

{

MENUITEM "&About About2...", IDM_ABOUT

}

}

#define TABGRP(WS_TABSTOP | WS_GROUP)

AboutBox DIALOG 20, 20, 140, 188

 

 

 

 

STYLE WS_POPUP | WS_DLGFRAME

 

 

 

 

{

 

 

 

 

 

 

CTEXT

"About2"

-1,

0,

12, 140,

8

ICON

"About2"

-1,

8,

8,

0,

0

CTEXT

"About Box Demo Program" -1, 4,

36, 130,

8

CTEXT

""

IDD_PAINT,

68,

54,

60,

60

GROUPBOX

"&Color"

-1,

4,

50,

54, 112

RADIOBUTTON

"&Black"

IDD_BLACK,

8,

60,

40,

12, TABGRP

RADIOBUTTON

"B&lue"

IDD_BLUE,

8,

72,

40,

12

RADIOBUTTON

"&Green"

IDD_GREEN,

8,

84,

40,

12

RADIOBUTTON

"Cya&n"

IDD_CYAN,

8,

96,

40,

12

RADIOBUTTON

"&Red"

IDD_RED,

8, 108,

40,

12

RADIOBUTTON

"&Magenta"

IDD_MAGENTA,

8, 120,

40,

12

RADIOBUTTON

"&Yellow"

IDD_YELLOW,

8, 132,

40,

12

RADIOBUTTON

"&White"

IDD_WHITE,

8, 144,

40,

12

GROUPBOX

"&Figure"

-1,

68, 120,

60,

40, WS_GROUP

RADIOBUTTON

"Rec&tangle"

IDD_RECT,

72, 134,

50,

12, TABGRP

RADIOBUTTON "&Ellipse"

IDD_ELL,

72, 146,

50,

12

DEFPUSHBUTTON "OK"

IDOK,

20, 168,

40,

14, WS_GROUP

PUSHBUTTON

"Cancel"

IDCANCEL,

80, 168,

40,

14, WS_GROUP

}

 

 

 

 

 

 

ABOUT2.H

/*----------------------

ABOUT2.H header file

----------------------

*/

#define IDM_ABOUT

1

Соседние файлы в предмете Операционные системы