Jiangsheng的CSDN Digest(April 3, 2006)

 爲了便於搜索,這裏儘可能保留了論壇上討論的原文,但是這並不表示本人贊同帖子中的表述方式和觀點。

CSDN 討論總結系列:


ComboBox在datagrid如何模糊查詢(.NET技術 C#)


想在datagrid的一列中放一個ComboBox,當輸入一個具體值時,能到某一綁定的數據源裏面把匹配的的數據用下拉德形式顯示出來,也就是實現一個快速篩選的作用。比如說,我輸入a,則在下啦列表中出現以a開頭的所有記錄,再輸入b時,在下拉列表中出現以ab開頭的所有記錄。


prevent unnecessary post backs such like this
use client side javascript and XML HTTP request.

http://weblogs.asp.net/mschwarz/archive/2005/11/11/430267.aspx


如何用宏限制WORD文檔打印(其他開發語言 Office開發/ VBA)


A DocumentBeforePrint event procedure looks like this:

Private Sub oApp_DocumentBeforePrint(ByVal Doc As Document, _ Cancel As Boolean) 'Your code here End Sub



If you want to prevent printing from occurring in certain circumstances, you can set the Cancel variable to True, e.g.:

Private Sub oApp_DocumentBeforePrint(ByVal Doc As Document, _ Cancel As Boolean) Dim Result As Long Result = MsgBox("Have you checked the " & "printer for letterhead paper?", vbYesNo) If Result = vbNo Then Cancel = True End Sub


http://word.mvps.org/FAQs/MacrosVBA/InterceptSavePrint.htm


CTreeCtrl 如何用SetItemData使Item保持額外數據(VC/MFC 界面)


你SetItemData之後將其刪除了,這樣它引用的就是無效數據了,這樣可能會引起程序的崩潰。

You should not delete the item data when it is being used by an item;
handle TVN_DELETEITEM to delete the itemdata when it is no longer used.

BTW, you need to reconsider if you need a tree control at all. Tree control sends several messages for each item. Destroying 40,000 items entails 40,000 TVN_DELETEITEM notifications. Just by sheer numbers that's going to take a while. 40,000 items in a treeview is unusably excessive.

Suggest reading:
http://www.codeproject.com/treectrl/waitingtreectrl.asp
 


在CHTMLView中,取IHTMLWindow2接口時,程序會死掉(VC/MFC HTML/XML )


在CHTMLView中調用IHTMLWindow2,來執行JS代碼,可當此view中顯示的不是網頁,而是word文檔內容,或是本地路徑的文件夾內容時,程序運行到pIDoc->get_parentWindow(&pIhtmlwindow2);就死掉了,程序進行了get_parentWindow()就出不來了。
 具體代碼如下:
   

IHTMLDocument2* pIDoc = (IHTMLDocument2*) GetHtmlDocument(); HRESULT hr; if(pIDoc ==NULL) return FALSE; IHTMLWindow2 * pIhtmlwindow2 = NULL;//= new IHTMLWindow2; hr = pIDoc->get_parentWindow(&pIhtmlwindow2); if(FAILED(hr)) { pIDoc->Release(); return FALSE; }

 


 


 

(IHTMLDocument2*) GetHtmlDocument();


這個有問題
GetHtmlDocument()返回的不是HtmlDocument,是IDispatch。你可以用如下方法來獲取文檔的IHTMLDocument2接口

CComPtr<IDispatch> spDisp; spDisp.p = CHtmlView::GetHtmlDocument(); CComQIPtr<IHTMLDocument2> pHTMLDocument2(spDisp);

不過對於你這種情況,GetHtmlDocument()返回的是Word的_Document的IDispatch接口,不是HTMLDocument的,用IHTMLDocument2接口訪問當然會出問題。用

CComQIPtr<IHTMLDocument2> pHTMLDocument2(spDisp);


之後判斷pHTMLDocument2是否爲空。


CFileDialog和SHBrowseForFolder瀏覽限制(VC/MFC 基礎類 )


我如何將CFileDialog和SHBrowseForFolder的對話框瀏覽的路徑限制在E盤?即其它盤屏蔽掉。
如果不能屏蔽的話,當選擇別的盤符自動跳回到E盤。


CFileDialog 不行,不過你可以在DoModal之後提示路徑不符並且重新顯示一個CFileDialog
用SHBrowseForFolder的話可以直接設置BROWSEINFO的pidlRoot成員爲E:的pidl。


SHBrowseForFolder可以用來選擇單個文件(BROWSEINFO.ulFlags|=BIF_BROWSEINCLUDEFILES),你也可以考慮重載BOOL CFileDialog::OnFileNameOK
過濾文件:http://msdn.microsoft.com/msdnmag/issues/05/06/CAtWork/default.aspx


如何知道某個網頁是已經訪問過的(VC/MFC 網絡編程)


用CHtmlView寫個瀏覽器,給定一個網頁地址字串,如何知道它是不是已經訪問過的?

用IE瀏覽的時候,訪問過的地址會變色.如果在自己的程序裏,如何判斷?一定要做數據庫記錄比較嗎?還是有便捷高效的方法?


IUrlHistoryStg::QueryUrl


將Delphi作爲ASP.NET的腳本語言(.NET技術 ASP.NET )


看到下面這篇文章
http://www.aspcool.com/lanmu/browse1.asp?ID=2230&bbsuser=aspnet
想將Delphi做爲ASP.NET的腳本語言

使如下代碼執行

<html> <script language="Delphi" runat="server"> procedure ButtonClick(Sender: System.Object; E: EventArgs); begin Message.Text := Edit1.Text; end; </script> <body> <form runat="server"> <asp:textbox id="Edit1" runat="server"/> <asp:button text="Click Me!" OnClick="ButtonClick" runat="server"/> </form> <p><b><asp:label id="Message" runat="server"/></b></p> </body> </html>


但是這篇文章使用的是Delphi 7
我想問問大家如何使用Delphi 2006啊
具體就是如何修改以下文件
%systemroot%/Microsoft.net/Framework/v1.1.4322/CONFIG/machine.config
將Delphi作爲ASP.NET的腳本語言


you can write code behind pages that inherit from assemblies compiled from Delphi source files.

支持ASP.NET的第一件事是讓ASP.NET將Delphi視爲腳本語言,讓ASP.NET能夠爲各種ASP文件類型調用Delphi的.NET編譯器。

ASP.NET要在IIS虛路徑的根目錄下尋找web.config文件。下面是ASP.NET中使用Delphi作腳本語言的web.config配製文件內容:

<configuration> <system.web> <compilation debug="true"> <assemblies> <add assembly="DelphiProvider" /> </assemblies> <compilers> <compiler language="Delphi" extension=".pas" type="Borland.Delphi.DelphiCodeProvider,DelphiProvider" /> </compilers> </compilation> </system.web> </configuration>


http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconformatofconfigurationfiles.asp


試過幾種編碼都不能正確顯示的一種漢字代碼(VC/MFC 硬件/系統 )


一文件的漢字內碼爲
3D 1B 24 42 4D 7B 31 51 1B 28 42 46 20 39 59 1B 2D 41

絕大多數軟件打開顯示的都是亂碼,改變不同編碼,例如 GB2312,UNICODE UTF-8,UNICODE UTF-16,GB18030,GBK,HZ, 也都是亂碼, 只有一種軟件打開能顯示爲:
李英F 9Y
經查閱,字符集應是一下某種,但不知如何轉化爲GB2312 ??
ISO 2022 IR 100/ISO 2022 IR 58/ISO 2022 IR 6/ISO 2022 IR 87


用MultibyteToWideChar和WideCharToMultibyte來轉換。ISO-2022-JP的代碼頁是50221。
參考
http://www.w3.org/International/O-charset-ms.html


ActiveX中關於獲取屬性的問題(VC/MFC ATL/ActiveX/COM )


建立一個OCX mytest,設置一個屬性filetype,在Active Control Test Container中加入mytest control
在Invoke Methods中選擇Method Name:filetype(PropPut)設置值,在程序中就能獲得filetype的值
但是如果在瀏覽器中調用

<OBJECT style="LEFT: 0px; TOP: 0px;WIDTH:500PX;HEIGHT:500PX;" classid=clsid:E87C2E03-5EE4-4AC1-91B2-0E48385584D6 VIEWASTEXT> <PARAM NAME="_Version" VALUE="6553"> <PARAM NAME="_ExtentX" VALUE="2646"> <PARAM NAME="_ExtentY" VALUE="1323"> <PARAM NAME="FILETYPE" VALUE="*.gif"> </OBJECT>

怎麼也獲取不到FILETYPE屬性的值


控件的數據的存儲方式是容器決定的。如果容器使用IPersistStreamInit和IPersistStorage 來保存數據,那麼COleControl::Serialize會被調用。但是對於你這樣使用IPersistPropertyBag來保存數據的情況下,你應該重載COleControl::DoPropExchange。


html 編碼的問題,就是把字符串"<"轉化爲"&lt;" (VC/MFC HTML/XML)


現在要把字符串中所有這類符號轉化爲相應的編碼字符串.不能用MFC.IL


use InternetCanonicalizeUrl or System::Web::UI::HtmlTextWriter
See also
http://msdn.microsoft.com/library/en-us/vclib/html/vclrfATLServerEncodingReference.asp


security descriptor的具體使用(VC/MFC 基礎類)


security descriptor的一般概念我會一點,但是現在我需要完成一個具體的任務:在CreateProcess函數的參數中設置安全選項,要求是讓系統中只有該進程的創建者(也就是CreateProcess函數的調用進程)才能完全訪問到這個被新創建的進程對象,而假如同一個機器上任意其他第三個用戶進程試圖使用諸如WriteProcessMemory之類的函數來改寫被創建的進程對象的內存區域的時候,會因爲權限不夠而失敗。


Writing to a process address space is controlled by the SecurityDescrptor of the Process Object and by the SeDebug privilege.
By default, you can always debug your own precesses, and, the Administrators are granted the SeDebug privilege. Protecting memory inspection from a debugger is pretty much a lost cause (unless you can leverage natural security-principal restrictions).

A determined person can always read the physical page from a driver/kernel-debugger.

If you want to run a process under user account, but protected from other processes of the same user, you need to make sure the process owner (in its ACL) is something privileged (LOCAL_SYSTEM, LOCAL_SERVICE). A service should start a process with an user token, but with a security descriptor which disallows most access.

SeDebugPrivilege can easily override any DACL if the person has the privilege enabled on their account

默認的權限設置可以在http://www.microsoft.com/technet/security/prodtech/windows2000/w2kccscg/w2kscgcc.mspx
找到
由於權限是基於用戶策略的,所以同一用戶創建的進程默認具有同樣的權限。進程級別的保護可以通過以不同身份執行進程,以及設置進程的用戶訪問控制列表來做到。
參見
http://www.tech-archive.net/Archive/Development/microsoft.public.win32.programmer.kernel/2004-06/0708.html
在用AdjustTokenPrivileges啓用了TOKEN_ADJUST_PRIVILEGES權限之後可以用SetKernelObjectSecurity設置進程的訪問權限
參考http://blog.csdn.net/jiangsheng/archive/2004/06/24/25563.aspx


CHtmlView某些情況下無法輸入回車的問題(VC/MFC HTML/XML)


載入的頁面中嵌有設爲編輯模式的層或內嵌框架(DIV contenteditable="true"),則無法響應回車。

找了很久都沒有搞定。

看了這個也無效
FAQ:WebBrowser Keystroke Problems
http://www.microsoft.com/mind/0499/faq/faq0499.asp

倒是搜到了這個:<
http://www.maxthon.com/support/history.htm
v 0.4
# Can not press enter in some web html editors and chat rooms
 


in the MFC 6.0 version of the CHTMLView based MFCIE sample, enter key in <DIV contenteditable> get swallowed. You can fix this by adding a TranslateMessage call

BOOL CMfcieView ::PreTranslateMessage(MSG* pMsg) { // TODO: Add your specialized code here and/or call the base class if (pMsg- >message == WM_KEYDOWN && (pMsg- >wParam == VK_RETURN | | pMsg- >wParam == VK_ESCAPE)) { ::TranslateMessage(pMsg); } return CHtmlView::PreTranslateMessage(pMsg); }



The problem occurs only when MFCIE is showing web page with a DHTML editor which has the focus. Otherwise CHtmlView intercepts enter key correctly even when the TranslateMessage call is absent. http://www.microsoft.com/mind/0499/faq/faq0499.asp suggested calling Win 32 API IsDialogMessage(), which is even worse. IDocHostUIHandler::TranslateAccelerator (and presumablly, IOleInPlaceActiveObject::TranslateAccelerator) is called and get ignored somewhere inside the web browser control. I can not reproduce this in MFC 7, but I can not find how it got fixed after WinDiffing the mfc sources, either.
 


DOS窗口下不能正常顯示Unicode(VC/MFC ATL/ActiveX/COM)


_putts(_T("English漢字/n"));
我用Unicode格式輸出的字符串,遇到漢字就結束了。
改成下面:
_tprintf_s(_T("%s/n"), _T("English漢字"));
其中漢字全變成了“?”

如果不能直接解決,請問怎麼把Unicode轉成ANSI?


use WideCharToMultiByte() to convert the unicode strings to MBCS before you print them to console with printf().

Alternatively, you might try changing the console code page with chcp or programatically (SetConsoleCP, SetConsoleOutputCP, SetFileApisToANSI). By default console uses OEM codepage.


綁定表達式的問題(.NET技術 ASP.NET)


本人在DataList里加了一個HyperLinker的控件
NavigateUrl屬性希望設置成fittingsbyproducer.aspx?pro=***
注:當前頁面不是fittingsbyproducer.aspx
其中***需要從數據源中讀取ProducerID字段數據,我寫不好,望各位指教


 

private void DataGridResult_ItemCreated(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e) { //只處理列表項 if(e.Item.ItemType ==ListItemType.Item||e.Item.ItemType == ListItemType.AlternatingItem) { //查找模板生成的控件並且修改其目標 System.Web.UI.WebControls.HyperLink hl=(HyperLink)e.Item.FindControl("HyperLinkTopic"); //注意對不同的數據源,e.Item.DataItem的類型也不同 System.Data.DataRowView rec=(System.Data.DataRowView)e.Item.DataItem; if(rec!=null) { //編碼會造成查詢時內存溢出的日文片假名 hl.Text=srwbbs.Global.JDecode(rec["Topic"].ToString()); //根據參數和值建立目標URL NameValueCollection nvc=new NameValueCollection(); nvc["ID"]=rec["ID"].ToString(); nvc["BoardID"]=rec["BoardID"].ToString(); string strURL="http://allgames.gamesh.com/srwbbs/topic.asp"; strURL+=srwbbs.Global.BuildQueryString(nvc).ToString(); //設置目標 hl.NavigateUrl=strURL; } } } //從字符串:值的集合建立URL的參數字符串 static public string BuildQueryString(NameValueCollection values) { StringBuilder sb = new StringBuilder(); for (int i =0; i < values.Count; ++ i) { sb.Append(i == 0 ? "?" : "&"); sb.Append(values.Keys[i]); sb.Append("="); sb.Append(HttpUtility.UrlEncode(values[i])); } return sb.ToString(); }

進程間通訊問題(VC/MFC 進程/線程/DLL )


在進程A中NEW 了一個套接字,然後啓動進程B。
通過共享內存等方法能否實現在B中使用A中套接字來發送和接受數據。


http://support.microsoft.com/default.aspx?scid=kb;en-us;150523


關於WebBrowser編輯中的自動換行問題(VC/MFC HTML/XML)


程序中使用了一個內嵌的WebBrowser,可編輯,現在問題是在WebBrowser中編輯文字時不會自動換行,即會出現HScroll bar,要回車才換行。有沒有人知道如何可以使WebBrowser編輯器能自動換行,而不出現HScroll bar


use IHTMLDocument2::get_body, IHTMLElement::get_style, QueryInterface for IHTMLStyle3, and call IHTMLStyle3::wordWrap with break-word


如何修改本地安全策略(.NET技術 VC.NET)


You can modify local policies using WMI in Windows XP/2003.
No such support in Windows 2000 or earlier.
Reference
http://www.microsoft.com/technet/prodtechnol/windowsserver2003/library/TechRef/4dca53ac-435b-45f7-9f5c-1cb77b7a6a39.mspx

You can also use DumpSec (http://www.somarsoft.com) instead.

You can do a call-out to the Win2k resource kit tool ntrights.exe for this:


ntrights.exe +r SeServiceLogonRight -u username


How to Set Logon User Rights with the Ntrights.exe Utility
http://support.microsoft.com/?kbid=279664 


How to: Determine NTRIGHTS Names and Meanings
http://support.microsoft.com/?kbid=245207
 

Ntrights.exe is also in the free Win2k3 resource kit (it very well might work
on Win2k as well):


Windows Server 2003 Resource Kit Tools
http://www.microsoft.com/downloads/details.aspx?FamilyID=9d467a69-57ff-4ae7-96ee-b18c4790cffd&DisplayLang=en


(The kit will install on WinXP or later, but you can copy Ntrights.exe to a
Win2k computer and try it out if necessary)


Or you can create your own program, an account can also be granted the user
right programmatically through the Local Security Authority (LSA) API calls.


132958 How to Manage User Privileges Programmatically in Windows NT
http://support.microsoft.com/default.aspx?scid=kb;EN-US;132958


讀取表格內容(VC/MFC HTML/XML )


獲得這樣的內容(bstrSrc):  

<td class="gridbodynobgColor">2200601883</td>


 
那麼請問如何只得到2200601883呢。  

VARIANT varIndex, var2; VariantInit(&varIndex); VariantInit(&var2); varIndex.vt=VT_I4; varIndex.lVal=i; CComPtr<IDispatch> pDisp3; pCollection3->item(varIndex,var2,&pDisp3); IHTMLElement* pElem3; pDisp3->QueryInterface(IID_IHTMLElement,(LPVOID*)&pElem3);

 
到這一步已經能獲得通過  

BSTR bstrSrc; pElem3->get_outerHTML(&bstrSrc);

我自己想這樣做:  

IHTMLTableCell* pTableCell=NULL; pElem3->QueryInterface(IID_IHTMLTableCell,(LPVOID*)&pTableCell);


 
這裏如何做我就不知道了。  
pTableCell->????  


 

query  for  IHTMLElement  and call get_innerText to  get  the text,  or  for IHTMLDOMNode  to  get  access  a child text  node


如何獲得本地所有ODBC數據源名(Delphi 數據庫相關)


用BDE中的TSession組件可以用如下方法獲得本地所有ODBC數據源名
Session1.GetAliasNames(Listbox1.items)
但是這樣的話,程序發佈的時候,需要打包BDE,程序本身很小,一打包就會變很大了,有沒有什麼其他方法可以獲得本地所有數據源名啊,ADO可以嗎?API怎麼實現?


...Extract the ODBC System Data Sources?
uses
Registry;

procedure TForm1.Button1Click(Sender: TObject); var n: Integer; List: TStringList; Reg: TRegistry; begin Reg := TRegistry.Create; try Reg.RootKey := HKEY_CURRENT_USER; Reg.LazyWrite := False; Reg.OpenKey('Software/ODBC/ODBC.INI/ODBC Data Sources', False); List := TStringList.Create; Reg.GetValueNames(List); ListBox1.Clear; for n := 0 to List.Count - 1 do ListBox1.Items.Add(List.Strings[n]); Reg.CloseKey; finally Reg.Free; end; end;

但是用Session1.GetAliasNames(Listbox1.items)的話,還能取得數據庫別名,用讀註冊表的方法不可以,有什麼更好的方法嗎?

An application can call SQLDataSources multiple times to retrieve all data source names. The Driver Manager retrieves this information from the system information. When there are no more data source names, the Driver Manager returns SQL_NO_DATA. If SQLDataSources is called with SQL_FETCH_NEXT immediately after it returns SQL_NO_DATA, it will return the first data source name. For information about how an application uses the information returned by SQLDataSources, see Choosing a Data Source or Driver.

If SQL_FETCH_NEXT is passed to SQLDataSources the very first time it is called, it will return the first data source name.

The driver determines how data source names are mapped to actual data sources.


http://msdn.microsoft.com/library/default.asp?url=/library/en-us/odbc/htm/odbcsqldatasources.asp


令人頭疼的MessageBox::Show (.NET技術 VC.NET )


想用彈出對話框顯示String字符串,

String *fmt = S"asdf"; MessageBox::Show( "ssss" );

但這樣不行,錯誤信息:
d:/CARTTRAIN/Form1.h(208) : error C2653: “MessageBoxA” : 不是類或命名空間名稱
d:/CARTTRAIN/Form1.h(208) : error C2660: “System::Windows::Forms::Control::Show” : 函數不接受 1 個參數

當然 MessageBox(NULL,("asdf"),0,0); 是可以的,但是想用MessageBox::Show( "ssss" );這種格式。


原因可能是我包含了兩個類所引起的,#include "DataArray.h".........,因爲去掉所有與包含的類相關的內容,去掉上面的#include就ok,但是我必須要包含這個類,請問該如何解決?


在包含DataArray.h之後
#undef MessageBox


怎樣在"EDIT框"編輯時自動切換爲全角/半角的輸入法(VC/MFC 基礎類)


 

hWnd = GetDlgItem(EDIT控件ID)->m_hWnd; HIMChImc; DWORDdwConvMode, dwSentMode; hImc = ImmGetContext(hWnd); ImmGetConversionStatus(hImc, &dwConvMode, &dwSentMode); ImmSetConversionStatus(hImc, dwConvMode | IME_CMODE_FULLSHAPE, dwSentMode)



參照上面代碼,可以設置爲全角
dwConvMode | IME_CMODE_FULLSHAPE改爲dwConvMode & ~IME_CMODE_FULLSHAPE
可以設置爲半角


用vb.net和asp.net怎麼實現程序自動將網頁下載到本地硬盤中(.NET技術 VB.NET)


比方說我想自動把RSS裏的新聞都下載到硬盤裏,如果網頁中有圖片以及js等,因其放在其它文件中,所以用以上代碼得不到,請問,如何實現像IE另存爲網頁一樣,把整個網頁拷貝下來


http://www.codeproject.com/aspnet/aspnethtml2mht.asp

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章