[置頂] 對話框(2)例子一個

一個相對簡單的例子,對學習對話框很有幫助:

1新建Win32項目,編寫代碼

2新建資源,添加ICON

如圖:

 

3資源,添加菜單:

如圖:

 

4資源,添加對話框,拖拽控件

如圖:

其中左上角的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;
}


看看運行結果如何:

 

代碼中有解釋了,其中有一些很有用的函數,自己琢磨琢磨

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章