窗口大小
hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, 680, 640, NULL, NULL, hInstance, NULL);
在资源文件Resource.h中添加控件ID
#define IDB_BUTTON_LOGIN 111
#define IDB_BUTTON_RESET 113
#define IDB_BUTTON_SEND 114
#define IDB_BUTTON_UPDATE 115
//声明变量
HWND Login_Button; //登录按钮
HWND Reset_Button; //登录信息重置
HWND Update_Button; //发送信息重置
HWND Send_Button; //发送信息
HWND edit_ip; //输入id编辑框
HWND edit_port; //输入端口号编辑框
HWND edit_name; //输入昵称编辑框
static HWND hWinRich_recv; //接收文本框
static HWND hWinRich_send; //发送文本框
static HWND hWinRich; //即将发送文本框
//加载创建Richedit控件的动态库
HINSTANCE hRich;
hRich = LoadLibrary(TEXT("RICHED20.dll")); //注意大小写;
在消息回调函数LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)中创建添加控件
//
// 函数: WndProc(HWND, UINT, WPARAM, LPARAM)
//
// 目的: 处理主窗口的消息。
//
// WM_COMMAND - 处理应用程序菜单
// WM_PAINT - 绘制主窗口
// WM_DESTROY - 发送退出消息并返回
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;
switch (message)
{
//在菜单响应中添加点击按钮响应函数
case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
// 分析菜单选择:
switch (wmId)
{
case IDM_ABOUT:
DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
break;
case IDM_EXIT:
DestroyWindow(hWnd);
break;
//登录按钮
case IDB_BUTTON_LOGIN:
{
//添加响应函数
}
break;
//断开连接按钮
case IDB_BUTTON_RESET:
{
//添加响应函数
}
break;
//发送信息按钮
case IDB_BUTTON_SEND:
{
//添加响应函数
}
break;
//更改昵称
case IDB_BUTTON_UPDATE:
{
//添加响应函数
}
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
// TODO: 在此添加任意绘图代码...
EndPaint(hWnd, &ps);
break;
//创建控件
case WM_CREATE:
{
Login_Button = CreateWindow(TEXT("button"),//必须为:button
TEXT("连接"),//按钮上显示的字符
WS_CHILD | WS_VISIBLE,
480, 20, 75, 20, //x,y,宽,高
hWnd, (HMENU)IDB_BUTTON_LOGIN,//添加按钮ID
((LPCREATESTRUCT)lParam)->hInstance, NULL);
Reset_Button = CreateWindow(TEXT("button"),//必须为:button
TEXT("断开"),//按钮上显示的字符
WS_CHILD | WS_VISIBLE,
565, 20, 75, 20, //x,y,宽,高
hWnd, (HMENU)IDB_BUTTON_RESET,//绑定按钮ID
((LPCREATESTRUCT)lParam)->hInstance, NULL);
Send_Button = CreateWindow(TEXT("button"),//必须为:button
TEXT("发送"),//按钮上显示的字符
WS_CHILD | WS_VISIBLE,
570, 540, 75, 30, //x,y,宽,高
hWnd, (HMENU)IDB_BUTTON_SEND,//绑定按钮ID
((LPCREATESTRUCT)lParam)->hInstance, NULL);
Update_Button = CreateWindow(TEXT("button"),//必须为:button
TEXT("更改"),//按钮上显示的字符
WS_CHILD | WS_VISIBLE,
160, 540, 50, 30, //x,y,宽,高
hWnd, (HMENU)IDB_BUTTON_UPDATE,//绑定按钮ID
((LPCREATESTRUCT)lParam)->hInstance, NULL);
//添加服务器ip的编辑框
edit_ip = CreateWindow("edit", NULL, WS_CHILD | WS_VISIBLE | WS_BORDER,
120, 20, 120, 20, hWnd, NULL, NULL, NULL);
//添加端口号的编辑框
edit_port = CreateWindow("edit", NULL, WS_CHILD | WS_VISIBLE | WS_BORDER,
325, 20, 120, 20, hWnd, NULL, NULL, NULL);
//添加昵称的编辑框
edit_name = CreateWindow("edit", NULL, WS_CHILD | WS_VISIBLE | WS_BORDER,
70, 540, 85, 30, hWnd, NULL, NULL, NULL);
HWND hStatic1;
hStatic1 = CreateWindow("static", TEXT("目的地址IP:"), WS_CHILD | WS_VISIBLE | SS_RIGHT, 20, 20, 90, 20, hWnd,
(HMENU)1,((LPCREATESTRUCT)lParam)->hInstance,NULL);
HWND hStatic2;
hStatic2 = CreateWindow("static", TEXT("昵称:"), WS_CHILD | WS_VISIBLE | SS_RIGHT, 20, 540, 45, 30, hWnd,
(HMENU)2, ((LPCREATESTRUCT)lParam)->hInstance, NULL);
HWND hStatic3;
hStatic3 = CreateWindow("static", TEXT("端口:"), WS_CHILD | WS_VISIBLE | SS_RIGHT, 250, 20, 65, 20, hWnd,
(HMENU)3, ((LPCREATESTRUCT)lParam)->hInstance, NULL);
//RichEdit控件的创建;
hWinRich_recv = CreateWindowEx(WS_EX_CLIENTEDGE, TEXT("RichEdit20A"), NULL,
WS_CHILD | WS_VISIBLE | WS_VSCROLL | WS_HSCROLL | ES_MULTILINE | ES_NOHIDESEL,
0, 0, 0, 0, hWnd, 0, ((LPCREATESTRUCT)lParam)->hInstance, NULL);
//发送的消息框
hWinRich = CreateWindowEx(WS_EX_CLIENTEDGE, TEXT("RichEdit20A"), NULL,
WS_CHILD | WS_VISIBLE | WS_VSCROLL | WS_HSCROLL | ES_MULTILINE | ES_NOHIDESEL,
0, 0, 0, 0, hWnd, 0, ((LPCREATESTRUCT)lParam)->hInstance, NULL);
}
break;
case WM_SIZE:
//添加richedit控件时必须添加这个语句,否则不能显示该控件
MoveWindow(hWinRich_recv, 20, 55, 625, 470, TRUE);
MoveWindow(hWinRich, 220, 540, 340, 30, TRUE);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}