基于VC的MFC界面开发
UpdateData(TRUE);//将输入数据赋给文本框变量。
UpdateData(FALSE);//将内容显示在文本框中
AfxMessageBox("演示弹窗");
当使用弹窗的时候会阻塞进程。
进阶(未实现):VC++6.0可视化窗口怎么填加图片
本例是基于C语言实现的Windows socket的编程。
注意,在新建时不要勾选“Windows Socket”,因为用到了<winsock2.h>,勾选后会报错,说有一个结构体被定义了两次:
(这前面是你的VC安装路径)include\winsock2.h(99) : error C2011: 'fd_set' : 'struct' type redefinition
参考代码如下:
// 333Dlg.cpp : implementation file
//客户端
#include "stdafx.h"
#include "333.h"
#include "333Dlg.h"
#include <stdlib.h>
#include<Winsock2.h>
#pragma comment (lib,"ws2_32.lib")
#define BUFFER_SIZE 1024
#define MY_PORT 3434
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CMy333Dlg dialog
CMy333Dlg::CMy333Dlg(CWnd* pParent /*=NULL*/)
: CDialog(CMy333Dlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CMy333Dlg)
m = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CMy333Dlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CMy333Dlg)
DDX_Text(pDX, IDC_EDIT1, m);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CMy333Dlg, CDialog)
//{{AFX_MSG_MAP(CMy333Dlg)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
ON_EN_CHANGE(IDC_EDIT1, OnChangeEdit1)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMy333Dlg message handlers
BOOL CMy333Dlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
return TRUE; // return TRUE unless you set the focus to a control
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CMy333Dlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CMy333Dlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CMy333Dlg::OnButton1()
{
// TODO: Add your control notification handler code here
// TODO: Add your control notification handler code here
SOCKET conn_sock;
struct sockaddr_in client_addr;
//WSA初始化
WSADATA wsadata;
WSAStartup(MAKEWORD(1,1),&wsadata);
//配置connect
conn_sock = socket(AF_INET,SOCK_STREAM,0);
bind(conn_sock,(struct sockaddr*)&client_addr,sizeof(struct sockaddr));
//配置server addr
client_addr.sin_family = AF_INET;
client_addr.sin_port = htons(MY_PORT);
client_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
//链接
connect(conn_sock,(struct sockaddr *)&client_addr,sizeof(struct sockaddr));
//接收
char rc[100] = "";
recv(conn_sock,rc,sizeof(rc),0);
//printf("From Server : %s\n",rc);
AfxMessageBox("连接成功!");
//输入路径
//printf("Please input path:\n");
//char path[100];
//scanf("%s",&path);
//发送路径
//send(conn_sock,path,strlen(path),0);
//打开文件,准备写入
FILE * fp = fopen(m, "rt");
if(NULL == fp)
{
//printf("File: %s Can Not Open To Write\n", path);
AfxMessageBox("打开文件失败!");
system("pause");
exit(1);
}
else
{
char buffer[BUFFER_SIZE];
memset(buffer, 0, BUFFER_SIZE);
int length = 0;
//发送文件内容,直到读不到文件
while((length = fread(buffer, sizeof(char), BUFFER_SIZE, fp)) > 0)
{
if (send(conn_sock, buffer, length, 0) < 0)//向服务端发送
{
//printf("Send File: %s Failed\n", path);
AfxMessageBox("发送失败!");
break;
}
memset(buffer, 0, BUFFER_SIZE);
}
//printf("File: %s Transfer Successful!\n", path);
AfxMessageBox("发送成功!");
}
fclose(fp); //关闭文件
closesocket(conn_sock);
WSACleanup();
}
void CMy333Dlg::OnChangeEdit1()
{
// TODO: If this is a RICHEDIT control, the control will not
// send this notification unless you override the CDialog::OnInitDialog()
// function and call CRichEditCtrl().SetEventMask()
// with the ENM_CHANGE flag ORed into the mask.
// TODO: Add your control notification handler code here
UpdateData(TRUE);//将输入数据赋给文本框变量。
UpdateData(FALSE);//将内容显示在文本框中
}
// 111Dlg.cpp : implementation file
//服务器端
#include "stdafx.h"
#include "111.h"
#include "111Dlg.h"
#include <Winsock2.h>
#pragma comment (lib,"ws2_32.lib")
#define BUFFER_SIZE 1024
#define MY_PORT 3434
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CMy111Dlg dialog
CMy111Dlg::CMy111Dlg(CWnd* pParent /*=NULL*/)
: CDialog(CMy111Dlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CMy111Dlg)
m = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CMy111Dlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CMy111Dlg)
DDX_Text(pDX, IDC_EDIT1, m);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CMy111Dlg, CDialog)
//{{AFX_MSG_MAP(CMy111Dlg)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON1, OnStart)
ON_EN_CHANGE(IDC_EDIT1, OnChangeEdit1)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMy111Dlg message handlers
BOOL CMy111Dlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
return TRUE; // return TRUE unless you set the focus to a control
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CMy111Dlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CMy111Dlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CMy111Dlg::OnStart()
{
// TODO: Add your control notification handler code here
//WSA初始化
WSADATA wsadata;
char *buffer = "Welcome ! Server connected.";//字符串
struct sockaddr_in server_addr;
SOCKET listen_socket;
WSAStartup(MAKEWORD(1,1),&wsadata);
//配置server addr
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(MY_PORT);
server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
//配置listener
listen_socket = socket(AF_INET,SOCK_STREAM,0);
bind(listen_socket,(struct sockaddr*)&server_addr,sizeof(struct sockaddr));
int flag = 1;
while (flag)
{
//开始监听
listen(listen_socket,5);
int dummy = sizeof(SOCKADDR);
//printf("Listening to client...\n");
AfxMessageBox("等待客户端连接......");
//建立连接
SOCKET new_socket = accept(listen_socket,NULL,&dummy);
//printf("Client connected!\n");
AfxMessageBox("连接成功!准备从客户端接收文件......");
//发送信息
send(new_socket,buffer,strlen(buffer),0);
char rc[100] = "";
//recv(new_socket,rc,sizeof(rc),0);
//printf("File name from Client : %s\n",rc);
//char path[100];
char buf[999];
//printf("Please set a path to save file:\n");
FILE * fp = fopen(m, "wt");//以写入的方式打开文件
if (NULL == fp)
{
//printf("File: %s Not Found\n", path);
AfxMessageBox("写入文件失败!");
}
else
{
memset(buf, 0, BUFFER_SIZE);
unsigned int length = 0;
//接受到客户端发来的文件数据
while ((length = recv(new_socket, buf, BUFFER_SIZE, 0)) > 0)
{
//把发来的数据写入到文件中
if (fwrite(buf, sizeof(char), length, fp) < length)
{
//printf("File: %s Write Failed\n", path);
AfxMessageBox("接收失败!");
break;
}
memset(buf, 0, BUFFER_SIZE);
}
//printf("Receive File: %s From Server Successful!\n", path);
AfxMessageBox("接收成功!");
flag = 0;
}
fclose(fp); //关闭文件
closesocket(new_socket);
}
closesocket(listen_socket);
WSACleanup();
}
void CMy111Dlg::OnChangeEdit1()
{
// TODO: If this is a RICHEDIT control, the control will not
// send this notification unless you override the CDialog::OnInitDialog()
// function and call CRichEditCtrl().SetEventMask()
// with the ENM_CHANGE flag ORed into the mask.
// TODO: Add your control notification handler code here
UpdateData(TRUE);//将输入数据赋给文本框变量。
UpdateData(FALSE);//将内容显示在文本框中
}
基于VC的MFC界面开发的更多相关文章
- (转载)用vs2010开发基于VC++的MFC 串口通信一*****两台电脑同一个串口号之间的通信
此文章以visual C++数据採集与串口通信測控应用实战为參考教程 此文章适合VC++串口通信入门 一.页面布局及加入控件 1, 安装好vs2010如图 2, 新建一个基于VC++的MFC项目com ...
- 如何在MFC界面开发中响应Button按钮的Down和Up事件
通过尝试有两种方案可以解决这个问题,第一种方案是通过PreTranslateMessage函数在调度消息之前对消息类型进行筛选,第二种方案是重载CButton类,在重载后的类CForTestButto ...
- 基于matlab的GUI界面开发软件
matlab工具deploytool编译M为可执行程序 http://blog.sina.com.cn/s/blog_60f8483a0100gnsm.html
- MFC界面开发(QQ透明皮肤:多层算法,一键适配各种背景 )
http://blog.csdn.net/kent19900125/article/category/1368203/3 QQ透明皮肤:多层算法,一键适配各种背景 . http://blog.csdn ...
- VC++/MFC(VC6)开发技术精品学习资料下载汇总
工欲善其事,必先利其器,VC开发MFC Windows程序,Visual C++或Visual Studio是必须的,恩,这里都给你总结好了,拿去吧:VC/MFC开发必备Visual C++.Visu ...
- 转:vc与界面开发之间的文章
[很好的一篇文章,很喜欢看同行的心路历程:http://www.vckbase.com/index.php/nv/444.html] 本屌丝在新春放假期间闲来无事,在各大编程论坛溜达了一圈.发现年前的 ...
- 基于VC++ Win32+CUDA+OpenGL组合与VC++ MFC SDI+CUDA+OpenGL组合两种方案的遥感影像显示:获得的重要结论!
1.基于VC++ Win32+CUDA+OpenGL组合的遥感影像显示 在该组合方案下,初始化时将OpenGL设置为下面两种方式,效果一样 //设置方式1 glutInitDisplayMode (G ...
- VS2010/MFC编程入门之五十二(Ribbon界面开发:创建Ribbon样式的应用程序框架)
上一节中鸡啄米讲了GDI对象之画刷CBrush,至此图形图像的入门知识就讲完了.从本节开始鸡啄米将为大家带来Ribbon界面开发的有关内容.本文先来说说如何创建Ribbon样式的应用程序框架. Rib ...
- 如何在VS2010的VC++ 基于对话框的MFC程序中添加菜单
方法1:亲测 成功 转载自https://social.msdn.microsoft.com/Forums/vstudio/zh-CN/48338f6b-e5d9-4c0c-8b17-05ca3ef ...
随机推荐
- SQLserver查询库中包含某个字段的表
select [name] from [TPMS_PRD].[dbo].sysobjects where id in(select id from [TPMS_PRD].[dbo].syscolumn ...
- 基于 HTML5 结合互联网+ 的 3D 隧道
前言 目前,物资采购和人力成本是隧道业发展的两大瓶颈.比如依靠民间借贷,融资成本很高:采购价格不透明,没有增值税发票:还有项目管控和供应链管理的问题.成本在不断上升,利润在不断下降,隧道产业的“互联网 ...
- codeforces#983 B. XOR-pyramid (dp)
参考博客:https://www.01hai.com/note/av137952. 题意:首先定义 (b代表一个数组) 给出一个区间,l,r,求它最大的连续子序列的函数值 分析: 定义dp[x][y] ...
- 解决hash冲突的三个方法
通过构造性能良好的哈希函数,可以减少冲突,但一般不可能完全避免冲突,因此解决冲突是哈希法的另一个关键问题.创建哈希表和查找哈希表都会遇到冲突,两种情况下解决冲突的方法应该一致.下面以创建哈希表为例,说 ...
- 解决虚拟机下安装CentOS无法上网
Centos7默认是不启用有线网卡的,需要手动开启. 操作步骤如下: 首先,打开终端.cd /etc/sysconfig/network-scripts/ls 查看一下ifcfg-eno后面对应的数字 ...
- Android艺术——深看Activity的生命周期
探究Activity的生命周期 1.典型情况下的生命周期分析:onCreate 初始化工作,加载布局资源和数据:onStart ac正在启动但是无法交互,后台:onResume ac可见,显示在前台: ...
- android O 打开设置->声音->“点按时震动问题”
主要原因是和导航栏和屏幕最下方3个按键的属性配置有关,因为在PhoneWindowManager中调用方法performHapticFeedbackLw(null, HapticFeedbackCon ...
- 2.5 time 模块
- C++ 动态链接库 DLL 的一些笔记
DLL 文件源代码: // test.h #ifdef TEST_EXPORTS #define TEST_API __declspec(dllexport) #endif class TEST_AP ...
- yii2 redirect重定向
redirect使用方法 $this->redirect(array('/site/contact','id'=>12)); //http://www.kuitao8.com/testwe ...