[置顶] 对话框(2)例子一个

一个相对简单的例子,对学习对话框很有帮助:

1新建Win32项目,编写代码

2新建资源,添加ICON

如图:

3资源,添加菜单:

如图:

4资源,添加对话框,拖拽控件

扫描二维码关注公众号,回复: 845277 查看本文章

如图:

其中左上角的ICON就是刚刚建立的ICON:

即:

具体代码如下

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

void PaintWindow(HWND hwnd,int iColor,int iFigure);//声明一个函数,这个函数主要是改变窗体背景颜色
BOOL CALLBACK DialogProc(HWND hwnd,UINT uMsg,WPARAM wParam,LPARAM lParam);//声明对话框过程函数
LRESULT CALLBACK WindowProc(
  HWND hwnd,      // handle to window
  UINT uMsg,      // message identifier
  WPARAM wParam,  // first message parameter
  LPARAM lParam   // second message parameter
);


int iCurrentColor=ID_BLACK;//声明两个全局变量,其中一个保存当前画刷的颜色
int iCurrentFigure=ID_RECTANGLE;//全局变量,保存当前的选中的图形

int WINAPI WinMain(
  HINSTANCE hInstance,      // handle to current instance
  HINSTANCE hPrevInstance,  // handle to previous instance
  LPSTR lpCmdLine,          // command line
  int nCmdShow              // show state
)
{
	static TCHAR szAppName[]=TEXT("leidemingzi");
	HWND hwnd;
	MSG msg;
	WNDCLASS wndclass;
	HMENU hMenu;

	wndclass.cbClsExtra=0;
	wndclass.cbWndExtra=0;
	wndclass.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
	wndclass.hCursor=LoadCursor(NULL,IDC_ARROW);
	wndclass.hIcon=LoadIcon(NULL,IDI_ERROR);
	wndclass.hInstance=hInstance;
	wndclass.lpfnWndProc=WindowProc;
	wndclass.lpszClassName=szAppName;
	wndclass.lpszMenuName=NULL;
	wndclass.style=CS_HREDRAW|CS_VREDRAW;
	
	if(!RegisterClass(&wndclass))//注册窗口
	{
		MessageBox(NULL,TEXT("the program require the window nt"),TEXT("tips"),MB_ICONERROR);
		return 0;
	}

	hMenu=LoadMenu(hInstance,MAKEINTRESOURCE(MENUID));//加载菜单
	hwnd=CreateWindow(
	  szAppName,  // registered class name
	  TEXT("this is title"), // window name
	  WS_OVERLAPPEDWINDOW,        // window style
	  CW_USEDEFAULT,                // horizontal position of window
	  CW_USEDEFAULT,                // vertical position of window
	  CW_USEDEFAULT,           // window width
	  CW_USEDEFAULT,          // window height
	  NULL,      // handle to parent or owner window
	  hMenu,          // menu handle or child identifier
	  hInstance,  // handle to application instance
	  NULL       // window-creation data
);

	ShowWindow(hwnd,nCmdShow);
	UpdateWindow(hwnd);

	while(GetMessage(&msg,NULL,0,0))
	{
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}

	return msg.wParam;
}


LRESULT CALLBACK WindowProc(
  HWND hwnd,      // handle to window
  UINT uMsg,      // message identifier
  WPARAM wParam,  // first message parameter
  LPARAM lParam   // second message parameter
)
{
	static HINSTANCE hInst;
	PAINTSTRUCT ps;

	switch(uMsg)
	{
	case WM_CREATE:
		hInst=((LPCREATESTRUCT)lParam)->hInstance;
		return 0;

	case WM_COMMAND:
		switch(LOWORD(wParam))
		{
		case ID_HELP_ABOUT:
			DialogBox(hInst,MAKEINTRESOURCE(DIALOGID),hwnd,DialogProc);//显示对话框
			break;
		}
		return 0;

	case WM_PAINT:
		BeginPaint(hwnd,&ps);

		PaintWindow(hwnd,iCurrentColor,iCurrentFigure);
		MessageBeep(0);
		EndPaint(hwnd,&ps);
		return 0;

		

	case WM_DESTROY:
		PostQuitMessage(0);
		return 0;
	}
	return DefWindowProc(hwnd,uMsg,wParam,lParam);
}


void PaintWindow(HWND hwnd,int iColor,int iFigure)
{
	static COLORREF color[8]={RGB(0,0,0),RGB(0,0,255),RGB(0,255,0),RGB(0,255,255)//定义颜色数组,8种颜色,刚好对应单选按钮的颜色
					,RGB(255,0,0),RGB(255,0,255),RGB(255,255,0),RGB(255,255,255)};
	
	HDC hdc;
	RECT rect;
	hdc=GetDC(hwnd);
	GetClientRect(hwnd,&rect);
	HBRUSH hbrush=CreateSolidBrush(color[iColor-ID_BLACK]);//根据传进来的参数,创建画刷
	SelectObject(hdc,hbrush);//把画刷选入环境设备中去
	
	if(iFigure==ID_RECTANGLE)
	{
		Rectangle(hdc,rect.left,rect.top,rect.right,rect.bottom);//绘画矩形,自动填充颜色
	}else
	{
		Ellipse(hdc,rect.left,rect.top,rect.right,rect.bottom);//绘画椭圆,自动填充颜色
	}
	
	ReleaseDC(hwnd,hdc);
}

void Flush(HWND hwnd)//这个方法是使客户区失效,从而触发WM_PAINT时间,不能直接在DialogProc使用InvaidateRect
                       //因为DialogProc中的hwnd指的是Dialog的句柄
{
	InvalidateRect(hwnd,NULL,TRUE);
}

BOOL CALLBACK DialogProc(HWND hwnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
{
	//static HWND hCtrlBlock;
	static int iColor,iFigure;


	switch(uMsg)
	{
	case WM_INITDIALOG:
		iColor=ID_WHITE;
		iFigure=ID_RECTANGLE;
		return TRUE;

	case WM_COMMAND:
		switch(LOWORD(wParam))//低位的wParam是ID
		{
		case IDOK:
			iCurrentColor=iColor;
			iCurrentFigure=iFigure;
			//InvalidateRect(hwnd,NULL,TRUE);这是错误的,该hwnd是Dialog的句柄
			Flush(GetParent(hwnd));
			EndDialog(hwnd,0);
			//MessageBeep(0);当时是用来测试用的,这个方法不错。
			return TRUE;

		case IDCANCEL:
			EndDialog(hwnd,0);
			return TRUE;

		case ID_BLACK:
		case ID_BLUE:
		case ID_GREEN:
		case ID_CYAN:
		case ID_RED:
		case ID_MAGENTA:
		case ID_YELLOW:
		case ID_WHITE:
			iColor=LOWORD(wParam);
			
			CheckRadioButton(hwnd,ID_BLACK,ID_WHITE,LOWORD(wParam));//切换按钮状态
			//MessageBeep(0);
			
			return TRUE;

		case ID_RECTANGLE:
		case ID_ELLIPSE:
			iFigure=LOWORD(wParam);
			CheckRadioButton(hwnd,ID_RECTANGLE,ID_ELLIPSE,LOWORD(wParam));//切换按钮状态
			//MessageBeep(0);
			return TRUE;


		}
		break;
	}

	return FALSE;
}


看看运行结果如何:

代码中有解释了,其中有一些很有用的函数,自己琢磨琢磨

猜你喜欢

转载自leili.iteye.com/blog/1625711