m8815010
版主
![](./myimg/board/mystar.gif) ![](./myimg/board/mystar.gif) ![](./myimg/board/mystar.gif) ![](./myimg/board/mystar.gif)
![](images/icon_photo_none.gif) 發表:99 回覆:372 積分:289 註冊:2003-11-13
發送簡訊給我
|
引言:
我想在TPrintDialog 增加選項,列印本頁。
請問要怎麼做ㄚ? 謝謝
wsxcv123你好 : 我的方法是新做一個 class="code">
In .h
~~ class TForm1 : public TForm
{
__published: // IDE-managed Components
TButton *Button1;
TPrintDialog *PrintDialog1;
TMemo *Memo1;
void __fastcall Button1Click(TObject *Sender);
private: // User declarations
TWndMethod OldFormWndProc; //舊的Form訊息處理函式
void __fastcall FormWndProc(TMessage& Message); //新的Form訊息處理函式
public: // User declarations
__fastcall TForm1(TComponent* Owner);
}; class TMyPrintDialog : public TPrintDialog //新的PrintDialog物件
{
__published: // IDE-managed Components
private: // User declarations
public: // User declarations
__fastcall TMyPrintDialog(TComponent* Owner);
}; //---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1; ~~
In .cpp
~~ TForm1 *Form1; BOOL CALLBACK EnumChindWnd(HWND hwnd,LPARAM lparam); //列舉子視窗函式 TMyPrintDialog* mypntdlg; //新的PrintDialog物件
HWND mybtn; //新物件上面的button
WNDPROC OldBtnWndProc; //button的舊訊息處理函式
LRESULT CALLBACK BtnWndProc(HWND,UINT,WPARAM,LPARAM ); //button的新訊息處理函式 int i=0; //---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
OldFormWndProc = this->WindowProc;
this->WindowProc = FormWndProc;
}
//---------------------------------------------------------------------------
__fastcall TMyPrintDialog::TMyPrintDialog(TComponent* Owner) //新PrintDialog物件的constructor
: TPrintDialog(Owner)
{
PostMessage(Form1->Handle,WM_USER 1234,0,0); //要求Form1幫忙做新增button的動作
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormWndProc(TMessage& Message)
{
OldFormWndProc(Message); if (Message.Msg==WM_USER 1234) { //Form1幫忙新增button開始
HWND hwnd=NULL;
hwnd=mypntdlg->Handle; if (hwnd!=NULL)
EnumChildWindows(hwnd,(WNDENUMPROC)EnumChindWnd,0);
}
}
//---------------------------------------------------------------------------
BOOL CALLBACK EnumChindWnd(HWND hwnd,LPARAM lparam)
{
char TextName[128];
ZeroMemory(TextName,128); GetWindowText(hwnd,TextName,128); if (AnsiString(TextName)=="列印範圍") { //如果是〝列印範圍〞這個子視窗,則新增button在上面
mybtn=CreateWindow("BUTTON",
"列印本頁",
WS_CHILD | WS_VISIBLE,
146,
66,
65,
25,
hwnd,
NULL,
NULL,
NULL
); OldBtnWndProc=(WNDPROC)GetWindowLong(mybtn,GWL_WNDPROC); //設定button的事件函式
SetWindowLong(mybtn,GWL_WNDPROC,(LONG)BtnWndProc); return false;
} return true;
}
//---------------------------------------------------------------------------
LRESULT CALLBACK BtnWndProc(HWND hwnd,UINT Msg,WPARAM wParam,LPARAM lParam)
{
if (hwnd==mybtn) {
if (Msg==WM_LBUTTONDOWN) //如果是click事件時
Form1->Caption=IntToStr( i); //這裏撰寫列印本頁的相關程式碼
} //目前以這行程式測試事件是否正常運作而已 return CallWindowProc((FARPROC)OldBtnWndProc,hwnd,Msg,wParam,lParam);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
mypntdlg=new TMyPrintDialog(this); //開啟一個新PrintDialog物件測試之
mypntdlg->Execute();
} ~~ 以上,請參著 ! 發表人 -
|
saint
一般會員
![](./myimg/board/mystar_empty.gif)
![](images/icon_photo_none.gif) 發表:0 回覆:2 積分:0 註冊:2004-12-17
發送簡訊給我
|
m8815010你好
我試了你所post's code,我發現在mypntdlg->Handle是NULL,
所以一直都無法進入EnumChildWindows,可否告訴我
那裏出錯了 謝謝
//---------------------------------------------------------------------------
void __fastcall TForm1::FormWndProc(TMessage& Message)
{
OldFormWndProc(Message); if (Message.Msg==WM_USER+1234) { //Form1幫忙新增button開始
HWND hwnd=NULL;
hwnd=mypntdlg->Handle; if (hwnd!=NULL)
EnumChildWindows(hwnd,(WNDENUMPROC)EnumChindWnd,0);
}
}
|
m8815010
版主
![](./myimg/board/mystar.gif) ![](./myimg/board/mystar.gif) ![](./myimg/board/mystar.gif) ![](./myimg/board/mystar.gif)
![](images/icon_photo_none.gif) 發表:99 回覆:372 積分:289 註冊:2003-11-13
發送簡訊給我
|
引言:
m8815010你好
我試了你所post's code,我發現在mypntdlg->Handle是NULL,
所以一直都無法進入EnumChildWindows,可否告訴我
那裏出錯了 謝謝
//---------------------------------------------------------------------------
void __fastcall TForm1::FormWndProc(TMessage& Message)
{
OldFormWndProc(Message); if (Message.Msg==WM_USER 1234) { //Form1幫忙新增button開始
HWND hwnd=NULL;
hwnd=mypntdlg->Handle; if (hwnd!=NULL)
EnumChildWindows(hwnd,(WNDENUMPROC)EnumChindWnd,0);
}
}
saint你好< >: 又試了一下我的程式碼,應該還是沒問題的< >! 所以你應該再仔細看一下程式碼是否有完全cp到,是否有漏!? 程式碼分.h和.cpp兩個部份! .h的部份有一個新的TPrintDialog物件的定義,.cpp內有相關function的撰寫,基本上都是照抄即可,唯一注意的是這一段:
void __fastcall TForm1::Button1Click(TObject *Sender)
{
mypntdlg=new TMyPrintDialog(this); //開啟一個新PrintDialog物件測試之
mypntdlg->Execute();
}
這是你自已必需先在你的form裏面拉一個button,再在這個button的click事件內呼叫出一個新的TPringDialog物件出來測試,測試是否有改造成功! 大概就是這樣了,程式碼若不正常運作應該也不是Handle值一直為NULL這樣的反應,所以我覺得應該是程式碼沒cp完整的關係吧 !
|