Rexchiang
一般會員
發表:8 回覆:8 積分:3 註冊:2003-05-22
發送簡訊給我
|
在站上看到有關於Contextmenuhandle的文章,但是卻是用Delphi寫的
小弟又看不懂Delphi,所以請教各位高手在BCB裏要如何作呢????
網路上有看到使用Type Library的範例(http://bdn.borland.com/article/0,1410,26650,00.html),但對我來說又有點難度,實在是看不太懂!!!!
所以還有別的方法嗎???謝謝
|
axsoft
版主
發表:681 回覆:1056 積分:969 註冊:2002-03-13
發送簡訊給我
|
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
發送簡訊給我
|
謝謝axsoft大大的指導,小弟現在就去試.....謝謝!!
|
alexkuo59
一般會員
發表:0 回覆:1 積分:0 註冊:2003-07-24
發送簡訊給我
|
引言:
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
發送簡訊給我
|
我有去抓source code來執行,可是發縣不能新增ㄝ..就是我選了一個.exe檔,然後按下copy,可是沒有動做ㄝ..context menu沒有新增選項出來ㄝ..請問一下為什麼阿...#include 這行我也有加了阿
|