基於VC的MFC界面開發


教你熟悉VC6.0創建一個可視化軟件的過程

UpdateData(TRUE);//將輸入數據賦給文本框變量。
UpdateData(FALSE);//將內容顯示在文本框中
AfxMessageBox("演示彈窗");

當使用彈窗的時候會阻塞進程。

使用VC++6.0開發一個彈窗程序

進階(未實現):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);//將內容顯示在文本框中
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM