TOOLBAR.C
上传用户:bjghjy
上传日期:2007-01-07
资源大小:379k
文件大小:12k
源码类别:

金融证券系统

开发平台:

Visual C++

  1. #include <windows.h>
  2. #include <string.h>
  3. #include "resource.h"
  4. #include "appmain.h"
  5. #include "global.h"
  6. #include "toolbar.h"
  7. typedef struct tag_BtnInfo{
  8. int bmpNO;
  9. UINT ID;
  10. int left, top;   
  11. BOOL enabled;
  12. } BtnInfo; 
  13. extern HWND ghWndToolBar, ghWndTips;
  14. BtnInfo BtnInfos[]=
  15. {
  16. {BTN_PLAY, IDM_GRAPH, 5, 2, TRUE},
  17. {BTN_PAUSE, IDM_JY, 5+BUTTON_WIDTH+1, 2, TRUE},
  18. {BTN_STOP, IDM_HQ_SZ, 5+2*(BUTTON_WIDTH+1), 2, TRUE},
  19. {BTN_NEXT, IDM_HQ_SH, 5+3*(BUTTON_WIDTH+1), 2, TRUE},
  20. {BTN_PREV, IDM_HQSEL_1, 5+4*(BUTTON_WIDTH+1), 2, TRUE},
  21. {BTN_RECORD, IDM_HQSEL_2, 5+5*(BUTTON_WIDTH+1), 2, TRUE},
  22. {BTN_SELE_3, IDM_HQSEL_3, 5+6*(BUTTON_WIDTH+1), 2, TRUE},
  23. {BTN_SELE_4, IDM_HQSEL_4, 5+7*(BUTTON_WIDTH+1), 2, TRUE},
  24. {BTN_SELE_5, IDM_HQSEL_5, 5+8*(BUTTON_WIDTH+1), 2, TRUE},
  25. {BTN_FX, IDM_FX, 5+9*(BUTTON_WIDTH+1), 2, TRUE},
  26. {BTN_NEWS,IDM_NEWS, 5+10*(BUTTON_WIDTH+1), 2, TRUE},
  27. {BTN_HELP,IDM_HELP, 5+11*(BUTTON_WIDTH+1), 2, TRUE},
  28. {BTN_SZDP,IDM_SZDP, 5+12*(BUTTON_WIDTH+1), 2, TRUE},
  29. {BTN_SHDP,IDM_SHDP, 5+13*(BUTTON_WIDTH+1), 2, TRUE},
  30. {BTN_GGXX,IDM_GGXX, 5+14*(BUTTON_WIDTH+1), 2, TRUE},
  31. {BTN_EXIT,IDM_EXIT, 5+15*(BUTTON_WIDTH+1), 2, TRUE}
  32. };
  33. //int TOOLBAR_HEIGHT =0;
  34. WORD gState=0;
  35. int count =0;
  36. int CurSelBtn=-1, nMoveIn =-1, nTimes =-1;
  37. RECT rcOld;             
  38. POINT ptOld;
  39. HBITMAP hBmpToolsUp=NULL,hBmpToolsDn=NULL,hBmpToolsDs=NULL;
  40. LRESULT CALLBACK __export ToolsWndProc(HWND, UINT, WPARAM, LPARAM);
  41. LRESULT CALLBACK __export TipsWndProc(HWND, UINT, WPARAM, LPARAM);
  42. int ShowTips(int nID);
  43. #define CTOOLTIPS "CTOOLTIPS"
  44. #define CTOOLBAR "CTOOLBAR"
  45. BOOL RegisterToolBar(void)
  46. {
  47. WNDCLASS wc;
  48. memset(&wc, 0, sizeof(wc));
  49. wc.lpfnWndProc =ToolsWndProc;
  50. wc.hInstance =ghInstance;
  51. wc.lpszClassName =CTOOLBAR; 
  52. wc.hbrBackground =GetStockObject(LTGRAY_BRUSH);
  53. wc.hCursor =LoadCursor(NULL, IDC_ARROW);
  54. if(!RegisterClass(&wc)) return FALSE;
  55. wc.style =CS_SAVEBITS;
  56. wc.lpfnWndProc =TipsWndProc;
  57. wc.lpszClassName =CTOOLTIPS;
  58. return RegisterClass(&wc);
  59. }
  60. HWND CreateToolBar(HWND hWnd)
  61. {
  62. int x, y;
  63. RECT rc;
  64.     GetClientRect(ghWndMain, &rc);
  65. x =GetSystemMetrics(SM_CXSCREEN);
  66. //y =GetSystemMetrics(SM_CYSCREEN)-GetSystemMetrics(SM_CYCAPTION);
  67. //x =rc.right -rc.left;
  68. y =rc.bottom -rc.top;
  69. count =sizeof(BtnInfos)/sizeof(BtnInfo);
  70.    
  71.     if(ghWndToolBar==NULL)
  72.     {
  73. ghWndToolBar =CreateWindow(CTOOLBAR, NULL,WS_CHILD|WS_CLIPSIBLINGS,//WS_POPUP,
  74. 0, 
  75. STATUS_HEIGHT -2, 
  76. x, 
  77. BUTTON_HEIGHT+4,
  78. ghWndMain, NULL, ghInstance, NULL);
  79.     }
  80. if(!ghWndToolBar) return NULL;
  81.     if(ghWndTips==NULL)
  82.     {
  83. ghWndTips =CreateWindow(CTOOLTIPS, CTOOLTIPS, WS_POPUP|WS_BORDER,
  84. 2, 
  85. STATUS_HEIGHT-2 +GetSystemMetrics(SM_CYCAPTION),
  86. 20, 
  87. 20, 
  88. ghWndToolBar, NULL, ghInstance, NULL);
  89. }
  90. if(!ghWndTips) return NULL;
  91. SetWindowPos(ghWndToolBar, HWND_TOPMOST, 
  92. 0, 
  93. STATUS_HEIGHT -2, 
  94. 0, 
  95. 0,
  96. SWP_NOACTIVATE|SWP_NOMOVE|SWP_NOSIZE|SWP_NOZORDER);
  97. return ghWndToolBar;
  98. }
  99. long CALLBACK __export ToolsWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  100. HPEN hPen;
  101. RECT rc;
  102. POINT pt; 
  103. static HDC hDC =NULL;
  104. int i;
  105. HBITMAP hBmp;
  106. HDC hMemDC; 
  107. LONG ltmp; 
  108. WORD wState;
  109. PAINTSTRUCT ps;     
  110. switch(message)
  111. {
  112. case WM_CREATE: 
  113. hBmpToolsUp = LoadBitmap(ghInstance,MAKEINTRESOURCE(IDB_TOOLSUP));
  114. if(hBmpToolsUp == NULL)
  115. {
  116. ErrMsg(NULL, "error load resource BMP_TOOLSUP");
  117. DestroyWindow(hWnd);
  118. return -1;
  119. }
  120. hBmpToolsDn = LoadBitmap(ghInstance,MAKEINTRESOURCE(IDB_TOOLSDN));
  121. if(hBmpToolsDn == NULL)
  122. {
  123. ErrMsg(NULL, "error load resource BMP_TOOLSDN");
  124. DestroyWindow(hWnd);
  125. return -1;
  126. }
  127. hBmpToolsDs = LoadBitmap(ghInstance,MAKEINTRESOURCE(IDB_TOOLSDS));
  128. if(hBmpToolsDs == NULL)
  129. {
  130. ErrMsg(NULL, "error load resource BMP_TOOLSDS");
  131. DestroyWindow(hWnd);
  132. return -1;
  133. }
  134. break;
  135. case WM_DRAWBUTTON:
  136. hBmp = NULL;
  137. if(CurSelBtn<0)
  138. if( hBmpToolsUp == NULL) return 0L;
  139. hMemDC = CreateCompatibleDC ((HDC)wParam);
  140. for(i=0;i<count;i++)
  141. {
  142. if(BtnInfos[i].enabled)
  143. SelectObject (hMemDC, hBmpToolsUp); 
  144. else SelectObject(hMemDC, hBmpToolsDs);
  145. BitBlt ((HDC)wParam, BtnInfos[i].left, BtnInfos[i].top, 
  146. BUTTON_WIDTH, BUTTON_HEIGHT,
  147. hMemDC,1+i*(BUTTON_WIDTH+1) , 1, SRCCOPY) ;
  148. }
  149. DeleteDC (hMemDC) ; 
  150. return 0L;
  151. }
  152. else
  153. {
  154. if(CurSelBtn>=count) return 0L;
  155. switch(lParam)
  156. {
  157. case 0L:
  158. hBmp=hBmpToolsUp;
  159. break;
  160. case 1L: 
  161. hBmp=hBmpToolsDn;
  162. break;
  163. case 2L: 
  164. hBmp=hBmpToolsDs;
  165. break;
  166. }
  167. if(hBmp==NULL) return 0L;
  168. hDC=GetDC(hWnd);
  169. hMemDC = CreateCompatibleDC (hDC) ;
  170. SelectObject (hMemDC, hBmp) ;
  171. BitBlt (hDC, BtnInfos[CurSelBtn].left, BtnInfos[CurSelBtn].top, 
  172. BUTTON_WIDTH, BUTTON_HEIGHT,
  173. hMemDC,1+CurSelBtn*(BUTTON_WIDTH+1) , 1, SRCCOPY) ;
  174. DeleteDC (hMemDC) ; 
  175. ReleaseDC(hWnd,hDC);   
  176. hDC =NULL;
  177. }
  178. return 0;
  179. case WM_LBUTTONDOWN:
  180. pt=MAKEPOINT(lParam);
  181. KillTimer(hWnd, 1);   
  182. SendMessage(ghWndStatus, WM_USER+1, 0, 0L);
  183. //ReleaseCapture();
  184. if(IsWindowVisible(ghWndTips))
  185. ShowWindow(ghWndTips, SW_HIDE);   
  186. nMoveIn =-1;
  187. for(i=0;i<count;i++)
  188. {
  189. SetRect(&rc,BtnInfos[i].left,BtnInfos[i].top,
  190. BtnInfos[i].left+BUTTON_WIDTH,BtnInfos[i].top+BUTTON_HEIGHT);
  191. if (PtInRect(&rc, pt))
  192. break;
  193. if(i==count)
  194. {
  195. CurSelBtn=-1;
  196. gState |=STATE_NCMOUSEDOWN;
  197. hDC =GetDC(NULL);
  198. GetWindowRect(hWnd, &rcOld);
  199. SelectObject(hDC, GetStockObject(NULL_BRUSH));
  200. //SelectObject(hDC, GetStockObject(BLACK_PEN));
  201. SetROP2(hDC, R2_NOT);  
  202. Rectangle(hDC, rcOld.left, rcOld.top, rcOld.right, rcOld.bottom);
  203. ptOld =pt;
  204. ClientToScreen(hWnd, &ptOld);
  205. rcOld.right -=rcOld.left;
  206. rcOld.bottom -=rcOld.top;
  207. }
  208. else
  209. {
  210. if(BtnInfos[i].enabled ==FALSE) return 0L;
  211. CurSelBtn=i;
  212. gState |=STATE_MOUSEDOWN;  
  213. gState &=~STATE_MOUSEOUT;
  214. SendMessage(hWnd,WM_DRAWBUTTON,0,1L);
  215. }
  216. SetCapture(hWnd);
  217. return 0L;
  218. case WM_MOUSEMOVE:   
  219. pt =MAKEPOINT(lParam);
  220. if(gState & STATE_NCMOUSEDOWN)
  221. {                  
  222. if(!hDC) break;
  223. Rectangle(hDC, rcOld.left, rcOld.top, rcOld.left+rcOld.right,
  224. rcOld.top+rcOld.bottom);
  225. ClientToScreen(hWnd, &pt);
  226. rcOld.left = rcOld.left +pt.x -ptOld.x;
  227. rcOld.top = rcOld.top +pt.y -ptOld.y;   
  228. ptOld.x =pt.x;
  229. ptOld.y =pt.y;
  230. Rectangle(hDC, rcOld.left, rcOld.top, rcOld.left+rcOld.right,
  231. rcOld.top+rcOld.bottom);
  232. break;
  233. }
  234. if(gState & STATE_MOUSEDOWN)
  235. {
  236. SetRect(&rc,BtnInfos[CurSelBtn].left,BtnInfos[CurSelBtn].top,
  237. BtnInfos[CurSelBtn].left+BUTTON_WIDTH,BtnInfos[CurSelBtn].top+BUTTON_HEIGHT);
  238. wState=gState;
  239. if(PtInRect(&rc,pt))
  240. gState &=~STATE_MOUSEOUT;
  241. else
  242. gState |=STATE_MOUSEOUT;
  243. if (wState!=gState)
  244. {
  245. if(gState & STATE_MOUSEOUT) ltmp=0L;
  246. else ltmp=1L;
  247. SendMessage(hWnd,WM_DRAWBUTTON,0,ltmp);
  248. }
  249. break;
  250. }
  251. for(i=0;i<count;i++)
  252. {
  253. SetRect(&rc,BtnInfos[i].left,BtnInfos[i].top,
  254. BtnInfos[i].left+BUTTON_WIDTH,BtnInfos[i].top+BUTTON_HEIGHT);
  255. if (PtInRect(&rc, pt))
  256. break;
  257. if(i ==count)
  258. {
  259. if(IsWindowVisible(ghWndTips))
  260. {
  261. nTimes =5;
  262. nMoveIn =-1;
  263. }
  264. break;
  265. }
  266. if(i ==nMoveIn) break;
  267. else
  268. {
  269. if(nMoveIn !=i) nTimes =-1;
  270. if(nTimes ==-1)// && nTimes <5)
  271. {
  272. if(!IsWindowVisible(ghWndTips))
  273. SetTimer(hWnd, 1, 1000, NULL);
  274. else
  275. ShowTips(i);
  276. }
  277. nTimes =-1;
  278. nMoveIn =i;
  279. }
  280. break;
  281. case WM_LBUTTONUP: 
  282. ReleaseCapture();
  283. if(gState & STATE_NCMOUSEDOWN)
  284. {
  285. if(!hDC) break;
  286. Rectangle(hDC, rcOld.left, rcOld.top, rcOld.left+rcOld.right,
  287. rcOld.top+rcOld.bottom);
  288. ReleaseDC(NULL, hDC);
  289. gState &=~STATE_NCMOUSEDOWN;
  290. SetWindowPos(hWnd, 0, rcOld.left, rcOld.top, 0, 0,
  291. SWP_NOSIZE|SWP_NOZORDER|SWP_NOACTIVATE);
  292. }
  293. if(CurSelBtn==-1) return 0L;
  294. if (gState & STATE_MOUSEDOWN)
  295. {
  296. gState &=~STATE_MOUSEDOWN;
  297. if(!(gState & STATE_MOUSEOUT))
  298. {
  299. SendMessage(hWnd,WM_DRAWBUTTON,0,0L);
  300. SendMessage(ghWndMain,WM_COMMAND,BtnInfos[CurSelBtn].ID,0L);
  301. else gState &=~STATE_MOUSEOUT;
  302. }               
  303. CurSelBtn=-1;
  304. return 0L;
  305. case WM_TIMER:
  306. if(nTimes++>5 || nMoveIn<0 || nMoveIn >=count)
  307. {
  308. KillTimer(hWnd, 1);
  309. SendMessage(ghWndStatus, WM_USER+1, 0, 0L);
  310. ShowWindow(ghWndTips, SW_HIDE);
  311. nTimes =5; 
  312. //nMoveIn =-1;
  313. break;
  314. }        
  315. GetCursorPos(&pt);
  316. GetWindowRect(hWnd, &rc);
  317. if(pt.x < rc.left+5 || pt.y < rc.top+5 ||
  318. pt.x >rc.right-5 || pt.y > rc.bottom-5)
  319. {
  320. KillTimer(hWnd, 1);
  321. SendMessage(ghWndStatus, WM_USER+1, 0, 0L);
  322. ShowWindow(ghWndTips, SW_HIDE);
  323. //nTimes =-1;  
  324. nMoveIn =-1;
  325. }             
  326. if(nTimes ==0)
  327. ShowTips(nMoveIn);
  328. break;
  329. case WM_KEYDOWN:
  330. SetFocus(ghWndMain);
  331. SendMessage(ghWndMain, WM_KEYDOWN, wParam, lParam);
  332. break;
  333. case WM_SYSCOMMAND:
  334. if(wParam ==SC_KEYMENU)
  335. {
  336. SetFocus(ghWndMain);
  337. SendMessage(ghWndMain, WM_SYSCOMMAND, wParam, lParam);
  338. return 0L;
  339. }
  340. break;
  341. case WM_PAINT: 
  342. BeginPaint(hWnd,&ps); 
  343. SelectObject(ps.hdc,GetStockObject(NULL_BRUSH)); 
  344. hPen=CreatePen(PS_SOLID,1,RGB(80,80,80));
  345. SelectObject(ps.hdc,hPen);
  346. GetClientRect(hWnd,&rc);
  347. Rectangle(ps.hdc,0,0,rc.right,rc.bottom);
  348. SelectObject(ps.hdc,GetStockObject(WHITE_PEN));
  349. DeleteObject(hPen);
  350. MoveTo(ps.hdc,0,rc.bottom-1);
  351. LineTo(ps.hdc,0,0);
  352. LineTo(ps.hdc,rc.right-1,0);
  353. SelectObject(ps.hdc,GetStockObject(BLACK_PEN));
  354. for(i=0;i<count;i++)
  355. Rectangle(ps.hdc,BtnInfos[i].left-1,BtnInfos[i].top-1,
  356. BtnInfos[i].left+BUTTON_WIDTH+1,BtnInfos[i].top+BUTTON_HEIGHT+1);
  357. SendMessage(hWnd,WM_DRAWBUTTON,(WPARAM)(HDC)ps.hdc,0L);
  358. EndPaint(hWnd,&ps);
  359. break; 
  360. case WM_DESTROY:
  361. if(hBmpToolsUp) DeleteObject(hBmpToolsUp);
  362. if(hBmpToolsDn) DeleteObject(hBmpToolsDn);
  363. if(hBmpToolsDs) DeleteObject(hBmpToolsDs);
  364. break;
  365. }
  366. return (DefWindowProc(hWnd, message, wParam, lParam));
  367. }
  368. HBRUSH ghBrushTips =NULL;
  369. LRESULT CALLBACK _export TipsWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  370. {                                                   
  371. char tmp[256];
  372. PAINTSTRUCT ps;
  373. switch(message)
  374. {
  375. case WM_CREATE:  
  376. ghBrushTips =CreateSolidBrush(RGB(127, 127, 65));
  377. if(ghBrushTips ==NULL)
  378. return -1;
  379. SetClassWord(hWnd, GCW_HBRBACKGROUND, (HBRUSH)ghBrushTips);
  380. break;
  381. case WM_MOUSEMOVE:
  382. nMoveIn =-1;   
  383. //nTimes =5;
  384. ShowWindow(hWnd, SW_HIDE);
  385. break;
  386. case WM_PAINT:                                
  387. BeginPaint(hWnd, &ps);
  388. SetBkMode(ps.hdc, TRANSPARENT);
  389. GetWindowText(hWnd, tmp, sizeof(tmp));
  390. TextOut(ps.hdc, 2, 2, tmp, strlen(tmp));
  391. EndPaint(hWnd, &ps);
  392. break; 
  393. case WM_DESTROY:                                  
  394. if(ghBrushTips) DeleteObject(ghBrushTips);
  395. break;
  396. }
  397. return (DefWindowProc(hWnd, message, wParam, lParam));
  398. }
  399. int ShowTips(int nID)
  400. {
  401. char tmp[256];
  402. char *tok;
  403. DWORD dw;                  
  404. RECT rc;
  405. HDC hDC;
  406. if(LoadString(ghInstance, BtnInfos[nMoveIn].ID, tmp, sizeof(tmp)))
  407. {
  408. tok =strtok(tmp, "\");
  409. if(!tok) return -1;
  410. SendMessage(ghWndStatus, WM_USER+1, 0, (LPARAM)(LPSTR)tok);
  411. tok =strtok(NULL, "\");
  412. if(!tok) return -1;
  413. SetWindowText(ghWndTips, tok);
  414. hDC =GetDC(ghWndTips);
  415. dw =GetTextExtent(hDC, tok, strlen(tok));
  416. ReleaseDC(ghWndTips, hDC);
  417. hDC =NULL;                 
  418. GetWindowRect(ghWndToolBar, &rc);
  419. ShowWindow(ghWndTips, SW_HIDE);
  420. SetWindowPos(ghWndTips, 0, 
  421. rc.left +BtnInfos[nMoveIn].left+4, 
  422. rc.top+BtnInfos[nMoveIn].top+BUTTON_HEIGHT+10, 
  423. LOWORD(dw)+4, HIWORD(dw)+4, 
  424. SWP_NOREDRAW|SWP_NOZORDER|SWP_NOACTIVATE);
  425. //if(!IsWindowVisible(ghWndTips))
  426. ShowWindow(ghWndTips, SW_SHOWNA); 
  427. //InvalidateRect(ghWndTips, NULL, TRUE);    
  428. }
  429. }
  430. int PushPlay(void)
  431. {
  432. BtnInfos[BTN_PLAY].enabled =FALSE;
  433. BtnInfos[BTN_PAUSE].enabled =TRUE;
  434. BtnInfos[BTN_STOP].enabled =TRUE;
  435. BtnInfos[BTN_NEXT].enabled =TRUE;
  436. BtnInfos[BTN_PREV].enabled =TRUE;
  437. BtnInfos[BTN_RECORD].enabled =TRUE;
  438. CurSelBtn =-1;
  439.     InvalidateRect(ghWndToolBar, NULL, FALSE);
  440. //SendMessage(ghDlgPlay, WM_USER+1, 0, (LPARAM)(LPSTR)lpCurNodeID);
  441. return 0;
  442. }
  443. int PushStop(void)
  444. {
  445. BtnInfos[BTN_PLAY].enabled =TRUE;
  446. BtnInfos[BTN_PAUSE].enabled =FALSE;
  447. BtnInfos[BTN_STOP].enabled =FALSE;
  448. BtnInfos[BTN_NEXT].enabled =FALSE;
  449. BtnInfos[BTN_PREV].enabled =FALSE;
  450. BtnInfos[BTN_RECORD].enabled =FALSE;
  451. CurSelBtn =-1;
  452.     InvalidateRect(ghWndToolBar, NULL, FALSE);
  453. return 0;
  454. }
  455.