《Windows程序设计》之非模态对话框

#include <Windows.h>

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

HWND hDlgModeless;

int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,PSTR szCmdLine,int iCmdShow)
{
	static TCHAR szAppName[]=TEXT("Color2");
	MSG msg;
	HWND hwnd;
	WNDCLASS wndclass;

	wndclass.style=CS_VREDRAW|CS_HREDRAW;
	wndclass.lpfnWndProc=WndProc;
	wndclass.cbClsExtra=0;
	wndclass.cbWndExtra=0;
	wndclass.hbrBackground=CreateSolidBrush(0L);
	wndclass.hInstance=hInstance;
	wndclass.hIcon=LoadIcon(NULL,IDI_APPLICATION);
	wndclass.hCursor=LoadCursor(NULL,IDC_ARROW);
	wndclass.lpszMenuName=NULL;
	wndclass.lpszClassName=szAppName;
	if(!RegisterClass(&wndclass))
	{
		MessageBox(NULL,TEXT("This program requires Windows NT!"),szAppName,MB_ICONERROR);
		return 0;
	}
         //WS_CLIPCHILDREN作用在不擦出对话框的前提下重绘方窗口
         hwnd=CreateWindow(szAppName,TEXT("Color Scroll"),WS_OVERLAPPEDWINDOW|WS_CLIPCHILDREN,CW_USEDEFAULT,CW_USEDEFAULT,
						CW_USEDEFAULT,CW_USEDEFAULT,NULL,NULL,hInstance,NULL);
	ShowWindow(hwnd,iCmdShow);
	UpdateWindow(hwnd);
	hDlgModeless=CreateDialog(hInstance,TEXT("ColorScrDlg"),hwnd,ColorScrDlg);
	while(GetMessage(&msg,NULL,0,0))
	{
		//这个地方来判断消息循环是不是对话框的
		if(hDlgModeless==0||!IsDialogMessage(hDlgModeless,&msg))
		{
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
	}
	return msg.wParam;
}

LRESULT CALLBACK WndProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam)
{
	switch(message)
	{
	case WM_DESTROY:
		//关闭前先把类的背景刷子给换掉,删掉新建的
		DeleteObject((HGDIOBJ)SetClassLong(hwnd,GCL_HBRBACKGROUND,(LONG)GetStockObject(WHITE_BRUSH)));
		PostQuitMessage(0);
		return 0;
	}
	return DefWindowProc(hwnd,message,wParam,lParam);
}

BOOL CALLBACK ColorScrDlg(HWND hDlg,UINT message,WPARAM wParam,LPARAM lParam)
{
	static int iColor[3];
	HWND hwndParent,hCtrl;
	int iCtrlID,iIndex;
	switch(message)
	{
	case WM_INITDIALOG:
		for(iCtrlID=10;iCtrlID<13;iCtrlID++)
		{
			//iCtrlID与资源中的是相对应的
			//获取句柄
			hCtrl=GetDlgItem(hDlg,iCtrlID);
			//设置范围
			SetScrollRange(hCtrl,SB_CTL,0,255,FALSE);
			//设置起始点
			SetScrollPos(hCtrl,SB_CTL,0,FALSE);
		}
		return TRUE;
	case WM_VSCROLL:
		//lParam传递进来的是句柄
		hCtrl=(HWND)lParam;
		//获取ID号,这里的hCtrl是窗口上三个滚动条
		iCtrlID=GetWindowLong(hCtrl,GWL_ID);
		iIndex=iCtrlID-10;
		//hwndParent是显示颜色变化的那个窗口句柄
		hwndParent=GetParent(hDlg);
		//wParam低位是控制
		switch(LOWORD(wParam))
		{
		//这个case语句后没有break,直接触发SB_LINEDOWN来再向下移动1,
		//并且判断是否是最低了
		case SB_PAGEDOWN:
			iColor[iIndex]+=15;
		//鼠标点击滚动条向下的按钮
		case SB_LINEDOWN:
			iColor[iIndex]=min(255,iColor[iIndex]+1);
			break;
		case SB_PAGEUP:
			iColor[iIndex]-=15;
		case SB_LINEUP:
			iColor[iIndex]=max(0,iColor[iIndex]-1);
			break;
		case SB_TOP:
			iColor[iIndex]=0;
			break;
		case SB_BOTTOM:
			iColor[iIndex]=255;
			break;
		case SB_THUMBPOSITION:
		case SB_THUMBTRACK:
			//wParam高位鼠标点击的位置
			iColor[iIndex]=HIWORD(wParam);
			break;
		default:
			return FALSE;
		}
		SetScrollPos(hCtrl,SB_CTL,iColor[iIndex],TRUE);
		//设置相应的每个滚动条下面的数字
		SetDlgItemInt(hDlg,iCtrlID+3,iColor[iIndex],FALSE);
		//替换与类有关的背景的刷子,删除返回的刷子句柄
		DeleteObject((HGDIOBJ)SetClassLong(hwndParent,GCL_HBRBACKGROUND,(LONG)CreateSolidBrush(RGB(iColor[0],iColor[1],iColor[2]))));
		//重绘
		InvalidateRect(hwndParent,NULL,TRUE);
		return TRUE;
	}
	return FALSE;
}


你可能感兴趣的:(windows,null,application,callback,scroll,winapi)