線上訂房服務-台灣趴趴狗聯合訂房中心
發文 回覆 瀏覽次數:3426
推到 Plurk!
推到 Facebook!

在BCB如何建立Contextmenuhandle!!!

尚未結案
Rexchiang
一般會員


發表:8
回覆:8
積分:3
註冊:2003-05-22

發送簡訊給我
#1 引用回覆 回覆 發表時間:2003-09-17 18:00:56 IP:61.220.xxx.xxx 未訂閱
在站上看到有關於Contextmenuhandle的文章,但是卻是用Delphi寫的 小弟又看不懂Delphi,所以請教各位高手在BCB裏要如何作呢???? 網路上有看到使用Type Library的範例(http://bdn.borland.com/article/0,1410,26650,00.html),但對我來說又有點難度,實在是看不太懂!!!! 所以還有別的方法嗎???謝謝
axsoft
版主


發表:681
回覆:1056
積分:969
註冊:2002-03-13

發送簡訊給我
#2 引用回覆 回覆 發表時間:2003-09-17 19:23:09 IP:61.218.xxx.xxx 未訂閱
Rexchiang您好:    1.Adding items to the shell context menu 2.Using the shell context menu 請參考上面兩篇文章 Source code下載: 1.原始網站:http://www.bridgespublishing.com/articles/source/June00Code.zip 2.本站下載:http://delphi.ktop.com.tw/loadfile.php?TOPICID=11746277&CC=262703
該範例的Source Code如下:
    MainU.cpp    //------------------------------------------------------------------
#define NO_WIN32_LEAN_AND_MEAN
#include 
#pragma hdrstop    #include 
#include 
#include  //使用C++ Builder5 請加上這一行    #include "MainU.h"    #if (__BORLANDC__ >= 0x530)
#pragma package(smart_init)
#endif
#pragma resource "*.dfm"
TForm1 *Form1;    __fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
}    void __fastcall TForm1::FormCreate(TObject *Sender)
{
  // Initialize OLE. This is required to get
  // the cut and copy actions to work.
  if (OleInitialize(0) != S_OK) {
    ShowMessage("Unable to initialize OLE.");
    return;
  }
  FileNameEdit->Text = Application->ExeName;
}    void __fastcall TForm1::FormDestroy(TObject *Sender)
{
  // Unitialize OLE
  OleUninitialize();
}    void __fastcall TForm1::GoBtnClick(TObject *Sender)
{
  // Get an IShellFolder for the desktop.
  LPSHELLFOLDER DesktopFolder;
  SHGetDesktopFolder(&DesktopFolder);
  if (!DesktopFolder) {
    ShowMessage(
      "Failed to get Desktop folder.");
    return;
  }      // Separate the file from the folder.
  String FilePath = ExtractFilePath(
    FileNameEdit->Text);
  String FileName = ExtractFileName(
    FileNameEdit->Text);      // Get a pidl for the folder the file
  // is located in.
  wchar_t Path[MAX_PATH];
  LPITEMIDLIST ParentPidl;
  DWORD Eaten;
  StringToWideChar(FilePath, Path, MAX_PATH);
  DWORD Result =
    DesktopFolder->ParseDisplayName(
      Handle, 0, Path, &Eaten, &ParentPidl, 0);
  if (Result != NOERROR) {
    ShowMessage("Invalid file name.");
    return;
  }      // Get an IShellFolder for the folder
  // the file is located in.
  LPSHELLFOLDER ParentFolder;
  DesktopFolder->BindToObject(ParentPidl,
    0, IID_IShellFolder, (void**)&ParentFolder);
  if (!ParentFolder) {
    ShowMessage("Invalid file name.");
    return;
  }      // Get a pidl for the file itself.
  LPITEMIDLIST Pidl;
  StringToWideChar(
    FileName, Path, MAX_PATH);
  ParentFolder->ParseDisplayName(
    Handle, 0, Path, &Eaten, &Pidl, 0);      // Get the IContextMenu for the file.
  LPCONTEXTMENU CM;
  ParentFolder->GetUIObjectOf(
    Handle, 1, (LPCITEMIDLIST*)&Pidl,
    IID_IContextMenu, 0, (void**)&CM);      if (!CM) {
    ShowMessage(
      "Unable to get context menu interface.");
    return;
  }      // Set up a CMINVOKECOMMANDINFO structure.
  CMINVOKECOMMANDINFO CI;
  ZeroMemory(&CI, sizeof(CI));
  CI.cbSize = sizeof(CMINVOKECOMMANDINFO);
  CI.hwnd = Handle;      if (Sender == GoBtn) {
    // Verbs that can be used are cut, paste,
    // properties, delete, and so on.
    String Action;
    if (CutRb->Checked)
      Action = "cut";
    else if (CopyRb->Checked)
      Action = "copy";
    else if (DeleteRb->Checked)
      Action = "delete";
    else if (PropertiesRb->Checked)
      Action = "properties";        CI.lpVerb = Action.c_str();
    Result = CM->InvokeCommand(&CI);
    if (Result)
      ShowMessage(
        "Error copying file to clipboard.");        // Clean up.
    CM->Release();
    ParentFolder->Release();
    DesktopFolder->Release();
  } else {
    HMENU hMenu = CreatePopupMenu();
    DWORD Flags = CMF_EXPLORE;
    // Optionally the shell will show the extended
    // context menu on some operating systems when
    // the shift key is held down at the time the
    // context menu is invoked. The following is
    // commented out but you can uncommnent this
    // line to show the extended context menu.
    // Flags |= 0x00000080;
    CM->QueryContextMenu(hMenu, 0, 1, 0x7FFF, Flags);        // Merge the form's popup menu with the shell
    // menu.
    MENUITEMINFO mi;
    char buff[80];
    // Work backwards, adding each item to the
    // top of the shell context menu.
    for (int i=PopupMenu1->Items->Count - 1;i>-1;i--) {
      ZeroMemory(&mi, sizeof(mi));
      mi.dwTypeData = buff;
      mi.cch = sizeof(buff);
      mi.cbSize = 44;
      mi.fMask = MIIM_TYPE | MIIM_ID | MIIM_DATA;
      // Get the menu item.
      DWORD result = GetMenuItemInfo(
        PopupMenu1->Handle, i, true, &mi);
        if (result) {
          // Modify its ID by adding 100 to the
          // Command property. This ensures that
          // there are no conflicts between the
          // shell command IDs and the popup items.
          mi.wID = PopupMenu1->Items->
            Items[i]->Command + 100;
          // Add the item to the shell menu.
          InsertMenuItem(hMenu, 0, true, &mi);
        }
    }
    // Show the menu.
    TPoint pt;
    GetCursorPos(&pt);
    int Cmd = TrackPopupMenu(hMenu,
      TPM_LEFTALIGN | TPM_LEFTBUTTON |
      TPM_RIGHTBUTTON | TPM_RETURNCMD,
      pt.x, pt.y, 0, Handle, 0);
    // Handle the command. If the return value
    // from TrackPopupMenu is less than 100 then
    // a shell item was clicked.
    if (Cmd < 100 && Cmd != 0) {
      CI.lpVerb = MAKEINTRESOURCE(Cmd - 1);
      CI.lpParameters = "";
      CI.lpDirectory = "";
      CI.nShow = SW_SHOWNORMAL;
      CM->InvokeCommand(&CI);
    }
    // If Cmd is > 100 then it's one of our
    // inserted menu items.
    else
      // Find the menu item.
      for (int i=0;iItems->Count;i++) {
        TMenuItem* menu =
          PopupMenu1->Items->Items[i];
        // Call its OnClick handler.
        if (menu->Command == Cmd - 100)
          menu->OnClick(this);
      }
    // Release the memory allocated for the menu.
    DestroyMenu(hMenu);
  }
}    void __fastcall TForm1::Button2Click(TObject *Sender)
{
  if (OpenDialog->Execute())
    FileNameEdit->Text = OpenDialog->FileName;
}    void __fastcall TForm1::CloseBtnClick(TObject *Sender)
{
  Close();
}    // The OnClick handlers for the poup menu.
void __fastcall TForm1::Hello1Click(TObject *Sender)
{
  ShowMessage("Hello!");
}    void __fastcall TForm1::Test1Click(TObject *Sender)
{
  ShowMessage("This is a test.");
}
//-------------------------------------------------------------------    MainU.h
//------------------------------------------------------------------
//---------------------------------------------------------------------------    #ifndef MainUH
#define MainUH
//---------------------------------------------------------------------------
#include 
#include 
#include 
#include <Forms.hpp>
#include 
#include 
#include 
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published:        // IDE-managed Components
  TLabel *Label1;
  TEdit *FileNameEdit;
  TOpenDialog *OpenDialog;
  TButton *Button2;
  TButton *CloseBtn;
  TGroupBox *GroupBox1;
  TRadioButton *CutRb;
  TRadioButton *CopyRb;
  TRadioButton *DeleteRb;
  TRadioButton *PropertiesRb;
  TButton *GoBtn;
  TButton *ShowCMBtn;
  TPopupMenu *PopupMenu1;
  TMenuItem *Hello1;
  TMenuItem *Test1;
  TMenuItem *N1;
  void __fastcall GoBtnClick(TObject *Sender);
  void __fastcall FormCreate(TObject *Sender);
  void __fastcall FormDestroy(TObject *Sender);
  void __fastcall Button2Click(TObject *Sender);
  void __fastcall CloseBtnClick(TObject *Sender);
  void __fastcall Hello1Click(TObject *Sender);
  void __fastcall Test1Click(TObject *Sender);
private:        // User declarations
public:                // User declarations
        __fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
#if (__BORLANDC__ < 0x530)
  #define PACKAGE
#endif
//---------------------------------------------------------------------------
#endif    
/*生活是一種藝術,用心生活才能享受生活*/
發表人 - axsoft 於 2003/09/17 19:25:19 發表人 - axsoft 於 2003/09/17 19:30:37
Rexchiang
一般會員


發表:8
回覆:8
積分:3
註冊:2003-05-22

發送簡訊給我
#3 引用回覆 回覆 發表時間:2003-09-18 09:40:01 IP:61.220.xxx.xxx 未訂閱
謝謝axsoft大大的指導,小弟現在就去試.....謝謝!!
alexkuo59
一般會員


發表:0
回覆:1
積分:0
註冊:2003-07-24

發送簡訊給我
#4 引用回覆 回覆 發表時間:2004-06-15 20:38:34 IP:211.21.xxx.xxx 未訂閱
引言: Rexchiang您好: 1.Adding items to the shell context menu 2.Using the shell context menu 請參考上面兩篇文章 Source code下載: 1.原始網站:http://www.bridgespublishing.com/articles/source/June00Code.zip 2.本站下載:http://delphi.ktop.com.tw/loadfile.php?TOPICID=11746277&CC=262703
該範例的Source Code如下:
    MainU.cpp    //------------------------------------------------------------------
#define NO_WIN32_LEAN_AND_MEAN
#include 
#pragma hdrstop    #include 
#include 
#include  //使用C++ Builder5 請加上這一行    #include "MainU.h"    #if (__BORLANDC__ >= 0x530)
#pragma package(smart_init)
#endif
#pragma resource "*.dfm"
TForm1 *Form1;    __fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
}    void __fastcall TForm1::FormCreate(TObject *Sender)
{
  // Initialize OLE. This is required to get
  // the cut and copy actions to work.
  if (OleInitialize(0) != S_OK) {
    ShowMessage("Unable to initialize OLE.");
    return;
  }
  FileNameEdit->Text = Application->ExeName;
}    void __fastcall TForm1::FormDestroy(TObject *Sender)
{
  // Unitialize OLE
  OleUninitialize();
}    void __fastcall TForm1::GoBtnClick(TObject *Sender)
{
  // Get an IShellFolder for the desktop.
  LPSHELLFOLDER DesktopFolder;
  SHGetDesktopFolder(&DesktopFolder);
  if (!DesktopFolder) {
    ShowMessage(
      "Failed to get Desktop folder.");
    return;
  }      // Separate the file from the folder.
  String FilePath = ExtractFilePath(
    FileNameEdit->Text);
  String FileName = ExtractFileName(
    FileNameEdit->Text);      // Get a pidl for the folder the file
  // is located in.
  wchar_t Path[MAX_PATH];
  LPITEMIDLIST ParentPidl;
  DWORD Eaten;
  StringToWideChar(FilePath, Path, MAX_PATH);
  DWORD Result =
    DesktopFolder->ParseDisplayName(
      Handle, 0, Path, &Eaten, &ParentPidl, 0);
  if (Result != NOERROR) {
    ShowMessage("Invalid file name.");
    return;
  }      // Get an IShellFolder for the folder
  // the file is located in.
  LPSHELLFOLDER ParentFolder;
  DesktopFolder->BindToObject(ParentPidl,
    0, IID_IShellFolder, (void**)&ParentFolder);
  if (!ParentFolder) {
    ShowMessage("Invalid file name.");
    return;
  }      // Get a pidl for the file itself.
  LPITEMIDLIST Pidl;
  StringToWideChar(
    FileName, Path, MAX_PATH);
  ParentFolder->ParseDisplayName(
    Handle, 0, Path, &Eaten, &Pidl, 0);      // Get the IContextMenu for the file.
  LPCONTEXTMENU CM;
  ParentFolder->GetUIObjectOf(
    Handle, 1, (LPCITEMIDLIST*)&Pidl,
    IID_IContextMenu, 0, (void**)&CM);      if (!CM) {
    ShowMessage(
      "Unable to get context menu interface.");
    return;
  }      // Set up a CMINVOKECOMMANDINFO structure.
  CMINVOKECOMMANDINFO CI;
  ZeroMemory(&CI, sizeof(CI));
  CI.cbSize = sizeof(CMINVOKECOMMANDINFO);
  CI.hwnd = Handle;      if (Sender == GoBtn) {
    // Verbs that can be used are cut, paste,
    // properties, delete, and so on.
    String Action;
    if (CutRb->Checked)
      Action = "cut";
    else if (CopyRb->Checked)
      Action = "copy";
    else if (DeleteRb->Checked)
      Action = "delete";
    else if (PropertiesRb->Checked)
      Action = "properties";        CI.lpVerb = Action.c_str();
    Result = CM->InvokeCommand(&CI);
    if (Result)
      ShowMessage(
        "Error copying file to clipboard.");        // Clean up.
    CM->Release();
    ParentFolder->Release();
    DesktopFolder->Release();
  } else {
    HMENU hMenu = CreatePopupMenu();
    DWORD Flags = CMF_EXPLORE;
    // Optionally the shell will show the extended
    // context menu on some operating systems when
    // the shift key is held down at the time the
    // context menu is invoked. The following is
    // commented out but you can uncommnent this
    // line to show the extended context menu.
    // Flags |= 0x00000080;
    CM->QueryContextMenu(hMenu, 0, 1, 0x7FFF, Flags);        // Merge the form's popup menu with the shell
    // menu.
    MENUITEMINFO mi;
    char buff[80];
    // Work backwards, adding each item to the
    // top of the shell context menu.
    for (int i=PopupMenu1->Items->Count - 1;i>-1;i--) {
      ZeroMemory(&mi, sizeof(mi));
      mi.dwTypeData = buff;
      mi.cch = sizeof(buff);
      mi.cbSize = 44;
      mi.fMask = MIIM_TYPE | MIIM_ID | MIIM_DATA;
      // Get the menu item.
      DWORD result = GetMenuItemInfo(
        PopupMenu1->Handle, i, true, &mi);
        if (result) {
          // Modify its ID by adding 100 to the
          // Command property. This ensures that
          // there are no conflicts between the
          // shell command IDs and the popup items.
          mi.wID = PopupMenu1->Items->
            Items[i]->Command + 100;
          // Add the item to the shell menu.
          InsertMenuItem(hMenu, 0, true, &mi);
        }
    }
    // Show the menu.
    TPoint pt;
    GetCursorPos(&pt);
    int Cmd = TrackPopupMenu(hMenu,
      TPM_LEFTALIGN | TPM_LEFTBUTTON |
      TPM_RIGHTBUTTON | TPM_RETURNCMD,
      pt.x, pt.y, 0, Handle, 0);
    // Handle the command. If the return value
    // from TrackPopupMenu is less than 100 then
    // a shell item was clicked.
    if (Cmd < 100 && Cmd != 0) {
      CI.lpVerb = MAKEINTRESOURCE(Cmd - 1);
      CI.lpParameters = "";
      CI.lpDirectory = "";
      CI.nShow = SW_SHOWNORMAL;
      CM->InvokeCommand(&CI);
    }
    // If Cmd is > 100 then it's one of our
    // inserted menu items.
    else
      // Find the menu item.
      for (int i=0;iItems->Count;i++) {
        TMenuItem* menu =
          PopupMenu1->Items->Items[i];
        // Call its OnClick handler.
        if (menu->Command == Cmd - 100)
          menu->OnClick(this);
      }
    // Release the memory allocated for the menu.
    DestroyMenu(hMenu);
  }
}    void __fastcall TForm1::Button2Click(TObject *Sender)
{
  if (OpenDialog->Execute())
    FileNameEdit->Text = OpenDialog->FileName;
}    void __fastcall TForm1::CloseBtnClick(TObject *Sender)
{
  Close();
}    // The OnClick handlers for the poup menu.
void __fastcall TForm1::Hello1Click(TObject *Sender)
{
  ShowMessage("Hello!");
}    void __fastcall TForm1::Test1Click(TObject *Sender)
{
  ShowMessage("This is a test.");
}
//-------------------------------------------------------------------    MainU.h
//------------------------------------------------------------------
//---------------------------------------------------------------------------    #ifndef MainUH
#define MainUH
//---------------------------------------------------------------------------
#include 
#include 
#include 
#include <Forms.hpp>
#include 
#include 
#include 
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published:        // IDE-managed Components
  TLabel *Label1;
  TEdit *FileNameEdit;
  TOpenDialog *OpenDialog;
  TButton *Button2;
  TButton *CloseBtn;
  TGroupBox *GroupBox1;
  TRadioButton *CutRb;
  TRadioButton *CopyRb;
  TRadioButton *DeleteRb;
  TRadioButton *PropertiesRb;
  TButton *GoBtn;
  TButton *ShowCMBtn;
  TPopupMenu *PopupMenu1;
  TMenuItem *Hello1;
  TMenuItem *Test1;
  TMenuItem *N1;
  void __fastcall GoBtnClick(TObject *Sender);
  void __fastcall FormCreate(TObject *Sender);
  void __fastcall FormDestroy(TObject *Sender);
  void __fastcall Button2Click(TObject *Sender);
  void __fastcall CloseBtnClick(TObject *Sender);
  void __fastcall Hello1Click(TObject *Sender);
  void __fastcall Test1Click(TObject *Sender);
private:        // User declarations
public:                // User declarations
        __fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
#if (__BORLANDC__ < 0x530)
  #define PACKAGE
#endif
//---------------------------------------------------------------------------
#endif    
/*生活是一種藝術,用心生活才能享受生活*/
發表人 - axsoft 於 2003/09/17 19:25:19 發表人 - axsoft 於 2003/09/17 19:30:37
小弟有件事還想請 axsoft 大大指導. 就是在此範例中如果是要 "一次Copy多個檔案到Clipboard裡" 程式該如何改寫呢? 謝謝您.
Egn
一般會員


發表:29
回覆:54
積分:16
註冊:2005-04-14

發送簡訊給我
#5 引用回覆 回覆 發表時間:2005-05-25 14:23:40 IP:61.220.xxx.xxx 未訂閱
我有去抓source code來執行,可是發縣不能新增ㄝ..就是我選了一個.exe檔,然後按下copy,可是沒有動做ㄝ..context menu沒有新增選項出來ㄝ..請問一下為什麼阿...#include 這行我也有加了阿
系統時間:2024-04-29 7:40:29
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!