走进windows编程的世界-----对话框、文本框、按钮

1 对话框的分类
  2 对话框的基本使用方式
  3 对话框资源
  4 有模式对话框的使用

      

int DialogBox(
 HINSTANCE hInstance,
 LPCTSTR lpTemplate,
 HWND hWndParent,
 DLGPROC lpDialogFunc
);


  5 无模式对话框的使用
    5.1 添加对话框资源
    5.2 定义窗口处理函数

复制代码
 BOOL CALLBACK DialogProc(
  HWND hwndDlg,  // handle to dialog box
    UINT uMsg,     // message
    WPARAM wParam, // first message parameter
    LPARAM lParam     ); // second message parameter
复制代码

    5.3 创建对话框

复制代码
 HWND CreateDialog(
  HINSTANCE hInstance,//应用程序实例句柄
  LPCTSTR lpTemplate,//对话框的资源ID
  HWND hWndParent,//父窗口
  DLGPROC lpDialogFunc );//对话框的窗口处理函数
复制代码

     返回值是创建好的对话框的窗口句柄。
    5.4 显示对话框
       ShowWindow
    5.5 关闭对话框
       EndDialog
    5.6 与有模式对话框对比
       创建函数: 有模式对话框 DialogBox
                 无模式对话框 CreateDialog
       程序执行方式: 
        有模式对话框会阻塞,禁止其他窗口的输入
        无模式对话框会让主程序,不影响其它窗口的输入

  6 对话框中的消息        
  
    WM_INITDIALOG消息,用途类似于WM_CREATE消息
    可以在对话框创建完成后,进行数据初始化.
    它与WM_CREATE不同之处是,WM_INITDIALOG消息
    会在对话框资源模板上的所有子控件(按钮等)
    创建完成后,再发送给对话框窗口. 
      wParam - 当前接受输入焦点子控件窗口句柄
      lParam - 创建时附带的参数

  7 对话框和普通窗口的对比
    7.1 创建方式 
      普通窗口: CreateWindow/Ex
      对话框: DialogBox/CreateDialog
    7.2 消息
      普通窗口: 会收到WM_CREATE
      对话框:   WM_INITDIALOG
    7.3 关闭方式:
      普通窗口: DestoryWindow等
      对话框: EndDialog

// windialog.cpp : 定义应用程序的入口点。
//

#include "stdafx.h"
#include "windialog.h"

#define MAX_LOADSTRING 100

// 全局变量:
HINSTANCE hInst;								// 当前实例
TCHAR szTitle[MAX_LOADSTRING];					// 标题栏文本
TCHAR szWindowClass[MAX_LOADSTRING];			// 主窗口类名

// 此代码模块中包含的函数的前向声明:
ATOM				MyRegisterClass(HINSTANCE hInstance);
BOOL				InitInstance(HINSTANCE, int);
LRESULT CALLBACK	WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK	About(HWND, UINT, WPARAM, LPARAM);

int APIENTRY _tWinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPTSTR    lpCmdLine,
                     int       nCmdShow)
{
	UNREFERENCED_PARAMETER(hPrevInstance);
	UNREFERENCED_PARAMETER(lpCmdLine);

 	// TODO: 在此放置代码。
	MSG msg;
	HACCEL hAccelTable;

	// 初始化全局字符串
	LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
	LoadString(hInstance, IDC_WINDIALOG, szWindowClass, MAX_LOADSTRING);
	MyRegisterClass(hInstance);

	// 执行应用程序初始化:
	if (!InitInstance (hInstance, nCmdShow))
	{
		return FALSE;
	}

	hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_WINDIALOG));

	// 主消息循环:
	while (GetMessage(&msg, NULL, 0, 0))
	{
		if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
		{
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
	}

	return (int) msg.wParam;
}



//
//  函数: MyRegisterClass()
//
//  目的: 注册窗口类。
//
//  注释:
//
//    仅当希望
//    此代码与添加到 Windows 95 中的“RegisterClassEx”
//    函数之前的 Win32 系统兼容时,才需要此函数及其用法。调用此函数十分重要,
//    这样应用程序就可以获得关联的
//    “格式正确的”小图标。
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
	WNDCLASSEX wcex;

	wcex.cbSize = sizeof(WNDCLASSEX);

	wcex.style			= CS_HREDRAW | CS_VREDRAW;
	wcex.lpfnWndProc	= WndProc;
	wcex.cbClsExtra		= 0;
	wcex.cbWndExtra		= 0;
	wcex.hInstance		= hInstance;
	wcex.hIcon			= LoadIcon(hInstance, MAKEINTRESOURCE(IDI_WINDIALOG));
	wcex.hCursor		= LoadCursor(NULL, IDC_ARROW);
	wcex.hbrBackground	= (HBRUSH)(COLOR_WINDOW+1);
	wcex.lpszMenuName	= MAKEINTRESOURCE(IDC_WINDIALOG);
	wcex.lpszClassName	= szWindowClass;
	wcex.hIconSm		= LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));

	return RegisterClassEx(&wcex);
}

//
//   函数: InitInstance(HINSTANCE, int)
//
//   目的: 保存实例句柄并创建主窗口
//
//   注释:
//
//        在此函数中,我们在全局变量中保存实例句柄并
//        创建和显示主程序窗口。
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
   HWND hWnd;

   hInst = hInstance; // 将实例句柄存储在全局变量中

   hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
      CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);

   if (!hWnd)
   {
      return FALSE;
   }

   ShowWindow(hWnd, nCmdShow);
   UpdateWindow(hWnd);

   return TRUE;
}

BOOL CALLBACK SettingDlgProc_1(HWND hwndDlg, UINT message, WPARAM wParam, LPARAM lParam)
{

    switch(message){
	case WM_CREATE: //不会发生
        MessageBox( NULL, "WM_CREATE",
            "WinDlg", MB_OK );
        break;

	case WM_INITDIALOG:
			/*类似于 WM_CREATE 消息,可以在对话框常见完成后,进行数据初始化
			*不同的是:WM_INITDIALOG 会在对话框资源莫班上所有的空间创建完成后发送给对话框窗口
			* wParam :当前接受输入焦点子控件窗口句柄
			* lParam :创建时附带的参数
			*/
        MessageBox( NULL, "WM_INITDIALOG","WinDlg", MB_OK );
        break;

	case WM_SYSCOMMAND:
		switch(wParam){
		case SC_CLOSE:
			EndDialog(hwndDlg,1);
			break;
		default:
			//EndDialog(hwndDlg,0);
			break;
		}
		break;
	default:
		break;
	}

	//默认对话框处理函数
	//return DefDlgProc(hwndDlg, message, wParam, lParam);
	return 0;
}
//
//  函数: 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;
	HWND hdlg;

	switch (message)
	{
	case WM_CREATE: //会发生
        MessageBox( NULL, "WM_CREATE",
            "WinDlg", MB_OK );
        break;

	case WM_COMMAND:
		wmId    = LOWORD(wParam);
		wmEvent = HIWORD(wParam);
		int ret;
		// 分析菜单选择:
		switch (wmId)
		{
		case ID_32771:
			//有模式对话框会阻塞,禁止其他窗口的输入 
			ret = DialogBox(hInst,MAKEINTRESOURCE(ID_SETTING),hWnd,SettingDlgProc_1);

			if(ret != 2){
				MessageBox(NULL,"取消配置","windialog",MB_OK);
			}else{
				MessageBox(NULL,"确定配置","windialog",MB_OK);
			}


			break;
		case ID_32772:
			//无模式对话框会让主程序,不影响其它窗口的输入
			hdlg = CreateDialog(hInst,MAKEINTRESOURCE(ID_SETTING),hWnd,SettingDlgProc_1);

			//显示对话框
			ShowWindow(hdlg,SW_SHOW);
			break;
		case IDM_ABOUT:
			DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
			break;
		case IDM_EXIT:
			DestroyWindow(hWnd);
			break;
		default:
			return DefWindowProc(hWnd, message, wParam, lParam);
		}
		break;
	case WM_PAINT:
		hdc = BeginPaint(hWnd, &ps);
		// TODO: 在此添加任意绘图代码...
		EndPaint(hWnd, &ps);
		break;
	case WM_DESTROY:
		PostQuitMessage(0);
		break;
	default:
		return DefWindowProc(hWnd, message, wParam, lParam);
	}
	return 0;
}

// “关于”框的消息处理程序。
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
	UNREFERENCED_PARAMETER(lParam);
	switch (message)
	{
	case WM_INITDIALOG:
		return (INT_PTR)TRUE;

	case WM_COMMAND:
		if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
		{
			EndDialog(hDlg, LOWORD(wParam));
			return (INT_PTR)TRUE;
		}
		break;
	}
	return (INT_PTR)FALSE;
}

1 静态框创建
    窗口类的名称 - STATIC, 用于窗口上
    显示文字等, 如果使用SS_ICON/SS_BITMAP
    风格可以显示ICON和BITMAP
    
    文本静态框 - 创建时使用SS_SIMPLE即可
    ICON静态框 - 创建时需要使用SS_ICON, 并且将静态框的窗口名称设置成对应ICON的资源ID号,例如"#101".
    
  2 静态框的命令相应
    2.1 静态框要添加SS_NOTIFY风格
    2.2 WM_COMMAND进行处理
       WPARAM - HIWORD 命令ID
                LOWORD 静态框的窗口ID
       LPARAM - 静态框的窗口句柄.  
    
  3 静态框的消息
    调用SendMessage函数,向Static控件发送消息
    例如: STM_SETICON消息修改控件显示图标
      SendMessage( hStatic, STM_SETICON,
    (WPARAM)hIcon, 0 );
  其他:

   GetDlgItem 从窗口中获取指定ID的子控件的窗口句柄

// WinStatic.cpp : 定义应用程序的入口点。
//

#include "stdafx.h"
#include "WinStatic.h"

#define MAX_LOADSTRING 100

// 全局变量:
HINSTANCE hInst;								// 当前实例
TCHAR szTitle[MAX_LOADSTRING];					// 标题栏文本
TCHAR szWindowClass[MAX_LOADSTRING];			// 主窗口类名

// 此代码模块中包含的函数的前向声明:
ATOM				MyRegisterClass(HINSTANCE hInstance);
BOOL				InitInstance(HINSTANCE, int);
LRESULT CALLBACK	WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK	About(HWND, UINT, WPARAM, LPARAM);

int APIENTRY _tWinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPTSTR    lpCmdLine,
                     int       nCmdShow)
{
	UNREFERENCED_PARAMETER(hPrevInstance);
	UNREFERENCED_PARAMETER(lpCmdLine);

 	// TODO: 在此放置代码。
	MSG msg;
	HACCEL hAccelTable;

	// 初始化全局字符串
	LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
	LoadString(hInstance, IDC_WINSTATIC, szWindowClass, MAX_LOADSTRING);
	MyRegisterClass(hInstance);

	// 执行应用程序初始化:
	if (!InitInstance (hInstance, nCmdShow))
	{
		return FALSE;
	}

	hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_WINSTATIC));

	// 主消息循环:
	while (GetMessage(&msg, NULL, 0, 0))
	{
		if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
		{
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
	}

	return (int) msg.wParam;
}



//
//  函数: MyRegisterClass()
//
//  目的: 注册窗口类。
//
//  注释:
//
//    仅当希望
//    此代码与添加到 Windows 95 中的“RegisterClassEx”
//    函数之前的 Win32 系统兼容时,才需要此函数及其用法。调用此函数十分重要,
//    这样应用程序就可以获得关联的
//    “格式正确的”小图标。
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
	WNDCLASSEX wcex;

	wcex.cbSize = sizeof(WNDCLASSEX);

	wcex.style			= CS_HREDRAW | CS_VREDRAW;
	wcex.lpfnWndProc	= WndProc;
	wcex.cbClsExtra		= 0;
	wcex.cbWndExtra		= 0;
	wcex.hInstance		= hInstance;
	wcex.hIcon			= LoadIcon(hInstance, MAKEINTRESOURCE(IDI_WINSTATIC));
	wcex.hCursor		= LoadCursor(NULL, IDC_ARROW);
	wcex.hbrBackground	= (HBRUSH)(COLOR_WINDOW+1);
	wcex.lpszMenuName	= MAKEINTRESOURCE(IDC_WINSTATIC);
	wcex.lpszClassName	= szWindowClass;
	wcex.hIconSm		= LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));

	return RegisterClassEx(&wcex);
}

//
//   函数: InitInstance(HINSTANCE, int)
//
//   目的: 保存实例句柄并创建主窗口
//
//   注释:
//
//        在此函数中,我们在全局变量中保存实例句柄并
//        创建和显示主程序窗口。
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
   HWND hWnd;

   hInst = hInstance; // 将实例句柄存储在全局变量中

   hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
      CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);

   if (!hWnd)
   {
      return FALSE;
   }

   ShowWindow(hWnd, nCmdShow);
   UpdateWindow(hWnd);

   return TRUE;
}

void OnCreate( HWND hWnd, UINT nMsg,
    WPARAM wParam, LPARAM lParam )
{
    CreateWindow( "STATIC", "hello Static",
        WS_CHILD|WS_VISIBLE|SS_SUNKEN|SS_SIMPLE|SS_NOTIFY,
        50, 50, 100, 100, hWnd, (HMENU)1001,
        hInst, NULL );

    CreateWindow( "STATIC", "#IDI_ICON2",     //已经创建了一个icon图标,ID为101
        WS_CHILD|WS_VISIBLE|SS_SUNKEN|SS_ICON|SS_NOTIFY,
        200, 50, 100, 100, hWnd, (HMENU)1002,
        hInst, NULL );
}

void OnCommand( HWND hWnd, UINT nMsg,
    WPARAM wParam, LPARAM lParam )
{
    int nCmdID  = HIWORD( wParam );
    int nCtrlID = LOWORD( wParam );
    switch( nCtrlID ){
	case ID_32771:
			{
				HICON hIcon = LoadIcon(hInst,MAKEINTRESOURCE(IDI_ICON1));
				//通过ID获取多窗口的句柄
				HWND  hStatic = GetDlgItem(hWnd,1002);
				SendMessage(hStatic,STM_SETICON,(WPARAM)hIcon,0);
			}
    case 1001:
		{
            switch( nCmdID )
            {
            case STN_CLICKED:
                MessageBox( NULL, "Hello Static 1001",
                    "WinStatic", MB_OK );
                break;
            }
        }
        break;
    case 1002:
        {
            switch( nCmdID )
            {
            case STN_CLICKED:
                MessageBox( NULL, "Hello Static 1002",
                    "WinStatic", MB_OK );
                break;
            }
        }
        break;
	case IDM_ABOUT:
			DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
			break;
	case IDM_EXIT:
			DestroyWindow(hWnd);
			break;
	default:
		 DefWindowProc(hWnd, nMsg, wParam, 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_CREATE:
        OnCreate( hWnd, message, wParam, lParam );
        break;
    case WM_COMMAND:
        OnCommand( hWnd, message, wParam, lParam );
        break;
	case WM_PAINT:
		hdc = BeginPaint(hWnd, &ps);
		// TODO: 在此添加任意绘图代码...
		EndPaint(hWnd, &ps);
		break;
	case WM_DESTROY:
		PostQuitMessage(0);
		break;
	default:
		return DefWindowProc(hWnd, message, wParam, lParam);
	}
	return 0;
}

// “关于”框的消息处理程序。
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
	UNREFERENCED_PARAMETER(lParam);
	switch (message)
	{
	case WM_INITDIALOG:
		return (INT_PTR)TRUE;

	case WM_COMMAND:
		if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
		{
			EndDialog(hDlg, LOWORD(wParam));
			return (INT_PTR)TRUE;
		}
		break;
	}
	return (INT_PTR)FALSE;
}

 按钮

   1 按钮的分类
     根据创建按钮时使用的风格:
     1.1 下压式按钮 PUSH BUTTON 
        正常下压式按钮 BS_PUSHBUTTON 
        三态下压按钮 BS_3STATE/BS_AUTO3STATE
     1.2 分组按钮 Group Button
        按钮样式:BS_GROUPBOX
     1.3 多选按钮   Check Box 
        按钮样式:BS_CHECKBOX/BS_AUTOCHECKBOX
     1.4 单选按钮   Radio Box
        按钮样式:BS_RADIOBUTTON/
                 BS_AUTORADIOBUTTON
                 
   2 下压式按钮的使用
     2.1 创建按钮
        创建时使用BUTTON
     2.2 命令消息
        BN_CLICKED消息: 当点击按钮通过WM_COMMAND消息通知父窗口
     2.3 通知消息 
        SendMessage
        
   3 分组按钮
     3.1 创建分组按钮
     3.2 常用于对窗口内控件划分区域,增加界面的友好性
        
   4 多选按钮 CheckBox
     4.1 创建使用 BS_CHECKBOX/BS_AUTOCHECKBOX
       BS_AUTOCHECKBOX 选择状态系统自动维护
       BS_CHECKBOX 选择状态需要自己维护
     4.2 命令响应
       BN_CLICKED 通过WM_COMMAND消息
     4.3 通知消息
       BM_GETCHECK 获取当前按钮被选择的状态
       BM_SETCHECK 设置当前按钮被选择的状态
          wParam 用于设置状态标示
       状态: BST_CHECKED 或 BST_UNCHECKED
       
   5 单选按钮 RadioBox
     5.1 创建使用 BS_RADIOBUTTON/BS_AUTORADIOBUTTON
       BS_AUTORADIOBUTTON 选择状态系统自动维护
       BS_RADIOBUTTON 选择状态需要自己维护
       注意:
       RadionBox分组 - 使用WS_GROUP样式,可以将
       RadionBox分成多个组.
          A CreateWindow (  ,,,WS_GROUP );
          B CreateWindow...
          C CreateWindow (  ,,,WS_GROUP );
          D CreaetWindow...
          A和B是一组, C和D是一组
     5.2 命令响应
       BN_CLICKED 通过WM_COMMAND消息
     5.3 通知消息
       与CheckBox类似

// WinButton.cpp : Defines the entry point for the application.
//

#include <Windows.h>
#include <stdio.h>

HINSTANCE g_hInst = NULL;

void OnCreate( HWND hWnd, UINT nMsg,
    WPARAM wParam, LPARAM lParam )
{    //PUSH BUTTON
    CreateWindow( "BUTTON", "PushButton",
        WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON, 
        100, 50, 100, 30, 
        hWnd, (HMENU)1001, g_hInst, NULL );
    //三态按钮
    CreateWindow( "BUTTON", "Auto3State",
        WS_CHILD|WS_VISIBLE|BS_AUTO3STATE|BS_PUSHLIKE, 
        100, 100, 100, 30, 
        hWnd, (HMENU)2001, g_hInst, NULL );
    //分组按钮
    CreateWindow( "BUTTON", "分组一",
        WS_CHILD|WS_VISIBLE|BS_GROUPBOX, 
        50, 30, 250, 400,
        hWnd, (HMENU)3001, g_hInst, NULL );
    //多选按钮 CheckBox
    CreateWindow( "BUTTON", "选项一",
        WS_CHILD|WS_VISIBLE|BS_AUTOCHECKBOX, 
        100, 150, 100, 30,
        hWnd, (HMENU)4001, g_hInst, NULL );
    CreateWindow( "BUTTON", "选项二",
        WS_CHILD|WS_VISIBLE|BS_CHECKBOX, 
        100, 180, 100, 30,
        hWnd, (HMENU)4002, g_hInst, NULL );
    // 单选按钮
    CreateWindow( "BUTTON", "A", 
        WS_CHILD|WS_VISIBLE|BS_AUTORADIOBUTTON|WS_GROUP,
        100, 240, 100, 30, hWnd, 
        (HMENU)5001, g_hInst, NULL );
    CreateWindow( "BUTTON", "B", 
        WS_CHILD|WS_VISIBLE|BS_AUTORADIOBUTTON,
        100, 270, 100, 30, hWnd, 
        (HMENU)5002, g_hInst, NULL );
    CreateWindow( "BUTTON", "C", 
        WS_CHILD|WS_VISIBLE|BS_AUTORADIOBUTTON|WS_GROUP,  //遇到WS_GROUP,表示新的一组单选按钮
        100, 300, 100, 30, hWnd, 
        (HMENU)5003, g_hInst, NULL );
    CreateWindow( "BUTTON", "D", 
        WS_CHILD|WS_VISIBLE|BS_AUTORADIOBUTTON,
        100, 330, 100, 30, hWnd, 
        (HMENU)5004, g_hInst, NULL );

	//创建记事本
	CreateWindow( "EDIT", "Hello EDIT",
        WS_CHILD|WS_VISIBLE/*|WS_HSCROLL|
        WS_VSCROLL|ES_AUTOHSCROLL|
		ES_AUTOVSCROLL|ES_MULTILINE 多行*/|ES_PASSWORD, 
        0, 0, 100, 50, hWnd,
        (HMENU)1001, g_hInst, NULL );
}

void OnCommand( HWND hWnd, UINT nMsg,
    WPARAM wParam, LPARAM lParam )
{    
    int nNotifyCode = HIWORD( wParam );
    int nEventID    = LOWORD( wParam );
    switch( nEventID )
    {
    case 1001:
        {
            switch( nNotifyCode )
            {
            case BN_CLICKED:
                MessageBox( NULL, "Hello PushButton",
                    "WinButton", MB_OK );
                break;
            }
        }
        break;

     case 2001:
        {
            switch( nNotifyCode )
            {
            case BN_CLICKED:
                MessageBox( NULL, "Hello Auto3State",
                    "WinButton", MB_OK );
                break;
            }
        }
        break;
    case 4002:
        {
            HWND hCtrl = GetDlgItem( hWnd, 4002 );
            LRESULT nCheck = 0;
            switch( nNotifyCode )
            {
            case BN_CLICKED:
                //获取当前CheckBox的状态
                nCheck = SendMessage( hCtrl, 
                    BM_GETCHECK, 0, 0 );
                //判断当前状态是否被选择
                if( nCheck & BST_CHECKED )
                {    //设置为不选择状态
                    SendMessage( hCtrl,BM_SETCHECK,
                        BST_UNCHECKED, 0 );
                }
                else
                {    //设置成选择状态
                    SendMessage( hCtrl,BM_SETCHECK,
                        BST_CHECKED, 0 );
                }
                break;
            }
        }
        break;
    }
}

LRESULT CALLBACK WndProc( HWND   hWnd, 
                          UINT   nMsg,
                          WPARAM wParam,
                          LPARAM lParam )
{
    switch( nMsg )
    {
    case WM_CREATE:
        OnCreate( hWnd, nMsg, wParam, lParam );
        break;
    case WM_COMMAND:
        OnCommand( hWnd, nMsg, wParam, lParam );
        break;
    case WM_DESTROY:
        PostQuitMessage( 0 );
        return 0;
    }
    return DefWindowProc( hWnd, nMsg,
        wParam, lParam );
}

BOOL RegisterWnd( LPSTR pszClassName )
{
    WNDCLASSEX wce = { 0 };
    wce.cbSize        = sizeof( wce );
    wce.cbClsExtra    = 0;
    wce.cbWndExtra    = 0;
    wce.hbrBackground = HBRUSH(COLOR_WINDOW);
    wce.hCursor       = NULL;
    wce.hIcon         = NULL;
    wce.hIconSm       = NULL;
    wce.hInstance     = g_hInst;
    wce.lpfnWndProc   = WndProc;
    wce.lpszClassName = pszClassName;
    wce.lpszMenuName  = NULL;
    wce.style         = CS_HREDRAW|CS_VREDRAW;

    ATOM nAtom = RegisterClassEx( &wce );
    if( 0 ==  nAtom )
    {
        return FALSE;
    }

    return TRUE;
}

HWND CreateWnd( LPSTR pszClassName )
{
    HWND hWnd = CreateWindowEx( 0,
        pszClassName, "MyWnd", 
        WS_OVERLAPPEDWINDOW, CW_USEDEFAULT,
        CW_USEDEFAULT, CW_USEDEFAULT,
        CW_USEDEFAULT, NULL, NULL, g_hInst,
        NULL );
    return hWnd;
}

void DisplayWnd( HWND hWnd )
{
    ShowWindow( hWnd, SW_SHOW );
    UpdateWindow( hWnd );
}

void Message( )
{
    MSG msg = { 0 };
    while( GetMessage( &msg, NULL, 0, 0 ) )
    {
        TranslateMessage( &msg );
        DispatchMessage( &msg );
    }
}

int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
{
    g_hInst = hInstance;
    RegisterWnd( "MYWND" );
    HWND hWnd = CreateWnd( "MYWND" );
    DisplayWnd( hWnd );
    Message( );
    return 0;
}

 1 文本编辑框的样式
    1.1 单行文本编辑框 - 默认样式
    1.2 多行文本编辑框 - ES_MULTILINE
    1.3 密码文本编辑框 - ES_PASSWORD
  2 文本编辑框的使用
    2.1 创建 
       窗口类名称EDIT.
    2.2 Notify消息
       EN_CHANGE 当编辑框内容发生变化时,会通知父窗口
    2.3 EDIT窗口消息
       EM_SETSEL 选择消息 WPARAM是起始字符位置
           LPARAM是终止字符位置
       窗口消息: WM_COPY, WM_PASET,WM_CUT等消息
         WM_SETTEXT, WM_GETTEXT, WM_GETTEXTLEN
         SetWindowText

         GetWindowText/GetWindowTextLen


/* File : WinEdit.cpp
 * Auth : sjin
 * DATE : 20140720
 * MAil : 
 */

#include <Windows.h>
#include <stdlib.h>
#include <stdio.h>
#include "resource.h"


HINSTANCE g_hInst = NULL;
HWND      g_hEdit = NULL;

void OnCreate( HWND hWnd, UINT nMsg, 
    WPARAM wParam, LPARAM lParam )
{
    g_hEdit = CreateWindow( "EDIT", "Hello EDIT",
        WS_CHILD|WS_VISIBLE|WS_HSCROLL|
        WS_VSCROLL|ES_AUTOHSCROLL|
        ES_AUTOVSCROLL|ES_MULTILINE, 
        0, 0, 100, 200, hWnd,
        (HMENU)1001, g_hInst, NULL );
}

void OnSize( HWND hWnd, UINT nMsg, 
    WPARAM wParam, LPARAM lParam )
{
    int nWidth = LOWORD( lParam );
    int nHeight= HIWORD( lParam );
    if( NULL != g_hEdit )
    {    //将EDIT窗口填满整个客户区
        MoveWindow( g_hEdit, 0, 0, nWidth,
            nHeight, TRUE );
    }
}

void OnSave( )
{
    //获取文字长度
    LRESULT nTextLen = SendMessage( g_hEdit, 
        WM_GETTEXTLENGTH, 0, 0 );
    //获取文字
    CHAR * pszBuff = NULL;
    pszBuff = (CHAR *)malloc( nTextLen + 1 );
    memset( pszBuff, 0, nTextLen + 1 );
    SendMessage( g_hEdit, WM_GETTEXT,
        nTextLen + 1, (LPARAM)pszBuff );
    MessageBox( NULL, pszBuff, "WinEdit", MB_OK );
    //保存文件
    FILE * pFile = fopen( "D:\\1.txt", "w+" );
    fwrite( pszBuff, nTextLen, 1, pFile );
    fclose( pFile );
    //释放内存
    free( pszBuff );
}

void OnOpen( )
{
    //打开文件读取数据
    FILE * pFile = fopen( "D:\\1.txt", "r" );
    //获取文件长度
    fseek( pFile, 0, SEEK_END );
    long nFileLen = ftell( pFile );
    fseek( pFile, 0, SEEK_SET );
    //读取文件数据
    CHAR * pszBuf = (CHAR *)
        malloc( nFileLen + 1 );
    memset( pszBuf, 0, nFileLen + 1 );
    fread( pszBuf, nFileLen, 1, pFile );
    //关闭文件
    fclose( pFile );
    //将字符显示在EDIT窗口
    SendMessage( g_hEdit, WM_SETTEXT,
        0, (LPARAM)pszBuf );
    //释放内存
    free( pszBuf );
}

void  OnCommand( HWND hWnd, UINT nMsg, 
    WPARAM wParam, LPARAM lParam )
{
    int nNotifyCode = HIWORD( wParam );
    int nEventID    = LOWORD( wParam );
    switch( nEventID )
    {
    case 1001:
        {
            switch( nNotifyCode )
            {
            case EN_CHANGE:
                //MessageBox( NULL, "EN_CHANGE",
                //    "WinEdit", MB_OK );
                break;
            }
        }
        break;
    case ID_SELALL:
        SendMessage( g_hEdit, EM_SETSEL, 0, -1 );
        break;
    case ID_COPY://复制
        SendMessage( g_hEdit, WM_COPY, 0, 0 );
        break;
    case ID_PASTE:
        SendMessage( g_hEdit, WM_PASTE, 0, 0 );
        break;
    case ID_SAVE:
        OnSave( );
        break;
    case ID_OPEN:
        OnOpen( );
        break;
    }
}

LRESULT CALLBACK WndProc( HWND   hWnd, 
                          UINT   nMsg,
                          WPARAM wParam,
                          LPARAM lParam )
{
    switch( nMsg )
    {
    case WM_CREATE:
        OnCreate( hWnd, nMsg, wParam, lParam );
        break;
    case WM_SIZE:
        OnSize( hWnd, nMsg, wParam, lParam );
        break;
    case WM_COMMAND:
        OnCommand( hWnd, nMsg, wParam, lParam );
        break;
    case WM_DESTROY:
        PostQuitMessage( 0 );
        return 0;
    }
    return DefWindowProc( hWnd, nMsg,
        wParam, lParam );
}

BOOL RegisterWnd( LPSTR pszClassName )
{
    WNDCLASSEX wce = { 0 };
    wce.cbSize        = sizeof( wce );
    wce.cbClsExtra    = 0;
    wce.cbWndExtra    = 0;
    wce.hbrBackground = HBRUSH(COLOR_WINDOW);
    wce.hCursor       = NULL;
    wce.hIcon         = NULL;
    wce.hIconSm       = NULL;
    wce.hInstance     = g_hInst;
    wce.lpfnWndProc   = WndProc;
    wce.lpszClassName = pszClassName;
    wce.lpszMenuName  = NULL;
    wce.style         = CS_HREDRAW|CS_VREDRAW;

    ATOM nAtom = RegisterClassEx( &wce );
    if( 0 ==  nAtom )
    {
        return FALSE;
    }

    return TRUE;
}

HWND CreateWnd( LPSTR pszClassName )
{
    HMENU hMenu = LoadMenu( g_hInst,
        MAKEINTRESOURCE(IDR_MAIN) );
    HWND hWnd = CreateWindowEx( 
        WS_EX_CLIENTEDGE,
        pszClassName, "我的记事本", 
        WS_OVERLAPPEDWINDOW, CW_USEDEFAULT,
        CW_USEDEFAULT, CW_USEDEFAULT,
        CW_USEDEFAULT, NULL, hMenu, g_hInst,
        NULL );
    return hWnd;
}

void DisplayWnd( HWND hWnd )
{
    ShowWindow( hWnd, SW_SHOW );
    UpdateWindow( hWnd );
}

void Message( )
{
    MSG msg = { 0 };
    while( GetMessage( &msg, NULL, 0, 0 ) )
    {
        TranslateMessage( &msg );
        DispatchMessage( &msg );
    }
}

int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
{
    g_hInst = hInstance;
    RegisterWnd( "MYWND" );
    HWND hWnd = CreateWnd( "MYWND" );
    DisplayWnd( hWnd );
    Message( );
    return 0;
}


参考资料:

其他控件介绍:

http://www.cnblogs.com/tangzhengyue/archive/2012/08/16/2643149.html

你可能感兴趣的:(文本框,按钮,对话框,静态框)