【Visual C++ 2010】Win32程序如何实现复杂的模态对话框
2015-10-09 09:53
459 查看
“`
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;
}
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;
}
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;
}
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;
// 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为:
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
相关文章推荐
- C++中引用(&)的用法和应用实例
- 拓展C++ STL功能,函数适配器,复合函数composite
- 浅析C++中的初始化列表(区别赋值和初始化)
- c语言程序执行顺序结构简述
- typedef用法
- C++类型转换
- C++编译后的函数名反向
- 【第6周 项目3 - 括号的匹配】
- 【第6周 项目2 - 建立链栈算法库】
- VS2010+2.4.11的persistence.cpp line 5008 错误
- 【第6周 项目1 - 建立顺序栈算法库】
- C++ string大小写转换
- eclipse c语言开发环境遇到的问题与解决
- C/C++动态内存分配之用malloc和指针实现动态数组排序
- c++ notes
- 【OJ日志】删除升序数组的重复数字
- C++通过TinyXML类库读写XML文件
- 黑马程序员--C语言之数组的定义以及使用
- 黑马程序员--C语言之for语句的定义及实现
- c++ primer第五版(中文)习题答案 第十章第三节第三小节-lambda捕获和返回