#include <windows.h>
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
HINSTANCE hInst;
/* The 'main' function of Win32 GUI programs: this is where execution starts */
int WINAPI WinMain(
HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
static TCHAR szClassName[] = TEXT("HelloWin"); /*窗口类名*/
HWND hwnd; /*窗口句柄*/
MSG msg; /*消息*/
WNDCLASS wndclass; /*窗口类*/
hInst = hInstance;
/***********第一步:注册窗口类*************/
/*为窗口类各个字段赋值*/
wndclass.style = CS_HREDRAW | CS_VREDRAW; /*窗口风格*/
wndclass.lpfnWndProc = WndProc; /*窗口过程*/
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance; /*当前窗口句柄*/
wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION); /*窗口图标*/
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);/*鼠标样式*/
wndclass.hbrBackground= (HBRUSH)GetStockObject(WHITE_BRUSH);/*窗口背景画刷*/
wndclass.lpszMenuName = NULL; /*窗口菜单*/
wndclass.lpszClassName= szClassName; /*窗口类名*/
/*注册窗口*/
RegisterClass(&wndclass);
/*************第二步:创建窗口(并让窗口显示出来)***************/
hwnd = CreateWindow(
szClassName, /*窗口名字*/
TEXT("Welcome"), /*窗口标题*/
WS_OVERLAPPEDWINDOW, /*窗口风格*/
CW_USEDEFAULT, /*初始化x轴的位置*/
CW_USEDEFAULT, /*初始化y轴的位置*/
640, /*窗口宽度*/
480, /*窗口高度*/
NULL, /*父窗口句柄*/
NULL, /*窗口菜单句柄*/
hInstance, /*当前窗口句柄*/
NULL /*不使用该值*/
);
if(hwnd == NULL)
{
MessageBox(NULL, "创建窗口出错!", "Error", MB_OK);
return -1;
}
/*显示窗口*/
ShowWindow(hwnd, nCmdShow);
/*更新(绘制)窗口*/
UpdateWindow(hwnd);
/*************第三步:消息循环*************/
while(GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg); /*翻译消息*/
DispatchMessage(&msg); /*分派消息*/
}
return msg.wParam; /*当GetMessage程序返回FALSE是程序结束*/
}
/*************第四步:窗口过程*****************/
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT ps;
HDC hdc;
static HFONT hFont; // 逻辑字体
static HWND hLabUsername; //静态文本框--用户名
static HWND hLabPassword; //静态文本框--密码
static HWND hEditUsername; //单行文本输入框
static HWND hEditPassword; //密码输入框
static HWND hBtnLogin; //登录按钮
switch(message)
{
case WM_CREATE:
{
//创建逻辑字体
hFont = CreateFont(
-14/*高度*/, -7/*宽度*/,0,0,400/*一般设值为400*/,
FALSE/*不带斜体*/, FALSE/*不带下划线*/,FALSE/*不带删除线*/,
DEFAULT_CHARSET, //使用默认字符集
OUT_CHARACTER_PRECIS, CLIP_CHARACTER_PRECIS,
DEFAULT_QUALITY, //默认输出质量
FF_DONTCARE, //不指定字体族
TEXT("微软雅黑") //字体名
);
//创建静态文本框控件--用户名
hLabUsername = CreateWindow(
TEXT("static"),
TEXT("用户名"),
WS_CHILD | WS_VISIBLE | SS_CENTERIMAGE/*垂直居中*/ | SS_RIGHT/*水平居右*/,
0, 20, 70, 26,//X坐标、Y坐标、宽度、高度
hWnd,
(HMENU)1,/*控件ID*/
hInst,
NULL
);
//创建静态文本框控件--密码
hLabPassword = CreateWindow(
TEXT("static"),
TEXT("密码"),
WS_CHILD | WS_VISIBLE | SS_CENTERIMAGE | SS_RIGHT,
0, 56, 70, 26,
hWnd,
(HMENU)2,
hInst,
NULL
);
//创建单行文本框输入控件
hEditUsername = CreateWindow(
TEXT("edit"),
TEXT(""),
WS_CHILD | WS_VISIBLE | WS_BORDER | ES_AUTOHSCROLL,/*水平滚动*/
80, 20, 200, 26,
hWnd,
(HMENU)3,
hInst,
NULL
);
//创建密码输入框
hEditPassword = CreateWindow(
TEXT("edit"),
TEXT(""),
WS_CHILD | WS_VISIBLE | WS_BORDER | ES_PASSWORD/*密码*/ | ES_AUTOHSCROLL/*水平滚动*/,
80, 56, 200, 26,
hWnd,
(HMENU)4,
hInst,
NULL
);
//创建按钮控件
hBtnLogin = CreateWindow(
TEXT("button"), //按钮控件类名
TEXT("登录"),
WS_CHILD | WS_VISIBLE | WS_BORDER | BS_FLAT/*扁平样式*/,
80/*X坐标*/,92/*Y坐标*/, 200/*宽度*/,30/*高度*/,
hWnd,
(HMENU)5/*控件唯一标识符*/,
hInst,
NULL
);
//依次设置控件字体
SendMessage(hLabUsername, WM_SETFONT, (WPARAM)hFont, NULL);
SendMessage(hLabPassword, WM_SETFONT, (WPARAM)hFont, NULL);
SendMessage(hEditUsername, WM_SETFONT, (WPARAM)hFont, NULL);
SendMessage(hEditPassword, WM_SETFONT, (WPARAM)hFont, NULL);
SendMessage(hBtnLogin, WM_SETFONT, (WPARAM)hFont, NULL);
break;
}
case WM_PAINT:
{
hdc = BeginPaint(hWnd, &ps);
//Ellipse(hdc, 0, 100, 200, 200);
EndPaint(hWnd, &ps);
break;
}
/*窗口销毁消息*/
case WM_DESTROY:
{
/*处理WM_DESTROY消息时删除之前我们创建的一切GDI对象*/
DeleteObject(hFont); //删除创建的字体
PostQuitMessage(0);
break;
}
default:
{
return DefWindowProc(hWnd, message, wParam, lParam);
}
}
return 0;
}
获取编辑框控件文本:
#include <windows.h>
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
HINSTANCE hInst;
/* The 'main' function of Win32 GUI programs: this is where execution starts */
int WINAPI WinMain(
HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
static TCHAR szClassName[] = TEXT("HelloWin"); /*窗口类名*/
HWND hwnd; /*窗口句柄*/
MSG msg; /*消息*/
WNDCLASS wndclass; /*窗口类*/
hInst = hInstance;
/***********第一步:注册窗口类*************/
/*为窗口类各个字段赋值*/
wndclass.style = CS_HREDRAW | CS_VREDRAW; /*窗口风格*/
wndclass.lpfnWndProc = WndProc; /*窗口过程*/
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance; /*当前窗口句柄*/
wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION); /*窗口图标*/
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);/*鼠标样式*/
wndclass.hbrBackground= (HBRUSH)GetStockObject(WHITE_BRUSH);/*窗口背景画刷*/
wndclass.lpszMenuName = NULL; /*窗口菜单*/
wndclass.lpszClassName= szClassName; /*窗口类名*/
/*注册窗口*/
RegisterClass(&wndclass);
/*************第二步:创建窗口(并让窗口显示出来)***************/
hwnd = CreateWindow(
szClassName, /*窗口名字*/
TEXT("Welcome"), /*窗口标题*/
WS_OVERLAPPEDWINDOW, /*窗口风格*/
CW_USEDEFAULT, /*初始化x轴的位置*/
CW_USEDEFAULT, /*初始化y轴的位置*/
640, /*窗口宽度*/
480, /*窗口高度*/
NULL, /*父窗口句柄*/
NULL, /*窗口菜单句柄*/
hInstance, /*当前窗口句柄*/
NULL /*不使用该值*/
);
if(hwnd == NULL)
{
MessageBox(NULL, "创建窗口出错!", "Error", MB_OK);
return -1;
}
/*显示窗口*/
ShowWindow(hwnd, nCmdShow);
/*更新(绘制)窗口*/
UpdateWindow(hwnd);
/*************第三步:消息循环*************/
while(GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg); /*翻译消息*/
DispatchMessage(&msg); /*分派消息*/
}
return msg.wParam; /*当GetMessage程序返回FALSE是程序结束*/
}
/*************第四步:窗口过程*****************/
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;
static HFONT hFont; // 逻辑字体
static HWND hLabUsername; //静态文本框--用户名
static HWND hLabPassword; //静态文本框--密码
static HWND hEditUsername; //单行文本输入框
static HWND hEditPassword; //密码输入框
static HWND hBtnLogin; //登录按钮
//定义缓冲区
TCHAR szUsername[100];
TCHAR szPassword[100];
TCHAR szUserInfo[200];
switch(message)
{
case WM_CREATE:
{
//创建逻辑字体
hFont = CreateFont(
-14/*高度*/, -7/*宽度*/,0,0,400/*一般设值为400*/,
FALSE/*不带斜体*/, FALSE/*不带下划线*/,FALSE/*不带删除线*/,
DEFAULT_CHARSET, //使用默认字符集
OUT_CHARACTER_PRECIS, CLIP_CHARACTER_PRECIS,
DEFAULT_QUALITY, //默认输出质量
FF_DONTCARE, //不指定字体族
TEXT("微软雅黑") //字体名
);
//创建静态文本框控件--用户名
hLabUsername = CreateWindow(
TEXT("static"),
TEXT("用户名"),
WS_CHILD | WS_VISIBLE | SS_CENTERIMAGE/*垂直居中*/ | SS_RIGHT/*水平居右*/,
0, 20, 70, 26,//X坐标、Y坐标、宽度、高度
hWnd,
(HMENU)1,/*控件ID*/
hInst,
NULL
);
//创建静态文本框控件--密码
hLabPassword = CreateWindow(
TEXT("static"),
TEXT("密码"),
WS_CHILD | WS_VISIBLE | SS_CENTERIMAGE | SS_RIGHT,
0, 56, 70, 26,
hWnd,
(HMENU)2,
hInst,
NULL
);
//创建单行文本框输入控件
hEditUsername = CreateWindow(
TEXT("edit"),
TEXT(""),
WS_CHILD | WS_VISIBLE | WS_BORDER | ES_AUTOHSCROLL,/*水平滚动*/
80, 20, 200, 26,
hWnd,
(HMENU)3,
hInst,
NULL
);
//创建密码输入框
hEditPassword = CreateWindow(
TEXT("edit"),
TEXT(""),
WS_CHILD | WS_VISIBLE | WS_BORDER | ES_PASSWORD/*密码*/ | ES_AUTOHSCROLL/*水平滚动*/,
80, 56, 200, 26,
hWnd,
(HMENU)4,
hInst,
NULL
);
//创建按钮控件
hBtnLogin = CreateWindow(
TEXT("button"), //按钮控件类名
TEXT("登录"),
WS_CHILD | WS_VISIBLE | WS_BORDER | BS_FLAT/*扁平样式*/,
80/*X坐标*/,92/*Y坐标*/, 200/*宽度*/,30/*高度*/,
hWnd,
(HMENU)5/*控件唯一标识符*/,
hInst,
NULL
);
//依次设置控件字体
SendMessage(hLabUsername, WM_SETFONT, (WPARAM)hFont, NULL);
SendMessage(hLabPassword, WM_SETFONT, (WPARAM)hFont, NULL);
SendMessage(hEditUsername, WM_SETFONT, (WPARAM)hFont, NULL);
SendMessage(hEditPassword, WM_SETFONT, (WPARAM)hFont, NULL);
SendMessage(hBtnLogin, WM_SETFONT, (WPARAM)hFont, NULL);
break;
}
case WM_COMMAND:
{
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
switch(wmId)
{
case 5: //按钮的HMENU参数
{
//获取输入框的数据
GetWindowText(hEditUsername, szUsername, 100);
GetWindowText(hEditPassword, szPassword, 100);
wsprintf(szUserInfo, TEXT("Warning:\r\nYour user name is: %s\r\nYour Password is: %s"), szUsername, szPassword);
MessageBox(hWnd, szUserInfo, TEXT("Information tips"), MB_ICONINFORMATION);
break;
}
default:
{
//不处理的消息一定要交给defWindowProc处理
return DefWindowProc(hWnd, message, wParam, lParam);
break;
}
}
break;
}
case WM_PAINT:
{
hdc = BeginPaint(hWnd, &ps);
//Ellipse(hdc, 0, 100, 200, 200);
EndPaint(hWnd, &ps);
break;
}
/*窗口销毁消息*/
case WM_DESTROY:
{
/*处理WM_DESTROY消息时删除之前我们创建的一切GDI对象*/
DeleteObject(hFont); //删除创建的字体
PostQuitMessage(0);
break;
}
default:
{
return DefWindowProc(hWnd, message, wParam, lParam);
}
}
return 0;
}