博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
15windows_15_Edit编辑框
阅读量:5915 次
发布时间:2019-06-19

本文共 4170 字,大约阅读时间需要 13 分钟。

15windows_15_Edit编辑框

 
  1. #include <windows.h>
  2. #include <iostream>
  3. #include "resource.h"
  4. CHAR szText[256] = {
    0 };
  5. #define PrintLog(x) WriteConsole(g_hStdout, x, strlen(x), NULL, NULL)
  6. HINSTANCE g_hInst = NULL; //窗口句柄
  7. HANDLE g_hStdout = NULL; //控制台句柄
  8. HWND g_hEdit = NULL; //保存Edit句柄
  9. //WM_CREATE消息处理
  10. void OnCreate(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam)
  11. {
  12. //创建Edit文本编辑框
  13. g_hEdit = CreateWindow("EDIT", "Hello Edit", WS_EX_CLIENTEDGE|WS_VSCROLL | WS_HSCROLL|WS_CHILD | WS_VISIBLE | ES_AUTOHSCROLL | ES_PASSWORD | ES_MULTILINE | ES_AUTOHSCROLL | ES_AUTOHSCROLL | ES_AUTOVSCROLL,
  14. 0, 0, 100, 200, hWnd, (HMENU)1001, g_hInst, NULL);
  15. }
  16. //让Edit铺满整个客户区
  17. void OnSize(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam)
  18. {
  19. int nWidth = LOWORD(lParam);
  20. int nHeight = HIWORD(lParam);
  21. if (g_hEdit != NULL)
  22. {
  23. MoveWindow(g_hEdit, 0, 0, nWidth, nHeight, TRUE);
  24. }
  25. }
  26. void OnSave()
  27. {
  28. //获取文字长度 WM_GETTEXTLENGTH 用这个消息`
  29. LRESULT nTextLen = SendMessage(g_hEdit, WM_GETTEXTLENGTH, 0, 0);
  30. //获取文字
  31. CHAR *pszBuff = NULL;
  32. pszBuff = (CHAR *)malloc(nTextLen + 1); //+1就是为了不越界
  33. memset(pszBuff, 0, nTextLen + 1);
  34. SendMessage(g_hEdit, WM_GETTEXT, nTextLen + 1 , (LPARAM)pszBuff);
  35. //保存文件
  36. FILE *pFile = NULL;
  37. fopen_s(&pFile, "c:\\1.txt", "w+"); //打开文件
  38. fwrite(pszBuff, nTextLen, 1, pFile); //把文件写进文件
  39. fclose(pFile);
  40. free(pszBuff);
  41. }
  42. void OnOpen()
  43. {
  44. //打开文件,读取数据
  45. FILE *pFile = NULL;
  46. fopen_s(&pFile, "c:\\1.txt", "r");
  47. fseek(pFile, 0, SEEK_END);//把fseek移动到文件结尾
  48. long nFileLen = ftell(pFile); //获得文件结尾的长度
  49. fseek(pFile, 0, SEEK_SET); //把fseek指针移动到原来位置
  50. CHAR *pszBuff = (CHAR*)malloc(nFileLen + 1); //多一个字符留给'\0'用
  51. memset(pszBuff, 0, nFileLen + 1); //把pszBuff置零
  52. fread(pszBuff, nFileLen, 1, pFile); //读取文件内容到buff
  53. fclose(pFile);
  54. //将字符显示在Edit窗口
  55. SendMessage(g_hEdit, WM_SETTEXT, 0, (LPARAM)pszBuff);
  56. }
  57. void OnCommand(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam)
  58. {
  59. int nNotifyCode = HIWORD(wParam);//Ctrl ID
  60. int nEventID = LOWORD(wParam); // 事件ID
  61. switch (nEventID)
  62. {
  63. case 1001:
  64. switch (nNotifyCode)
  65. {
  66. case EN_CHANGE: //内容发生变化
  67. //MessageBox(NULL, "EN_CHANGE", "EN", MB_OK);
  68. break;
  69. }
  70. break;
  71. case ID_SELALL: //全部选择
  72. //发送全选消息
  73. SendMessage(g_hEdit, EM_SETSEL, 0, -1); //从第0个开始,到-1结尾
  74. break;
  75. case ID_COPY: //复制
  76. SendMessage(g_hEdit, WM_COPY, 0, 0);
  77. break;
  78. case ID_PASTE: //粘贴消息
  79. SendMessage(g_hEdit, WM_PASTE, 0, 0);
  80. break;
  81. case ID_SAVE:
  82. OnSave();
  83. break;
  84. case ID_OPEN:
  85. OnOpen();
  86. break;
  87. case ID_EXIT:
  88. PostQuitMessage(0);
  89. break;
  90. }
  91. }
  92. LRESULT CALLBACK WndProc(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam)
  93. {
  94. switch (nMsg)
  95. {
  96. case WM_CREATE:
  97. OnCreate(hWnd, nMsg, wParam, lParam);
  98. break;
  99. case WM_COMMAND:
  100. OnCommand(hWnd, nMsg, wParam, lParam);
  101. break;
  102. case WM_SIZE:
  103. OnSize(hWnd, nMsg, wParam, lParam);
  104. break;
  105. case WM_DESTROY:
  106. PostQuitMessage(0);
  107. break;
  108. }
  109. return DefWindowProc(hWnd, nMsg, wParam, lParam);
  110. }
  111. BOOL RegisterWnd(LPSTR pszClassName)
  112. {
  113. WNDCLASSEX wce = {
    0 };
  114. wce.cbSize = sizeof(wce);
  115. wce.cbClsExtra = 0;
  116. wce.cbWndExtra = 0;
  117. wce.hbrBackground = HBRUSH(COLOR_BTNFACE + 1);
  118. wce.hCursor = NULL;
  119. wce.hIcon = NULL;
  120. wce.hIconSm = NULL;
  121. wce.hInstance = g_hInst;
  122. wce.lpfnWndProc = WndProc;
  123. wce.lpszClassName = pszClassName;
  124. wce.lpszMenuName = NULL;
  125. wce.style = CS_HREDRAW | CS_VREDRAW;
  126. ATOM atom = RegisterClassEx(&wce);
  127. if (atom == NULL)
  128. {
  129. return FALSE;
  130. }
  131. else
  132. {
  133. return TRUE;
  134. }
  135. }
  136. HWND CreateWnd(LPSTR pszClassName)
  137. {
  138. HMENU hMainMenu = LoadMenu(g_hInst, MAKEINTRESOURCE(IDR_MENU1));
  139. HWND hWnd = CreateWindowEx(0, pszClassName, "飞天", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
  140. CW_USEDEFAULT, NULL, hMainMenu, g_hInst, 0);
  141. return hWnd;
  142. }
  143. void ShowWnd(HWND hWnd)
  144. {
  145. ShowWindow(hWnd, SW_SHOW);
  146. UpdateWindow(hWnd);
  147. }
  148. void Msg()
  149. {
  150. MSG msg = {
    0 };
  151. while (GetMessage(&msg, NULL, 0, 0))
  152. {
  153. TranslateMessage(&msg);
  154. DispatchMessage(&msg);
  155. }
  156. }
  157. void ConsoleWnd()
  158. {
  159. AllocConsole();
  160. g_hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
  161. CHAR szText[] = "Debug start:\n";
  162. WriteConsole(g_hStdout, szText, strlen(szText), NULL, NULL);
  163. }
  164. int WINAPI WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPSTR lpCmdLine, _In_ int nShowCmd)
  165. {
  166. g_hInst = hInstance;
  167. //ConsoleWnd();
  168. RegisterWnd("oooo");
  169. HWND hWnd = CreateWnd("oooo");
  170. ShowWnd(hWnd);
  171. Msg();
  172. return 0;
  173. }

转载于:https://www.cnblogs.com/nfking/p/5573175.html

你可能感兴趣的文章
SCCM 2007 sp2 eva安装之一:sql server 2005安装及升级sp2
查看>>
电商企业适用基础快递接口对接demo
查看>>
通过chkconfig设置linux开机自启动服务- 老男孩Linux运维学习笔记1
查看>>
CENTOS 安装 jenkins
查看>>
iptables详解
查看>>
Kaptchar详细配置表
查看>>
Java程序员从笨鸟到菜鸟之(四十三)细谈struts2(六)获取servletAPI和封装表单数据...
查看>>
OSPF详述
查看>>
红黑树的原理分析和算法设计
查看>>
ant_Jmeter持续集成测试报告优化之添加throughput显示
查看>>
iostat介绍
查看>>
Linux计算字符串md5/sha1
查看>>
我的友情链接
查看>>
dancer cookbook 小议4
查看>>
CORBA版HelloWorld
查看>>
Selenium IDE XPath元素定位器
查看>>
加入51CTO
查看>>
一个=号引发的错误.......
查看>>
CPU显卡内存与3DMAX渲染的关系
查看>>
【Java】方法重载于覆写的区别;This与Super的区别;Final关键字的作用
查看>>