MyToolBar.cpp
上传用户:sz4588
上传日期:2021-04-13
资源大小:2399k
文件大小:3k
源码类别:

工具条

开发平台:

Visual C++

  1. #include "StdAfx.h"
  2. #include "resource.h"
  3. #include "MyToolBar.h"
  4. CMyToolBar::CMyToolBar(void)
  5. {
  6. }
  7. CMyToolBar::~CMyToolBar(void)
  8. {
  9. }
  10. HWND CMyToolBar::CreateToolBar(int styles, HWND hParent,  int id , HINSTANCE hInst)
  11. {
  12. InitCommonControls();
  13. hInstance=hInst;
  14. hToolBar=CreateWindow( TOOLBARCLASSNAME,  NULL, styles 
  15.         , 0, 0, 0, 0, hParent, 
  16. (HMENU) id, hInst, NULL); 
  17. SendMessage(hToolBar,TB_BUTTONSTRUCTSIZE,sizeof(TBBUTTON),0);
  18. //SendMessage(hToolBar,TB_SETEXTENDEDSTYLE,0,TBSTYLE_EX_DRAWDDARROWS);
  19. //SendMessage(hToolBar, TB_SETBUTTONWIDTH,0,MAKELONG(32,32));
  20. return hToolBar;
  21. }
  22. BOOL CMyToolBar::AddButton(BYTE fsState, BYTE fsStyle, int iBitmap, int idCommand, LPTSTR iString, DWORD_PTR dwData)
  23. {
  24. int iStr = SendMessage(hToolBar, TB_ADDSTRING, 0, (LPARAM) iString); 
  25. TBBUTTON button[1];
  26. button[0].dwData=dwData;
  27. button[0].fsState=fsState|TBSTATE_ENABLED;
  28. button[0].iBitmap=STD_COPY;//iBitmap;
  29. button[0].fsStyle=fsStyle;
  30. button[0].idCommand=idCommand;
  31. button[0].iString=iStr;//(INT_PTR)iString;
  32. BOOL kq=SendMessage(hToolBar,TB_ADDBUTTONS ,1,(LPARAM)&button);
  33. return kq;
  34. }
  35. void CMyToolBar::SetButtonImages(int cx,int cy, int idb_Image,int btnNums)
  36. {
  37. SendMessage(hToolBar,TB_SETBITMAPSIZE,0,MAKELONG(cx,cy));
  38. TBADDBITMAP tbAddBitmap;
  39. tbAddBitmap.hInst=HINST_COMMCTRL;//hInstance;
  40. tbAddBitmap.nID=IDB_STD_SMALL_COLOR;//IDB_BITMAP1;
  41. SendMessage(hToolBar,TB_ADDBITMAP,btnNums,(LPARAM)&tbAddBitmap);
  42. }
  43. //Neu goi ham nay nhieu lan thi phai goi theo thu tu btnIndex tang dan
  44. HWND CMyToolBar::AddNonButtonControl(LPTSTR className,int styles,int ID,int width,int height,int btnIndex,int iCommand)
  45. {
  46. TBBUTTON button;
  47. button.fsState=TBSTATE_ENABLED;
  48. button.fsStyle=BTNS_SEP;
  49. button.idCommand=iCommand;
  50. BOOL kq=SendMessage(hToolBar,TB_INSERTBUTTON ,btnIndex,(LPARAM)&button);
  51. TBBUTTONINFO info;
  52. info.cbSize=sizeof(TBBUTTONINFO);
  53. info.dwMask=TBIF_SIZE;
  54. info.cx=width;
  55. SendMessage(hToolBar,TB_SETBUTTONINFO,iCommand,(LPARAM)&info);
  56. RECT rect;
  57. SendMessage(hToolBar,TB_GETITEMRECT,btnIndex,(LPARAM) &rect);
  58. //Neu height=0 thi chieu cao se duoc tinh bang bottom-top
  59. //Neu height<>0 thi chieu cao se la height 
  60. HWND hWnd=CreateWindow( className, NULL, styles, rect.left,rect.top, rect.right-rect.left, height==0?rect.bottom-rect.top:height, hToolBar, (HMENU) ID, hInstance, 0 );
  61. return hWnd;
  62. }
  63. BOOL CMyToolBar::DoNotify(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
  64. {
  65. #define lpnm   ((LPNMHDR)lParam)
  66. #define lpnmTB ((LPNMTOOLBAR)lParam)
  67.    RECT      rc;
  68.    TPMPARAMS tpm;
  69.    HMENU     hPopupMenu = NULL;
  70.    HMENU     hMenuLoaded;
  71.    BOOL      bRet = FALSE;
  72.    switch(lpnm->idFrom)
  73.    {
  74.    case 32883:
  75.    {
  76.    int t;
  77.    t=8;
  78.    break;
  79.    }
  80.    }
  81.    switch(lpnm->code)
  82.    {
  83.    
  84.    case TBN_DROPDOWN:
  85. {   
  86.  SendMessage(lpnmTB->hdr.hwndFrom, TB_GETRECT,
  87.                      (WPARAM)lpnmTB->iItem, (LPARAM)&rc);
  88.          MapWindowPoints(lpnmTB->hdr.hwndFrom,
  89.                          HWND_DESKTOP, (LPPOINT)&rc, 2);                         
  90.          tpm.cbSize = sizeof(TPMPARAMS);
  91.          tpm.rcExclude = rc;
  92.  
  93.  hMenuLoaded = LoadMenu(hInstance, MAKEINTRESOURCE(IDR_MENU1)); 
  94.          
  95.  hPopupMenu = GetSubMenu(hMenuLoaded,0);
  96.          TrackPopupMenuEx(hPopupMenu,
  97.             TPM_LEFTALIGN|TPM_LEFTBUTTON|TPM_VERTICAL,               
  98.             rc.left, rc.bottom, hToolBar, &tpm); 
  99.         DestroyMenu(hMenuLoaded);
  100.  }
  101. break;
  102.    }
  103.    return FALSE;
  104. }