教你熟悉VC6.0创建一个可视化软件的过程

  1. UpdateData(TRUE);//将输入数据赋给文本框变量。
  2. UpdateData(FALSE);//将内容显示在文本框中
  3. AfxMessageBox("演示弹窗");

当使用弹窗的时候会阻塞进程。

使用VC++6.0开发一个弹窗程序

进阶(未实现):VC++6.0可视化窗口怎么填加图片

本例是基于C语言实现的Windows socket的编程。

注意,在新建时不要勾选“Windows Socket”,因为用到了<winsock2.h>,勾选后会报错,说有一个结构体被定义了两次:

  1. (这前面是你的VC安装路径)include\winsock2.h(99) : error C2011: 'fd_set' : 'struct' type redefinition

参考代码如下:

  1. // 333Dlg.cpp : implementation file
  2. //客户端
  3. #include "stdafx.h"
  4. #include "333.h"
  5. #include "333Dlg.h"
  6. #include <stdlib.h>
  7. #include<Winsock2.h>
  8. #pragma comment (lib,"ws2_32.lib")
  9. #define BUFFER_SIZE 1024
  10. #define MY_PORT 3434
  11. #ifdef _DEBUG
  12. #define new DEBUG_NEW
  13. #undef THIS_FILE
  14. static char THIS_FILE[] = __FILE__;
  15. #endif
  16. /////////////////////////////////////////////////////////////////////////////
  17. // CMy333Dlg dialog
  18. CMy333Dlg::CMy333Dlg(CWnd* pParent /*=NULL*/)
  19. : CDialog(CMy333Dlg::IDD, pParent)
  20. {
  21. //{{AFX_DATA_INIT(CMy333Dlg)
  22. m = _T("");
  23. //}}AFX_DATA_INIT
  24. // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
  25. m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  26. }
  27. void CMy333Dlg::DoDataExchange(CDataExchange* pDX)
  28. {
  29. CDialog::DoDataExchange(pDX);
  30. //{{AFX_DATA_MAP(CMy333Dlg)
  31. DDX_Text(pDX, IDC_EDIT1, m);
  32. //}}AFX_DATA_MAP
  33. }
  34. BEGIN_MESSAGE_MAP(CMy333Dlg, CDialog)
  35. //{{AFX_MSG_MAP(CMy333Dlg)
  36. ON_WM_PAINT()
  37. ON_WM_QUERYDRAGICON()
  38. ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
  39. ON_EN_CHANGE(IDC_EDIT1, OnChangeEdit1)
  40. //}}AFX_MSG_MAP
  41. END_MESSAGE_MAP()
  42. /////////////////////////////////////////////////////////////////////////////
  43. // CMy333Dlg message handlers
  44. BOOL CMy333Dlg::OnInitDialog()
  45. {
  46. CDialog::OnInitDialog();
  47. // Set the icon for this dialog. The framework does this automatically
  48. // when the application's main window is not a dialog
  49. SetIcon(m_hIcon, TRUE); // Set big icon
  50. SetIcon(m_hIcon, FALSE); // Set small icon
  51. // TODO: Add extra initialization here
  52. return TRUE; // return TRUE unless you set the focus to a control
  53. }
  54. // If you add a minimize button to your dialog, you will need the code below
  55. // to draw the icon. For MFC applications using the document/view model,
  56. // this is automatically done for you by the framework.
  57. void CMy333Dlg::OnPaint()
  58. {
  59. if (IsIconic())
  60. {
  61. CPaintDC dc(this); // device context for painting
  62. SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
  63. // Center icon in client rectangle
  64. int cxIcon = GetSystemMetrics(SM_CXICON);
  65. int cyIcon = GetSystemMetrics(SM_CYICON);
  66. CRect rect;
  67. GetClientRect(&rect);
  68. int x = (rect.Width() - cxIcon + 1) / 2;
  69. int y = (rect.Height() - cyIcon + 1) / 2;
  70. // Draw the icon
  71. dc.DrawIcon(x, y, m_hIcon);
  72. }
  73. else
  74. {
  75. CDialog::OnPaint();
  76. }
  77. }
  78. // The system calls this to obtain the cursor to display while the user drags
  79. // the minimized window.
  80. HCURSOR CMy333Dlg::OnQueryDragIcon()
  81. {
  82. return (HCURSOR) m_hIcon;
  83. }
  84. void CMy333Dlg::OnButton1()
  85. {
  86. // TODO: Add your control notification handler code here
  87. // TODO: Add your control notification handler code here
  88. SOCKET conn_sock;
  89. struct sockaddr_in client_addr;
  90. //WSA初始化
  91. WSADATA wsadata;
  92. WSAStartup(MAKEWORD(1,1),&wsadata);
  93. //配置connect
  94. conn_sock = socket(AF_INET,SOCK_STREAM,0);
  95. bind(conn_sock,(struct sockaddr*)&client_addr,sizeof(struct sockaddr));
  96. //配置server addr
  97. client_addr.sin_family = AF_INET;
  98. client_addr.sin_port = htons(MY_PORT);
  99. client_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
  100. //链接
  101. connect(conn_sock,(struct sockaddr *)&client_addr,sizeof(struct sockaddr));
  102. //接收
  103. char rc[100] = "";
  104. recv(conn_sock,rc,sizeof(rc),0);
  105. //printf("From Server : %s\n",rc);
  106. AfxMessageBox("连接成功!");
  107. //输入路径
  108. //printf("Please input path:\n");
  109. //char path[100];
  110. //scanf("%s",&path);
  111. //发送路径
  112. //send(conn_sock,path,strlen(path),0);
  113. //打开文件,准备写入
  114. FILE * fp = fopen(m, "rt");
  115. if(NULL == fp)
  116. {
  117. //printf("File: %s Can Not Open To Write\n", path);
  118. AfxMessageBox("打开文件失败!");
  119. system("pause");
  120. exit(1);
  121. }
  122. else
  123. {
  124. char buffer[BUFFER_SIZE];
  125. memset(buffer, 0, BUFFER_SIZE);
  126. int length = 0;
  127. //发送文件内容,直到读不到文件
  128. while((length = fread(buffer, sizeof(char), BUFFER_SIZE, fp)) > 0)
  129. {
  130. if (send(conn_sock, buffer, length, 0) < 0)//向服务端发送
  131. {
  132. //printf("Send File: %s Failed\n", path);
  133. AfxMessageBox("发送失败!");
  134. break;
  135. }
  136. memset(buffer, 0, BUFFER_SIZE);
  137. }
  138. //printf("File: %s Transfer Successful!\n", path);
  139. AfxMessageBox("发送成功!");
  140. }
  141. fclose(fp); //关闭文件
  142. closesocket(conn_sock);
  143. WSACleanup();
  144. }
  145. void CMy333Dlg::OnChangeEdit1()
  146. {
  147. // TODO: If this is a RICHEDIT control, the control will not
  148. // send this notification unless you override the CDialog::OnInitDialog()
  149. // function and call CRichEditCtrl().SetEventMask()
  150. // with the ENM_CHANGE flag ORed into the mask.
  151. // TODO: Add your control notification handler code here
  152. UpdateData(TRUE);//将输入数据赋给文本框变量。
  153. UpdateData(FALSE);//将内容显示在文本框中
  154. }
  1. // 111Dlg.cpp : implementation file
  2. //服务器端
  3. #include "stdafx.h"
  4. #include "111.h"
  5. #include "111Dlg.h"
  6. #include <Winsock2.h>
  7. #pragma comment (lib,"ws2_32.lib")
  8. #define BUFFER_SIZE 1024
  9. #define MY_PORT 3434
  10. #ifdef _DEBUG
  11. #define new DEBUG_NEW
  12. #undef THIS_FILE
  13. static char THIS_FILE[] = __FILE__;
  14. #endif
  15. /////////////////////////////////////////////////////////////////////////////
  16. // CMy111Dlg dialog
  17. CMy111Dlg::CMy111Dlg(CWnd* pParent /*=NULL*/)
  18. : CDialog(CMy111Dlg::IDD, pParent)
  19. {
  20. //{{AFX_DATA_INIT(CMy111Dlg)
  21. m = _T("");
  22. //}}AFX_DATA_INIT
  23. // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
  24. m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  25. }
  26. void CMy111Dlg::DoDataExchange(CDataExchange* pDX)
  27. {
  28. CDialog::DoDataExchange(pDX);
  29. //{{AFX_DATA_MAP(CMy111Dlg)
  30. DDX_Text(pDX, IDC_EDIT1, m);
  31. //}}AFX_DATA_MAP
  32. }
  33. BEGIN_MESSAGE_MAP(CMy111Dlg, CDialog)
  34. //{{AFX_MSG_MAP(CMy111Dlg)
  35. ON_WM_PAINT()
  36. ON_WM_QUERYDRAGICON()
  37. ON_BN_CLICKED(IDC_BUTTON1, OnStart)
  38. ON_EN_CHANGE(IDC_EDIT1, OnChangeEdit1)
  39. //}}AFX_MSG_MAP
  40. END_MESSAGE_MAP()
  41. /////////////////////////////////////////////////////////////////////////////
  42. // CMy111Dlg message handlers
  43. BOOL CMy111Dlg::OnInitDialog()
  44. {
  45. CDialog::OnInitDialog();
  46. // Set the icon for this dialog. The framework does this automatically
  47. // when the application's main window is not a dialog
  48. SetIcon(m_hIcon, TRUE); // Set big icon
  49. SetIcon(m_hIcon, FALSE); // Set small icon
  50. // TODO: Add extra initialization here
  51. return TRUE; // return TRUE unless you set the focus to a control
  52. }
  53. // If you add a minimize button to your dialog, you will need the code below
  54. // to draw the icon. For MFC applications using the document/view model,
  55. // this is automatically done for you by the framework.
  56. void CMy111Dlg::OnPaint()
  57. {
  58. if (IsIconic())
  59. {
  60. CPaintDC dc(this); // device context for painting
  61. SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
  62. // Center icon in client rectangle
  63. int cxIcon = GetSystemMetrics(SM_CXICON);
  64. int cyIcon = GetSystemMetrics(SM_CYICON);
  65. CRect rect;
  66. GetClientRect(&rect);
  67. int x = (rect.Width() - cxIcon + 1) / 2;
  68. int y = (rect.Height() - cyIcon + 1) / 2;
  69. // Draw the icon
  70. dc.DrawIcon(x, y, m_hIcon);
  71. }
  72. else
  73. {
  74. CDialog::OnPaint();
  75. }
  76. }
  77. // The system calls this to obtain the cursor to display while the user drags
  78. // the minimized window.
  79. HCURSOR CMy111Dlg::OnQueryDragIcon()
  80. {
  81. return (HCURSOR) m_hIcon;
  82. }
  83. void CMy111Dlg::OnStart()
  84. {
  85. // TODO: Add your control notification handler code here
  86. //WSA初始化
  87. WSADATA wsadata;
  88. char *buffer = "Welcome ! Server connected.";//字符串
  89. struct sockaddr_in server_addr;
  90. SOCKET listen_socket;
  91. WSAStartup(MAKEWORD(1,1),&wsadata);
  92. //配置server addr
  93. server_addr.sin_family = AF_INET;
  94. server_addr.sin_port = htons(MY_PORT);
  95. server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
  96. //配置listener
  97. listen_socket = socket(AF_INET,SOCK_STREAM,0);
  98. bind(listen_socket,(struct sockaddr*)&server_addr,sizeof(struct sockaddr));
  99. int flag = 1;
  100. while (flag)
  101. {
  102. //开始监听
  103. listen(listen_socket,5);
  104. int dummy = sizeof(SOCKADDR);
  105. //printf("Listening to client...\n");
  106. AfxMessageBox("等待客户端连接......");
  107. //建立连接
  108. SOCKET new_socket = accept(listen_socket,NULL,&dummy);
  109. //printf("Client connected!\n");
  110. AfxMessageBox("连接成功!准备从客户端接收文件......");
  111. //发送信息
  112. send(new_socket,buffer,strlen(buffer),0);
  113. char rc[100] = "";
  114. //recv(new_socket,rc,sizeof(rc),0);
  115. //printf("File name from Client : %s\n",rc);
  116. //char path[100];
  117. char buf[999];
  118. //printf("Please set a path to save file:\n");
  119. FILE * fp = fopen(m, "wt");//以写入的方式打开文件
  120. if (NULL == fp)
  121. {
  122. //printf("File: %s Not Found\n", path);
  123. AfxMessageBox("写入文件失败!");
  124. }
  125. else
  126. {
  127. memset(buf, 0, BUFFER_SIZE);
  128. unsigned int length = 0;
  129. //接受到客户端发来的文件数据
  130. while ((length = recv(new_socket, buf, BUFFER_SIZE, 0)) > 0)
  131. {
  132. //把发来的数据写入到文件中
  133. if (fwrite(buf, sizeof(char), length, fp) < length)
  134. {
  135. //printf("File: %s Write Failed\n", path);
  136. AfxMessageBox("接收失败!");
  137. break;
  138. }
  139. memset(buf, 0, BUFFER_SIZE);
  140. }
  141. //printf("Receive File: %s From Server Successful!\n", path);
  142. AfxMessageBox("接收成功!");
  143. flag = 0;
  144. }
  145. fclose(fp); //关闭文件
  146. closesocket(new_socket);
  147. }
  148. closesocket(listen_socket);
  149. WSACleanup();
  150. }
  151. void CMy111Dlg::OnChangeEdit1()
  152. {
  153. // TODO: If this is a RICHEDIT control, the control will not
  154. // send this notification unless you override the CDialog::OnInitDialog()
  155. // function and call CRichEditCtrl().SetEventMask()
  156. // with the ENM_CHANGE flag ORed into the mask.
  157. // TODO: Add your control notification handler code here
  158. UpdateData(TRUE);//将输入数据赋给文本框变量。
  159. UpdateData(FALSE);//将内容显示在文本框中
  160. }

基于VC的MFC界面开发的更多相关文章

  1. (转载)用vs2010开发基于VC++的MFC 串口通信一*****两台电脑同一个串口号之间的通信

    此文章以visual C++数据採集与串口通信測控应用实战为參考教程 此文章适合VC++串口通信入门 一.页面布局及加入控件 1, 安装好vs2010如图 2, 新建一个基于VC++的MFC项目com ...

  2. 如何在MFC界面开发中响应Button按钮的Down和Up事件

    通过尝试有两种方案可以解决这个问题,第一种方案是通过PreTranslateMessage函数在调度消息之前对消息类型进行筛选,第二种方案是重载CButton类,在重载后的类CForTestButto ...

  3. 基于matlab的GUI界面开发软件

    matlab工具deploytool编译M为可执行程序 http://blog.sina.com.cn/s/blog_60f8483a0100gnsm.html

  4. MFC界面开发(QQ透明皮肤:多层算法,一键适配各种背景 )

    http://blog.csdn.net/kent19900125/article/category/1368203/3 QQ透明皮肤:多层算法,一键适配各种背景 . http://blog.csdn ...

  5. VC++/MFC(VC6)开发技术精品学习资料下载汇总

    工欲善其事,必先利其器,VC开发MFC Windows程序,Visual C++或Visual Studio是必须的,恩,这里都给你总结好了,拿去吧:VC/MFC开发必备Visual C++.Visu ...

  6. 转:vc与界面开发之间的文章

    [很好的一篇文章,很喜欢看同行的心路历程:http://www.vckbase.com/index.php/nv/444.html] 本屌丝在新春放假期间闲来无事,在各大编程论坛溜达了一圈.发现年前的 ...

  7. 基于VC++ Win32+CUDA+OpenGL组合与VC++ MFC SDI+CUDA+OpenGL组合两种方案的遥感影像显示:获得的重要结论!

    1.基于VC++ Win32+CUDA+OpenGL组合的遥感影像显示 在该组合方案下,初始化时将OpenGL设置为下面两种方式,效果一样 //设置方式1 glutInitDisplayMode (G ...

  8. VS2010/MFC编程入门之五十二(Ribbon界面开发:创建Ribbon样式的应用程序框架)

    上一节中鸡啄米讲了GDI对象之画刷CBrush,至此图形图像的入门知识就讲完了.从本节开始鸡啄米将为大家带来Ribbon界面开发的有关内容.本文先来说说如何创建Ribbon样式的应用程序框架. Rib ...

  9. 如何在VS2010的VC++ 基于对话框的MFC程序中添加菜单

    方法1:亲测 成功  转载自https://social.msdn.microsoft.com/Forums/vstudio/zh-CN/48338f6b-e5d9-4c0c-8b17-05ca3ef ...

随机推荐

  1. Spring-boot使用eclipse搭建项目(一)

    https://blog.csdn.net/qq_37421862/article/details/80484625

  2. 关于wxpython多线程研究包括(import Publisher等错误研究)

    作为一个自动化测试人员,开发基本的应用桌面程序是必须的!最近在研究wxpython相关知识,目前看到多线程一块,发现官方文档介绍说:"在线程中不能修改修改窗口属性!",但是实际情况 ...

  3. 父进程结束,其子进程不会结束,会挂到init进程下

    结论:一个父进程产生子进程,父进程结束(kill),子进程不会结束,子进程被init进程托管 下面是过程: d.sh脚本是一个ping命令,执行d.sh 目前,103310 进程为父进程,103344 ...

  4. python中打印中文

    python中打印中文 在python 2.x版本中,默认是ASCII编码方式,在有业务需要输入中文时,就会出现乱码的情况.解决这种问题的一个方式就是设置py文件的编码方式.实现方式如下: 在py文件 ...

  5. Kubernetes — 深入解析Pod对象:基本概念(一)

    在上一篇文章中,我详细介绍了 Pod 这个 Kubernetes 项目中最重要的概念. 现在,你已经非常清楚:Pod,而不是容器,才是 Kubernetes 项目中的最小编排单位.将这个设计落实到 A ...

  6. 使用Crowd集成Confluence与JIRA

    一. 独立安装Crowd,步骤1-步骤13的内容二. 设置Confluence使用Crowd进行认证.步骤14-18的内容三. 设置JIRA使用Crowd进行认证,并使用Confluence的组织机构 ...

  7. kettle变量(param命名参数)

    1.定义: 编辑-设置-命名参数 在当前界面下定义参数名称和缺省值. 2.引用:原始数据 通过${var}引用变量 输出 注:1.字符串在命名参数引用是需要添加单引号的,但位置参数是不需要进行转译: ...

  8. Linux--虚拟环境

    一 . 虚拟环境的安装 如果我们在进行多个django项目的话,只用一个物理环境的话,那么会影响效率,这时候我们局可以应用虚拟环境了 virtualenv #指定清华源下载pip的包 pip3 ins ...

  9. Pyspark 使用 Spark Udf 的一些经验

    起初开始写一些 udf 的时候感觉有一些奇怪,在 spark 的计算中,一般通过转换(Transformation) 在不触发计算(Action) 的情况下就行一些预处理.udf 就是这样一个好用的东 ...

  10. vim编辑器的命令总结

    1. 设置显示行数 :set nu 2. 复制第100至第103行的代码