0

关于投票
我的第2个VC例子

#include <afxwin.h>
#include "resource.h"


//主窗口类//
class MyFrameWindow:public CFrameWnd
{
public:

//在窗口建立之前先执行这个函数//
BOOL PreCreateWindow(CREATESTRUCT& cs)
{
 //载入menu资源并且将它指定为窗口的主菜单//
 cs.hMenu = LoadMenu(NULL,MAKEINTRESOURCE(IDR_MAINMENU));
 return CFrameWnd::PreCreateWindow(cs);
}
afx_msg void OnFileExit()    //消息响应 file/exit //
{
 PostMessage(WM_CLOSE);   //发送关闭窗口的消息给自己//
}

//重点//
afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
//输入焦点//
afx_msg void OnSetCursor(CWnd *pOldWnd);
afx_msg void OnKillFocus(CWnd* pNewWnd);
//放开鼠标左键//
afx_msg void OnLButtonUp(UINT nFlags,CPoint point);
//输入字符//
afx_msg void OnChar(UINT nChar,UINT nRepCnt,UINT nFlags);
//设定鼠标光标//
afx_msg BOOL OnSetCursor(CWnd *pWnd,UINT nHitTest,UINT message);

CPoint caretPos;    //记录文本光标的位置//
HCURSOR mouseCur;   //鼠标的光标资源//

DECLARE_MESSAGE_MAP()  //声明消息响应表//

};

//消息响应表//
BEGIN_MESSAGE_MAP(MyFrameWindow,CFrameWnd)
ON_COMMAND(ID_FILE_EXIT,OnFileExit)
//建立新窗口//
ON_WM_CREATE()
//输入焦点//
ON_WM_SETFOCUS()
ON_WM_KILLFOCUS()
//放开鼠标左键//
ON_WM_LBUTTONUP()
//输入字符//
ON_WM_CHAR()
//设定鼠标光标//
ON_WM_SETFOCUS()
END_MESSAGE_MAP()

int MyFrameWindow::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
 //先调用父类处理这个消息//
 int apiRet;
 apiRet = CFrameWnd::OnCreate(lpCreateStruct);
 //以下是我们自己的程序//
 caretPos.x = 0;    //重置文本光标的位置//
 caretPos.y = 0;

 //载入系统默认的I型鼠标光标//
 mouseCur = AfxGetApp()->LoadStandardCursor(IDC_IBEAM);

 return apiRet;
}


//取得输入的焦点//
void MyFrameWindow::OnSetFocus(CWnd* pOldWnd)
{
 //先调用父类处理这个消息//
 CFrameWnd::OnSetFocus(pOldWnd);
 //一下是我们自己的程序//
 //建立文本光标大小是 3*20//
 ::CreateCaret(*this,(HBITMAP)NULL,3,20);
 //设定文本光标位置//
 SetCaretPos(caretPos);
 //显示文本光标//
 ShowCaret();
}

//失去输入焦点//
void MyFrameWindow::OnKillFocus(CWnd* pNewWnd)
{
 //先调用父类处理这个消息//
 CFrameWnd::OnKillFocus(pNewWnd);

 //隐藏文本光标//
 HideCaret();
 //删除文本光标//
 DestroyCaret();
}

//放开鼠标左键//
void MyFrameWindow::OnLButtonUp(UINT nFlags,CPoint point)
{
 //先调用父类处理这个消息//
 CFrameWnd::OnLButtonUp(nFlags,point);

 caretPos = point;
 //将文本光标移到鼠标所单击的位置//
 SetCaretPos(caretPos);
}

//输入字符//
void MyFrameWindow::OnChar(UINT OnChar,UINT nRepCnt,UINT nFlags)
{
 //先调用父类处理这个消息//
 CFrameWnd::OnChar(OnChar,nRepCnt,nFlags);

 TCHAR str[2];
 CSize size;

 //构造只有一个字符的字符串//
 str[0] = (TCHAR)OnChar;
 str[1] = 0;

 HideCaret();   //隐藏文本光标//
 CClientDC dc(this); //建立工作区的DC//
 dc.TextOut(caretPos.x,caretPos.y,str);  //显示使用者键入的字符//
 //计算字符串显示在屏幕上的长宽点数//
 size = dc.GetTextExtent(str,1);
 //将文本光标移到输出字符串后的右边//
 caretPos.x += size.cx;
 SetCaretPos(caretPos);
 //再恢复文本光标//
 ShowCaret();
}

BOOL MyFrameWindow::OnSetFocus(CWnd* pWnd,UINT nHitTest,UINT message)
{
 BOOL apiRet;
 //先调用父类处理这个消息//
 apiRet = CFrameWnd::OnSetFocus(pWnd,nHitTest,message);
 //设定鼠标光标//
 if((apiRet==FALSE)&&(nHitTest==HTCLIENT))
  ::SetCursor(mouseCur);
 return apiRet;

}

//应用程序类//
class MainApp:public CWinApp
{
public:

 MainApp():CWinApp(appTitle)   //设定应用程序名称//
 {

 }
 BOOL InitInstance()
 {
  HICON hIcon;    //ICON资源的handle//
  hIcon = LoadIcon(IDI_MAINICON); //载入ICON资源//

  CFrameWnd* MyFrame = new MyFrameWindow; //产生主窗口对象//
  m_pMainWnd = MyFrame;
  MyFrame->Create(NULL,appTitle); //建立主窗口//

  MyFrame->SetIcon(hIcon,TRUE);  //设定窗口的大ICON//
  MyFrame->SetIcon(hIcon,FALSE); //设定窗口的小ICON//
  MyFrame->ShowWindow(SW_SHOW);  //显示主窗口//
  return TRUE;
 }

 static const TCHAR appTitle[];
}theApp;

const TCHAR MainApp::appTitle[] = "打字模拟

系统分类: 单片机
用户分类: VC++学习
标签: VC
来源: 原创
发表评论 阅读全文(658) | 回复(0)

0

关于投票
我的第一个VC例子注解

void CEx7_2Dlg::OnCleartext()
{
 // TODO: Add your control notification handler code here
 GetDlgItem(IDC_MYTEXT)->SetWindowText("");  //获取ID控件
 
}

void CEx7_2Dlg::OnCopyText()
{
 // TODO: Add your control notification handler code here
 CString str;
 CListBox* lbx;

 GetDlgItem(IDC_MYTEXT)->GetWindowText(str);
 lbx=(CListBox*)GetDlgItem(IDC_TEXTLIST);  //转化类型
 lbx->AddString(str);
 
}

void CEx7_2Dlg::OnDblclkTextlist()      //列表框双击
{
 // TODO: Add your control notification handler code here
 CListBox* lbx;
 CString str;

 lbx=(CListBox*)GetDlgItem(IDC_TEXTLIST);
 lbx->GetText(lbx->GetCurSel(),str);     //返回项目编号,LB_ERR,
 GetDlgItem(IDC_MYTEXT)->SetWindowText(str);


}

void CEx7_2Dlg::OnClearlist()
{
 // TODO: Add your control notification handler code here
 CListBox *lbx;

 lbx=(CListBox*)GetDlgItem(IDC_TEXTLIST);
 lbx->ResetContent();         //清除列表框数据
 
}

BOOL CEx7_2App::InitInstance()
{
 // Standard initialization
 // If you are not using these features and wish to reduce the size
 //  of your final executable, you should remove from the following
 //  the specific initialization routines you do not need.

 CEx7_2Dlg dlg;
 m_pMainWnd = &dlg;             //CEx7_2Dlg作为主窗口
 int nResponse = dlg.DoModal(); //启动对话框
 if (nResponse == IDOK)
 {
  // TODO: Place code here to handle when the dialog is
  //  dismissed with OK
 }
 else if (nResponse == IDCANCEL)
 {
  // TODO: Place code here to handle when the dialog is
  //  dismissed with Cancel
 }

 // Since the dialog has been closed, return FALSE so that we exit the
 //  application, rather than start the application's message pump.
 return FALSE;
}

class CEx7_2Dlg : public CDialog
{
// Construction
public:
 CEx7_2Dlg(CWnd* pParent = NULL); // 父窗口指针

// Dialog Data
 //{{AFX_DATA(CEx7_2Dlg)
 enum { IDD = IDD_EX7_2_DIALOG };       //对话框资源的ID
  // NOTE: the ClassWizard will add data members here
 //}}AFX_DATA

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

// Implementation
protected:
 HICON m_hIcon;

 // Generated message map functions
 //{{AFX_MSG(CEx7_2Dlg)
 virtual BOOL OnInitDialog();     //成员函数
 afx_msg void OnPaint();
 afx_msg HCURSOR OnQueryDragIcon();
 afx_msg void OnCleartext();
 afx_msg void OnCopyText();
 afx_msg void OnDblclkTextlist();
 afx_msg void OnClearlist();
 //}}AFX_MSG
 DECLARE_MESSAGE_MAP()           //消息响应列表
};

CEx7_2Dlg::CEx7_2Dlg(CWnd* pParent /*=NULL*/)
 : CDialog(CEx7_2Dlg::IDD, pParent)       //调用父类CDialog的构造函数
{
 //{{AFX_DATA_INIT(CEx7_2Dlg)
  // 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);        //哪个CWinApp对象的指针,载入图标
}

BOOL CEx7_2Dlg::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
}

BEGIN_MESSAGE_MAP(CEx7_2Dlg, CDialog)     //消息列表
 //{{AFX_MSG_MAP(CEx7_2Dlg)
 ON_WM_PAINT()
 ON_WM_QUERYDRAGICON()
 ON_BN_CLICKED(IDC_CLEARTEXT, OnCleartext)
 ON_BN_CLICKED(IDC_COPY, OnCopyText)
 ON_LBN_DBLCLK(IDC_TEXTLIST, OnDblclkTextlist)
 ON_BN_CLICKED(IDC_CLEARLIST, OnClearlist)
 //}}AFX_MSG_MAP
END_MESSAGE_MAP()

 


 

系统分类: 单片机
用户分类: VC++学习
标签: VC
来源: 原创
发表评论 阅读全文(1187) | 回复(0)
总共 , 当前 /