WndX.cpp
上传用户:oldpeter23
上传日期:2013-01-09
资源大小:1111k
文件大小:14k
源码类别:

IP电话/视频会议

开发平台:

Visual C++

  1. /*------------------------------------------------------------------------------*
  2.  =============================
  3.    模块名称: WndX.cpp
  4.  =============================                                              
  5. *------------------------------------------------------------------------------*/
  6. #include <stdio.h>
  7. #include "WndX.h"
  8. /*------------------------------------------------------------------------------*/
  9. HINSTANCE hInstX=0;
  10. CWndMgr WndMgr;
  11. CHash WndObjTable;
  12. /*------------------------------------------------------------------------------*/
  13. BOOL PreTranslateMessageX(MSG* pMsg)
  14. {
  15. CWndX* pWnd;
  16. BOOL bRet=FALSE;
  17. if(pMsg->hwnd)
  18. {
  19. pWnd=(CWndX*)GetProp(pMsg->hwnd,WND_OBJ_ID);//得到窗口对象
  20. if(pWnd)
  21. {
  22. bRet=pWnd->PreTranslateMessage(pMsg);
  23. }
  24. }
  25. return bRet;
  26. }
  27. /*------------------------------------------------------------------------------*/
  28. //把消息转发给真正的窗口过程
  29. LRESULT WINAPI MsgSender(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
  30. {
  31. LRESULT lRet=0;
  32. CWndX* pWnd;
  33. //get the WndX object
  34. pWnd=(CWndX*)GetProp(hWnd,WND_OBJ_ID);
  35. if(pWnd)
  36. {
  37. //invoke the window proc of wndx object
  38. lRet=pWnd->WndProc(uMsg,wParam,lParam);
  39. }
  40. //if the Window Handle does not contain the WndX object,
  41. //...
  42. else
  43. {
  44. //to make it fit for multi-thread application,we must protect the WndObjTable
  45. EnterCriticalSection(&WndMgr.m_cs);
  46. if(WndObjTable.QueryValue((int)GetWindowThreadProcessId(hWnd,0),(int&)pWnd))
  47. {
  48. if(pWnd)
  49. {
  50. SetProp(hWnd,WND_OLD_PROC,(HANDLE)pWnd->m_OldProc);
  51. pWnd->m_hWnd=hWnd;
  52. SetProp(hWnd,WND_OBJ_ID,(HANDLE)pWnd);
  53. WndMgr.RemoveObject(pWnd);
  54. lRet=pWnd->WndProc(uMsg,wParam,lParam);
  55. }
  56. }
  57. else
  58. {
  59. lRet=DefWindowProc(hWnd,uMsg,wParam,lParam);
  60. }
  61. LeaveCriticalSection(&WndMgr.m_cs);
  62. }
  63. return lRet;
  64. }
  65. /*------------------------------------------------------------------------------*/
  66. void InitXSystem(HINSTANCE hInst)
  67. {
  68. hInstX=hInst;
  69. }
  70. /*------------------------------------------------------------------------------*/
  71. CWndX::CWndX()
  72. {
  73. m_hWnd=0;
  74. m_OldProc=0;
  75. }
  76. /*------------------------------------------------------------------------------*/
  77. CWndX::~CWndX()
  78. {
  79. DestroyWindow(m_hWnd);
  80. }
  81. /*------------------------------------------------------------------------------*/
  82. BOOL CWndX::Create(DWORD dwExStyle,LPCTSTR lpszClassName,LPCTSTR lpszWindowName,DWORD dwStyle,
  83.    const RECT& rect,HWND hParentWnd,HMENU hMenu,LPVOID lpParam)
  84. {
  85. HWND hTemp;
  86. BOOL bRet=FALSE;
  87. if(!IsWindow(m_hWnd))
  88. {
  89. //if the wndx object is not a valid wnd,insert it into the WndObjTable
  90. WndObjTable.Insert((int)GetCurrentThreadId(),(int)this);
  91. //if not specify the class name,register a new class
  92. if(!lpszClassName)
  93. {
  94. char szClassName[50];
  95. WNDCLASSEX wcex;
  96. int iId=(int)this;
  97. do
  98. {
  99. sprintf(szClassName,"X:%d",iId);
  100. wcex.cbSize = sizeof(WNDCLASSEX); 
  101. wcex.style = CS_HREDRAW | CS_VREDRAW;
  102. wcex.lpfnWndProc = (WNDPROC)MsgSender;
  103. wcex.cbClsExtra = 0;
  104. wcex.cbWndExtra = 0;
  105. wcex.hInstance = hInstX;
  106. wcex.hIcon = 0;
  107. wcex.hCursor        =  LoadCursor(NULL, IDC_ARROW);
  108. wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  109. wcex.lpszMenuName = 0;
  110. wcex.lpszClassName = szClassName;
  111. wcex.hIconSm =0;
  112. iId++;
  113. }
  114. while(!RegisterClassEx(&wcex));
  115. lpszClassName=szClassName;
  116. }
  117. hTemp=CreateWindowEx(dwExStyle,lpszClassName,lpszWindowName,dwStyle,
  118. rect.left ,rect.top,rect.right-rect.left,rect.bottom-rect.top,
  119. hParentWnd,hMenu,hInstX,lpParam);
  120. if(hTemp)
  121. {
  122. m_hWnd=hTemp;
  123. bRet=TRUE;
  124. }
  125. }
  126. return bRet;
  127. }
  128. /*------------------------------------------------------------------------------*/
  129. HWND CWndX::GetHwnd() const
  130. {
  131. return m_hWnd;
  132. }
  133. /*------------------------------------------------------------------------------*/
  134. CWndX::operator HWND() const
  135. {
  136. return m_hWnd;
  137. }
  138. /*------------------------------------------------------------------------------*/
  139. LRESULT CWndX::WndProc(UINT uMsg,WPARAM wParam,LPARAM lParam)
  140. {
  141. switch(uMsg)
  142. {
  143. case WM_NCDESTROY:
  144. OnNcDestroy();
  145. return FALSE;
  146. break;
  147. default:
  148. return DefWindowProc(m_hWnd,uMsg,wParam,lParam);
  149. }
  150. if(m_OldProc)
  151. {
  152. return CallWindowProc(m_OldProc,m_hWnd,uMsg,wParam,lParam);
  153. }
  154. else
  155.     return 0;
  156. }
  157. /*------------------------------------------------------------------------------*/
  158. void CWndX::operator =(HWND hWnd)
  159. {
  160. Attach(hWnd);
  161. }
  162. /*------------------------------------------------------------------------------*/
  163. BOOL CWndX::Attach(HWND hWnd)
  164. {
  165. BOOL bRet=FALSE;
  166. if(!IsWindow(m_hWnd))//自己并不与窗口关联
  167. {
  168. if(IsWindow(hWnd))//参数对应的窗口要有效
  169. {
  170. if(!GetProp(hWnd,WND_OBJ_ID))//不是CWndX创建的
  171. {
  172. m_OldProc=(WNDPROC)GetWindowLong(hWnd,GWL_WNDPROC);
  173. //
  174. if(SetProp(hWnd,WND_OLD_PROC,(HANDLE)GetWindowLong(hWnd,GWL_WNDPROC)))
  175. {
  176. //将对象指针与窗口句柄关联
  177. if(SetProp(hWnd,WND_OBJ_ID,(HANDLE)this))
  178. {
  179. //由于SetWindowLong成功时也有可能返回0
  180. SetLastError(0);
  181. SetWindowLong(hWnd,GWL_WNDPROC,(LONG)MsgSender);
  182. if(GetLastError()==0)
  183. {
  184. m_hWnd=hWnd;
  185. bRet=TRUE;
  186. }
  187. else
  188. {
  189. RemoveProp(hWnd,WND_OLD_PROC);
  190. RemoveProp(hWnd,WND_OBJ_ID);
  191. }
  192. }
  193. else
  194. {
  195. RemoveProp(hWnd,WND_OLD_PROC);
  196. }
  197. }
  198. }
  199. }
  200. }
  201. return bRet;
  202. }
  203. /*------------------------------------------------------------------------------*/
  204. HWND CWndX::Detach()
  205. {
  206. HWND hRet=0;
  207. if(IsWindow(m_hWnd))//必须有效
  208. {
  209. SetLastError(0);
  210. SetWindowLong(m_hWnd,GWL_WNDPROC,(LONG)m_OldProc);//设置老窗口过程
  211. if(GetLastError()==0)
  212. {
  213. m_OldProc=0;
  214. RemoveProp(m_hWnd,WND_OBJ_ID);
  215. RemoveProp(m_hWnd,WND_OLD_PROC);
  216. hRet=m_hWnd;
  217. m_hWnd=0;
  218. }
  219. }
  220. else
  221. m_hWnd=0;
  222. return hRet;
  223.  
  224. }
  225. /*------------------------------------------------------------------------------*/
  226. BOOL CWndX::PreTranslateMessage(MSG *pMsg)
  227. {
  228. return TRUE;
  229. }
  230. /*------------------------------------------------------------------------------*/
  231. void CWndX::OnNcDestroy()
  232. {
  233. m_hWnd=0;
  234. m_OldProc=0;
  235. //除掉之后就不会有消息来了
  236. RemoveProp(m_hWnd,WND_OBJ_ID);
  237. RemoveProp(m_hWnd,WND_OLD_PROC);
  238. }
  239. /*------------------------------------------------------------------------------*/
  240. /////////////////////////
  241. CWndMgr::CWndMgr()
  242. {
  243. InitializeCriticalSection(&m_cs);
  244. }
  245. /*------------------------------------------------------------------------------*/
  246. CWndMgr::~CWndMgr()
  247. {
  248. DeleteCriticalSection(&m_cs);
  249. }
  250. /*------------------------------------------------------------------------------*/
  251. BOOL CWndMgr::AddObject(CWndX* pWndObj)
  252. {
  253. BOOL bRet;
  254. EnterCriticalSection(&m_cs);
  255. bRet=WndObjTable.Insert((int)GetCurrentThreadId(),(int)pWndObj);
  256. LeaveCriticalSection(&m_cs);
  257. return bRet;
  258. }
  259. /*------------------------------------------------------------------------------*/
  260. BOOL CWndMgr::RemoveObject(CWndX* pWndObj)
  261. {
  262. BOOL bRet;
  263. EnterCriticalSection(&m_cs);
  264. bRet=WndObjTable.Remove((int)GetWindowThreadProcessId(pWndObj->GetHwnd(),0));
  265. LeaveCriticalSection(&m_cs);
  266. return bRet;
  267. }
  268. /*------------------------------------------------------------------------------*/
  269. //对话框的实现
  270. int CDialogX::Create(LPCTSTR lpszTemplateName,HWND hWndParent)
  271. {
  272. if(IsWindow(m_hWnd))
  273. return 0;
  274. WndMgr.AddObject(this);
  275. return (int)CreateDialog(hInstX,lpszTemplateName,hWndParent,(DLGPROC)MsgSender);
  276. }
  277. /*------------------------------------------------------------------------------*/
  278. int CDialogX::DoModal(LPCTSTR lpszTemplateName,HWND hWndParent)
  279. {
  280. if(IsWindow(m_hWnd))
  281. return 0;
  282. WndMgr.AddObject(this);
  283. m_bModal=TRUE;
  284. return DialogBox(hInstX,lpszTemplateName,hWndParent,(DLGPROC)MsgSender);
  285. }
  286. /*------------------------------------------------------------------------------*/
  287. void CDialogX::OnOK()
  288. {
  289. if(m_bModal)
  290. EndDialog(m_hWnd,TRUE);
  291. }
  292. /*------------------------------------------------------------------------------*/
  293. void CDialogX::OnCancel()
  294. {
  295. if(m_bModal)
  296. EndDialog(m_hWnd,FALSE);
  297. }
  298. /*------------------------------------------------------------------------------*/
  299. CDialogX::CDialogX()
  300. {
  301. m_bModal=0;
  302. }
  303. /*------------------------------------------------------------------------------*/
  304. CDialogX::~CDialogX()
  305. {
  306. }
  307. /*------------------------------------------------------------------------------*/
  308. LRESULT CDialogX::WndProc(UINT uMsg,WPARAM wParam,LPARAM lParam)
  309. {
  310. switch(uMsg)
  311. {
  312. case WM_COMMAND:
  313. return OnCommand(wParam,lParam);
  314. break;
  315. case WM_NCDESTROY:
  316. OnNcDestroy();
  317. break;
  318. case WM_CLOSE:
  319. OnClose();
  320. break;
  321. default:
  322. return FALSE;
  323. }
  324. return TRUE;
  325. }
  326. /*------------------------------------------------------------------------------*/
  327. void CDialogX::OnNcDestroy()
  328. {
  329. CWndX::OnNcDestroy();
  330. m_bModal=FALSE;
  331. }
  332. /*------------------------------------------------------------------------------*/
  333. void CDialogX::OnClose()
  334. {
  335. if(m_bModal)
  336. EndDialog(m_hWnd,FALSE);
  337. else
  338. DestroyWindow(m_hWnd);
  339. }
  340. /*------------------------------------------------------------------------------*/
  341. BOOL CDialogX::OnCommand(WPARAM wParam, LPARAM lParam)
  342. {
  343. switch(LOWORD(wParam))
  344. {
  345. case IDOK:
  346. OnOK();
  347. break;
  348. case IDCANCEL:
  349. OnCancel();
  350. break;
  351. }
  352. return TRUE;
  353. }
  354. /*------------------------------------------------------------------------------*/
  355. /////
  356. CMenuX::CMenuX()
  357. {
  358. }
  359. /*------------------------------------------------------------------------------*/
  360. CMenuX::~CMenuX()
  361. {
  362. if(IsMenu(m_hMenu))
  363. DestroyMenu(m_hMenu);
  364. }
  365. /*------------------------------------------------------------------------------*/
  366. HMENU CMenuX::GetHandle()
  367. {
  368. return m_hMenu;
  369. }
  370. /*------------------------------------------------------------------------------*/
  371. BOOL CMenuX::Attach(HMENU hMenu)
  372. {
  373. BOOL bRet=FALSE;
  374. if((!IsMenu(m_hMenu))&&IsMenu(hMenu))
  375. {
  376. m_hMenu=hMenu;
  377. bRet=TRUE;
  378. }
  379. return bRet;
  380. }
  381. /*------------------------------------------------------------------------------*/
  382. HMENU CMenuX::Detach()
  383. {
  384. if(IsMenu(m_hMenu))
  385. return m_hMenu;
  386. return 0;
  387. }
  388. /*------------------------------------------------------------------------------*/
  389. BOOL CCtrlX::Associate(HWND hWndParent, UINT uIDDlgItem)
  390. {
  391. return Attach(GetDlgItem(hWndParent,uIDDlgItem));
  392. }
  393. /*------------------------------------------------------------------------------*/
  394. LRESULT CCtrlX::WndProc(UINT uMsg, WPARAM wParam, LPARAM lParam)
  395. {
  396. return CallWindowProc(m_OldProc,m_hWnd,uMsg,wParam,lParam);
  397. }
  398. /*------------------------------------------------------------------------------*/
  399. BOOL CCtrlX::Create(DWORD dwStyleEx,LPCTSTR pszClass,LPCTSTR pszCaption,DWORD dwStyle, const RECT &rect, HWND hParentWnd, UINT nID)
  400. {
  401. BOOL bRet=FALSE;
  402. WNDCLASSEX wcx;
  403. char* pszNewClass=new char[strlen(pszClass)+2];
  404. sprintf(pszNewClass,"%sX",pszClass);
  405.     if(!GetClassInfoEx(hInstX,pszNewClass,&wcx))
  406. {
  407. if(GetClassInfoEx(hInstX,pszClass,&wcx))
  408. {
  409. wcx.lpszClassName=pszNewClass;
  410. wcx.cbSize=sizeof(wcx);
  411. m_OldProc=wcx.lpfnWndProc;
  412. wcx.lpfnWndProc=MsgSender;
  413. if(RegisterClassEx(&wcx))
  414. {
  415. bRet=TRUE;
  416. }
  417. }
  418. }
  419. else
  420. {
  421. if(GetClassInfoEx(hInstX,pszClass,&wcx))
  422. {
  423. m_OldProc=wcx.lpfnWndProc;
  424. bRet=TRUE;
  425. }
  426. }
  427. if(bRet==TRUE)
  428. {
  429. if(!CWndX::Create(dwStyleEx,pszNewClass,pszCaption,dwStyle,
  430. rect,hParentWnd,(HMENU)nID,0))
  431. {
  432. bRet=FALSE;
  433. }
  434. }
  435. delete []pszNewClass;
  436. return bRet;
  437. }
  438. /*------------------------------------------------------------------------------*/
  439. //
  440. BOOL CButtonX::Create(LPCTSTR pszCaption,DWORD dwStyle, const RECT &rect, HWND hParentWnd, UINT nID)
  441. {
  442. return CCtrlX::Create(0,"BUTTON",pszCaption,dwStyle|WS_CHILD|WS_VISIBLE,rect,hParentWnd,nID);
  443. }
  444. /*------------------------------------------------------------------------------*/
  445. ////////////////////////////////////////
  446. BOOL CEditX::Create(LPCTSTR pszCaption,DWORD dwStyle, const RECT &rect, HWND hParentWnd, UINT nID)
  447. {
  448. return CCtrlX::Create(0,"EDIT",pszCaption,dwStyle|WS_CHILD|WS_VISIBLE,rect,hParentWnd,nID);
  449. }
  450. /*------------------------------------------------------------------------------*/
  451. /////////////
  452. BOOL CStaticX::Create(LPCTSTR pszCaption, DWORD dwStyle, const RECT &rect, HWND hParentWnd, UINT nID)
  453. {
  454. return CCtrlX::Create(0,"STATIC",pszCaption,dwStyle|WS_CHILD|WS_VISIBLE,rect,hParentWnd,nID);
  455. }
  456. /*------------------------------------------------------------------------------*/
  457. //
  458. BOOL CComboBoxX::Create(LPCTSTR pszCaption, DWORD dwStyle, const RECT &rect, HWND hParentWnd, UINT nID)
  459. {
  460. return CCtrlX::Create(0,"COMBOBOX",pszCaption,dwStyle|WS_CHILD|WS_VISIBLE,rect,hParentWnd,nID);
  461. }
  462. /*------------------------------------------------------------------------------*/
  463. //
  464. BOOL CListBoxX::Create(LPCTSTR pszCaption, DWORD dwStyle, const RECT &rect, HWND hParentWnd, UINT nID)
  465. {
  466. return CCtrlX::Create(0,"LISTBOX",pszCaption,dwStyle|WS_CHILD|WS_VISIBLE,rect,hParentWnd,nID);
  467. }
  468. /*------------------------------------------------------------------------------*/
  469. //
  470. BOOL CScrollBarX::Create(DWORD dwStyle, const RECT &rect, HWND hParentWnd, UINT nID)
  471. {
  472. return CCtrlX::Create(0,"SCROLLBAR",0,dwStyle|WS_CHILD|WS_VISIBLE,rect,hParentWnd,nID);
  473. }
  474. //
  475. /*------------------------------------------------------------------------------*/
  476. BOOL CStatusBarX::Create(LPCTSTR pszCaption,DWORD dwStyle, HWND hParentWnd,UINT nID)
  477. {
  478. RECT rect={0,0,0,0};
  479. return CCtrlX::Create(0,STATUSCLASSNAME,pszCaption,dwStyle|WS_CHILD|WS_VISIBLE,rect,hParentWnd,nID);
  480. }
  481. /*------------------------------------------------------------------------------*/
  482. BOOL CToolBarX::Create(DWORD dwStyle, HWND hParentWnd, UINT nID)
  483. {
  484. RECT rect={0,0,0,0};
  485. return CCtrlX::Create(0,TOOLBARCLASSNAME,0,dwStyle|WS_CHILD|WS_VISIBLE,rect,hParentWnd,nID);
  486. }