Win32_文本编辑框

一 文本编辑框
  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

文本编辑框实例
  1 // WinDlg.cpp : Defines the entry point for the application.
  2 //
  3 
  4 #include "stdafx.h"
  5 #include "resource.h"
  6 #include "stdlib.h"
  7 #include "stdio.h"
  8 
  9 HINSTANCE g_hInst = NULL;
 10 HWND    g_hEdit = NULL;
 11 
 12 void OnCreate(HWND hWnd,UINT nMsg,
 13               WPARAM wParam,LPARAM lParam)
 14 {
 15     g_hEdit = CreateWindow("EDIT","Hello EDIT",WS_CHILD | WS_VISIBLE |
 16         WS_VSCROLL | WS_HSCROLL | ES_AUTOHSCROLL | ES_MULTILINE,
 17         0,0,100,200,hWnd,(HMENU)1001,g_hInst,NULL);
 18 }
 19 void OnSize(HWND hWnd,UINT nMsg,
 20             WPARAM wParam,LPARAM lParam)
 21 {
 22     int nWidth  = LOWORD(lParam);
 23     int nHeight = HIWORD(lParam);
 24     if (NULL != g_hEdit)
 25     {
 26         //将EDIT 窗口填满整个客户区
 27         MoveWindow(g_hEdit,0,0,nWidth,nHeight,TRUE);
 28     }
 29 }
 30 
 31 void OnSave()
 32 {
 33     //获取文字长度
 34     LRESULT nTextLen = SendMessage(g_hEdit,WM_GETTEXTLENGTH,0,0);
 35     //获取文字
 36     CHAR * pszBuff = NULL;
 37     pszBuff = (CHAR *)malloc(nTextLen + 1);
 38     memset(pszBuff,0,nTextLen + 1);
 39     SendMessage(g_hEdit,WM_GETTEXT,nTextLen + 1,(LPARAM)pszBuff);
 40     //MessageBox(NULL,pszBuff,"WinEdit",MB_OK);
 41     //保存文件
 42     FILE* pFile = fopen("C:\\1.txt","w+");
 43     fwrite(pszBuff,nTextLen,1,pFile);
 44     fclose(pFile);
 45     
 46     //释放内存
 47     free(pszBuff);
 48 }
 49 void OnOpen()
 50 {
 51     //打开文件读取数据
 52     FILE *pFile = fopen("C:\\1.txt","r");
 53     //获取文件长度
 54     fseek(pFile,0,SEEK_END);
 55     long nFilen = ftell(pFile);
 56     fseek(pFile,0,SEEK_SET);
 57     //读取文件数据
 58     CHAR *pszBuf = (CHAR *)malloc(nFilen + 1);
 59     memset(pszBuf,0,nFilen + 1);
 60     fread(pszBuf,nFilen,1,pFile);
 61     //关闭文件
 62     fclose(pFile);
 63     //将字符显示在EDIT窗口
 64     SendMessage(g_hEdit,WM_SETTEXT,0,(LPARAM)pszBuf);
 65     //释放内存
 66     free(pszBuf);
 67 }
 68 void OnCommand(HWND hWnd,UINT nMsg,
 69                WPARAM wParam,LPARAM lParam)
 70 {
 71     int nNotifyCode = HIWORD(wParam);
 72     int nEventID    = LOWORD(wParam);
 73     switch(nEventID)
 74     {
 75     case 1001:
 76         {
 77             switch(nNotifyCode)
 78             {
 79             case EN_CHANGE:
 80                 //MessageBox(NULL,"EN_CHANGE","WinEdit",MB_OK);
 81                 break;
 82             }
 83         }
 84         break;
 85     case ID_SELALL:
 86         SendMessage(g_hEdit,EM_SETSEL,0,-1);
 87         break;
 88     case ID_COPY:
 89         SendMessage(g_hEdit,WM_COPY,0,0);
 90         break;
 91     case ID_PASTE:
 92         SendMessage(g_hEdit,WM_PASTE,0,0);
 93         break;
 94     case ID_SAVE:
 95         OnSave();
 96         break;
 97     case ID_OPEN:
 98         OnOpen();
 99         break;
100     }
101 }
102 
103 LRESULT CALLBACK WndProc(HWND hWnd,
104                          UINT nMsg,
105                          WPARAM wParam,
106                          LPARAM lParam)
107 {
108     switch(nMsg)
109     {
110     case WM_COMMAND:
111             OnCommand(hWnd,nMsg,wParam,lParam);
112             break;
113     case WM_CREATE:
114         OnCreate(hWnd,nMsg,wParam,lParam);
115         break;
116     case WM_SIZE:
117         OnSize(hWnd,nMsg,wParam,lParam);
118         break;
119     case WM_DESTROY:
120         PostQuitMessage(0);
121         return 0;
122     }
123     return DefWindowProc(hWnd,nMsg,wParam,lParam);
124 }
125 
126 BOOL RegisterWnd(LPSTR pszClassName)
127 {
128     WNDCLASSEX wce = {0};
129     wce.style    = CS_HREDRAW | CS_VREDRAW;
130     wce.cbSize    = sizeof(wce);
131     wce.cbClsExtra = 0;
132     wce.cbWndExtra = 0;
133     wce.hbrBackground = HBRUSH(COLOR_WINDOW);
134     wce.hCursor    = NULL;
135     wce.hIcon    = NULL;
136     wce.hIconSm    = NULL;
137     wce.hInstance    = g_hInst;
138     wce.lpfnWndProc = WndProc;
139     wce.lpszClassName = pszClassName;
140     wce.lpszMenuName    = NULL;
141     
142     ATOM nAtom = RegisterClassEx(&wce);
143     if (0 == nAtom)
144     {
145         return FALSE;
146     }
147     return TRUE;
148 }
149 
150 HWND CreateWnd(LPSTR pszClassName)
151 {
152     HMENU hMenu = LoadMenu(g_hInst,MAKEINTRESOURCE(IDR_MAIN));
153     HWND hWnd = CreateWindowEx(0,pszClassName,"MyWnd",
154         WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,
155         CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,NULL,hMenu,g_hInst,NULL);
156     return hWnd;
157 }
158 
159 void DisplayWnd(HWND hWnd)
160 {
161     ShowWindow(hWnd,SW_SHOW);
162     UpdateWindow(hWnd);
163 }
164 
165 void Message()
166 {
167     MSG msg = {0};
168     while(GetMessage(&msg,NULL,0,0))
169     {
170         TranslateMessage(&msg);
171         DispatchMessage(&msg);
172     }
173 }
174 
175 int APIENTRY WinMain(HINSTANCE hInstance,
176                      HINSTANCE hPrevInstance,
177                      LPSTR     lpCmdLine,
178                      int       nCmdShow)
179 {
180     // TODO: Place code here.
181     g_hInst = hInstance;
182     RegisterWnd("MYWND");
183     HWND hWnd = CreateWnd("MYWND");
184     DisplayWnd(hWnd);
185     Message();
186     
187     return 0;
188 }

 

posted @ 2012-10-11 17:04  吟唱黑暗  阅读(1097)  评论(2编辑  收藏  举报