注:转载至:https://my.oschina.net/u/241930/blog/528285,谢谢该博主的分享
#include
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
HINSTANCE hInst;
int WINAPI WinMain(
HINSTANCEhInstance,
HINSTANCEhPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
static TCHARszClassName[] = 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,
CW_USEDEFAULT,
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;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAMwParam, LPARAM lParam)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;
static HFONT hFont; //逻辑字体
static HWNDhLabUsername; //静态文本框--用户名
static HWNDhLabPassword; //静态文本框--密码
static HWNDhEditUsername; //单行文本输入框
static HWNDhEditPassword; //密码输入框
static HWND hBtnLogin; //登录按钮
static HBRUSH hBrush; //画刷
//定义缓冲区
TCHARszUsername[100];
TCHARszPassword[100];
TCHARszUserInfo[200];
switch(message)
{
case WM_CREATE:
{
//创建画刷
hBrush =CreateSolidBrush(RGB(0X41, 0X96, 0X4F));//翠绿色
//创建逻辑字体
hFont =CreateFont(
-14, -7,0,0,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,
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,92, 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 username is: %s\r\nYour Password is: %s"), szUsername,szPassword);
MessageBox(hWnd, szUserInfo, TEXT("Informationtips"), MB_ICONINFORMATION);
break;
}
default:
{
//不处理的消息一定要交给defWindowProc处理
return DefWindowProc(hWnd, message, wParam,lParam);
break;
}
}
break;
}
case WM_CTLCOLORSTATIC:
{
return(INT_PTR)hBrush; //这里必须返回画刷句柄
}
case WM_PAINT:
{
hdc =BeginPaint(hWnd, &ps);
//Ellipse(hdc, 0, 100, 200, 200);
EndPaint(hWnd, &ps);
break;
}
case WM_DESTROY:
{
DeleteObject(hFont); //删除创建的字体
DeleteObject(hBrush);
PostQuitMessage(0);
break;
}
default:
{
returnDefWindowProc(hWnd, message, wParam, lParam);
}
}
return 0;
}
设置控件字体的背景颜色和控件字体的颜色:
#include
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
HINSTANCE hInst;
int WINAPI WinMain(
HINSTANCEhInstance,
HINSTANCEhPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
static TCHARszClassName[] = 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,
CW_USEDEFAULT,
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;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAMwParam, LPARAM lParam)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;
HDC hdcWnd;
HDC hdcStatic;
static HFONT hFont; //逻辑字体
static HWNDhLabUsername; //静态文本框--用户名
static HWNDhLabPassword; //静态文本框--密码
static HWNDhEditUsername; //单行文本输入框
static HWNDhEditPassword; //密码输入框
static HWND hBtnLogin; //登录按钮
static HBRUSH hBrush; //画刷
//定义缓冲区
TCHARszUsername[100];
TCHARszPassword[100];
TCHARszUserInfo[200];
switch(message)
{
case WM_CREATE:
{
//创建画刷
hBrush =CreateSolidBrush(RGB(0X41, 0X96, 0X4F));//翠绿色,控件的背景颜色
//创建逻辑字体
hFont =CreateFont(
-14, -7,0,0,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,
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,92, 200,30,
hWnd,
(HMENU)5,
hInst,
NULL
);
//依次设置控件字体
SendMessage(hLabUsername, WM_SETFONT, (WPARAM)hFont, 0);
SendMessage(hLabPassword, WM_SETFONT, (WPARAM)hFont, 0);
SendMessage(hEditUsername, WM_SETFONT, (WPARAM)hFont, 0);
SendMessage(hEditPassword, WM_SETFONT, (WPARAM)hFont, 0);
SendMessage(hBtnLogin, WM_SETFONT, (WPARAM)hFont, 0);
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 username is: %s\r\nYour Password is: %s"), szUsername,szPassword);
MessageBox(hWnd, szUserInfo, TEXT("Informationtips"), MB_ICONINFORMATION);
break;
}
default:
{
//不处理的消息一定要交给defWindowProc处理
return DefWindowProc(hWnd, message, wParam,lParam);
break;
}
}
break;
}
case WM_CTLCOLORSTATIC:
{
hdcStatic= (HDC)wParam;
SetTextColor(hdcStatic, RGB(255,255,255));//控件文本的字体颜色
SetBkColor(hdcStatic, RGB(0X41, 0X96,0X4F));//控件字体的背景颜色,注意跟以上区分
return(INT_PTR)hBrush; //这里必须返回画刷句柄
}
case WM_PAINT:
{
hdc =BeginPaint(hWnd, &ps);
//Ellipse(hdc, 0, 100, 200, 200);
EndPaint(hWnd, &ps);
break;
}
case WM_DESTROY:
{
DeleteObject(hFont); //删除创建的字体
DeleteObject(hBrush);
PostQuitMessage(0);
break;
}
default:
{
returnDefWindowProc(hWnd, message, wParam, lParam);
}
}
return 0;
}
设置透明背景:
#include
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
HINSTANCE hInst;
int WINAPI WinMain(
HINSTANCEhInstance,
HINSTANCEhPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
static TCHARszClassName[] = 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,
CW_USEDEFAULT,
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;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAMwParam, LPARAM lParam)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;
HDC hdcWnd;
HDC hdcStatic;
static HFONT hFont; //逻辑字体
static HWNDhLabUsername; //静态文本框--用户名
static HWNDhLabPassword; //静态文本框--密码
static HWNDhEditUsername; //单行文本输入框
static HWNDhEditPassword; //密码输入框
static HWND hBtnLogin; //登录按钮
static HBRUSH hBrush; //画刷
//定义缓冲区
TCHARszUsername[100];
TCHARszPassword[100];
TCHARszUserInfo[200];
switch(message)
{
case WM_CREATE:
{
//创建画刷
hBrush =CreateSolidBrush(RGB(0X41, 0X96, 0X4F));//翠绿色,控件的背景颜色
//创建逻辑字体
hFont =CreateFont(
-14, -7,0,0,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,
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,92, 200,30,
hWnd,
(HMENU)5,
hInst,
NULL
);
//依次设置控件字体
SendMessage(hLabUsername, WM_SETFONT, (WPARAM)hFont, 0);
SendMessage(hLabPassword, WM_SETFONT, (WPARAM)hFont, 0);
SendMessage(hEditUsername, WM_SETFONT, (WPARAM)hFont, 0);
SendMessage(hEditPassword, WM_SETFONT, (WPARAM)hFont, 0);
SendMessage(hBtnLogin, WM_SETFONT, (WPARAM)hFont, 0);
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 username is: %s\r\nYour Password is: %s"), szUsername,szPassword);
MessageBox(hWnd, szUserInfo, TEXT("Informationtips"), MB_ICONINFORMATION);
break;
}
default:
{
//不处理的消息一定要交给defWindowProc处理
return DefWindowProc(hWnd, message, wParam,lParam);
break;
}
}
break;
}
case WM_CTLCOLORSTATIC:
{
hdcStatic= (HDC)wParam;
SetTextColor(hdcStatic, RGB(0x41, 0x96, 0x4f));//控件文本的字体颜色
//SetBkColor(hdcStatic, RGB(0X41, 0X96,0X4F));//控件字体的背景颜色,注意跟以上区分
SetBkMode(hdcStatic, TRANSPARENT);//透明背景
return(INT_PTR)GetStockObject(NULL_BRUSH); //这里返回无颜色画刷
}
case WM_PAINT:
{
hdc =BeginPaint(hWnd, &ps);
//Ellipse(hdc, 0, 100, 200, 200);
EndPaint(hWnd, &ps);
break;
}
case WM_DESTROY:
{
DeleteObject(hFont); //删除创建的字体
DeleteObject(hBrush);
PostQuitMessage(0);
break;
}
default:
{
returnDefWindowProc(hWnd, message, wParam, lParam);
}
}
return 0;
}