C/C++:使用Windows API完成基本的窗口创建、编写新循环处理对话框消息(模态窗口)

由于期末大作业的要求,要求使用API编写窗口程序,不允许使用MFC等类库,在最后对BUTTON的消息处理上需要弹出另一个窗口出现了问题,多方查询资料,以及MFC的相关接口底层写法,找到了如下解决方法:

#include
LRESULT CALLBACK  ConnectProc(
	HWND hwnd,
	UINT uMsg,
	WPARAM wParam,
	LPARAM lParam
);
HWND hbtnConnect;
HWND hbtnQuit_1;
HWND heditIPaddress;//声明第一个窗口的三个窗口句柄,分别表示两个按钮和一个文本框
void DisplayModelDialog(HWND hParent);
LRESULT CALLBACK TalkProc(HWND hdig, UINT message, WPARAM wParam, LPARAM lParam);
HWND hbtnSend;
HWND hbtnQuit_2;
HWND heditIPlist;
HWND heditReciveText;
HWND heditSendText;
int x = LOWORD(GetDialogBaseUnits()),
    y = HIWORD(GetDialogBaseUnits());//对话框的单元长度

int WINAPI WinMain(
	HINSTANCE hInstance,
	HINSTANCE hPreInstance,
	LPSTR lpCmdLine,
	int nCmdShow
)
{
	CISocket.CreatSocket();
	//窗口对象初始化
	windowclass.cbClsExtra = 0;
	windowclass.cbWndExtra = 0;
	windowclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
	windowclass.hCursor = LoadCursor(NULL, IDC_ARROW);
	windowclass.hIcon = LoadIcon(NULL, IDI_ERROR);
	windowclass.hInstance = hInstance;
	windowclass.style = CS_HREDRAW | CS_VREDRAW;
	windowclass.lpfnWndProc = ConnectProc;
	windowclass.lpszClassName = "Windows";
	windowclass.lpszMenuName = NULL;
	//先注册一个窗口
	RegisterClass(&windowclass);

	//注册第二个窗口
	windowclass.lpfnWndProc = TalkProc;
	windowclass.lpszClassName = "Talk";
	windowclass.lpszMenuName = NULL;
	RegisterClass(&windowclass);
	
//创建父窗口
hwnd = CreateWindow("windows","Connection Request",WS_OVERLAPPEDWINDOW,0,0, 460, 260 ,NULL,NULL,hInstance,NULL);

ShowWindow(hwnd,SW_SHOWNORMAL);
UpdateWindow(hwnd);

//消息循环
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
	TranslateMessage(&msg);
	DispatchMessage(&msg);
}

return msg.wParam;
}
LRESULT CALLBACK ConnectProc(
	HWND hwnd,
	UINT uMsg,
	WPARAM wParam,
	LPARAM lParam
)
{

	//HINSTANCE hInstance;
	switch (uMsg)
	{

	case WM_CREATE:
		hbtnConnect = CreateWindow("button", "连接", WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,  4*x, y*7, 20 * x, 7 * y / 4, hwnd, (HMENU)ID_BTNCONNECT, ((LPCREATESTRUCT)lParam)->hInstance, NULL);
		hbtnQuit_1 = CreateWindow("button", "退出", WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, 26 * x, y * 7, 20 * x, 7 * y / 4, hwnd, (HMENU)ID_BTNQUIT_1, ((LPCREATESTRUCT)lParam)->hInstance, NULL);
		heditIPaddress = CreateWindow("edit", "输入服务端IP", WS_CHILD | WS_VISIBLE | WS_BORDER | ES_LEFT, 11*x, y*2.5, 30 * x, 7 * y / 4, hwnd, (HMENU)ID_IPADDRESS, ((LPCREATESTRUCT)lParam)->hInstance, NULL);
		break;
	case WM_COMMAND:
		if (LOWORD(wParam) == ID_BTNCONNECT)
		{
			DisplayModelDialog(hwnd);
		}
		else if (LOWORD(wParam) == ID_BTNQUIT_1)
		{
			PostQuitMessage(0);
		}
		break;
	case WM_DESTROY:
		PostQuitMessage(0);
		break;
	default:
		return DefWindowProc(hwnd, uMsg, wParam, lParam);
	}
	return 0;
}


LRESULT CALLBACK TalkProc(HWND hdig, UINT message, WPARAM wParam, LPARAM lParam)
{

	switch (message)
	{
	case WM_CREATE:
		hbtnSend = CreateWindow("button", "发送", WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, 15 * x, y * 38, 10 * x, 7 * y / 4, hdig, (HMENU)ID_BTNSEND, ((LPCREATESTRUCT)lParam)->hInstance, NULL);
		hbtnQuit_1 = CreateWindow("button", "退出", WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, 35 * x, y * 38, 10 * x, 7 * y / 4, hdig, (HMENU)ID_BTNQUIT_2, ((LPCREATESTRUCT)lParam)->hInstance, NULL);
		heditReciveText = CreateWindow("edit", "", WS_CHILD | WS_VISIBLE | WS_BORDER | ES_LEFT| ES_MULTILINE| ES_WANTRETURN, 8 * x, y*2.5, 50 * x, 100 * y / 4, hdig, (HMENU)ID_RECEIVETEXT, ((LPCREATESTRUCT)lParam)->hInstance, NULL);
		heditSendText = CreateWindow("edit", "", WS_CHILD | WS_VISIBLE | WS_BORDER | ES_LEFT, 8 * x, y * 29, 50 * x, 30 * y / 4, hdig, (HMENU)ID_RECEIVETEXT, ((LPCREATESTRUCT)lParam)->hInstance, NULL);
		break;
	case WM_COMMAND:
		if (LOWORD(wParam) == ID_BTNSEND)
		{
			
			
		}
		else if (LOWORD(wParam) == ID_BTNQUIT_2)
		{
			PostQuitMessage(0);
		}
		break;
	case WM_DESTROY:
		PostQuitMessage(0);
		break;
	default:
		return DefWindowProc(hdig, message, wParam, lParam);
	}
	return 0;
}


//新建消息循环实现弹出另一个窗口
void DisplayModelDialog(HWND hParent) {

	EnableWindow(hParent, FALSE);

	hdialog = CreateWindow("Talk", "Talkdialog", WS_OVERLAPPEDWINDOW, 0, 0, 560, 720, hParent, NULL, (HINSTANCE)GetWindowLong(hParent, GWL_HINSTANCE),  NULL);
	
	ShowWindow(hdialog, SW_SHOW);
	UpdateWindow(hdialog);
	MSG msg;

	while (GetMessage(&msg, NULL, 0, 0)) {

		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}
	
	EnableWindow(hParent, TRUE);
	SetForegroundWindow(hParent);
}

其中各个窗口ID可自己设置 
  


PS:DisplayModelDialog()函数出自

https://www.cnblogs.com/zplutor/archive/2011/02/20/1958973.html

你可能感兴趣的:(代码记录)