⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mynetsenddlg.cpp

📁 通过开启NET MESSAGE服务传输消息的小工具,能最小化到系统托盘并响应菜单
💻 CPP
字号:
// mynetsendDlg.cpp : implementation file
//

#include "stdafx.h"
#include "mynetsend.h"
#include "mynetsendDlg.h"
#include "cinifile.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

#define WM_SHOW_TASK WM_USER+150 //这个是自定义的一消息,用来传递mouse点到拖盘区的信息

#pragma comment( lib, "ws2_32.lib" ) /*这句不能少。*/

// Packet format found thanks to a bit a sniffing
static unsigned char packet_header[] =
"\x04\x00\x28\x00"
"\x10\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
"\x00\x00\x00\x00\xf8\x91\x7b\x5a\x00\xff\xd0\x11\xa9\xb2\x00\xc0"
"\x4f\xb6\xe6\xfc"
"\xff\xff\xff\xff" // @40 : unique id over 16 bytes ?
"\xff\xff\xff\xff"
"\xff\xff\xff\xff"
"\xff\xff\xff\xff"
"\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00"
"\x00\x00\xff\xff\xff\xff"
"\xff\xff\xff\xff" // @74 : fields length
"\x00\x00";

unsigned char field_header[] =
"\xff\xff\xff\xff" // @0 : field length
"\x00\x00\x00\x00"
"\xff\xff\xff\xff"; // @8 : field length
//////////////////////////////////////////////////////////////////

int sd(unsigned long ip,CString from,CString machine,CString msg);

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMynetsendDlg dialog

CMynetsendDlg::CMynetsendDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CMynetsendDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CMynetsendDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CMynetsendDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMynetsendDlg)
	DDX_Control(pDX, IDC_COMBO1, m_box);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CMynetsendDlg, CDialog)
	//{{AFX_MSG_MAP(CMynetsendDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
	ON_MESSAGE(WM_SHOW_TASK,OnShowTask)
	ON_WM_SHOWWINDOW()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMynetsendDlg message handlers

BOOL CMynetsendDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// 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

	CIniFile ini;
	CString oldip;
	
	if (ini.OpenIniFileForRead("mynetsend.ini"))    //打开mynetsend.ini文件,如果成功:
	{
	ini.GetItemString("history","ip",oldip);	//取历史IP
	SetDlgItemText(IDC_COMBO1,oldip);
	ini.CloseIniFile();	
	}

	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CMynetsendDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
	
	if (nID==SC_MINIMIZE)
	{
		
	NOTIFYICONDATA nid;
	nid.cbSize=(DWORD)sizeof(NOTIFYICONDATA);
	nid.hWnd=this->m_hWnd;
	nid.uID=IDR_MAINFRAME;
	nid.uFlags=NIF_ICON|NIF_MESSAGE|NIF_TIP ;
	nid.uCallbackMessage=WM_SHOW_TASK;//自定义的在托盘区显示图标的消息
	nid.hIcon=LoadIcon(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDR_MAINFRAME));
	strcpy(nid.szTip,"要发消息点我!");//信息提示条
	Shell_NotifyIcon(NIM_ADD,&nid);//在托盘区添加图标
	ShowWindow(SW_HIDE);//隐藏窗口

	}

}

LRESULT CMynetsendDlg::OnShowTask(WPARAM wParam,LPARAM lParam)
//wParam接收的是图标的ID,而lParam接收的是鼠标的行为
{	
	if(wParam!=IDR_MAINFRAME) return 1;

	switch(lParam)
	{
	case WM_RBUTTONUP://右键起来时弹出快捷菜单
		{			
			LPPOINT lpoint=new tagPOINT;
			::GetCursorPos(lpoint);//得到鼠标位置

			CMenu menu;
			int pMenuID = 0;

			VERIFY(menu.LoadMenu(IDR_MENU1));//得到菜单
			CMenu* pPopup = menu.GetSubMenu(pMenuID);//得到子菜单
			ASSERT(pPopup != NULL);
			
			DWORD Made = pPopup->TrackPopupMenu(TPM_LEFTALIGN|TPM_LEFTBUTTON|TPM_NONOTIFY|TPM_RETURNCMD,lpoint->x,lpoint->y,this,NULL);//使用子菜单
			
			switch(Made)
			{
			case  ID_MENUITEM32771: {DestroyWindow();} break;
			case  ID_MENUITEM32772: {ShowWindow(SW_SHOW);ShowWindow(SW_SHOWNORMAL);}  break;  
			case  ID_MENUITEM32773: {CAboutDlg dlgAbout;dlgAbout.DoModal();}  break;  
			}

			pPopup->DestroyMenu();
			HMENU hmenu=menu.Detach();//资源回收
			menu.DestroyMenu();
			delete lpoint;
		}

		break;
	case WM_LBUTTONDOWN://左键的处理
		{
		
			ShowWindow(SW_SHOW);		//简单的显示主窗口完事儿
			ShowWindow(SW_SHOWNORMAL); //可以避免原来的最小化
		}
		break;
	}
	return 0;
}


BOOL CMynetsendDlg::Delete_Task_Icon() 
{	
	// 删除任务条中的图标 
	NOTIFYICONDATA nid; 
	nid.cbSize = sizeof(nid); 
	nid.hWnd = m_hWnd; 
	nid.uID = IDR_MAINFRAME; 
	return Shell_NotifyIcon(NIM_DELETE, &nid); 
}
// 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 CMynetsendDlg::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 drag
//  the minimized window.
HCURSOR CMynetsendDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CMynetsendDlg::OnButton1() 
{
	// TODO: Add your control notification handler code here
	CString Sip,Stxt,Sname;
	int i=0;
	GetDlgItemText(IDC_EDIT1,Stxt);
	GetDlgItemText(IDC_EDIT3,Sname);
	GetDlgItemText(IDC_COMBO1,Sip);
	

	sd(inet_addr(Sip),Sname,"",Stxt);


	

}

//打包发送函数,单ip可调用之。
int sd(unsigned long ip,CString from,CString machine,CString msg)
{
        int i, packet_size, fields_size, s;
        char packet[8192];
		struct	sockaddr_in addr;
	    WSADATA wsaData;
		WSAStartup(0x0202, &wsaData);
    	ZeroMemory(&addr, sizeof(addr));
		addr.sin_addr.s_addr = ip; //IP地址
		addr.sin_port = htons(135);
        addr.sin_family = AF_INET;
		ZeroMemory(packet, sizeof(packet));
        packet_size = 0;
        memcpy(&packet[packet_size], packet_header, 80);
        packet_size += 80;
        i = from.GetLength()+1;
        *(unsigned int *)(&field_header[0]) = i;
        *(unsigned int *)(&field_header[8]) = i;
        memcpy(&packet[packet_size], field_header, 12);
        packet_size += 12;
        strcpy(&packet[packet_size], from);
        packet_size += (((i - 1) >> 2) + 1) << 2; // padded to a multiple of 4
        i = machine.GetLength() + 1;
        *(unsigned int *)(&field_header[0]) = i;
        *(unsigned int *)(&field_header[8]) = i;
        memcpy(&packet[packet_size], field_header, 12);
        packet_size +=12;
        strcpy(&packet[packet_size], machine);
        packet_size += (((i - 1) >> 2) + 1) << 2; // padded to a multiple of 4
		i = msg.GetLength() + 1;
        *(unsigned int *)(&field_header[0]) = i;
        *(unsigned int *)(&field_header[8]) = i;
        memcpy(&packet[packet_size], field_header, 12);
        packet_size += 12;
        strcpy(&packet[packet_size], msg);
        packet_size += i;
        fields_size = packet_size - 80;
        *(unsigned int *)(&packet[40]) = time(NULL);
        *(unsigned int *)(&packet[74]) = fields_size;
		if ((s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1)
              return 0;
        if (sendto(s, packet, packet_size, 0, (struct sockaddr *)&addr, sizeof(addr)) == -1)
             return 0;
     
return 1;
}

BOOL CMynetsendDlg::DestroyWindow() 
{
	// TODO: Add your specialized code here and/or call the base class
	CString nowip;
	GetDlgItemText(IDC_COMBO1,nowip);	

	CIniFile ini;
	if (ini.OpenIniFileForWrite("mynetsend.ini"))		//打开list文件
	{
		ini.WriteSection("history");
		ini.WriteItemString("ip",nowip);	//写IP
		ini.CloseIniFile();	
	}

	Delete_Task_Icon();
	return CDialog::DestroyWindow();
}

void CMynetsendDlg::OnShowWindow(BOOL bShow, UINT nStatus) 
{
	CDialog::OnShowWindow(bShow, nStatus);
	
//	if (!bShow) ShowWindow(SW_SHOW);
	// TODO: Add your message handler code here
	
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -