1、怎么樣在delphi中調動其它*.exe文件? 例如:winexec(''d:\Project1.exe'',sw_show); ============================================================================== 2,、如何讓工程運行時主窗體就是最大化的? 答:設置主窗體的WindowsState屬性為wsMaximized就可以了! wsNormal 窗體以普通狀態(tài)顯示 wsMinimized 窗體以最小化狀態(tài)顯示,。 wsMaximized 窗體以最大化狀態(tài)顯示,。 ============================================================================== 3、我想先->閃現窗體->主窗體->登錄窗體,,工程源文件怎么設置,? 答: ⒈開始一個新工程。給表格起名為MainForm,MainForm的單元起名為Main, 工程文 件起名為Test,。 ⒉在MainForm中插入一個Button部件,,將其Caption屬性設為“關閉”,為該部件 的onClick事件創(chuàng)建一個過程,,并在過程的begin和end之間插入Close語句,。 ⒊在應用程序添加一個表格,將這個表格起名為MoveForm,,MoveForm 的單元起名 為Move,。 ⒋為便于演示,在MoveForm中插入一個Label部件,,設置其Caption 屬性為“歡迎 進入本系統”,。 5.下一步修改工程的源代碼。選擇View/Project Source,,修改begin和end之間的 語句如下: 程序清單Test.Dpr program Test uses forms, Main in ''MAIN.PAS'', Move in ''Move.PAS'' {$R *.RES} begin MoveForm:=TMoveForm.Create(Application); MoveForm.Show; MoveForm.Update; Application.CreateForm(TMainForm,MainForm); MoveForm.Hide; MoveForm.Free; Application.Run; end. 第一條語句創(chuàng)建了對象,,該對象存在內存中,但還不能看見,, 為了讓它出現并更 新它的內容,,調用對象的Show和Update成員函數:Show和Update。 當閃現窗口使 用完后,,用Hide函數將它隱藏起來,,然后用Free函數釋放它所占據的內存。 6.如果此刻你編譯和運行程序,,MoveForm窗口一閃而過,, 你可能未來得及看 清。為使MoveForm窗口顯示幾秒種,我們可為MainForm的OnCreate 事件創(chuàng)建一個 處理程序,,延遲MoveForm窗口的顯現時間,。 program TMainForm.FormCreate(sender:Tobject); var currentTime:LongInt; begin currentTime:=GetTickCount div 1000; while ((GetTickCount div 1000)<(currentTime+3) do {不做任何事); end; end. GetTickCount函數返回窗口啟動后過去的毫秒數,這個值除以1000 轉化為秒數,。 此時你編譯運行程序,就能得到一個延遲3秒多的閃現窗口,。 為閃現窗口添加上Image部件,,再對字體及窗口進行修飾,我們就能為應用程 序,,創(chuàng)建一個精美的封面或在程序啟動時顯示重要提示,。 制作登錄窗體一個很方便的方法就是主窗體作為主窗體,登錄成功Hide掉就行了,。 如果登錄窗體不可以作為主窗體,,那么和閃現窗體一樣的方法創(chuàng)建登錄窗體,加在Application.Run;之前,,MoveForm.Free;之后,, 用showmodal顯示登錄窗體 ============================================================================== 4、button上面的文字怎么樣換行,? 答: button控件不行 bitbtn控件可以,。 bitbtn1.caption:=''aaaa''#13''bbbbb'' ============================================================================== 5、怎么樣判別焦點是否在某個控件上,? 答: if Tobject.focused then //焦點在某某控件上 else ============================================================================== 6,、怎么樣在程序中使一個節(jié)點的子節(jié)點展開及收閉? 答: treeview1.selected.Expanded; //判斷節(jié)點的子節(jié)點是否展開True展開,,否則閉攏 treeview1.selected.Expand(True);//子節(jié)點展開 treeview1.selected.collapse(True)://子節(jié)點閉攏 樹節(jié)點全部展開: procedure TForm1.Button1Click(Sender: TObject); var node:TTreeNode; begin if treeview1.Items[0]<>nil then begin node:=treeview1.Items[0]; node.Expand(true); while node.getNextSibling<>nil do begin node:=node.getNextSibling; node.Expand(true); end; end; end; 樹節(jié)點全部收縮: procedure TForm1.Button2Click(Sender: TObject); var node:TTreeNode; begin if treeview1.Items[0]<>nil then begin node:=treeview1.Items[0]; node.Collapse(true); while node.getNextSibling<>nil do begin node:=node.getNextSibling; node.Collapse(true); end; end; end; ============================================================================== 7,、如何用delphi編程實現給access數據庫加密碼? 答:1,新建Project,。 2,,在FORM中放入ADOConnection控件。 3,,雙擊ADOConnection控件,,然后點擊Build...按鈕,在“提供者”頁中選擇“Microsoft Jet 4.0 OLE DB Provider”,,然后點擊“下一步”按鈕,,在“連接”頁中選擇要連接的Access數據庫的路徑和數據庫的文件名,這時如果點“測試連接”按鈕 時,,出現“初始化提供者時發(fā)生錯誤,,測試連接失敗,密碼無效”的錯誤提示。 4,,這時點“所有”頁,,然后雙擊“Jet OLEDB:Database Password”,出現對話框,,添入密碼后,,選擇“連接”頁中的“測試連接”按鈕,出現“測試連接成功”的對話框,。把ADOConnection控件的 LoginPromtp設為false. 5,,設置連接完成。 ============================================================================== 8,、如何判斷Treeview中我選中的節(jié)點是否有子節(jié)點,?如果沒有給出提示啊? 答: if Treeview.Selected.HasChildren then //有 else //無 var Node :TTreeNode; begin Node :=TreeView1.Selected; if Node.HasChildren then .... 對復雜的程序最好用Node過渡 ============================================================================== 9、能否解釋一下try...except...end及try...finally...end;? 1.(1)是用于撲捉異常,,(2)是用于保證代碼執(zhí)行的完整性 2.(1)中finally處的代碼不管什么情況都會被執(zhí)行,,(2)中except處的代碼僅在發(fā)生異常時才會執(zhí)行 3.try finally之間的代碼雖可保證finally 和 end之間的程序能執(zhí)行,但不能保證程序不崩潰, 而try except就不會使程序崩潰 ============================================================================== 10、怎么樣在主程序控制器中加入音樂,? 在implementation下加入 mmsystem單元(windows多媒體函數動態(tài)聯結庫),。然後在的onShow,onCreate事件中編寫代碼:sndplaysound(''sound.wav'',snd_async) ============================================================================== 11、我在form1上有四個edit,輸完后我想用下上箭頭鍵進行上移下移,?怎么辦,? 答: procedure TForm1.Edit1KeyDown(Sender: TObject; var Key: Word; Shift: TShiftState); begin if key=vk_down then perform(WM_NEXTDLGCTL,0,0) else if key=vk_up then perform(WM_NEXTDLGCTL,1,0); end; ============================================================================== 12、如何用delphi5實現讀文本文件指定的一行,,并得到文本文件的總行數,?謝謝! 答: Delphi讀文件文件一般使用Readln過程,,如要讀第3行可以這樣: var i : Integer; F: TextFile; S: string; begin if OpenDialog1.Execute then { Display Open dialog box } begin AssignFile(F, OpenDialog1.FileName); { File selected in dialog } Reset(F); For i = 1 To 3 Do Readln(F, S); Edit1.Text := S; { Put string in a TEdit control } CloseFile(F); . end; 要統計總行數,,只能從頭逐行讀,直到文件尾(Eof函數為True),,每讀一行計數器加1,。 不過由于文本文件的每行長度不相等,它不能象數據庫文件那樣想讀那行就讀哪行,,只能順序讀,。 上面的方法容易理解,也容易實現,。如果希望提高速度,,編程上要麻煩一些,可以以二進制方式打開文件,,將所有內容讀入一個內存變量,,然后使用Pos函數查找其中的回車(#13)個數,這樣可以快速地統計總行數并能快速地找到指定行。 ============================================================================== 13,、制作主窗口顯示前的版權窗口 答: 在 工程文件中選File->New Form新建一個窗口,,設計好窗口的外觀。給窗口起名為AboutBox,選Project->Options,,將新建的窗口從自動建立中去掉,。 選View->Project Source,打開工程文件的源文件,,在下面加入紅色的句子,。 Uses AboutBox Var lTime :TDateTime; Begin Application.Initialize(); AboutBox=TAboutBox.Create(AboutBox); AboutBox.Show; AboutBox.Update; lTime=GetTickCount; Application.CreateForm(TMainForm,MainForm); while((GetTickCount-lTime) / 1000 <3) do; AboutBox.Hide; AboutBox.Free; Application.Run; end; ============================================================================== 14、Delphi中RichEdit的奧妙 一,、如何得知當前行號 用RichEdit(或者memo)控件制作文本編輯器時,通過訪問lines?count屬性可以得到總行數,,但是若想知道光標當前所在行的行號就麻煩了,,因為delphi沒有提供這個屬性。要實現這個編輯器必備功能,,就須調用em_ LineFromChar,。 請試試下面的程序。 先在窗口中布置一個RichEdit或者memo(命名為editor),,以及一個button,。在button的onclick事件中寫入下列代碼。 var CurrentLine:Integer; begin CurrentLine:=Editor.Perform(em_ LineFromChar,SFFFF,0); Application.MessageBox(PChar(′當前行號是′+IntToStr(CurrentLine)),′消息′,mb_ iconinformation); end; 需要注意的是,,第一行的行號為零,。 二、如何撤消操作(undo) 對于memo來說,,實現undo是不需編程的,,只要讓popupmenu屬性為空,運行時就能用鼠標右鍵激活一個常用操作菜單,,其中包括撤消,、剪切、復 制,、粘貼,、刪除和全選六項。 但可惜的是,,這一招對于功能強大的RichEdit控件居然行不通,,害得我們還要自己設計一個popupmemu。當你用 CutToClipBoard等語句輕松而順利地完成了“剪切”等功能,,接著便會無奈地發(fā)現,,竟找不到undo或cancel之類的語句來執(zhí)行“撤消”。 這時你需要這樣處理: RichEdit1?Perform(EM_UNDO,0,0); 另外還應檢查是否允許撤消,從而開啟或關閉彈出菜單中的“撤消”項: Undo1?Enabled:=RichEdit? Perform(EM_CANUNDO,0,0)<>0; 以上程序在Delphi3中調試通過,?! ? ============================================================================== 15、在主窗口中打開另一個獨立的窗口,而這個被打開的窗口固定顯示在..? 答: procedure TForm2.FormCreate(Sender: TObject); begin form2.Hide; self.Parent:=form1.Panel1; end; ============================================================================== 16,、SaveDialog1確認文件存不存在的辦法,? 答: procedure TForm1.SaveDialog1CanClose(Sender: TObject; var CanClose: Boolean); begin if FileExists(SaveDialog1.FileName) then //如果文件已經存在 if MessageDlg(''文件已經存在,保存嗎,?'', mtConfirmation, [mbYes, mbNo], 0) <> mrYes then Button2.Click ; //如果選擇了覆蓋,,則退出,否則,,重新讓用戶選擇文件 end; ============================================================================== 17,、正確關閉一個MDI子窗口? 答: Delphi中MDI子窗口的關閉方式默認為縮小而不是關閉,,所以當你單擊子窗口右上角的關閉按鈕時會發(fā)覺該子窗口只是最小化,,而不是你預期的那樣被關閉。解決辦法是在子窗口的OnClose事件處理過程中加入如下代碼,,示例: procedure ChildForm.OnClose(Sender: TObject; var Action: TCloseAction); begin Action := caFree; end; Delphi為一個Form的關閉行為指定了四種方式,,分別是: caNone 禁止Form被關閉 caHide Form不被關閉,但是被隱藏,。被隱藏的Form仍然可以被程序訪問,。 caFree Form被關閉,并且釋放其占用的資源,。 caMinimize Form被最小化而不是被關閉,,這是MDI子窗口的默認關閉行為。 ============================================================================== 18,、怎樣記MDI子窗口不在母體運行時就被打開,? 答: 在project下的options中forms里面除了form1外,其余的移到右邊的框里,,然后在調用顯示的按鈕下編寫語句,,以form2調用為例: form2:=Tform2.create(self); form2.show; ============================================================================== 19、限制FORM的大小 答: 在FORM私有聲明部分加上如下一行: procedure WMGetMinMaxInfo( var Message:TWMGetMinMaxInfo );message WM_GETMINMAXINFO; 在聲明部分加上如下幾行: procedure TForm1.WMGetMinMaxInfo( var Message :TWMGetMinMaxInfo ); begin with Message.MinMaxInfo^ do begin ptMaxSize.X := 200; ptMaxSize.Y := 200; ptMaxPosition.X := 99; ptMaxPosition.Y := 99; end; Message.Result := 0; {告訴Windows你改變了 minmaxinfo} inherited; end; ============================================================================== 20,、隨機數生成法 答: Randomize; rn:=inttostr(random(9999)); rn1:=inttostr(random(9999)); ..... ============================================================================== 21,、怎樣把程序隱藏起來,在WINDOWS界面上沒有顯示,?,? 答: 在application.run之前加入application.showmain:=false! ============================================================================== 22、怎樣將一個form1.free的form1窗體重新顯示,? 答: form2:=TForm2.Create(application); form2.Show; 如果你要創(chuàng)建的Form2窗體能嵌入一個Panel中,,指定Parent: form2:=TForm2.Create(application); form2.Parent:=panel1; form2.Show; ============================================================================== 23,、我想在bitbtn上設快捷按鈕Esc,怎么辦,? 答: procedure TForm1.BitBtn1KeyDown(Sender: TObject; var Key: Word; Shift: TShiftState); begin if key=27 then application.Terminate; end; 設它的cancel屬性為true就行了~~ ============================================================================== 24,、什么叫做托盤區(qū)? 答: 托盤區(qū)就是在windows的狀態(tài)欄下方顯示時鐘,、輸入法狀態(tài)的地方,, 要把你的程序顯示在托盤區(qū): 下面是一個托盤類,只要把下面粘貼到文本文件中,,改成TrayIcon.pas,,使用時uses TrayIcon就可以了。 先聲明一個全局變量: var tray:TTrayNotifyIcon; 然后在窗體的OnCreate事件中: tray:=TTrayNotifyIcon.Create(self);//將窗體創(chuàng)建為托盤 tray.Icon:=application.Icon;//定義托盤的顯示圖標 tray.IconVisible:=true;//托盤可見 tray.PopupMenu:=popmenu;//給托盤定義一個右擊時的彈出菜單 tray.OnDblClick:=trayDblClick;//給托盤定義一個雙擊事件(當然要自己寫了,,不過多數情況只有一行,,就是Form1.show); unit TrayIcon; interface uses Windows, SysUtils, Messages, ShellAPI, Classes, Graphics, Forms, Menus, StdCtrls, ExtCtrls; type ENotifyIconError = class(Exception); TTrayNotifyIcon = class(TComponent) private FDefaultIcon: THandle; FIcon: TIcon; FHideTask: Boolean; FHint: string; FIconVisible: Boolean; FPopupMenu: TPopupMenu; FonClick: TNotifyEvent; FOnDblClick: TNotifyEvent; FNoShowClick: Boolean; FTimer: TTimer; Tnd: TNotifyIconData; procedure SetIcon(Value: TIcon); procedure SetHideTask(Value: Boolean); procedure SetHint(Value: string); procedure SetIconVisible(Value: Boolean); procedure SetPopupMenu(Value: TPopupMenu); procedure SendTrayMessage(Msg: DWORD; Flags: UINT); function ActiveIconHandle: THandle; procedure OnButtonTimer(Sender: TObject); protected procedure Loaded; override; procedure LoadDefaultIcon; virtual; procedure Notification(AComponent: TComponent; Operation: TOperation); override; public constructor Create(AOwner: TComponent); override; destructor Destroy; override; published property Icon: TIcon read FIcon write SetIcon; property HideTask: Boolean read FHideTask write SetHideTask default False; property Hint: String read FHint write SetHint; property IconVisible: Boolean read FIconVisible write SetIconVisible default False; property PopupMenu: TPopupMenu read FPopupMenu write SetPopupMenu; property onClick: TNotifyEvent read FonClick write FonClick; property OnDblClick: TNotifyEvent read FOnDblClick write FOnDblClick; end; implementation { TIconManager } { This class creates a hidden window which handles and routes } { tray icon messages } type TIconManager = class private FHWindow: HWnd; procedure TrayWndProc(var Message: TMessage); public constructor Create; destructor Destroy; override; property HWindow: HWnd read FHWindow write FHWindow; end; var IconMgr: TIconManager; DDGM_TRAYICON: Cardinal; constructor TIconManager.Create; begin FHWindow := AllocateHWnd(TrayWndProc); end; destructor TIconManager.Destroy; begin if FHWindow <> 0 then DeallocateHWnd(FHWindow); inherited Destroy; end; procedure TIconManager.TrayWndProc(var Message: TMessage); { This allows us to handle all tray callback messages } { from within the context of the component. } var Pt: TPoint; TheIcon: TTrayNotifyIcon; begin with Message do begin { if it’s the tray callback message } if (Msg = DDGM_TRAYICON) then begin TheIcon := TTrayNotifyIcon(WParam); case lParam of { enable timer on first mouse down. } { onClick will be fired by OnTimer method, provided } { double click has not occurred. } WM_LBUTTONDOWN: TheIcon.FTimer.Enabled := True; { Set no click flag on double click. This will supress } { the single click. } WM_LBUTTONDBLCLK: begin TheIcon.FNoShowClick := True; if Assigned(TheIcon.FOnDblClick) then TheIcon.FOnDblClick(Self); end; WM_RBUTTONDOWN: begin if Assigned(TheIcon.FPopupMenu) then begin { Call to SetForegroundWindow is required by API } SetForegroundWindow(IconMgr.HWindow); { Popup local menu at the cursor position. } GetCursorPos(Pt); TheIcon.FPopupMenu.Popup(Pt.X, Pt.Y); { Message post required by API to force task switch } PostMessage(IconMgr.HWindow, WM_USER, 0, 0); end; end; end; end else { If it isn’t a tray callback message, then call DefWindowProc } Result := DefWindowProc(FHWindow, Msg, wParam, lParam); end; end; { TTrayNotifyIcon } constructor TTrayNotifyIcon.Create(AOwner: TComponent); begin inherited Create(AOwner); FIcon := TIcon.Create; FTimer := TTimer.Create(Self); with FTimer do begin Enabled := False; Interval := GetDoubleClickTime; OnTimer := OnButtonTimer; end; { Keep default windows icon handy... } LoadDefaultIcon; end; destructor TTrayNotifyIcon.Destroy; begin if FIconVisible then SetIconVisible(False); // destroy icon FIcon.Free; // free stuff FTimer.Free; inherited Destroy; end; function TTrayNotifyIcon.ActiveIconHandle: THandle; { Returns handle of active icon } begin { If no icon is loaded, then return default icon } if (FIcon.Handle <> 0) then Result := FIcon.Handle else Result := FDefaultIcon; end; procedure TTrayNotifyIcon.LoadDefaultIcon; { Loads default window icon to keep it handy. } { This will allow the component to use the windows logo } { icon as the default when no icon is selected in the } { Icon property. } begin FDefaultIcon := LoadIcon(0, IDI_WINLOGO); end; procedure TTrayNotifyIcon.Loaded; { Called after component is loaded from stream } begin inherited Loaded; { if icon is supposed to be visible, create it. } if FIconVisible then SendTrayMessage(NIM_ADD, NIF_MESSAGE or NIF_ICON or NIF_TIP); end; procedure TTrayNotifyIcon.Notification(AComponent: TComponent; Operation: TOperation); begin inherited Notification(AComponent, Operation); if (Operation = opRemove) and (AComponent = PopupMenu) then PopupMenu := nil; end; procedure TTrayNotifyIcon.OnButtonTimer(Sender: TObject); { Timer used to keep track of time between two clicks of a } { double click. This delays the first click long enough to } { ensure that a double click hasn’t occurred. The whole } { point of these gymnastics is to allow the component to } { receive onClicks and OnDblClicks independently. } begin { Disable timer because we only want it to fire once. } FTimer.Enabled := False; { if double click has not occurred, then fire single click. } if (not FNoShowClick) and Assigned(FonClick) then FonClick(Self); FNoShowClick := False; // reset flag end; procedure TTrayNotifyIcon.SendTrayMessage(Msg: DWORD; Flags: UINT); { This method wraps up the call to the API’s Shell_NotifyIcon } begin { Fill up record with appropriate values } with Tnd do begin cbSize := SizeOf(Tnd); StrPLCopy(szTip, PChar(FHint), SizeOf(szTip)); uFlags := Flags; uID := UINT(Self); Wnd := IconMgr.HWindow; uCallbackMessage := DDGM_TRAYICON; hIcon := ActiveIconHandle; end; Shell_NotifyIcon(Msg, @Tnd); end; procedure TTrayNotifyIcon.SetHideTask(Value: Boolean); { Write method for HideTask property } const { Flags to show application normally or hide it } ShowArray: array[Boolean] of integer = (sw_ShowNormal, sw_Hide); begin if FHideTask <> Value then begin FHideTask := Value; { Don’t do anything in design mode } if not (csDesigning in ComponentState) then ShowWindow(Application.Handle, ShowArray[FHideTask]); end; end; procedure TTrayNotifyIcon.SetHint(Value: string); { Set method for Hint property } begin if FHint <> Value then begin FHint := Value; if FIconVisible then { Change hint on icon on tray notification area } SendTrayMessage(NIM_MODIFY, NIF_TIP); end; end; procedure TTrayNotifyIcon.SetIcon(Value: TIcon); { Write method for Icon property. } begin FIcon.Assign(Value); // set new icon { Change icon on notification tray } if FIconVisible then SendTrayMessage(NIM_MODIFY, NIF_ICON); end; procedure TTrayNotifyIcon.SetIconVisible(Value: Boolean); { Write method for IconVisible property } const { Flags to add or delete a tray notification icon } MsgArray: array[Boolean] of DWORD = (NIM_Delete, NIM_ADD); begin if FIconVisible <> Value then begin FIconVisible := Value; { Set icon as appropriate } SendTrayMessage(MsgArray[Value], NIF_MESSAGE or NIF_ICON or NIF_TIP); end; end; procedure TTrayNotifyIcon.SetPopupMenu(Value: TPopupMenu); { Write method for PopupMenu property } begin FPopupMenu := Value; if Value <> nil then Value.FreeNotification(Self); end; const { String to identify registered window message } TrayMsgStr = ’DDG.TrayNotifyIconMsg’; initialization { Get a unique windows message ID for tray callback } DDGM_TRAYICON := RegisterWindowMessage(TrayMsgStr); IconMgr := TIconManager.Create; finalization IconMgr.Free; end. ============================================================================== 25、關于窗體釋放的問題(formX.free),? 答: 這個我知道,模式窗口用:form2 := TForm2.Create(Application); try if form2.showModal = mrOK then {do Something} finally form2.free; form2 := nil; end; 非模式窗口用:if not Assigned(form2) then form2 := Tfrom2.Create(Application); form2.show; //然后在form2的Close事件中加入以下句 Action := caFree; //在from2的Destory事件中加入以下句 form2 := nil; 搞定!!! ============================================================================== 26,、關于MDI窗體的問題? 答: 我不知道是如何實現,但我知道一個方法可以實現同樣的功能,在打開子窗體前加一句 button1.SendToBack; ============================================================================== 27,、小數點''.''的鍵號是什么?回車是#13,,''.''是什么,? 答: 你可以用此得出所有鍵的值procedure TForm1.Edit1KeyDown(Sender: TObject; var Key: Word; Shift: TShiftState); begin label1.caption:=IntToStr(key); end; |
|