第316章 IE様メニューの試作 その3−4


では、前章の続きを見てみましょう。

BOOL MyInsertForm(HWND hEdit, HWND hEdit_hide) { MYFORMTAG fm; HINSTANCE hInst; char szBuf[256]; hInst = (HINSTANCE)GetWindowLong(hEdit, GWL_HINSTANCE); if (DialogBoxParam(hInst, "MYFORMTAG", hEdit, (DLGPROC)MyFormProc, (LPARAM)&fm) == IDCANCEL) return FALSE; strcpy(szBuf, "<FORM"); if (fm.bMethod) { strcat(szBuf, " METHOD=\""); strcat(szBuf, fm.szMethod); strcat(szBuf, "\""); } if (fm.bAction) { strcat(szBuf, " ACTION=\""); strcat(szBuf, fm.szAction); strcat(szBuf, "\""); } if (fm.bName) { strcat(szBuf, " NAME=\""); strcat(szBuf, fm.szName); strcat(szBuf, "\""); } if (fm.bID) { strcat(szBuf, " ID=\""); strcat(szBuf, fm.szID); strcat(szBuf, "\""); } strcat(szBuf, ">\r\n</FORM>"); MyInsertTag(hEdit, hEdit_hide, szBuf); return TRUE; } LRESULT CALLBACK MyFormProc(HWND hDlg, UINT msg, WPARAM wp, LPARAM lp) { static LPMYFORMTAG lpfm; static HWND hMethodCheck, hActionCheck, hMethodCombo, hActionEdit, hNameCheck, hNameEdit, hIDCheck, hIDEdit; static BOOL bMethod, bAction, bName, bID; static char szMethod[8], szAction[64], szName[16], szID[16]; switch (msg) { case WM_INITDIALOG: lpfm = (LPMYFORMTAG)lp; hMethodCheck = GetDlgItem(hDlg, IDC_CHECK1); hActionCheck = GetDlgItem(hDlg, IDC_CHECK2); hMethodCombo = GetDlgItem(hDlg, IDC_COMBO1); hActionEdit = GetDlgItem(hDlg, IDC_EDIT1); hNameCheck = GetDlgItem(hDlg, IDC_CHECK3); hNameEdit = GetDlgItem(hDlg, IDC_EDIT2); hIDCheck = GetDlgItem(hDlg, IDC_CHECK4); hIDEdit = GetDlgItem(hDlg, IDC_EDIT3); ComboBox_AddString(hMethodCombo, "POST"); ComboBox_AddString(hMethodCombo, "GET"); if (bMethod) { Button_SetCheck(hMethodCheck, BST_CHECKED); EnableWindow(hMethodCombo, TRUE); } else { Button_SetCheck(hMethodCheck, BST_UNCHECKED); EnableWindow(hMethodCombo, FALSE); } if (bAction) { Button_SetCheck(hActionCheck, BST_CHECKED); EnableWindow(hActionEdit, TRUE); } else { Button_SetCheck(hActionCheck, BST_UNCHECKED); EnableWindow(hActionEdit, FALSE); } if (bName) { Button_SetCheck(hNameCheck, BST_CHECKED); EnableWindow(hNameEdit, TRUE); } else { Button_SetCheck(hNameCheck, BST_UNCHECKED); EnableWindow(hNameEdit, FALSE); } if (bID) { Button_SetCheck(hIDCheck, BST_CHECKED); EnableWindow(hIDEdit, TRUE); } else { Button_SetCheck(hIDCheck, BST_UNCHECKED); EnableWindow(hIDEdit, FALSE); } ComboBox_SetText(hMethodCombo, szMethod); Edit_SetText(hActionEdit, szAction); Edit_SetText(hNameEdit, szName); Edit_SetText(hIDEdit, szID); return TRUE; case WM_COMMAND: switch (LOWORD(wp)) { case IDOK: if (Button_GetCheck(hMethodCheck) == BST_CHECKED) { ComboBox_GetText(hMethodCombo, lpfm->szMethod, sizeof(lpfm->szMethod)); lpfm->bMethod = TRUE; bMethod = TRUE; } else { strcpy(lpfm->szMethod, ""); lpfm->bMethod = FALSE; bMethod = FALSE; } if (Button_GetCheck(hActionCheck) == BST_CHECKED) { Edit_GetText(hActionEdit, lpfm->szAction, sizeof(lpfm->szAction)); lpfm->bAction = TRUE; bAction = TRUE; } else { strcpy(lpfm->szAction, ""); lpfm->bAction = FALSE; bAction = FALSE; } if (Button_GetCheck(hNameCheck) == BST_CHECKED) { Edit_GetText(hNameEdit, lpfm->szName, sizeof(lpfm->szName)); lpfm->bName = TRUE; bName = TRUE; } else { strcpy(lpfm->szName, ""); lpfm->bName = FALSE; bName = FALSE; } if (Button_GetCheck(hIDCheck) == BST_CHECKED) { Edit_GetText(hIDEdit, lpfm->szID, sizeof(lpfm->szID)); lpfm->bID = TRUE; bID = TRUE; } else { strcpy(lpfm->szID, ""); lpfm->bID = FALSE; bID = FALSE; } strcpy(szMethod, lpfm->szMethod); strcpy(szAction, lpfm->szAction); strcpy(szName, lpfm->szName); strcpy(szID, lpfm->szID); EndDialog(hDlg, IDOK); return TRUE; case IDCANCEL: EndDialog(hDlg, IDCANCEL); return TRUE; case IDC_CHECK1: if (Button_GetCheck(hMethodCheck) == BST_CHECKED) { EnableWindow(hMethodCombo, TRUE); bMethod = TRUE; } else { EnableWindow(hMethodCombo, FALSE); bMethod = FALSE; } return TRUE; case IDC_CHECK2: if (Button_GetCheck(hActionCheck) == BST_CHECKED) { EnableWindow(hActionEdit, TRUE); SetFocus(hActionEdit); bAction = TRUE; } else { EnableWindow(hActionEdit, FALSE); bAction = FALSE; } return TRUE; case IDC_CHECK3: if (Button_GetCheck(hNameCheck) == BST_CHECKED) { EnableWindow(hNameEdit, TRUE); SetFocus(hNameEdit); bName = TRUE; } else { EnableWindow(hNameEdit, FALSE); bName = FALSE; } return TRUE; case IDC_CHECK4: if (Button_GetCheck(hIDCheck) == BST_CHECKED) { EnableWindow(hIDEdit, TRUE); SetFocus(hIDEdit); bID = TRUE; } else { EnableWindow(hIDEdit, FALSE); bID = FALSE; } return TRUE; } return FALSE; } return FALSE; } LRESULT CALLBACK MyInputProc(HWND hDlg, UINT msg, WPARAM wp, LPARAM lp) { static LPMYINPUTTAG lpin; static HWND hValueCheck, hValueEdit, hTypeCombo, hNameCheck, hIDCheck, hOnClickCheck, hNameEdit, hIDEdit, hOnClickEdit, hOnBlurCheck, hOnBlurEdit, hOnChangeCheck, hOnChangeEdit, hOnFocusCheck, hOnFocusEdit, hOnSubmitCheck, hOnSubmitEdit; static BOOL bValue, bName, bID, bOnClick, bOnBlur, bOnChange, bOnFocus, bOnSubmit; static char szType[16], szValue[64], szName[16], szID[16], szOnClick[64], szOnBlur[64], szOnChange[64], szOnFocus[64], szOnSubmit[64]; switch (msg) { case WM_INITDIALOG: lpin = (LPMYINPUTTAG)lp; hValueCheck = GetDlgItem(hDlg, IDC_CHECK1); hValueEdit = GetDlgItem(hDlg, IDC_EDIT1); hTypeCombo = GetDlgItem(hDlg, IDC_COMBO1); hNameCheck = GetDlgItem(hDlg, IDC_CHECK2); hIDCheck = GetDlgItem(hDlg, IDC_CHECK3); hOnClickCheck = GetDlgItem(hDlg, IDC_CHECK4); hNameEdit = GetDlgItem(hDlg, IDC_EDIT2); hIDEdit = GetDlgItem(hDlg, IDC_EDIT3); hOnClickEdit = GetDlgItem(hDlg, IDC_EDIT4); hOnBlurCheck = GetDlgItem(hDlg, IDC_CHECK5); hOnBlurEdit = GetDlgItem(hDlg, IDC_EDIT5); hOnChangeCheck = GetDlgItem(hDlg, IDC_CHECK6); hOnChangeEdit = GetDlgItem(hDlg, IDC_EDIT6); hOnFocusCheck = GetDlgItem(hDlg, IDC_CHECK7); hOnFocusEdit = GetDlgItem(hDlg, IDC_EDIT7); hOnSubmitCheck = GetDlgItem(hDlg, IDC_CHECK8); hOnSubmitEdit = GetDlgItem(hDlg, IDC_EDIT8); ComboBox_AddString(hTypeCombo, "button"); ComboBox_AddString(hTypeCombo, "text"); ComboBox_AddString(hTypeCombo, "radio"); ComboBox_AddString(hTypeCombo, "checkbox"); ComboBox_AddString(hTypeCombo, "submit"); ComboBox_AddString(hTypeCombo, "reset"); if (bValue) { ShowWindow(hValueEdit, SW_SHOW); Button_SetCheck(hValueCheck, BST_CHECKED); } else { ShowWindow(hValueEdit, SW_HIDE); Button_SetCheck(hValueCheck, BST_UNCHECKED); } if (bName) { ShowWindow(hNameEdit, SW_SHOW); Button_SetCheck(hNameCheck, BST_CHECKED); } else { ShowWindow(hNameEdit, SW_HIDE); Button_SetCheck(hNameCheck, BST_UNCHECKED); } if (bID) { ShowWindow(hIDEdit, SW_SHOW); Button_SetCheck(hIDCheck, BST_CHECKED); } else { ShowWindow(hIDEdit, SW_HIDE); Button_SetCheck(hIDCheck, BST_UNCHECKED); } if (bOnClick) { ShowWindow(hOnClickEdit, SW_SHOW); Button_SetCheck(hOnClickCheck, BST_CHECKED); } else { ShowWindow(hOnClickEdit, SW_HIDE); Button_SetCheck(hOnClickCheck, BST_UNCHECKED); } if (bOnBlur) { ShowWindow(hOnBlurEdit, SW_SHOW); Button_SetCheck(hOnBlurCheck, BST_CHECKED); } else { ShowWindow(hOnBlurEdit, SW_HIDE); Button_SetCheck(hOnBlurCheck, BST_UNCHECKED); } if (bOnChange) { ShowWindow(hOnChangeEdit, SW_SHOW); Button_SetCheck(hOnChangeCheck, BST_CHECKED); } else { ShowWindow(hOnChangeEdit, SW_HIDE); Button_SetCheck(hOnChangeCheck, BST_UNCHECKED); } if (bOnFocus) { ShowWindow(hOnFocusEdit, SW_SHOW); Button_SetCheck(hOnFocusCheck, BST_CHECKED); } else { ShowWindow(hOnFocusEdit, SW_HIDE); Button_SetCheck(hOnFocusCheck, BST_UNCHECKED); } if (bOnSubmit) { ShowWindow(hOnSubmitEdit, SW_SHOW); Button_SetCheck(hOnSubmitCheck, BST_CHECKED); } else { ShowWindow(hOnSubmitEdit, SW_HIDE); Button_SetCheck(hOnSubmitCheck, BST_UNCHECKED); } ComboBox_SetText(hTypeCombo, szType); Edit_SetText(hValueEdit, szValue); Edit_SetText(hNameEdit, szName); Edit_SetText(hIDEdit, szID); Edit_SetText(hOnClickEdit, szOnClick); Edit_SetText(hOnBlurEdit, szOnBlur); Edit_SetText(hOnChangeEdit, szOnChange); Edit_SetText(hOnFocusEdit, szOnFocus); Edit_SetText(hOnSubmitEdit, szOnSubmit); return TRUE; case WM_COMMAND: switch (LOWORD(wp)) { case IDOK: ComboBox_GetText(hTypeCombo, lpin->szType, sizeof(lpin->szType)); if (Button_GetCheck(hValueCheck) == BST_CHECKED) { Edit_GetText(hValueEdit, lpin->szValue, sizeof(lpin->szValue)); strcpy(szValue, lpin->szValue); lpin->bValue = TRUE; bValue = TRUE; } else { strcpy(lpin->szValue, ""); lpin->bValue = FALSE; bValue = FALSE; } if (Button_GetCheck(hNameCheck) == BST_CHECKED) { Edit_GetText(hNameEdit, lpin->szName, sizeof(lpin->szName)); lpin->bName = TRUE; bName = TRUE; } else { strcpy(lpin->szName, ""); lpin->bName = FALSE; bName = FALSE; } if (Button_GetCheck(hIDCheck) == BST_CHECKED) { Edit_GetText(hIDEdit, lpin->szID, sizeof(lpin->szID)); lpin->bID = TRUE; bID = TRUE; } else { strcpy(lpin->szID, ""); lpin->bID = FALSE; bID = FALSE; } if (Button_GetCheck(hOnClickCheck) == BST_CHECKED) { Edit_GetText(hOnClickEdit, lpin->szOnClick, sizeof(lpin->szOnClick)); lpin->bClick = TRUE; bOnClick = TRUE; } else { strcpy(lpin->szOnClick, ""); lpin->bClick = FALSE; bOnClick = FALSE; } if (Button_GetCheck(hOnBlurCheck) == BST_CHECKED) { Edit_GetText(hOnBlurEdit, lpin->szOnBlur, sizeof(lpin->szOnBlur)); lpin->bBlur = TRUE; bOnBlur = TRUE; } else { strcpy(lpin->szOnBlur, ""); lpin->bBlur = FALSE; bOnBlur = FALSE; } if (Button_GetCheck(hOnChangeCheck) == BST_CHECKED) { Edit_GetText(hOnChangeEdit, lpin->szOnChange, sizeof(lpin->szOnChange)); lpin->bChange = TRUE; bOnChange = TRUE; } else { strcpy(lpin->szOnChange, ""); lpin->bChange = FALSE; bOnChange = FALSE; } if (Button_GetCheck(hOnFocusCheck) == BST_CHECKED) { Edit_GetText(hOnFocusEdit, lpin->szOnFocus, sizeof(lpin->szOnFocus)); lpin->bFocus = TRUE; bOnFocus = TRUE; } else { strcpy(lpin->szOnFocus, ""); lpin->bFocus = FALSE; bOnFocus = FALSE; } if (Button_GetCheck(hOnSubmitCheck) == BST_CHECKED) { Edit_GetText(hOnSubmitEdit, lpin->szOnSubmit, sizeof(lpin->szOnSubmit)); lpin->bSubmit = TRUE; bOnSubmit = TRUE; } else { strcpy(lpin->szOnSubmit, ""); lpin->bSubmit = FALSE; bOnSubmit = FALSE; } strcpy(szType, lpin->szType); strcpy(szName, lpin->szName); strcpy(szID, lpin->szID); strcpy(szOnClick, lpin->szOnClick); strcpy(szOnBlur, lpin->szOnBlur); strcpy(szOnChange, lpin->szOnChange); strcpy(szOnFocus, lpin->szOnFocus); strcpy(szOnSubmit, lpin->szOnSubmit); EndDialog(hDlg, IDOK); return TRUE; case IDCANCEL: EndDialog(hDlg, IDCANCEL); return TRUE; case IDC_CHECK1: if (Button_GetCheck(hValueCheck) == BST_CHECKED) { ShowWindow(hValueEdit, SW_SHOW); SetFocus(hValueEdit); bValue = TRUE; } else { ShowWindow(hValueEdit, SW_HIDE); bValue = FALSE; } case IDC_CHECK2: if (Button_GetCheck(hNameCheck) == BST_CHECKED) { ShowWindow(hNameEdit, SW_SHOW); SetFocus(hNameEdit); bName = TRUE; } else { ShowWindow(hNameEdit, SW_HIDE); bName = FALSE; } return TRUE; case IDC_CHECK3: if (Button_GetCheck(hIDCheck) == BST_CHECKED) { ShowWindow(hIDEdit, SW_SHOW); SetFocus(hIDEdit); bID = TRUE; } else { ShowWindow(hIDEdit, SW_HIDE); bID = FALSE; } return TRUE; case IDC_CHECK4: if (Button_GetCheck(hOnClickCheck) == BST_CHECKED) { ShowWindow(hOnClickEdit, SW_SHOW); SetFocus(hOnClickEdit); bOnClick = TRUE; } else { ShowWindow(hOnClickEdit, SW_HIDE); bOnClick = FALSE; } return TRUE; case IDC_CHECK5: if (Button_GetCheck(hOnBlurCheck) == BST_CHECKED) { ShowWindow(hOnBlurEdit, SW_SHOW); SetFocus(hOnBlurEdit); bOnBlur = TRUE; } else { ShowWindow(hOnBlurEdit, SW_HIDE); bOnBlur = FALSE; } return TRUE; case IDC_CHECK6: if (Button_GetCheck(hOnChangeCheck) == BST_CHECKED) { ShowWindow(hOnChangeEdit, SW_SHOW); SetFocus(hOnChangeEdit); bOnChange = TRUE; } else { ShowWindow(hOnChangeEdit, SW_HIDE); bOnChange = FALSE; } return TRUE; case IDC_CHECK7: if (Button_GetCheck(hOnFocusCheck) == BST_CHECKED) { ShowWindow(hOnFocusEdit, SW_SHOW); SetFocus(hOnFocusEdit); bOnFocus = TRUE; } else { ShowWindow(hOnFocusEdit, SW_HIDE); bOnFocus = FALSE; } return TRUE; case IDC_CHECK8: if (Button_GetCheck(hOnSubmitCheck) == BST_CHECKED) { ShowWindow(hOnSubmitEdit, SW_SHOW); SetFocus(hOnSubmitEdit); bOnSubmit = TRUE; } else { ShowWindow(hOnSubmitEdit, SW_HIDE); bOnSubmit = FALSE; } return TRUE; } return FALSE; } return FALSE; } BOOL MyInsertInput(HWND hEdit, HWND hEdit_hide) { HINSTANCE hInst; MYINPUTTAG in; char szBuf[256]; hInst = (HINSTANCE)GetWindowLong(hEdit, GWL_HINSTANCE); if (DialogBoxParam(hInst, "MYINPUTTAG", hEdit, (DLGPROC)MyInputProc, (LPARAM)&in) == IDCANCEL) return FALSE; strcpy(szBuf, "<INPUT TYPE=\""); strcat(szBuf, in.szType); strcat(szBuf, "\""); if (in.bValue) { strcat(szBuf, " VALUE=\""); strcat(szBuf, in.szValue); strcat(szBuf, "\""); } if (in.bName) { strcat(szBuf, " NAME=\""); strcat(szBuf, in.szName); strcat(szBuf, "\""); } if (in.bID) { strcat(szBuf, " ID=\""); strcat(szBuf, in.szID); strcat(szBuf, "\""); } if (in.bClick) { strcat(szBuf, " onClick=\""); strcat(szBuf, in.szOnClick); strcat(szBuf, "\""); } if (in.bBlur) { strcat(szBuf, " onBlur=\""); strcat(szBuf, in.szOnBlur); strcat(szBuf, "\""); } if (in.bChange) { strcat(szBuf, " onChange=\""); strcat(szBuf, in.szOnChange); strcat(szBuf, "\""); } if (in.bFocus) { strcat(szBuf, " onFocus=\""); strcat(szBuf, in.szOnFocus); strcat(szBuf, "\""); } if (in.bSubmit) { strcat(szBuf, " onSubmit=\""); strcat(szBuf, in.szOnSubmit); strcat(szBuf, "\""); } strcat(szBuf, ">"); MyInsertTag(hEdit, hEdit_hide, szBuf); return TRUE; } BOOL MyInsertScript(HWND hEdit, HWND hEdit_hide) { char szBuf[256]; strcpy(szBuf, "<SCRIPT LANGUAGE=\"JavaScript\">\r\n"); strcat(szBuf, "<!--\r\n\r\n//-->\r\n</SCRIPT>"); MyInsertTag(hEdit, hEdit_hide, szBuf); return TRUE; } HWND MakeMyToolbar(HWND hWnd, int n, TBBUTTON *tbButton, int id) { HWND hTool; TBADDBITMAP tbab; int stdid, i; hTool = CreateToolbarEx(hWnd, WS_VISIBLE | WS_CHILD| CCS_NODIVIDER | CCS_NORESIZE | TBSTYLE_FLAT, id , 0, NULL, NULL, tbButton, 0, 0, 0, 0, 0, sizeof(TBBUTTON)); tbab.hInst = HINST_COMMCTRL; tbab.nID = IDB_STD_SMALL_COLOR; stdid = SendMessage(hTool, TB_ADDBITMAP, n, (LPARAM)&tbab); for (i = 0; i < n; i++) tbButton[i].iBitmap += stdid; SendMessage(hTool, TB_ADDBUTTONS, n, (LONG)tbButton); SendMessage(hTool, TB_AUTOSIZE, 0, 0); return hTool; } BOOL MyOpen(HWND hEdit, HWND hEdit_hide) { int id; OPENFILENAME ofn; HGLOBAL hMem; HANDLE hFile; DWORD dwFSizeHigh, dwFSize, dwAccBytes; char *lpszBuf, szWinTitle[64]; HWND hParent; if (SendMessage(hEdit, EM_GETMODIFY, 0, 0) && strcmp(szFileTitle, "無題") != 0) { id = MessageBox(hEdit, "変更を保存しますか", "注意", MB_YESNO | MB_ICONQUESTION); if (id == IDYES) MySave(hEdit); } memset(&ofn, 0, sizeof(OPENFILENAME)); ofn.lStructSize = sizeof(OPENFILENAME); ofn.hwndOwner = hEdit; ofn.lpstrFilter = "ソースファイル(htm, html)\0*.htm;*.html\0すべてのファイル(*.*)\0*.*\0\0"; ofn.lpstrFile = szFileName; ofn.lpstrFileTitle = szFileTitle; ofn.nMaxFile = MAX_PATH; ofn.nMaxFileTitle = MAX_PATH; ofn.Flags = OFN_FILEMUSTEXIST | OFN_HIDEREADONLY; ofn.lpstrDefExt = "html"; ofn.lpstrTitle = "HTMLソースファイルのオープン"; if (!GetOpenFileName(&ofn)) return FALSE; hFile = CreateFile(szFileName, GENERIC_READ, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if (hFile == INVALID_HANDLE_VALUE) { MessageBox(hEdit, "ファイルをオープンできません", "Error", MB_OK); return FALSE; } dwFSize = GetFileSize(hFile, &dwFSizeHigh); if (dwFSizeHigh != 0) { MessageBox(hEdit, "ファイルが大きすぎます", "Error", MB_OK); CloseHandle(hFile); return FALSE; } hMem = GlobalAlloc(GHND, dwFSize + 1); if (hMem == NULL) { MessageBox(hEdit, "メモリを確保できません", "Error", MB_OK); CloseHandle(hFile); return FALSE; } lpszBuf = (char *)GlobalLock(hMem); ReadFile(hFile, lpszBuf, dwFSize, &dwAccBytes, NULL); lpszBuf[dwFSize] = '\0'; SetWindowText(hEdit, ""); MyInsertTag(hEdit, hEdit_hide, lpszBuf); wsprintf(szWinTitle, lpszWinTitleOrg, szFileTitle); hParent = GetParent(hEdit); SetWindowText(hParent, szWinTitle); CloseHandle(hFile); GlobalUnlock(hMem); GlobalFree(hMem); SendMessage(hEdit, EM_SETMODIFY, (WPARAM)FALSE, 0); return TRUE; } BOOL CheckMyMenu(HMENU hMenu, HWND hEdit, HWND hTool2) { HWND hWnd; DWORD dwStart, dwEnd; UINT uOK, uNO; uOK = MF_BYCOMMAND | MF_ENABLED; uNO = MF_BYCOMMAND | MF_GRAYED; hWnd = GetParent(hEdit); if (CanPaste(hWnd)) { EnableMenuItem(hMenu, IDM_PASTE, uOK); SendMessage(hTool2, TB_ENABLEBUTTON, (WPARAM)IDM_PASTE, (LPARAM)MAKELONG(TRUE, 0)); } else { EnableMenuItem(hMenu, IDM_PASTE, uNO); SendMessage(hTool2, TB_ENABLEBUTTON, (WPARAM)IDM_PASTE, (LPARAM)MAKELONG(FALSE, 0)); } if (SendMessage(hEdit, EM_GETMODIFY, 0, 0)) { EnableMenuItem(hMenu, IDM_UNDO, uOK); SendMessage(hTool2, TB_ENABLEBUTTON, (WPARAM)IDM_UNDO, (LPARAM)MAKELONG(TRUE, 0)); } else { EnableMenuItem(hMenu, IDM_UNDO, uNO); SendMessage(hTool2, TB_ENABLEBUTTON, (WPARAM)IDM_UNDO, (LPARAM)MAKELONG(FALSE, 0)); } SendMessage(hEdit, EM_GETSEL, (WPARAM)&dwStart, (LPARAM)&dwEnd); if (dwStart == dwEnd) { EnableMenuItem(hMenu, IDM_COPY, uNO); EnableMenuItem(hMenu, IDM_DELETE, uNO); EnableMenuItem(hMenu, IDM_CUT, uNO); SendMessage(hTool2, TB_ENABLEBUTTON, (WPARAM)IDM_COPY, (LPARAM)MAKELONG(FALSE, 0)); SendMessage(hTool2, TB_ENABLEBUTTON, (WPARAM)IDM_DELETE, (LPARAM)MAKELONG(FALSE, 0)); SendMessage(hTool2, TB_ENABLEBUTTON, (WPARAM)IDM_CUT, (LPARAM)MAKELONG(FALSE, 0)); } else { EnableMenuItem(hMenu, IDM_COPY, uOK); EnableMenuItem(hMenu, IDM_DELETE, uOK); EnableMenuItem(hMenu, IDM_CUT, uOK); SendMessage(hTool2, TB_ENABLEBUTTON, (WPARAM)IDM_COPY, (LPARAM)MAKELONG(TRUE, 0)); SendMessage(hTool2, TB_ENABLEBUTTON, (WPARAM)IDM_DELETE, (LPARAM)MAKELONG(TRUE, 0)); SendMessage(hTool2, TB_ENABLEBUTTON, (WPARAM)IDM_CUT, (LPARAM)MAKELONG(TRUE, 0)); } if (Edit_GetTextLength(hEdit) == 0) EnableMenuItem(hMenu, IDM_ALL, uNO); else EnableMenuItem(hMenu, IDM_ALL, uOK); return TRUE; } BOOL CanPaste(HWND hWnd) { HANDLE hMem; if (OpenClipboard(hWnd) == 0) { return FALSE; } hMem = GetClipboardData(CF_TEXT); if (hMem == NULL) { CloseClipboard(); return FALSE; } else { CloseClipboard(); return TRUE; } } LRESULT CALLBACK NewEditProc(HWND hEdit, UINT msg, WPARAM wp, LPARAM lp) { HWND hParent; static HWND hTool; HMENU hMenu; hParent = GetParent(hEdit); hMenu = GetMenu(hParent); switch (msg) { case MY_MSG: hTool = (HWND)lp; return 0; case WM_KEYUP: case WM_LBUTTONUP: CheckMyMenu(hMenu, hEdit, hTool); break; default: break; } return (CallWindowProc(OrgEditProc, hEdit, msg, wp, lp)); } BOOL MySetStatusText(HWND hStatus, int n, char *lpszBuf) { SendMessage(hStatus, SB_SETTEXT, (WPARAM)n, (LPARAM)lpszBuf); return TRUE; } HWND MakeMyToolbar2(HWND hWnd, int n, TBBUTTON *tb, int nID, int nRID, int nRIDX) { HWND hTool; HINSTANCE hInst; hInst = (HINSTANCE)GetWindowLong(hWnd, GWL_HINSTANCE); hTool = CreateToolbarEx(hWnd, //親ウィンドウ WS_CHILD | WS_VISIBLE | CCS_NODIVIDER | CCS_NORESIZE | TBSTYLE_FLAT, //ウィンドウスタイル nID, //コントロールID n, //ボタンイメージの数 hInst, //インスタンスハンドル nRID, //リソースID tb, //TBBUTTON構造体のアドレス n, //ボタンの数 0, 0, //ボタンの幅、高さ 0, 0, //ボタンイメージの幅、高さ sizeof(TBBUTTON)); //TBBUTTON構造体の大きさ if (hTool == NULL) { MessageBox(hWnd, "エラー", "Error", MB_OK); return NULL; } hImage = ImageList_LoadBitmap(hInst, MAKEINTRESOURCE(nRIDX), 16, 0, RGB(0, 0, 255)); SendMessage(hTool, TB_SETHOTIMAGELIST, 0, (LPARAM)hImage); SendMessage(hTool, TB_SETEXTENDEDSTYLE, 0, (LPARAM)TBSTYLE_EX_DRAWDDARROWS); return hTool; } HWND MakeMyToolTip(HWND hMain, HWND hTool, int nStart, int nEnd, TBBUTTON *tbButton, char (*szText)[256]) { HWND hTT; TOOLINFO ti; int i; RECT rc; HINSTANCE hInst; hInst = (HINSTANCE)GetWindowLong(hTool, GWL_HINSTANCE); hTT = CreateWindowEx(0, TOOLTIPS_CLASS, "", WS_POPUP | TTS_NOPREFIX | TTS_BALLOON | TTS_ALWAYSTIP, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, hMain, (HMENU)NULL, hInst, NULL); SendMessage(hTT, TTM_SETTITLE, (WPARAM)1, (LPARAM)"Hint!"); memset(&ti, 0, sizeof(TOOLINFO)); ti.cbSize = sizeof(TOOLINFO); ti.uFlags = TTF_SUBCLASS; ti.hwnd = hTool; ti.hinst = hInst; for (i = 0; i <= nEnd - nStart; i++) { SendMessage(hTool, TB_GETITEMRECT, i, (LPARAM)&ti.rect); ti.uId = tbButton[i].idCommand; ti.lpszText = szText[i + nStart]; SendMessage(hTT, TTM_ADDTOOL, 0, (LPARAM)&ti); } SendMessage(hTool, TB_SETTOOLTIPS, (WPARAM)hTT, 0); rc.top = 5; rc.left = 5; rc.bottom = 5; rc.right = 5; SendMessage(hTT, TTM_SETMARGIN, 0, (LPARAM)&rc); SendMessage(hTT, TTM_SETMAXTIPWIDTH, 0, (LPARAM)200); return hTT; } void SetTipColor(HWND hWnd, HWND *lphTip, COLORREF *lpcrTip, int nNo) { CHOOSECOLOR cc; static DWORD dwCust[16]; int i; memset(&cc, 0, sizeof(CHOOSECOLOR)); cc.lStructSize = sizeof(CHOOSECOLOR); cc.hwndOwner = hWnd; cc.lpCustColors = dwCust; cc.rgbResult = *lpcrTip; cc.Flags = CC_RGBINIT; if(ChooseColor(&cc)) { *lpcrTip = cc.rgbResult; for (i = 0; i < nNo; i++) SendMessage(lphTip[i], TTM_SETTIPTEXTCOLOR, (WPARAM)*lpcrTip, 0); } return; } void SetTipBkColor(HWND hWnd, HWND *lphTip, COLORREF *lpcrTipBk, int nNo) { CHOOSECOLOR cc; static DWORD dwCust[16]; int i; memset(&cc, 0, sizeof(CHOOSECOLOR)); cc.lStructSize = sizeof(CHOOSECOLOR); cc.hwndOwner = hWnd; cc.lpCustColors = dwCust; cc.rgbResult = *lpcrTipBk; cc.Flags = CC_RGBINIT; if(ChooseColor(&cc)) { *lpcrTipBk = cc.rgbResult; for (i = 0; i < nNo; i++) SendMessage(lphTip[i], TTM_SETTIPBKCOLOR, (WPARAM)*lpcrTipBk, 0); } return; } BOOL GetInitialSetting(LPMYINITIALDATA lpmyinit) { HKEY hKey; DWORD dwResult, dwType, dwByte; LONG lResult; lResult = RegCreateKeyEx(HKEY_CURRENT_USER, "SoftWare\\Kumei\\htmledit", 0, "", REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hKey, &dwResult); if (lResult != ERROR_SUCCESS) { MessageBox(NULL, "レジストリ操作に失敗しました", "Error", MB_OK); return FALSE; } else if (dwResult == REG_CREATED_NEW_KEY) { return FALSE; } dwType = REG_DWORD; dwByte = (DWORD)sizeof(DWORD); RegQueryValueEx(hKey, "x", NULL, &dwType, (BYTE *)&(lpmyinit->x), &dwByte); RegQueryValueEx(hKey, "y", NULL, &dwType, (BYTE *)&(lpmyinit->y), &dwByte); RegQueryValueEx(hKey, "wx", NULL, &dwType, (BYTE *)&(lpmyinit->wx), &dwByte); RegQueryValueEx(hKey, "wy", NULL, &dwType, (BYTE *)&(lpmyinit->wy), &dwByte); dwByte = sizeof(COLORREF); RegQueryValueEx(hKey, "TipTextColor", NULL, &dwType, (BYTE *)&(lpmyinit->crTipText), &dwByte); RegQueryValueEx(hKey, "TipBkColor", NULL, &dwType, (BYTE *)&(lpmyinit->crTipBackground), &dwByte); RegCloseKey(hKey); return TRUE; } BOOL SetInitialSetting(LPMYINITIALDATA lpmyinit) { HKEY hKey; DWORD dwResult; LONG lResult; lResult = RegCreateKeyEx(HKEY_CURRENT_USER, "SoftWare\\Kumei\\htmledit", 0, "", REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hKey, &dwResult); if (lResult != ERROR_SUCCESS) { MessageBox(NULL, "レジストリ操作に失敗しました", "Error", MB_OK); return FALSE; } RegSetValueEx(hKey, "x", 0, REG_DWORD, (CONST BYTE *)&(lpmyinit->x), sizeof(DWORD)); RegSetValueEx(hKey, "y", 0, REG_DWORD, (CONST BYTE *)&(lpmyinit->y), sizeof(DWORD)); RegSetValueEx(hKey, "wx", 0, REG_DWORD, (CONST BYTE *)&(lpmyinit->wx), sizeof(DWORD)); RegSetValueEx(hKey, "wy", 0, REG_DWORD, (CONST BYTE *)&(lpmyinit->wy), sizeof(DWORD)); RegSetValueEx(hKey, "TipTextColor", 0, REG_DWORD, (CONST BYTE *)&(lpmyinit->crTipText), sizeof(COLORREF)); RegSetValueEx(hKey, "TipBkColor", 0, REG_DWORD, (CONST BYTE *)&(lpmyinit->crTipBackground), sizeof(COLORREF)); RegCloseKey(hKey); return TRUE; } HWND MakeMyToolbarMenu(HWND hRebar) { HWND hTool; int iFile, iEdit, iInsert, iBrowser, iOption; char szBuf[32]; HINSTANCE hInst; hInst = (HINSTANCE)GetWindowLong(hRebar, GWL_HINSTANCE); hTool = CreateToolbarEx(hRebar, //親ウィンドウ WS_CHILD | WS_VISIBLE | CCS_NODIVIDER | CCS_NORESIZE | TBSTYLE_LIST | TBSTYLE_FLAT, //ウィンドウスタイル ID_TOOLMENU, //ID 0, //ボタンイメージの数 NULL,//hInst, //ビットマップリソースの入っているモジュール NULL,//IDR_TOOLBAR1, //ビットマップリソースのID tbButtonMenu, //TBBUTTON構造体のアドレス 0, //ツールバーに加えるボタンの数 0, 0, 0, 0, //ボタンの幅、高さ、イメージの幅、高さ sizeof(TBBUTTON)); //TBBUTTON構造体の大きさ LoadString(hInst, IDS_FILE, szBuf, sizeof(szBuf) - 1); iFile = SendMessage(hTool, TB_ADDSTRING, 0, (LPARAM)szBuf); tbButtonMenu[0].iString = iFile; LoadString(hInst, IDS_EDIT, szBuf, sizeof(szBuf) - 1); iEdit = SendMessage(hTool, TB_ADDSTRING, 0, (LPARAM)szBuf); tbButtonMenu[1].iString = iEdit; LoadString(hInst, IDS_INSERT, szBuf, sizeof(szBuf) - 1); iInsert = SendMessage(hTool, TB_ADDSTRING, 0, (LPARAM)szBuf); tbButtonMenu[2].iString = iInsert; LoadString(hInst, IDS_BROWSER, szBuf, sizeof(szBuf) - 1); iBrowser = SendMessage(hTool, TB_ADDSTRING, 0, (LPARAM)szBuf); tbButtonMenu[3].iString = iBrowser; LoadString(hInst, IDS_OPTION, szBuf, sizeof(szBuf) - 1); iOption = SendMessage(hTool, TB_ADDSTRING, 0, (LPARAM)szBuf); tbButtonMenu[4].iString = iOption; SendMessage(hTool, TB_ADDBUTTONS, 5, (LPARAM)tbButtonMenu); return hTool; }

これらの関数に、特に変更はありません。

BOOL ShowMyPopMenu(HWND hWnd, HWND hToolMenuW, char *szMenuName, int nId, HWND hEdit, HWND hTool2) { RECT rc, rc_scr; POINT pt; HMENU hMenu, hSubMenu; HINSTANCE hInst; int nCommandID[] = {IDM_FILE, IDM_EDIT, IDM_INSERT, IDM_BRVIEW, IDM_OPTION}; hInst = (HINSTANCE)GetWindowLong(hWnd, GWL_HINSTANCE); SendMessage(hToolMenuW, TB_GETITEMRECT, (WPARAM)nId, (LPARAM)&rc); pt.x = rc.left; pt.y = rc.top; ClientToScreen(hToolMenuW, &pt); rc_scr.left = pt.x; rc_scr.top = pt.y; pt.x = rc.right; pt.y = rc.bottom; ClientToScreen(hToolMenuW, &pt); rc_scr.right = pt.x; rc_scr.bottom = pt.y; hMenu = LoadMenu(hInst, szMenuName); if (hMenu == NULL) { MessageBox(hWnd, "LoadMenu Error", "Error", MB_OK); return FALSE; } hSubMenu = GetSubMenu(hMenu, nId); CheckMyMenu(hMenu, hEdit, hTool2); SendMessage(hToolMenuW, TB_SETHOTITEM, (WPARAM)-1, 0); SendMessage(hToolMenuW, TB_SETSTATE, (WPARAM)nCommandID[nId], ((LPARAM)MAKELONG(TBSTATE_ENABLED | TBSTATE_PRESSED, 0))); TrackPopupMenu(hSubMenu, TPM_LEFTALIGN, rc_scr.left, rc_scr.bottom, 0, hWnd, NULL); DestroyMenu(hMenu); SendMessage(hToolMenuW, TB_SETSTATE, (WPARAM)nCommandID[nId], ((LPARAM)MAKELONG(TBSTATE_ENABLED, 0))); return TRUE; }

TrackPopupMenu関数が呼び出される前に、メニュー様ツールバーのホットアイテムを解除して、 ボタンが押された状態(TBSTATE_PRESSED)にします。TrackPopupMenu関数が終了した後に TBSTATE_PRESSEDの状態を解除します。

LRESULT CALLBACK KbdHook(int code, WPARAM wp, LPARAM lp) { int nId, nBtnNo; UINT uId; int nCommandID[] = {IDM_FILE, IDM_EDIT, IDM_INSERT, IDM_BRVIEW, IDM_OPTION}; BOOL bPressed; if (code < 0) return CallNextHookEx(hHook, code, wp, lp); switch (code) { case HC_ACTION: nBtnNo = SendMessage(hMytoolMenu, TB_BUTTONCOUNT, 0, 0); nId = SendMessage(hMytoolMenu, TB_GETHOTITEM, 0, 0); if (HIWORD(lp) & KF_UP) bPressed = FALSE; else bPressed = TRUE; if ((wp == VK_F10 || wp == VK_MENU) && nId == -1 && bPressed) { SendMessage(hMytoolMenu, TB_SETHOTITEM, 0, 0); return -100; } if (wp == VK_ESCAPE) { SendMessage(hMytoolMenu, TB_SETHOTITEM, (WPARAM)-1, 0); break; } if (wp == VK_LEFT && nId != -1 && nId != 0 && bPressed) { SendMessage(hMytoolMenu, TB_SETHOTITEM, (WPARAM)nId - 1, 0); break; } if (wp == VK_RIGHT && nId != -1 && nId < nBtnNo && bPressed) { SendMessage(hMytoolMenu, TB_SETHOTITEM, (WPARAM)nId + 1, 0); break; } if (SendMessage(hMytoolMenu, TB_MAPACCELERATOR, (WPARAM)wp, (LPARAM)&uId) != 0 && nId != -1 ) { ShowMyPopMenu(hParent, hMytoolMenu, "MYMENU", uId - IDM_FILE, hEdit_g, hTool2_g); return -200; } default: break; } return CallNextHookEx(hHook, code, wp, lp); }

キーボードフックの中身(?)です。

F10または、ALTキーが押されたら最初のメニューアイテムをホットアイテムにします。この時 マイナスの値を返すようにします。breakするとCallNextHookEx関数が呼ばれてしまうので注意してください。

エスケープキーが押されたら、ホットキーを解除します。

その他、左右のカーソルキー、アクセラレータキーが押されたときの動作はプログラムを見るとわかるでしょう。

さて、今回のプログラムではポップアップメニューが出ているときに、左カーソルキーなどを押しても、反応しません。 IEでは、1つ前のメニュー項目のポップアップが表示されます。しかし、まあ、実用上はあまり問題がないと思われます。 気になる人は、プログラムを改良してください。


[SDK第4部 Index] [総合Index] [Previous Chapter] [Next Chapter]

Update 11/Aug/2001 By Y.Kumei
当ホーム・ページの一部または全部を無断で複写、複製、 転載あるいはコンピュータ等のファイルに保存することを禁じます。