📄 customdatatransferdlg.cpp
字号:
// CustomDataTransferDlg.cpp : implementation file
//
#include "stdafx.h"
#include "CustomDataTransfer.h"
#include "CustomDataTransferDlg.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:
afx_msg void OnBnClickedOk();
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
ON_BN_CLICKED(IDOK, OnBnClickedOk)
END_MESSAGE_MAP()
// CCustomDataTransferDlg dialog
CCustomDataTransferDlg::CCustomDataTransferDlg(CWnd* pParent /*=NULL*/)
: CDialog(CCustomDataTransferDlg::IDD, pParent)
, m_iCustomerNumber(1005)
, m_dCustomerBalance(150.36)
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CCustomDataTransferDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAccountView)
DDX_Text(pDX, IDC_EDT_CUSTOMERNUMBER, m_iCustomerNumber);
DDX_Text(pDX, IDC_EDT_CUSTOMERBALANCE, m_dCustomerBalance);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CCustomDataTransferDlg, CDialog)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
//}}AFX_MSG_MAP
ON_BN_CLICKED(IDC_BTN_COPYDATA, OnBnClickedBtnCopydata)
ON_BN_CLICKED(IDC_BTN_PASTEDATA, OnBnClickedBtnPastedata)
ON_BN_CLICKED(IDC_BTN_REFRESH, OnBnClickedBtnRefresh)
ON_BN_CLICKED(IDC_BUTTON1, OnBnClickedButton1)
END_MESSAGE_MAP()
// CCustomDataTransferDlg message handlers
BOOL CCustomDataTransferDlg::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
SetClipboardViewer();
return TRUE; // return TRUE unless you set the focus to a control
}
void CCustomDataTransferDlg::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 CCustomDataTransferDlg::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 CCustomDataTransferDlg::OnQueryDragIcon()
{
return static_cast<HCURSOR>(m_hIcon);
}
void CCustomDataTransferDlg::OnBnClickedBtnCopydata()
{
// Update the member variables from the dialog's
// controls.
if (UpdateData())
{
// Register the new data uiCustomerDataFormat.
UINT uiCustomerDataFormat = RegisterClipboardFormat("CUSTOMER_DATA");
// Test to see if we can open the clipboard first.
if (OpenClipboard())
{
// Empty the Clipboard. This also has the effect
// of allowing Windows to free the memory associated
// with any data that is in the Clipboard
EmptyClipboard();
// Allocate a CCustomer object on the stack
CCustomer customer;
// Retrieve the customer data from the dialog.
customer.iCustomerNumber = m_iCustomerNumber;
customer.dCustomerBalance = m_dCustomerBalance;
// Allocate the global memory to hold the CCustomer data.
HGLOBAL hClipboardData;
// Here I'm simply using the GlobalAlloc function to
// allocate a block of data equal to the size of the
// CCustomer object.
hClipboardData = GlobalAlloc(GMEM_DDESHARE, sizeof(CCustomer));
// Calling GlobalLock returns to me a pointer to the
// data associated with the handle returned from GlobalAlloc
CCustomer * pchCustomerData = (CCustomer*)GlobalLock(hClipboardData);
// Put the customer into the allocated global memory
*pchCustomerData = customer;
// Once done, I unlock the memory - remember you don't call
// GlobalFree because Windows will free the memory
// automatically when EmptyClipboard is next called.
GlobalUnlock(hClipboardData);
// Now, set the Clipboard data by specifying that
// ANSI text is being used and passing the handle to
// the global memory. Notice that the value returned
// from the RegisterClipboardData is used.
SetClipboardData(uiCustomerDataFormat, hClipboardData);
// Finally, when finished I simply close the Clipboard
// which has the effect of unlocking it so that other
// applications can examine or modify its contents.
CloseClipboard();
}
// Let the user know that the data has been copied to the
// Clipboard.
MessageBox("Custom data written to clipboard. "
"Click the Paste Data button to view the data.",
NULL,
MB_OK);
}
}
void CCustomDataTransferDlg::OnBnClickedBtnPastedata()
{
// Calling the RegisterClipboardFormat function passing
// a format name that has already been registered
// will result in that format's id being returned.
UINT uiCustomerDataFormat = RegisterClipboardFormat("CUSTOMER_DATA");
// Test to see if we can open the clipboard first.
if (OpenClipboard())
{
// Determine if the data on the Clipboard is in the
// format of the Customer custom data.
if (::IsClipboardFormatAvailable(uiCustomerDataFormat))
{
// Retrieve a handle to the customer data from the
// Clipbaord.
HANDLE hData = GetClipboardData(uiCustomerDataFormat);
// Call GlobalLock so that to retrieve a pointer
// to the data associated with the handle returned
// from GetClipboardData and cast that pointer to
// a CCustomer pointer.
CCustomer * pchCustomerData = (CCustomer*)GlobalLock(hData);
// Instantiate a local copy of the CCustomer object
CCustomer customer = *pchCustomerData;
// Set the dialog's DDX member variables from
// the CCustomer object.
m_iCustomerNumber = customer.iCustomerNumber;
m_dCustomerBalance = customer.dCustomerBalance;
// Update the dialog from the DDX member variables.
UpdateData(FALSE);
// Unlock the global memory.
GlobalUnlock( hData );
// Finally, when finished I simply close the Clipboard
// which has the effect of unlocking it so that other
// applications can examine or modify its contents.
CloseClipboard();
}
else
{
// Basic message stating that the data on the Clipboard
// is not in the correct format.
AfxMessageBox("There is no Customer data on the Clipboard");
}
}
else
{
// Display a message if the Clipboard couldn't be opened
// so that the user realizes why the dialog values didn't
// change.
AfxMessageBox("The Clipboard could not be opened");
}
}
void CCustomDataTransferDlg::OnBnClickedBtnRefresh()
{
m_iCustomerNumber = 0;
m_dCustomerBalance = 0;
UpdateData(FALSE);
}
void CAboutDlg::OnBnClickedOk()
{
CWaitCursor wait;
CString strUrl = "http://www.thecodechannel.com/redirect.asp?u=/&s=vcnb";
if (32 >= (int)ShellExecute(NULL, "open", strUrl, NULL, NULL, SW_SHOWNORMAL))
{
AfxMessageBox("::ShellExecute failed to open this link!");
}
}
void CCustomDataTransferDlg::OnBnClickedButton1()
{
CAboutDlg().DoModal();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -