win32 编辑窗口与按钮完整使用 --------------- 学习记录
2016-03-20 16:00
525 查看
#define IDC_MAIN_BUTTON 101
#define IDC_MAIN_EDIT 102 // Edit box identifier
HWND hEdit;
LRESULT CALLBACK WinProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
int APIENTRY _tWinMain( _In_ HINSTANCE hInstance,
_In_opt_ HINSTANCE hPrevInstance,
_In_ LPTSTR lpCmdLine,
_In_ int nCmdShow)
{
/*
cbClsExtra Additional parameters
cbSize Specifies the size of the window class
cbWndExtra Additional parameters
hbrBackground Sets background color for the window
hCursor The cursor that will appear in the window
hIcon Icon for the window
hIconSm Small icon for the window
hInstance Handle to the application instance
lpfnWndProc The callback procedure (more on that later)
lpszClassName The unique name of the window class
lpszMenuName Used for menus
style The look and feel of the window
*/
WNDCLASSEX wClass;
ZeroMemory(&wClass, sizeof (WNDCLASSEX)); //结构体清零,不让结构的成员数值具有不确定性。
wClass.cbClsExtra = NULL;
wClass.cbSize = sizeof(WNDCLASSEX );
wClass.cbWndExtra = NULL;
wClass.hbrBackground = ( HBRUSH)COLOR_WINDOW ;
wClass.hCursor = LoadCursor(NULL , IDC_ARROW);
wClass.hIcon = NULL;
wClass.hIconSm = NULL;
wClass.hInstance = hInstance;
wClass.lpfnWndProc = ( WNDPROC)WinProc;
wClass.lpszClassName = _T("Window Class" );
wClass.lpszMenuName = NULL;
wClass.style = CS_HREDRAW | CS_VREDRAW ;
if (!RegisterClassEx (&wClass))
{
int nResult = GetLastError();//返回错误编码
MessageBox(NULL ,
_T("Window class creation failed" ),
_T("Window Class Failed" ),
MB_ICONERROR);
}
HWND hWnd = CreateWindowEx (NULL,
_T("Window Class" ),
_T("Windows application" ),
WS_OVERLAPPEDWINDOW,
200,
200,
640,
480,
NULL,
NULL,
hInstance,
NULL);
ShowWindow(hWnd, nCmdShow);
MSG msg;
ZeroMemory(&msg, sizeof (MSG));
while (GetMessage (&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
LRESULT CALLBACK WinProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg )
{
case WM_CREATE :{
hEdit = CreateWindowEx (
WS_EX_CLIENTEDGE,
_T("EDIT" ),
_T("" ),
WS_CHILD | WS_VISIBLE | ES_MULTILINE | ES_AUTOHSCROLL | ES_AUTOVSCROLL,
50,100,200,100,
hWnd,
(HMENU)IDC_MAIN_EDIT ,
GetModuleHandle(NULL ),
NULL
);
HGDIOBJ hfDefault = GetStockObject(DEFAULT_GUI_FONT );//该函数检索预定义的备用笔、刷子、字体或者调色板的句柄。
SendMessage(hEdit,
WM_SETFONT,
(WPARAM)hfDefault,
MAKELPARAM(FALSE , 0));
SendMessage(hEdit,
WM_SETTEXT,
NULL,
(LPARAM)_T ("Insert text here..."));
HWND hWndButton = CreateWindowEx (NULL, _T("BUTTON" ), _T("OK"), WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON,
50, 220, 100, 24, hWnd , (HMENU)IDC_MAIN_BUTTON ,
GetModuleHandle(NULL ),
NULL);
//makelparam和makewparam都是一样的 将两个word型合并成一个dword型。一个在高16位,一个在低16位
SendMessage(hWndButton, WM_SETFONT , (WPARAM)hfDefault, MAKELPARAM (FALSE, 0));
} break;
case WM_COMMAND : //窗口菜单 或子窗口控件产生的消息
/*
窗口菜单:
LOWORD(wParam): 菜单id
HIWORD(wParam): 0
lParam: 0
如果这个消息是由子窗口控件产生,如button产生则:
LOWORD(wParam): 控件ID
HIWORD(wParam): 通知码
lParam: 子窗口句柄。
如果这个消息是由子窗口或者快捷键产生则通知码为1,由菜单产生通知码为0。
通过参数,可以区分这个消息的来源是来自于控件,快捷键还是菜单。
*/
switch (LOWORD (wParam))
{
case IDC_MAIN_BUTTON :
{
char buffer[256];
SendMessage(hEdit,
WM_GETTEXT,
sizeof(buffer) / sizeof (buffer[0]),
reinterpret_cast<LPARAM >(buffer));
/*
reinterpret_cast C++里的强制类型转换符。
reinterpret_cast<type-id> (expression)
type-id 必须是一个指针、引用、算术类型、函数指针或者成员指针。
它可以把一个指针转换成一个整数,也可以把一个整数转换成一个指针
(先把一个指针转换成一个整数,再把该整数转换成原类型的指针,还可以得到原先的指针值)。
*/
MessageBox(NULL ,
( LPCWSTR)buffer,
_T("Information" ),
MB_ICONINFORMATION);
}
break;
}
break;
case WM_DESTROY :
{
PostQuitMessage(0);
return 0;
}
break;
}
return DefWindowProc (hWnd, msg, wParam, lParam);
}
#define IDC_MAIN_EDIT 102 // Edit box identifier
HWND hEdit;
LRESULT CALLBACK WinProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
int APIENTRY _tWinMain( _In_ HINSTANCE hInstance,
_In_opt_ HINSTANCE hPrevInstance,
_In_ LPTSTR lpCmdLine,
_In_ int nCmdShow)
{
/*
cbClsExtra Additional parameters
cbSize Specifies the size of the window class
cbWndExtra Additional parameters
hbrBackground Sets background color for the window
hCursor The cursor that will appear in the window
hIcon Icon for the window
hIconSm Small icon for the window
hInstance Handle to the application instance
lpfnWndProc The callback procedure (more on that later)
lpszClassName The unique name of the window class
lpszMenuName Used for menus
style The look and feel of the window
*/
WNDCLASSEX wClass;
ZeroMemory(&wClass, sizeof (WNDCLASSEX)); //结构体清零,不让结构的成员数值具有不确定性。
wClass.cbClsExtra = NULL;
wClass.cbSize = sizeof(WNDCLASSEX );
wClass.cbWndExtra = NULL;
wClass.hbrBackground = ( HBRUSH)COLOR_WINDOW ;
wClass.hCursor = LoadCursor(NULL , IDC_ARROW);
wClass.hIcon = NULL;
wClass.hIconSm = NULL;
wClass.hInstance = hInstance;
wClass.lpfnWndProc = ( WNDPROC)WinProc;
wClass.lpszClassName = _T("Window Class" );
wClass.lpszMenuName = NULL;
wClass.style = CS_HREDRAW | CS_VREDRAW ;
if (!RegisterClassEx (&wClass))
{
int nResult = GetLastError();//返回错误编码
MessageBox(NULL ,
_T("Window class creation failed" ),
_T("Window Class Failed" ),
MB_ICONERROR);
}
HWND hWnd = CreateWindowEx (NULL,
_T("Window Class" ),
_T("Windows application" ),
WS_OVERLAPPEDWINDOW,
200,
200,
640,
480,
NULL,
NULL,
hInstance,
NULL);
ShowWindow(hWnd, nCmdShow);
MSG msg;
ZeroMemory(&msg, sizeof (MSG));
while (GetMessage (&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
LRESULT CALLBACK WinProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg )
{
case WM_CREATE :{
hEdit = CreateWindowEx (
WS_EX_CLIENTEDGE,
_T("EDIT" ),
_T("" ),
WS_CHILD | WS_VISIBLE | ES_MULTILINE | ES_AUTOHSCROLL | ES_AUTOVSCROLL,
50,100,200,100,
hWnd,
(HMENU)IDC_MAIN_EDIT ,
GetModuleHandle(NULL ),
NULL
);
HGDIOBJ hfDefault = GetStockObject(DEFAULT_GUI_FONT );//该函数检索预定义的备用笔、刷子、字体或者调色板的句柄。
SendMessage(hEdit,
WM_SETFONT,
(WPARAM)hfDefault,
MAKELPARAM(FALSE , 0));
SendMessage(hEdit,
WM_SETTEXT,
NULL,
(LPARAM)_T ("Insert text here..."));
HWND hWndButton = CreateWindowEx (NULL, _T("BUTTON" ), _T("OK"), WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON,
50, 220, 100, 24, hWnd , (HMENU)IDC_MAIN_BUTTON ,
GetModuleHandle(NULL ),
NULL);
//makelparam和makewparam都是一样的 将两个word型合并成一个dword型。一个在高16位,一个在低16位
SendMessage(hWndButton, WM_SETFONT , (WPARAM)hfDefault, MAKELPARAM (FALSE, 0));
} break;
case WM_COMMAND : //窗口菜单 或子窗口控件产生的消息
/*
窗口菜单:
LOWORD(wParam): 菜单id
HIWORD(wParam): 0
lParam: 0
如果这个消息是由子窗口控件产生,如button产生则:
LOWORD(wParam): 控件ID
HIWORD(wParam): 通知码
lParam: 子窗口句柄。
如果这个消息是由子窗口或者快捷键产生则通知码为1,由菜单产生通知码为0。
通过参数,可以区分这个消息的来源是来自于控件,快捷键还是菜单。
*/
switch (LOWORD (wParam))
{
case IDC_MAIN_BUTTON :
{
char buffer[256];
SendMessage(hEdit,
WM_GETTEXT,
sizeof(buffer) / sizeof (buffer[0]),
reinterpret_cast<LPARAM >(buffer));
/*
reinterpret_cast C++里的强制类型转换符。
reinterpret_cast<type-id> (expression)
type-id 必须是一个指针、引用、算术类型、函数指针或者成员指针。
它可以把一个指针转换成一个整数,也可以把一个整数转换成一个指针
(先把一个指针转换成一个整数,再把该整数转换成原类型的指针,还可以得到原先的指针值)。
*/
MessageBox(NULL ,
( LPCWSTR)buffer,
_T("Information" ),
MB_ICONINFORMATION);
}
break;
}
break;
case WM_DESTROY :
{
PostQuitMessage(0);
return 0;
}
break;
}
return DefWindowProc (hWnd, msg, wParam, lParam);
}
相关文章推荐
- HDOJ 2028 Lowest Common Multiple Plus
- Python爬虫之MySql5.7.11数据库安装教程及其路径选择
- 排序算法:归并排序
- 【边喝caffee边Caffe 】(二)python版本的报错:Mean shape incompatible with input shape
- 第一篇——软件的可行性分析
- 排序算法:归并排序
- 猎豹MFC--滚动条CSrollBar
- 杭电计算机研究生复试2015年第二题
- 多线程安全CAS实现的无锁
- poj 2352 树状数组
- hdu2044 一只小蜜蜂.
- 猎豹MFC--下拉框ComboBox 列表框ListBox
- hibernate3与hibernate4区别
- openssl 线程安全 Windows
- 堆和栈区别
- Java学习之InputStream中read()与read(byte[] b)
- Hibernate学习——(十)继承映射
- 记住,常量不能被修改。 尤其是字符串常量。
- 猎豹MFC--对话框 复选框 单选按钮
- JS异步加载的三种方式