您的位置:首页 > 编程语言 > C语言/C++

【Visual C++ 2010】Win32程序如何实现复杂的模态对话框

2015-10-09 09:53 459 查看
“`

include

include

include “resource.h”

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

BOOL CALLBACK AboutProc(HWND hDlg,UINT message,WPARAM wParam,LPARAM lParam);

int iCurrentColor=IDC_BLACK,

iCurrentFigure=IDC_RECT;

int WINAPI WinMain( __in HINSTANCE hInstance, __in_opt HINSTANCE hPrevInstance, __in LPSTR lpCmdLine, __in int nShowCmd )

{

static TCHAR szAppName[]=TEXT(“Clock”);

HWND hwnd;

MSG msg;

WNDCLASS wndclass;

wndclass.style=CS_HREDRAW|CS_VREDRAW;
wndclass.lpfnWndProc=WndProc;
wndclass.cbClsExtra=0;
wndclass.cbWndExtra=0;
wndclass.hInstance=hInstance;
wndclass.hIcon=LoadIcon(NULL,IDI_QUESTION);
wndclass.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.hCursor=LoadCursor(NULL,IDC_ARROW);
wndclass.lpszClassName=szAppName;
wndclass.lpszMenuName=MAKEINTRESOURCE(IDR_MENU);

if (!RegisterClass(&wndclass))
{
MessageBox(NULL,TEXT("注册窗口类失败!"),szAppName,MB_ICONERROR);
return 0;
}

hwnd=CreateWindow(szAppName,TEXT("Clock"),WS_OVERLAPPEDWINDOW|WS_VSCROLL|WS_HSCROLL,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,NULL,NULL,hInstance,NULL);

ShowWindow(hwnd,nShowCmd);
UpdateWindow(hwnd);

while (GetMessage(&msg,NULL,NULL,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-IDC_BLACK]);
hBrush=(HBRUSH)SelectObject(hdc,hBrush);
if (iFigure==IDC_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 message,WPARAM wParam,LPARAM lParam)

{

static HINSTANCE hInstance;

PAINTSTRUCT ps;

switch(message)
{
case WM_COMMAND:

switch(LOWORD(wParam))
{
case IDM_ABOUT:
if(DialogBox(hInstance,MAKEINTRESOURCE(IDD_DIALOG1),hwnd,AboutProc))
InvalidateRect(hwnd,NULL,TRUE);
return 0;
}
break;
case WM_CREATE:
hInstance=((LPCREATESTRUCT)lParam)->hInstance;
return 0;
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,message,wParam,lParam);


}

BOOL CALLBACK AboutProc(HWND hDlg,UINT message,WPARAM wParam,LPARAM lParam)

{

static HWND hCtrlBlock;

static int iColor,iFigure;

switch(message)

{

case WM_INITDIALOG:

iColor=iCurrentColor;

iFigure=iCurrentFigure;

CheckRadioButton(hDlg,IDC_BLACK,IDC_WHITE,iColor);
CheckRadioButton(hDlg,IDC_RECT,IDC_ELLIPSE,iFigure);

hCtrlBlock=GetDlgItem(hDlg,IDC_PAINT);

SetFocus(GetDlgItem(hDlg,iColor));
return FALSE;
case WM_COMMAND:
switch(LOWORD(wParam))
{
case IDOK:
iCurrentColor=iColor;
iCurrentFigure=iFigure;
EndDialog(hDlg,TRUE);


// PaintTheBlock(GetParent(hDlg),iCurrentColor,iCurrentFigure);

return TRUE;

case IDCANCEL:

EndDialog(hDlg,FALSE);

return TRUE;

case IDC_BLACK:

case IDC_RED:

case IDC_GREEN:

case IDC_YELLOW:

case IDC_BLUE:

case IDC_MAGENTA:

case IDC_CYAN:

case IDC_WHITE:

iColor=LOWORD(wParam);

CheckRadioButton(hDlg,IDC_BLACK,IDC_WHITE,LOWORD(wParam));

PaintTheBlock(hCtrlBlock,iColor,iFigure);

return TRUE;

case IDC_RECT:

case IDC_ELLIPSE:

iFigure=LOWORD(wParam);

CheckRadioButton(hDlg,IDC_RECT,IDC_ELLIPSE,LOWORD(wParam));

PaintTheBlock(hCtrlBlock,iColor,iFigure);

return TRUE;

}

break;

case WM_PAINT:

PaintTheBlock(hCtrlBlock,iColor,iFigure);

break;

}

return FALSE;

}

其中,菜单资源如下:


其中,对话框资源如下:


相应的资源ID为:

define IDD_DIALOG1 101

define IDR_MENU 102

define IDC_BLACK 1000

define IDC_BLUE 1001

define IDC_GREEN 1002

define IDC_CYAN 1003

define IDC_RED 1004

define IDC_MAGENTA 1005

define IDC_YELLOW 1006

define IDC_WHITE 1007

define IDC_RECT 1008

define IDC_ELLIPSE 1009

define IDC_PAINT 1010

define ID_40001 40001

define IDM_ABOUT 40003

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: