📄 dlgtrydlg.cpp
字号:
// DlgTryDlg.cpp : implementation file
//
#include "stdafx.h"
#include "DlgTry.h"
#include "DlgTryDlg.h"
#include <algorithm>
using namespace std;
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// 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()
/////////////////////////////////////////////////////////////////////////////
// CDlgTryDlg dialog
CDlgTryDlg::CDlgTryDlg(CWnd* pParent /*=NULL*/)
: CDialog(CDlgTryDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CDlgTryDlg)
m_nTry = 0;
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CDlgTryDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CDlgTryDlg)
DDX_Control(pDX, IDC_LIST_TRY, m_lstTry);
DDX_Control(pDX, IDC_EDIT_Try, m_ctlTry);
DDX_Text(pDX, IDC_EDIT_Try, m_nTry);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CDlgTryDlg, CDialog)
//{{AFX_MSG_MAP(CDlgTryDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(ID_OK, OnBtnOk)
ON_NOTIFY(NM_CUSTOMDRAW, IDC_LIST_TRY, OnCustomDrawTry)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CDlgTryDlg message handlers
BOOL CDlgTryDlg::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
//利用API函数创建控件
//HINSTANCE hInstance;
//CWinApp *pApp = AfxGetApp();
//hInstance =pApp->m_hInstance;// (HINSTANCE)AfxGetInstanceHandle();
HWND hEditWnd = CreateWindow("EDIT", NULL, WS_CHILD | WS_VISIBLE,
10,10,100,25, m_hWnd,(HMENU)1234,
AfxGetInstanceHandle(),NULL);
m_myEdit.SubclassDlgItem(1234,this);
CRect rect(200,10,300,30);
m_Edit.Create(WS_CHILD | WS_VISIBLE, rect, this,1235);
CRect btnRect(10,100,160,142);
m_btnTry.Create("Just For a try!",WS_CHILD | WS_VISIBLE |
BS_OWNERDRAW | BS_ICON | BS_LEFT , btnRect, this, IDC_btnTRY);
m_btnTry.SetIcon(AfxGetApp()->LoadIcon(IDI_ICON_TRY));
m_btnTry.ShowWindow(SW_SHOWNORMAL);
//m_btnTry.SetWindowText("wokao!");
//创建图象列表
m_imgList.Create(IDB_BITMAP1, 45, 1, RGB(0,255,0));
m_imgList.Add(AfxGetApp()->LoadIcon(IDI_ICON1));
m_imgList.Add(AfxGetApp()->LoadIcon(IDI_ICON2));
m_imgList.Add(AfxGetApp()->LoadIcon(IDI_ICON3));
m_lstTry.SetImageList(&m_imgList, TVSIL_NORMAL);
m_lstTry.InsertItem(1,"Clock1",1);
m_lstTry.InsertItem(2,"Clock2",2);
m_lstTry.InsertItem(3,"Clock3",3);
return TRUE; // return TRUE unless you set the focus to a control
}
void CDlgTryDlg::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 CDlgTryDlg::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 CDlgTryDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CDlgTryDlg::OnOK()
{
// TODO: Add extra validation here
//CDialog::OnOK();
}
BOOL CDlgTryDlg::PreTranslateMessage(MSG* pMsg)
{
// TODO: Add your specialized code here and/or call the base class
//
if(pMsg->message==WM_KEYDOWN)
{
CEdit *pEdit=static_cast<CEdit*>(GetDlgItem(IDC_EDIT_Try));
if(pMsg->wParam==VK_RETURN)
{
UpdateData(TRUE);
if(pMsg->hwnd==pEdit->GetSafeHwnd())
{
if(m_nTry<0)
{
MessageBox("输入数字要大于0!");
m_ctlTry.SetFocus();
m_ctlTry.SetSel(0,-1);
}
}
}
}
return CDialog::PreTranslateMessage(pMsg);
}
void CDlgTryDlg::OnBtnOk()
{
// TODO: Add your control notification handler code here
m_btnTry.SetWindowText("Try again!");
}
void CDlgTryDlg::OnCustomDrawTry(NMHDR* pNMHDR, LRESULT* pResult)
{
NMLVCUSTOMDRAW* pLstDraw = (NMLVCUSTOMDRAW*)pNMHDR;
*pResult = 0;
switch(pLstDraw->nmcd.dwDrawStage)
{
case CDDS_PREPAINT:
*pResult = CDRF_NOTIFYITEMDRAW;
return;
case CDDS_ITEMPREPAINT:
pLstDraw->clrText = RGB(50,200,180);
*pResult = CDRF_NOTIFYPOSTPAINT;
return;
case CDDS_ITEMPOSTPAINT:
LVITEM rItem;
//Item number. What is contained in this member will depend on the type
//of control that is sending the notification.
//See the NM_CUSTOMDRAW notification reference for
//the specific control to determine what, if anything,
//is contained in this member.
int nItem = (int)(pLstDraw->nmcd.dwItemSpec);
ZeroMemory(&rItem, sizeof(LVITEM));
rItem.mask = LVIF_IMAGE | LVIF_STATE;
rItem.iItem = nItem;
rItem.stateMask = LVIS_SELECTED;
m_lstTry.GetItem(&rItem); //取得取得当前条目的图片及状态信息
CDC* pDc = CDC::FromHandle(pLstDraw->nmcd.hdc);
CRect rcIcon;
//取得图片所在矩形区域
m_lstTry.GetItemRect(nItem, &rcIcon, LVIR_ICON);
if(rItem.state & LVIS_SELECTED)
{
pDc->FillSolidRect(&rcIcon, ::GetSysColor(COLOR_WINDOW));
//如果是选择状态,偏移正常位置重画图标
rcIcon.OffsetRect(-2, -1);
m_imgList.Draw(pDc, rItem.iImage, rcIcon.TopLeft(),
ILD_TRANSPARENT);
}
else
{
//恢复图片正常位置
//rcIcon.OffsetRect(6, 3);
pDc->FillSolidRect(&rcIcon, ::GetSysColor(COLOR_WINDOW));
rcIcon.OffsetRect(6, 3);
m_imgList.Draw(pDc, rItem.iImage, rcIcon.TopLeft(),
ILD_TRANSPARENT);
}
*pResult = CDRF_DODEFAULT;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -