SimpleTextTransferDlg.cpp
上传用户:xsxdsb
上传日期:2009-12-14
资源大小:672k
文件大小:5k
- // SimpleTextTransferDlg.cpp : implementation file
- //
- #include "stdafx.h"
- #include "SimpleTextTransfer.h"
- #include "SimpleTextTransferDlg.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #endif
- // CAboutDlg dialog used for App About
- class CAboutDlg : public CDialog
- {
- public:
- CAboutDlg();
- // Dialog Data
- enum { IDD = IDD_ABOUTBOX };
- protected:
- virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
- // Implementation
- protected:
- DECLARE_MESSAGE_MAP()
- public:
- };
- CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
- {
- }
- void CAboutDlg::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
- }
- BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
-
- END_MESSAGE_MAP()
- // CSimpleTextTransferDlg dialog
- CSimpleTextTransferDlg::CSimpleTextTransferDlg(CWnd* pParent /*=NULL*/)
- : CDialog(CSimpleTextTransferDlg::IDD, pParent)
- {
- m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
- }
- void CSimpleTextTransferDlg::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
- DDX_Control(pDX, ID_EDTTOCLIPBOARD, m_edtToClipboard);
- DDX_Control(pDX, ID_EDTFROMCLIPBOARD, m_edtFromClipboard);
- }
- BEGIN_MESSAGE_MAP(CSimpleTextTransferDlg, CDialog)
- ON_WM_SYSCOMMAND()
- ON_WM_PAINT()
- ON_WM_QUERYDRAGICON()
- //}}AFX_MSG_MAP
- ON_BN_CLICKED(ID_BTNCOPY, OnBnClickedBtncopy)
- ON_BN_CLICKED(ID_BTNCUT, OnBnClickedBtncut)
- ON_BN_CLICKED(ID_BTNPASTE, OnBnClickedBtnpaste)
- END_MESSAGE_MAP()
- // CSimpleTextTransferDlg message handlers
- BOOL CSimpleTextTransferDlg::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
- m_edtToClipboard.SetWindowText("Sample data to copy to Clipboard");
-
- return TRUE; // return TRUE unless you set the focus to a control
- }
- void CSimpleTextTransferDlg::OnSysCommand(UINT nID, LPARAM lParam)
- {
- if ((nID & 0xFFF0) == IDM_ABOUTBOX)
- {
- CAboutDlg dlgAbout;
- dlgAbout.DoModal();
- }
- else
- {
- CDialog::OnSysCommand(nID, lParam);
- }
- }
- // 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 CSimpleTextTransferDlg::OnPaint()
- {
- if (IsIconic())
- {
- CPaintDC dc(this); // device context for painting
- SendMessage(WM_ICONERASEBKGND, reinterpret_cast<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 function to obtain the cursor to display while the user drags
- // the minimized window.
- HCURSOR CSimpleTextTransferDlg::OnQueryDragIcon()
- {
- return static_cast<HCURSOR>(m_hIcon);
- }
- void CSimpleTextTransferDlg::OnBnClickedBtncopy()
- {
- if (UpdateData())
- {
- //获得数据
- CString strData;
- m_edtToClipboard.GetWindowText(strData);
-
- //打开系统剪贴板
- if (OpenClipboard())
- {
- //使用之前,清空系统剪贴板
- EmptyClipboard();
-
- //分配一内存,大小等于要拷贝的字符串的大小,返回一的内存控制句柄
- HGLOBAL hClipboardData;
- hClipboardData = GlobalAlloc(GMEM_DDESHARE,
- strData.GetLength()+1);
-
- //内存控制句柄加锁,返回值为指向那内存控制句柄所在的特定数据格式的指针
- char * pchData;
- pchData = (char*)GlobalLock(hClipboardData);
-
- //将本地变量的值赋给全局内存
- strcpy(pchData, LPCSTR(strData));
-
- //给加锁的全局内存控制句柄解锁
- GlobalUnlock(hClipboardData);
-
- //通过全局内存句柄将要拷贝的数据放到剪贴板上
- SetClipboardData(CF_TEXT,hClipboardData);
-
- //使用完后关闭剪贴板
- CloseClipboard();
- }
- }
- }
- void CSimpleTextTransferDlg::OnBnClickedBtncut()
- {
- //将数据放到剪贴板上,同时清空编辑框内的数据
- OnBnClickedBtncopy();
- m_edtToClipboard.SetWindowText("");
- }
- void CSimpleTextTransferDlg::OnBnClickedBtnpaste()
- {
- //打开系统剪贴板
- if (OpenClipboard())
- {
- //判断剪贴板上的数据是否是指定的数据格式
- if (::IsClipboardFormatAvailable(CF_TEXT)
- || ::IsClipboardFormatAvailable(CF_OEMTEXT))
- {
- // Retrieve the Clipboard data (specifying that
- // we want ANSI text (via the CF_TEXT value).
- //从剪贴板上获得数据
- HANDLE hClipboardData = GetClipboardData(CF_TEXT);
-
- //通过给内存句柄加锁,获得指向指定格式数据的指针
- char *pchData = (char*)GlobalLock(hClipboardData);
-
- //本地变量获得数据
- CString strFromClipboard = pchData;
- m_edtFromClipboard.SetWindowText(strFromClipboard);
-
- //给内存句柄解锁
- GlobalUnlock(hClipboardData);
- }
- else
- {
- AfxMessageBox("There is no text (ANSI) data on the Clipboard.");
- }
-
- //使用完后关闭剪贴板
- CloseClipboard();
- }
- }