NMHDR = Notify Message Handler

NMHDR结构解析与应用
本文详细介绍了Windows消息处理中的NMHDR结构,包括其成员变量的含义及如何使用该结构来处理控件的通知消息。此外,还提供了具体的代码示例,帮助读者更好地理解NMHDR的应用场景。
NMHDR   =   Notify   Message   Handler

 

NMHDR   结构包含下列成员: 
  typedef   struct   tagNMHDR   { 
  HWND   hwndFrom;     //   handle   of   control   sending   message 
  UINT   idFrom;//   identifier   of   control   sending   message 
  UINT   code;     //   notification   code;   see   below 
  }   NMHDR; 
  
  消息通过下面的宏定义之: 
  ON_NOTIFY(   wNotifyCode,   idControl,   memberFxn   ) 
  wNotifyCode   
  通知消息标识符代码,如   TBN_BEGINADJUST。   
  idControl   
  发送通知的控件的标识符。   
  memberFxn   
  接收到通知时调用的成员函数。   
  
  成员函数将用下列原型声明: 
  afx_msg   void   memberFxn(   NMHDR   *   pNotifyStruct,   LRESULT   *   result   ); 

 

在WM_NOTIFY中,lParam中放的是一个称为NMHDR结构的指针。在wParam中放的则是控件的ID。 

 

 

 

举例://得到选中的列

void   CMyView::OnColumnclick(NMHDR*   pNMHDR,   LRESULT*   pResult)   
  {       
  NM_LISTVIEW*   pNMListView   =   (NM_LISTVIEW*)pNMHDR; 
    

//得到选中的列      
  column=pNMListView->iSubItem;  

}

 

 

void CBillDlg2::OnGridEndEdit(NMHDR *pNotifyStruct ,LRESULT *pResult)//响应控件通知消息GVN_ENDLABELEDIT
{//自定义这个消息响应函数
  if(m_showbill)//控制显示对话框
  return ;
  /*typedef   struct   tagNMHDR  
  {  
          HWND     hwndFrom;  
          UINT     idFrom;  
          UINT     code;                   //   NM_   code  
  }      NMHDR; */  

  NM_GRIDVIEW *pitem=(NM_GRIDVIEW *)pNotifyStruct;
  if(pitem->iRow<0)
   return ;
 
  if(pitem->iColumn==m_nAmountCol||pitem->iColumn==m_nPriceCol)
  {
    CString  strAmount;
 CString  strPrice;
 strAmount=m_Grid.GetItemText(pitem->iRow,m_nAmountCol);
 strPrice=m_Grid.GetItemText(pitem->iRow,m_nPriceCol);
 int nAmount=atoi(strAmount);
 if(nAmount<=0)
  return;
 float fPrice=atof(strPrice);
 if(fPrice<=0)
 

 return;

 double dmoney=fPrice*(double)nAmount;
 CString  strMoney;
    strMoney.Format("%f",dmoney);
 m_Grid.SetItemText(pitem->iRow,m_nMoneyCol,strMoney);


  }

m_Grid.Refresh();

}

 

本文来自优快云博客,转载请标明出处:http://blog.youkuaiyun.com/markman101/archive/2008/12/23/3591355.aspx

/*SUNIX??ì¨?′?? v1.4*/ // SelectVectorDlg.cpp : implementation file // #include "stdafx.h" #include "sp_draw.h" #include "SelectVectorDlg.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // CVectorStatic CVectorStatic::CVectorStatic() { } CVectorStatic::~CVectorStatic() { } BEGIN_MESSAGE_MAP(CVectorStatic, CStatic) //{{AFX_MSG_MAP(CVectorStatic) ON_WM_PAINT() //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CVectorStatic message handlers void CVectorStatic::OnPaint() { CPaintDC dc(this); // device context for painting // TODO: Add your message handler code here CRect rt1; GetClientRect(&rt1); CBrush brush; brush.CreateSolidBrush(RGB(255,255,255)); CBrush* pOldBrush=dc.SelectObject(&brush); CPen pen; pen.CreatePen(PS_SOLID,1,RGB(192,192,192)); CPen *poldpen=dc.SelectObject(&pen); dc.Rectangle(&rt1); dc.SelectObject(pOldBrush); dc.SelectObject(poldpen); CSize sz=m_vector.GetSize(); if ((sz.cx==0)||(sz.cy==0)) return; float scaleX=1; float scaleY=1; if((rt1.Width()<sz.cx)||(rt1.Height()<sz.cy)) { if (rt1.Width()<sz.cx) scaleX=((float)rt1.Width())/(float)(sz.cx+1); if (rt1.Height()<sz.cy) scaleY=((float)rt1.Height())/(float)(sz.cy+1); float scale=__max(scaleX,scaleY); sz.cx=__min(rt1.Width()-10,(int)((float)(sz.cx-1)*scale)); sz.cy=__min(rt1.Height()-10,(int)((float)(sz.cy-1)*scale)); } else { if (rt1.Width()>sz.cx) scaleX=(int)((float)rt1.Width())/(float)(sz.cx+1); if (rt1.Height()>sz.cy) scaleY=(int)((float)rt1.Height())/(float)(sz.cy+1); float scale=__min(scaleX,scaleY); sz.cx=__min(rt1.Width()-10,(int)((float)(sz.cx-1)*scale)); sz.cy=__min(rt1.Height()-10,(int)((float)(sz.cy-1)*scale)); } rt1.SetRect(5,5,sz.cx,sz.cy); m_vector.Draw(&dc,rt1,rt1,0,FALSE); // Do not call CStatic::OnPaint() for painting messages } void CVectorStatic::SetVectFile(LPCSTR filepath) { m_vector.Load(filepath); Invalidate(TRUE); } ///////////////////////////////////////////////////////////////////////////// // CSelectVectorDlg property page IMPLEMENT_DYNCREATE(CSelectVectorDlg, CPropertyPage) CSelectVectorDlg::CSelectVectorDlg() : CPropertyPage(CSelectVectorDlg::IDD) { //{{AFX_DATA_INIT(CSelectVectorDlg) m_bFill = FALSE; m_vector = _T(""); //}}AFX_DATA_INIT } CSelectVectorDlg::~CSelectVectorDlg() { } void CSelectVectorDlg::DoDataExchange(CDataExchange* pDX) { CPropertyPage::DoDataExchange(pDX); //{{AFX_DATA_MAP(CSelectVectorDlg) DDX_Control(pDX, IDC_LIST1, m_list); DDX_Check(pDX, IDC_FILL, m_bFill); DDX_Text(pDX, IDC_EDIT1, m_vector); DDV_MaxChars(pDX, m_vector, 32); //}}AFX_DATA_MAP DDX_Control(pDX, IDC_TAB1, m_TabCtrl); } BEGIN_MESSAGE_MAP(CSelectVectorDlg, CPropertyPage) //{{AFX_MSG_MAP(CSelectVectorDlg) ON_LBN_SELCHANGE(IDC_LIST1, OnSelchangeList1) ON_BN_CLICKED(IDC_COLOR, OnColor) //}}AFX_MSG_MAP ON_NOTIFY(TCN_SELCHANGE, IDC_TAB1, &CSelectVectorDlg::OnTcnSelchangeTab1) END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CSelectVectorDlg message handlers BOOL CSelectVectorDlg::OnInitDialog() { CPropertyPage::OnInitDialog(); // TODO: Add extra initialization here m_ctrl_ToColor.SubclassDlgItem(IDC_COLOR,this); m_ctrlStatic.SubclassDlgItem(IDC_VECTOR,this); CString str; CStdioFile file; BOOL res = true; if (file.Open("C://sunpac//par//global//vec//vec.ini",CFile::modeRead|CFile::typeText)==FALSE) { res = false; } if(res) { res = ReadVecType(&file); } if(!res) { FillBox(); } else { int i = m_TabCtrl.GetCurSel(); TCITEM tcItem; char buffer[64]={0}; tcItem.pszText= buffer; tcItem.cchTextMax=32; tcItem.mask= TCIF_TEXT; m_TabCtrl.GetItem(i,&tcItem); CString type = buffer; FillBox(&file,type); } if(res) { file.Close(); } if(!m_vector.IsEmpty()) m_list.SelectString(0,m_vector); OnSelchangeList1(); m_ctrl_ToColor.m_Color=m_ToColor; m_ctrl_ToColor.Invalidate(); return TRUE; // return TRUE unless you set the focus to a control // EXCEPTION: OCX Property Pages should return FALSE } BOOL CSelectVectorDlg::ReadVecType(CStdioFile *pfile) { CString str; BOOL bl1,bl2; DWORD dwflag = 0; char strName[33]; int iret; int index = 0; //移动读指针到画面段落 if (SeekSection(pfile,"type")==FALSE) return FALSE; try { //读取下一行字符串 bl1=pfile->ReadString(str); if (!bl1) return FALSE; str.TrimLeft(); str.TrimRight(); //判断字符串是否是段落名 bl2=HitTestSectionID(str); if (bl2) return FALSE; while ((!bl2)&&(bl1)) { //读入画面名称和画面标志 iret=sscanf(str,"%x=%[^;]33s",&dwflag,strName);//%[^=]s if (iret==2) { m_TabCtrl.InsertItem(index, strName); index++; } //读取下一行字符串 bl1=pfile->ReadString(str); if (!bl1) break; str.TrimLeft(); str.TrimRight(); //判断字符串是否是段落名 bl2=HitTestSectionID(str); if (bl2) break; } return TRUE; } catch(CFileException* e) { e->Delete(); return FALSE; } } BOOL CSelectVectorDlg::SeekSection(CStdioFile *pfile, LPCSTR strSection) { CString str; BOOL bret; assert(strSection!=NULL); assert(pfile!=NULL); try { pfile->SeekToBegin(); bret=pfile->ReadString(str); while (bret) { if (HitTestSectionID(str)==TRUE) { if ((HitTestSectionName(str,strSection))==TRUE) return TRUE; } bret=pfile->ReadString(str); } return FALSE; } catch(CFileException* e) { e->Delete(); return FALSE; } } BOOL CSelectVectorDlg::HitTestSectionID(LPCSTR str) { CString s; assert(str!=NULL); s=str; s.TrimLeft(); if (s.IsEmpty()) return FALSE; if ((s[0]=='[')&&(s.FindOneOf("]")>0)) return TRUE; return FALSE; } BOOL CSelectVectorDlg::HitTestSectionName(LPCSTR strSection, LPCSTR strName) { assert(strSection!=NULL); assert(strName!=NULL); CString str="["; str=str+strName+"]"; if (_strnicmp(strSection,str,str.GetLength())==0) return TRUE; else return FALSE; } void CSelectVectorDlg::SetContent(LPCSTR vectorname, BOOL bFill, COLORREF color) { m_ToColor=color; m_bFill=bFill; m_vector=vectorname; } void CSelectVectorDlg::GetContent(CString &vectorname, BOOL &bFill, COLORREF &color) { color=m_ToColor; bFill=m_bFill; vectorname=m_vector; } void CSelectVectorDlg::FillBox() { CFileFind vectordir; CString file=theApp.GetDirectory(); CString vectorname; file+="global\\vec\\*.vec"; m_list.ResetContent(); if (vectordir.FindFile(file)) { while (vectordir.FindNextFile()) { vectorname=vectordir.GetFileTitle(); m_list.AddString(vectorname); } vectorname=vectordir.GetFileTitle(); m_list.AddString(vectorname); vectordir.Close(); } } void CSelectVectorDlg::FillBox(CStdioFile *pfile,CString type) { m_list.ResetContent(); CString str; BOOL bl1,bl2; DWORD dwflag = 0; char strName[33]; int iret; //移动读指针到画面段落 if (SeekSection(pfile,type)==FALSE) return; try { //读取下一行字符串 bl1=pfile->ReadString(str); if (!bl1) return; str.TrimLeft(); str.TrimRight(); //判断字符串是否是段落名 bl2=HitTestSectionID(str); if (bl2) return; while ((!bl2)&&(bl1)) { //读入画面名称和画面标志 iret=sscanf(str,"%x=%[^;]33s",&dwflag,strName);//%[^=]s if (iret==2) { m_list.AddString(strName); } //读取下一行字符串 bl1=pfile->ReadString(str); if (!bl1) break; str.TrimLeft(); str.TrimRight(); //判断字符串是否是段落名 bl2=HitTestSectionID(str); if (bl2) break; } return; } catch(CFileException* e) { e->Delete(); return; } } void CSelectVectorDlg::OnSelchangeList1() { // TODO: Add your control notification handler code here int pos=m_list.GetCurSel(); CString vectorname; if (pos>=0) { m_list.GetText(pos,vectorname); m_vector=vectorname; CString file; file.Format("%sglobal\\vec\\%s.vec",theApp.GetDirectory(),vectorname); m_ctrlStatic.SetVectFile(file); } UpdateData(FALSE); } void CSelectVectorDlg::OnColor() { // TODO: Add your control notification handler code here if (m_ToColor==m_ctrl_ToColor.m_Color) return; m_ToColor=m_ctrl_ToColor.m_Color; } void CSelectVectorDlg::OnTcnSelchangeTab1(NMHDR *pNMHDR, LRESULT *pResult) { int i = m_TabCtrl.GetCurSel(); TCITEM tcItem; char buffer[64]={0}; tcItem.pszText= buffer; tcItem.cchTextMax=32; tcItem.mask= TCIF_TEXT; m_TabCtrl.GetItem(i,&tcItem); CStdioFile file; if (file.Open("C://sunpac//par//global//vec//vec.ini",CFile::modeRead|CFile::typeText)==TRUE) { FillBox(&file,buffer); if(!m_vector.IsEmpty()) m_list.SelectString(0,m_vector); OnSelchangeList1(); file.Close(); } *pResult = 0; }
07-18
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值