??xml version="1.0" encoding="utf-8" standalone="yes"?>久久久久久曰本AV免费免费,久久A级毛片免费观看,久久福利青草精品资源站免费http://www.shnenglu.com/AutomateProgram/category/12533.html当音乐和传说在深夜中沉寂后,E序的每个字W还在蟩动!zh-cnMon, 18 Oct 2010 15:21:57 GMTMon, 18 Oct 2010 15:21:57 GMT60修改IAT实现本进EAPI HOOK http://www.shnenglu.com/AutomateProgram/archive/2010/10/14/129849.html漂漂漂漂Thu, 14 Oct 2010 07:42:00 GMThttp://www.shnenglu.com/AutomateProgram/archive/2010/10/14/129849.htmlhttp://www.shnenglu.com/AutomateProgram/comments/129849.htmlhttp://www.shnenglu.com/AutomateProgram/archive/2010/10/14/129849.html#Feedback0http://www.shnenglu.com/AutomateProgram/comments/commentRss/129849.htmlhttp://www.shnenglu.com/AutomateProgram/services/trackbacks/129849.html原文地址Q?a >http://blog.csdn.net/leeeryan/archive/2010/06/08/5656364.aspx

// APIHOOKImageDirectoryEntryToData.cpp : Defines the entry point for the console application.
//

#include 
"stdafx.h"
#include 
<windows.h>
#include 
<imagehlp.h>//ImageDirectoryEntryToData
#pragma comment(lib,"imagehlp.lib")

char *szModName = NULL ;
char *szHacked = "不好意思hookC!" ;  
DWORD dwHookFun ;  
PROC dwHookApiAddr;  
PIMAGE_IMPORT_DESCRIPTOR pImportDesc ;  
PIMAGE_THUNK_DATA32 pThunk;  
ULONG uSize ;  
/************************************************************/  
void MYhook()  
{  
    __asm  
    
{  
        mov  esp,ebp  
            push szHacked  
            pop  DWORD PTR [ebp
+12]  
            pop  ebp  
            jmp dwHookApiAddr  
    }
 
}


int main(int argc, char* argv[])
{
    HMODULE hInstance 
=GetModuleHandle(NULL);  
    dwHookFun 
= (DWORD)MYhook;  
    dwHookApiAddr 
= GetProcAddress(LoadLibrary("USER32.dll"), "MessageBoxA") ;  
    
//通过函数)ImageDirectoryEntryToData获得IAT  
    
    pImportDesc 
= (PIMAGE_IMPORT_DESCRIPTOR)ImageDirectoryEntryToData(hInstance,  
        TRUE,  
        IMAGE_DIRECTORY_ENTRY_IMPORT,  
        
&uSize) ;  
    
//扑ֈ要HOOK的函数所在的模块  
    
    
while(pImportDesc->Name)  
    
{  
        szModName 
= (char *)((PBYTE)hInstance+pImportDesc->Name) ;  
        
if (strcmp(szModName,"USER32.dll")==0)  
            
break ;   
        pImportDesc
++ ;  
    }
  
    
//原始的THUNK信息指针  
    pThunk= (PIMAGE_THUNK_DATA32)((PBYTE)hInstance+pImportDesc->FirstThunk) ;
    
for(;pThunk->u1.Function;pThunk++)  
    
{  
        PROC
* ppfn = (PROC*&pThunk->u1.Function;
        
if (*ppfn == dwHookApiAddr)  
        
{  
            VirtualProtect(
&pThunk->u1.Function, 4096,PAGE_READWRITE,0);  
            pThunk
->u1.Function = (PDWORD)dwHookFun;  
            
break ;  
        }
  
    }
  

    
//要hook下面q个API  
   MessageBoxA(0,"q是正常?","xicao",0); 

    
return 0;
}



漂漂 2010-10-14 15:42 发表评论
]]>
预编译头文g介绍和说?/title><link>http://www.shnenglu.com/AutomateProgram/archive/2010/10/14/129846.html</link><dc:creator>漂漂</dc:creator><author>漂漂</author><pubDate>Thu, 14 Oct 2010 07:14:00 GMT</pubDate><guid>http://www.shnenglu.com/AutomateProgram/archive/2010/10/14/129846.html</guid><wfw:comment>http://www.shnenglu.com/AutomateProgram/comments/129846.html</wfw:comment><comments>http://www.shnenglu.com/AutomateProgram/archive/2010/10/14/129846.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.shnenglu.com/AutomateProgram/comments/commentRss/129846.html</wfw:commentRss><trackback:ping>http://www.shnenglu.com/AutomateProgram/services/trackbacks/129846.html</trackback:ping><description><![CDATA[<p>原文地址Q?a >http://www.neu.edu.cn/cxsj/pointchart/c11/Untitled(3).html</a><br></p> <div align=left>TAG:<span>预编译和宏定?VC++,预编译头文g说明</span><span></span></div> <div align=left>TEXT:</div> <div align=left><span>所谓头文g预编译,是把一个工E?Project)中用的一些MFC标准头文?如Windows.H?Afxwin.H)预先~译Q以后该工程~译Ӟ不再~译q部分头文gQ仅仅用预~译的结果。这样可以加快编译速度Q节省时间?/span><span></span></div> <div align=left><span>   预编译头文g通过~译stdafx.cpp生成Q以工程名命名,׃预编译的头文件的后缀?#8220;pch”Q所以编译结果文件是projectname.pch?/span><span></span></div> <div align=left><span>   ~译器通过一个头文gstdafx.h来用预~译头文件。stdafx.hq个头文件名是可以在project的编译设|里指定的。编译器认ؓQ所有在指o#include "stdafx.h"前的代码都是预编译的Q它跌#include "stdafx. h"指oQ用projectname.pch~译q条指o之后的所有代码?/span><span></span></div> <div align=left><span>   因此Q所有的CPP实现文gW一条语句都是:#include "stdafx.h"?/span><span></span></div> <div align=left><span>   另外Q每一个实现文件CPP都包含了如下语句Q?/span><span></span></div> <div align=left>#ifdef _DEBUG</div> <div align=left>#undef THIS_FILE</div> <div align=left>static char THIS_FILE[] = __FILE__;</div> <div align=left>#endif</div> <div align=left><span>   q是表示Q如果生成调试版本,要指C当前文件的名称。__FILE__是一个宏Q在~译器编译过E中l它赋gؓ当前正在~译的文件名U?/span><span></span></div> <div align=left><span>   VC默认情况下用预~译?/Yu)Q不明白的在加入?h文g后编译时d现fatal errorC1010: 在查N~译头指令时遇到意外的文件结错误。解x法是在include头文件的地方加上#include"stdafx.h",或者打目属性,扑ֈ“C/C++”文g夹,单击“预编译头”属性页。修?#8220;创徏/使用预编译头”属性ؓ“不用预~译?#8221;?/span><span></span></div> <div align=left><br></div> <p><br><br>PS:<br>预编译头的概念: </p> <p>所谓的预编译头是把一个工E中的那一部分代码,预先~译好放在一个文仉(通常?</p> <p>?pch为扩展名?Q这个文件就UCؓ预编译头文gq些预先~译好的代码可以是Q何的 </p> <p>C/C++代码--------甚至是inline的函敎ͼ但是必须是稳定的Q在工程开发的q程中不?</p> <p>被经常改变。如果这些代码被修改Q则需要重新编译生成预~译头文件。注意生成预~?</p> <p>译头文g是很耗时间的。同时你得注意预~译头文仉常很大Q通常?-7M大。注意及 </p> <p>时清理那些没有用的预~译头文件?</p> <p>也许你会问:现在的编译器都有Time stamp的功能,~译器在~译整个工程的时候,?</p> <p>只会~译那些l过修改的文Ӟ而不会去~译那些从上ơ编译过Q到现在没有被修改过 </p> <p>的文件。那么ؓ什么还要预~译头文件呢Q答案在q里Q我们知道编译器是以文g为单 </p> <p>位编译的Q一个文件经q修改后Q会重新~译整个文gQ当然在q个文g里包含的所?</p> <p>头文件中的东西(.eg Macro, Preprocesser Q都要重新处理一遍。VC的预~译头文?</p> <p>保存的正是这部分信息。以避免每次都要重新处理q些头文件?</p> <p>预编译头的作用: </p> <p>Ҏ上文介绍Q预~译头文件的作用当然是提高便宜速度了,有了它你没有必要每次 </p> <p>都编译那些不需要经常改变的代码。编译性能当然提高了?</p> <p>预编译头的用: </p> <p>要用预~译_我们必须指定一个头文gQ这个头文g包含我们不会l常改变?</p> <p>代码和其他的头文Ӟ然后我们用这个头文g来生成一个预~译头文Ӟ.pch文gQ?</p> <p>惛_大家都知?StdAfx.hq个文g。很多h都认是VC提供的一?#8220;pȝU别”?</p> <p>Q编译器带的一个头文g。其实不是的Q这个文件可以是M名字的。我们来考察一?</p> <p>典型的由AppWizard生成的MFC Dialog Based E序的预~译头文件。(因ؓAppWizard </p> <p>会ؓ我们指定好如何用预~译头文Ӟ默认的是StdAfx.hQ这是VCL名字Q。我?</p> <p>会发现这个头文g里包含了以下的头文gQ?</p> <p>#include <afxwin.h> // MFC core and standard components </p> <p>#include <afxext.h> // MFC extensions </p> <p>#include <afxdisp.h> // MFC Automation classes </p> <p>#include <afxdtctl.h> // MFC support for Internet Explorer 4 </p> <p>Common Controls </p> <p>#include <afxcmn.h> </p> <p>q些正是使用MFC的必d含的头文Ӟ当然我们不太可能在我们的工程中修改这些头?</p> <p>件的Q所以说他们是稳定的?</p> <p>那么我们如何指定它来生成预编译头文g。我们知道一个头文g是不能编译的。所以我 </p> <p>们还需要一个cpp文g来生?pch 文g。这个文仉认的是StdAfx.cpp。在q个文g </p> <p>里只有一句代码就是:#include “Stdafx.h”。原因是理所当然的,我们仅仅是要它能 </p> <p>够编译而已?D?D?D也就是说Q要的只是它?cpp的扩展名。我们可以用/Yc~译开x?</p> <p>定StdAfx.cpp来生成一?pch文gQ通过/Fp~译开x指定生成的pch文g的名字。打 </p> <p>开project ->Setting->C/C++ 对话框。把Category指向Precompiled Header。在左边?</p> <p>树Ş视图里选择整个工程  </p> <p>Project Options(右下角的那个白的地方)可以看到 /Fp “debug/PCH.pch”Q这是?</p> <p>定生成的.pch文g的名字,默认的通常是 <工程?gt;.pchQ我的示例工E名是PCHQ?</p> <p>然后Q在左边的树形视N选择StdAfx.cpp.//q时只能选一个cpp文gQ?</p> <p>q时原来的Project Option变成?Source File OptionQ原来是工程Q现在是一个文?</p> <p>Q当然变了)。在q里我们可以看到 /Yc开养I/Yc的作用就是指定这个文件来创徏一?</p> <p>Pch文g?Yc后面的文件名是那个包含了E_代码的头文gQ一个工E里只能有一个文 </p> <p>件的可以有YC开兟뀂VC根据这个选项?StdAfx.cpp~译成一个Obj文g和一个PCH文g </p> <p>?</p> <p>然后我们再选择一个其它的文g来看看,//其他cpp文g </p> <p>在这里,Precomplier 选择?Use ⋯⋯⋯一,头文件是我们指定创徏PCH 文g的stda </p> <p>fx.h </p> <p>文g。事实上Q这里是使用工程里的讄Q(如图1Q?Yu”stdafx.h”?</p> <p>q样Q我们就讄好了预编译头文g。也是_我们可以使用预编译头功能了。以 </p> <p>下是注意事项Q?</p> <p>1):如果使用?YuQ就是说使用了预~译Q我们在每个.cpp文g的最开_我强调一?</p> <p>是最开_包含 你指定生pch文g?h文gQ默认是stdafx.hQ不然就会有问题。如 </p> <p>果你没有包含q个文gQ就告诉你Unexpected file end. 如果你不是在最开头包含的Q?</p> <p>你自p以下q道了Q绝Ҏ很惊人的效果?. </p> <p>fatal error C1010: unexpected end of file while looking for precompiled </p> <p>header directive </p> <p>Generating Code... </p> <p>2Q如果你把pch文g不小心丢了,~译的时候就会生很多的不正常的行ؓ。根据以?</p> <p>的分析,你只要让~译器生成一个pch文g。也是说把 stdafx.cppQ即指定/Yc的那?</p> <p>cpp文gQ从新编译一遍。当然你可以d?Rebuild All。简单一点就是选择那个cpp </p> <p>文gQ按一下Ctrl + F7可以了。不然可是很费旉的哦?</p> <img src ="http://www.shnenglu.com/AutomateProgram/aggbug/129846.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.shnenglu.com/AutomateProgram/" target="_blank">漂漂</a> 2010-10-14 15:14 <a href="http://www.shnenglu.com/AutomateProgram/archive/2010/10/14/129846.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Socket 一些知识和常识Qsocket入门http://www.shnenglu.com/AutomateProgram/archive/2010/10/11/129457.html漂漂漂漂Mon, 11 Oct 2010 09:23:00 GMThttp://www.shnenglu.com/AutomateProgram/archive/2010/10/11/129457.htmlhttp://www.shnenglu.com/AutomateProgram/comments/129457.htmlhttp://www.shnenglu.com/AutomateProgram/archive/2010/10/11/129457.html#Feedback0http://www.shnenglu.com/AutomateProgram/comments/commentRss/129457.htmlhttp://www.shnenglu.com/AutomateProgram/services/trackbacks/129457.htmlhttp://www.shnenglu.com/lapcca/archive/2010/09/10/126329.html

q两天看csdn有一些关于socket_包Qsocket~冲|的问题Q发现自׃是很清楚Q所以查资料了解记录一下:

一两个单概念长q接与短q接Q?br>1.长连?/p>

    Client方与Server方先建立通讯q接Q连接徏立后不断开Q?然后再进行报文发送和接收?/p>

2.短连?/p>

    Client方与Server每进行一ơ报文收发交易时才进行通讯q接Q交易完毕后立即断开q接。此U方式常用于一点对多点
通讯Q比如多个Clientq接一个Server.

?什么时候需要考虑_包问题?

1:如果利用tcp每次发送数据,׃Ҏ建立q接Q然后双方发送完一D|据后Q就关闭q接Q这样就不会出现_包问题Q因为只有一U包l构,cM于http协议Q。关闭连接主要要双方都发送closeq接Q参考tcp关闭协议Q。如QA需要发送一D字W串lBQ那么A与B建立q接Q然后发送双斚w默认好的协议字符?hello give me sth abour yourself"Q然后B收到报文后,将~冲区数据接?然后关闭q接Q这L包问题不用考虑刎ͼ因ؓ大家都知道是发送一D字W?br>
2Q如果发送数据无l构Q如文g传输Q这样发送方只管发送,接收方只接收存储就okQ也不用考虑_包

3Q如果双方徏立连接,需要在q接后一D|间内发送不同结构数据,如连接后Q有好几U结构:
1)"hello give me sth abour yourself"
2)"Don't give me sth abour yourself" 

 那这L话,如果发送方q箋发送这个两个包出去Q接收方一ơ接收可能会?hello give me sth abour yourselfDon't give me sth abour yourself" q样接收方就MQ到底是要干嘛?不知道,因ؓ协议没有规定q么诡异的字W串Q所以要处理把它分包Q怎么分也需要双方组l一个比较好的包l构Q所以一般可能会在头加一个数据长度之cȝ包,以确保接收?br>

?_包出现原因Q在传输中出现QUDP不会出现_包Q因为它有消息边?参考Windows |络~程)
1 发送端需要等~冲区满才发送出去,造成_包
2 接收方不及时接收~冲区的包,造成多个包接?/p>

解决办法Q?br>Z避免_包现象Q可采取以下几种措施。一是对于发送方引v的粘包现象,用户可通过~程讄来避免,TCP提供了强制数据立即传送的操作指opushQTCP软g收到该操作指令后Q就立即本D|据发送出去,而不必等待发送缓冲区满;二是对于接收方引L_包Q则可通过优化E序设计、精接收q程工作量、提高接收进E优先{措施,使其及时接收数据Q从而尽量避免出现粘包现象;三是由接收方控制Q将一包数据按l构字段Qh为控制分多次接收Q然后合qӞ通过q种手段来避免粘包?/p>

以上提到的三U措施,都有其不之处。第一U编E设|方法虽然可以避免发送方引v的粘包,但它关闭了优化算法,降低了网l发送效率,影响应用E序的性能Q一般不使用。第二种Ҏ只能减少出现_包的可能性,但ƈ不能完全避免_包Q当发送频率较高时Q或׃|络H发可能使某个时间段数据包到达接收方较快Q接收方q是有可能来不及接收Q从而导致粘包。第三种Ҏ虽然避免了粘包,但应用程序的效率较低Q对实时应用的场合不适合?/p>
解决_包问题Q?a >http://www.vckbase.com/document/viewdoc/?id=1203

在socket 文g传输中,获取某个目录下的所有文Ӟ如果一个文件名传输一ơ通讯的话Q?27.0.0.1上测试没有什么问题,当在局域网中传?ơ以上,׃出现包丢失问题。我猜这个问题跟_包有点怼?

漂漂 2010-10-11 17:23 发表评论
]]>
VC中常见错误和解决Ҏhttp://www.shnenglu.com/AutomateProgram/archive/2010/09/16/126789.html漂漂漂漂Thu, 16 Sep 2010 10:35:00 GMThttp://www.shnenglu.com/AutomateProgram/archive/2010/09/16/126789.htmlhttp://www.shnenglu.com/AutomateProgram/comments/126789.htmlhttp://www.shnenglu.com/AutomateProgram/archive/2010/09/16/126789.html#Feedback0http://www.shnenglu.com/AutomateProgram/comments/commentRss/126789.htmlhttp://www.shnenglu.com/AutomateProgram/services/trackbacks/126789.html链接错误Q?br>LINK : fatal error LNK1000: Internal error during IncrBuildImage

原文地址Q?a >http://www.a3gs.com/BookViews.asp?InfoID=3425&classID=819

?/span>Win7旗舰版下使用VS2008~译E序时出?/span>LINK : fatal error LNK1000: Internal error during IncrBuildImage错有如下两种解决ҎQ?/span>

Ҏ一Q打补丁

    到微软网站下?/span>343461_ENU_i386_zip补丁包,然后打补丁既可?/span>

Ҏ二:修改目讄

    ->属?/span>->链接?/span>->常规 下面?#8220;启用增量链接”Q将“?/span>(/INCREMENTAL)”改ؓ“?/span>(/INCREMENTAL:NO)”既可?br>
psQ找不到补丁包,按W二U方法?/span>



漂漂 2010-09-16 18:35 发表评论
]]>
g:安装、停用、usb讑֤?和c#实现http://www.shnenglu.com/AutomateProgram/archive/2010/09/13/126529.html漂漂漂漂Mon, 13 Sep 2010 13:27:00 GMThttp://www.shnenglu.com/AutomateProgram/archive/2010/09/13/126529.htmlhttp://www.shnenglu.com/AutomateProgram/comments/126529.htmlhttp://www.shnenglu.com/AutomateProgram/archive/2010/09/13/126529.html#Feedback0http://www.shnenglu.com/AutomateProgram/comments/commentRss/126529.htmlhttp://www.shnenglu.com/AutomateProgram/services/trackbacks/126529.html阅读全文

漂漂 2010-09-13 21:27 发表评论
]]>
vc/C++中用网l校正系l时?/title><link>http://www.shnenglu.com/AutomateProgram/archive/2010/06/16/118031.html</link><dc:creator>漂漂</dc:creator><author>漂漂</author><pubDate>Wed, 16 Jun 2010 10:09:00 GMT</pubDate><guid>http://www.shnenglu.com/AutomateProgram/archive/2010/06/16/118031.html</guid><wfw:comment>http://www.shnenglu.com/AutomateProgram/comments/118031.html</wfw:comment><comments>http://www.shnenglu.com/AutomateProgram/archive/2010/06/16/118031.html#Feedback</comments><slash:comments>2</slash:comments><wfw:commentRss>http://www.shnenglu.com/AutomateProgram/comments/commentRss/118031.html</wfw:commentRss><trackback:ping>http://www.shnenglu.com/AutomateProgram/services/trackbacks/118031.html</trackback:ping><description><![CDATA[<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><span style="COLOR: #008080"> 1</span><img src="http://www.shnenglu.com/Images/OutliningIndicators/None.gif" align=top><span style="COLOR: #000000">BOOL UpdateSysTime()<br></span><span style="COLOR: #008080"> 2</span><span style="COLOR: #000000"><img id=Codehighlighter1_21_2209_Open_Image onclick="this.style.display='none'; Codehighlighter1_21_2209_Open_Text.style.display='none'; Codehighlighter1_21_2209_Closed_Image.style.display='inline'; Codehighlighter1_21_2209_Closed_Text.style.display='inline';" src="http://www.shnenglu.com/Images/OutliningIndicators/ExpandedBlockStart.gif" align=top><img id=Codehighlighter1_21_2209_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_21_2209_Closed_Text.style.display='none'; Codehighlighter1_21_2209_Open_Image.style.display='inline'; Codehighlighter1_21_2209_Open_Text.style.display='inline';" src="http://www.shnenglu.com/Images/OutliningIndicators/ContractedBlock.gif" align=top></span><span id=Codehighlighter1_21_2209_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img src="http://www.shnenglu.com/Images/dot.gif"></span><span id=Codehighlighter1_21_2209_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080"> 3</span><span style="COLOR: #000000"><img src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>    WORD    wVersionRequested;<br></span><span style="COLOR: #008080"> 4</span><span style="COLOR: #000000"><img src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>    WSADATA wsaData;<br></span><span style="COLOR: #008080"> 5</span><span style="COLOR: #000000"><img src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>    <br></span><span style="COLOR: #008080"> 6</span><span style="COLOR: #000000"><img src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>    </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000"> 初始化版?/span><span style="COLOR: #008000"><br></span><span style="COLOR: #008080"> 7</span><span style="COLOR: #008000"><img src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top></span><span style="COLOR: #000000">    wVersionRequested </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> MAKEWORD( </span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">, </span><span style="COLOR: #000000">1</span><span style="COLOR: #000000"> );<br></span><span style="COLOR: #008080"> 8</span><span style="COLOR: #000000"><img src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>    </span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000"> (</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">!=</span><span style="COLOR: #000000">WSAStartup(wVersionRequested, </span><span style="COLOR: #000000">&</span><span style="COLOR: #000000">wsaData)) <br></span><span style="COLOR: #008080"> 9</span><span style="COLOR: #000000"><img id=Codehighlighter1_168_203_Open_Image onclick="this.style.display='none'; Codehighlighter1_168_203_Open_Text.style.display='none'; Codehighlighter1_168_203_Closed_Image.style.display='inline'; Codehighlighter1_168_203_Closed_Text.style.display='inline';" src="http://www.shnenglu.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><img id=Codehighlighter1_168_203_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_168_203_Closed_Text.style.display='none'; Codehighlighter1_168_203_Open_Image.style.display='inline'; Codehighlighter1_168_203_Open_Text.style.display='inline';" src="http://www.shnenglu.com/Images/OutliningIndicators/ContractedSubBlock.gif" align=top>    </span><span id=Codehighlighter1_168_203_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img src="http://www.shnenglu.com/Images/dot.gif"></span><span id=Codehighlighter1_168_203_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">10</span><span style="COLOR: #000000"><img src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>        WSACleanup();<br></span><span style="COLOR: #008080">11</span><span style="COLOR: #000000"><img src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>        </span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000"> FALSE;<br></span><span style="COLOR: #008080">12</span><span style="COLOR: #000000"><img src="http://www.shnenglu.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>    }</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">13</span><span style="COLOR: #000000"><img src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>    </span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000"> (LOBYTE(wsaData.wVersion)</span><span style="COLOR: #000000">!=</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">||</span><span style="COLOR: #000000"> HIBYTE(wsaData.wVersion)</span><span style="COLOR: #000000">!=</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">) <br></span><span style="COLOR: #008080">14</span><span style="COLOR: #000000"><img id=Codehighlighter1_272_309_Open_Image onclick="this.style.display='none'; Codehighlighter1_272_309_Open_Text.style.display='none'; Codehighlighter1_272_309_Closed_Image.style.display='inline'; Codehighlighter1_272_309_Closed_Text.style.display='inline';" src="http://www.shnenglu.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><img id=Codehighlighter1_272_309_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_272_309_Closed_Text.style.display='none'; Codehighlighter1_272_309_Open_Image.style.display='inline'; Codehighlighter1_272_309_Open_Text.style.display='inline';" src="http://www.shnenglu.com/Images/OutliningIndicators/ContractedSubBlock.gif" align=top>    </span><span id=Codehighlighter1_272_309_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img src="http://www.shnenglu.com/Images/dot.gif"></span><span id=Codehighlighter1_272_309_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">15</span><span style="COLOR: #000000"><img src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>        WSACleanup( );<br></span><span style="COLOR: #008080">16</span><span style="COLOR: #000000"><img src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>        </span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000"> FALSE; <br></span><span style="COLOR: #008080">17</span><span style="COLOR: #000000"><img src="http://www.shnenglu.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>    }</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">18</span><span style="COLOR: #000000"><img src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>    <br></span><span style="COLOR: #008080">19</span><span style="COLOR: #000000"><img src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>    </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000"> q个IP是中国大陆时间同步服务器地址Q可自行修改</span><span style="COLOR: #008000"><br></span><span style="COLOR: #008080">20</span><span style="COLOR: #008000"><img src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top></span><span style="COLOR: #000000">    SOCKET soc</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP);<br></span><span style="COLOR: #008080">21</span><span style="COLOR: #000000"><img src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>    </span><span style="COLOR: #0000ff">struct</span><span style="COLOR: #000000"> sockaddr_in addrSrv;<br></span><span style="COLOR: #008080">22</span><span style="COLOR: #000000"><img src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>    addrSrv.sin_addr.S_un.S_addr</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">inet_addr(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">210.72.145.44</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">);<br></span><span style="COLOR: #008080">23</span><span style="COLOR: #000000"><img src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>    addrSrv.sin_family</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">AF_INET;<br></span><span style="COLOR: #008080">24</span><span style="COLOR: #000000"><img src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>    addrSrv.sin_port</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">htons(</span><span style="COLOR: #000000">123</span><span style="COLOR: #000000">);<br></span><span style="COLOR: #008080">25</span><span style="COLOR: #000000"><img src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>    <br></span><span style="COLOR: #008080">26</span><span style="COLOR: #000000"><img src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>    NTP_Packet NTP_Send,NTP_Recv; <br></span><span style="COLOR: #008080">27</span><span style="COLOR: #000000"><img src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>    NTP_Send.Control_Word   </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">   htonl(</span><span style="COLOR: #000000">0x0B000000</span><span style="COLOR: #000000">);   <br></span><span style="COLOR: #008080">28</span><span style="COLOR: #000000"><img src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>    NTP_Send.root_delay        </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">   </span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;   <br></span><span style="COLOR: #008080">29</span><span style="COLOR: #000000"><img src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>    NTP_Send.root_dispersion   </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">   </span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;   <br></span><span style="COLOR: #008080">30</span><span style="COLOR: #000000"><img src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>    NTP_Send.reference_identifier    </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">   </span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;   <br></span><span style="COLOR: #008080">31</span><span style="COLOR: #000000"><img src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>    NTP_Send.reference_timestamp    </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">   </span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;   <br></span><span style="COLOR: #008080">32</span><span style="COLOR: #000000"><img src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>    NTP_Send.originate_timestamp    </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">   </span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;   <br></span><span style="COLOR: #008080">33</span><span style="COLOR: #000000"><img src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>    NTP_Send.receive_timestamp        </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">   </span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;   <br></span><span style="COLOR: #008080">34</span><span style="COLOR: #000000"><img src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>    NTP_Send.transmit_timestamp_seconds        </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">   </span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;   <br></span><span style="COLOR: #008080">35</span><span style="COLOR: #000000"><img src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>    NTP_Send.transmit_timestamp_fractions   </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">   </span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">; <br></span><span style="COLOR: #008080">36</span><span style="COLOR: #000000"><img src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>    <br></span><span style="COLOR: #008080">37</span><span style="COLOR: #000000"><img src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>    </span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(SOCKET_ERROR</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">sendto(soc,(</span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">)</span><span style="COLOR: #000000">&</span><span style="COLOR: #000000">NTP_Send,</span><span style="COLOR: #0000ff">sizeof</span><span style="COLOR: #000000">(NTP_Send),<br></span><span style="COLOR: #008080">38</span><span style="COLOR: #000000"><img src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>        </span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">,(</span><span style="COLOR: #0000ff">struct</span><span style="COLOR: #000000"> sockaddr</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">)</span><span style="COLOR: #000000">&</span><span style="COLOR: #000000">addrSrv,</span><span style="COLOR: #0000ff">sizeof</span><span style="COLOR: #000000">(addrSrv)))<br></span><span style="COLOR: #008080">39</span><span style="COLOR: #000000"><img id=Codehighlighter1_1074_1113_Open_Image onclick="this.style.display='none'; Codehighlighter1_1074_1113_Open_Text.style.display='none'; Codehighlighter1_1074_1113_Closed_Image.style.display='inline'; Codehighlighter1_1074_1113_Closed_Text.style.display='inline';" src="http://www.shnenglu.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><img id=Codehighlighter1_1074_1113_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_1074_1113_Closed_Text.style.display='none'; Codehighlighter1_1074_1113_Open_Image.style.display='inline'; Codehighlighter1_1074_1113_Open_Text.style.display='inline';" src="http://www.shnenglu.com/Images/OutliningIndicators/ContractedSubBlock.gif" align=top>    </span><span id=Codehighlighter1_1074_1113_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img src="http://www.shnenglu.com/Images/dot.gif"></span><span id=Codehighlighter1_1074_1113_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">40</span><span style="COLOR: #000000"><img src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>        closesocket(soc);<br></span><span style="COLOR: #008080">41</span><span style="COLOR: #000000"><img src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>        </span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000"> FALSE;<br></span><span style="COLOR: #008080">42</span><span style="COLOR: #000000"><img src="http://www.shnenglu.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>    }</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">43</span><span style="COLOR: #000000"><img src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>    </span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> sockaddr_Size </span><span style="COLOR: #000000">=</span><span style="COLOR: #0000ff">sizeof</span><span style="COLOR: #000000">(addrSrv);<br></span><span style="COLOR: #008080">44</span><span style="COLOR: #000000"><img src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>    </span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(SOCKET_ERROR</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">recvfrom(soc,(</span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">)</span><span style="COLOR: #000000">&</span><span style="COLOR: #000000">NTP_Recv,</span><span style="COLOR: #0000ff">sizeof</span><span style="COLOR: #000000">(NTP_Recv),<br></span><span style="COLOR: #008080">45</span><span style="COLOR: #000000"><img src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>        </span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">,(</span><span style="COLOR: #0000ff">struct</span><span style="COLOR: #000000"> sockaddr</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">)</span><span style="COLOR: #000000">&</span><span style="COLOR: #000000">addrSrv,</span><span style="COLOR: #000000">&</span><span style="COLOR: #000000">sockaddr_Size))<br></span><span style="COLOR: #008080">46</span><span style="COLOR: #000000"><img id=Codehighlighter1_1267_1306_Open_Image onclick="this.style.display='none'; Codehighlighter1_1267_1306_Open_Text.style.display='none'; Codehighlighter1_1267_1306_Closed_Image.style.display='inline'; Codehighlighter1_1267_1306_Closed_Text.style.display='inline';" src="http://www.shnenglu.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><img id=Codehighlighter1_1267_1306_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_1267_1306_Closed_Text.style.display='none'; Codehighlighter1_1267_1306_Open_Image.style.display='inline'; Codehighlighter1_1267_1306_Open_Text.style.display='inline';" src="http://www.shnenglu.com/Images/OutliningIndicators/ContractedSubBlock.gif" align=top>    </span><span id=Codehighlighter1_1267_1306_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img src="http://www.shnenglu.com/Images/dot.gif"></span><span id=Codehighlighter1_1267_1306_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">47</span><span style="COLOR: #000000"><img src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>        closesocket(soc);<br></span><span style="COLOR: #008080">48</span><span style="COLOR: #000000"><img src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>        </span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000"> FALSE;<br></span><span style="COLOR: #008080">49</span><span style="COLOR: #000000"><img src="http://www.shnenglu.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>    }</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">50</span><span style="COLOR: #000000"><img src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>    closesocket(soc);<br></span><span style="COLOR: #008080">51</span><span style="COLOR: #000000"><img src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>    WSACleanup();<br></span><span style="COLOR: #008080">52</span><span style="COLOR: #000000"><img src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>    <br></span><span style="COLOR: #008080">53</span><span style="COLOR: #000000"><img src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>    SYSTEMTIME    newtime;<br></span><span style="COLOR: #008080">54</span><span style="COLOR: #000000"><img src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>    </span><span style="COLOR: #0000ff">float</span><span style="COLOR: #000000">        Splitseconds;<br></span><span style="COLOR: #008080">55</span><span style="COLOR: #000000"><img src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>    </span><span style="COLOR: #0000ff">struct</span><span style="COLOR: #000000">        tm    </span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">lpLocalTime;<br></span><span style="COLOR: #008080">56</span><span style="COLOR: #000000"><img src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>    time_t        ntp_time;<br></span><span style="COLOR: #008080">57</span><span style="COLOR: #000000"><img src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>    <br></span><span style="COLOR: #008080">58</span><span style="COLOR: #000000"><img src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>    </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000"> 获取旉服务器的旉</span><span style="COLOR: #008000"><br></span><span style="COLOR: #008080">59</span><span style="COLOR: #008000"><img src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top></span><span style="COLOR: #000000">    ntp_time    </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> ntohl(NTP_Recv.transmit_timestamp_seconds)</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">2208988800</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">60</span><span style="COLOR: #000000"><img src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>    lpLocalTime </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> localtime(</span><span style="COLOR: #000000">&</span><span style="COLOR: #000000">ntp_time);<br></span><span style="COLOR: #008080">61</span><span style="COLOR: #000000"><img src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>    </span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(lpLocalTime </span><span style="COLOR: #000000">==</span><span style="COLOR: #000000"> NULL)<br></span><span style="COLOR: #008080">62</span><span style="COLOR: #000000"><img id=Codehighlighter1_1579_1598_Open_Image onclick="this.style.display='none'; Codehighlighter1_1579_1598_Open_Text.style.display='none'; Codehighlighter1_1579_1598_Closed_Image.style.display='inline'; Codehighlighter1_1579_1598_Closed_Text.style.display='inline';" src="http://www.shnenglu.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><img id=Codehighlighter1_1579_1598_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_1579_1598_Closed_Text.style.display='none'; Codehighlighter1_1579_1598_Open_Image.style.display='inline'; Codehighlighter1_1579_1598_Open_Text.style.display='inline';" src="http://www.shnenglu.com/Images/OutliningIndicators/ContractedSubBlock.gif" align=top>    </span><span id=Codehighlighter1_1579_1598_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img src="http://www.shnenglu.com/Images/dot.gif"></span><span id=Codehighlighter1_1579_1598_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">63</span><span style="COLOR: #000000"><img src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>        </span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000"> FALSE;<br></span><span style="COLOR: #008080">64</span><span style="COLOR: #000000"><img src="http://www.shnenglu.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>    }</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">65</span><span style="COLOR: #000000"><img src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>    <br></span><span style="COLOR: #008080">66</span><span style="COLOR: #000000"><img src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>    </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000"> 获取新的旉</span><span style="COLOR: #008000"><br></span><span style="COLOR: #008080">67</span><span style="COLOR: #008000"><img src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top></span><span style="COLOR: #000000">    newtime.wYear      </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">lpLocalTime</span><span style="COLOR: #000000">-></span><span style="COLOR: #000000">tm_year</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1900</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">68</span><span style="COLOR: #000000"><img src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>    newtime.wMonth     </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">lpLocalTime</span><span style="COLOR: #000000">-></span><span style="COLOR: #000000">tm_mon</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">69</span><span style="COLOR: #000000"><img src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>    newtime.wDayOfWeek </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">lpLocalTime</span><span style="COLOR: #000000">-></span><span style="COLOR: #000000">tm_wday;<br></span><span style="COLOR: #008080">70</span><span style="COLOR: #000000"><img src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>    newtime.wDay       </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">lpLocalTime</span><span style="COLOR: #000000">-></span><span style="COLOR: #000000">tm_mday;<br></span><span style="COLOR: #008080">71</span><span style="COLOR: #000000"><img src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>    newtime.wHour      </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">lpLocalTime</span><span style="COLOR: #000000">-></span><span style="COLOR: #000000">tm_hour;<br></span><span style="COLOR: #008080">72</span><span style="COLOR: #000000"><img src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>    newtime.wMinute    </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">lpLocalTime</span><span style="COLOR: #000000">-></span><span style="COLOR: #000000">tm_min;<br></span><span style="COLOR: #008080">73</span><span style="COLOR: #000000"><img src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>    newtime.wSecond    </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">lpLocalTime</span><span style="COLOR: #000000">-></span><span style="COLOR: #000000">tm_sec;<br></span><span style="COLOR: #008080">74</span><span style="COLOR: #000000"><img src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>    <br></span><span style="COLOR: #008080">75</span><span style="COLOR: #000000"><img src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>    </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000"> 讄旉_ֺ</span><span style="COLOR: #008000"><br></span><span style="COLOR: #008080">76</span><span style="COLOR: #008000"><img src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top></span><span style="COLOR: #000000">    Splitseconds</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">(</span><span style="COLOR: #0000ff">float</span><span style="COLOR: #000000">)ntohl(NTP_Recv.transmit_timestamp_fractions);<br></span><span style="COLOR: #008080">77</span><span style="COLOR: #000000"><img src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>    Splitseconds</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">(</span><span style="COLOR: #0000ff">float</span><span style="COLOR: #000000">)</span><span style="COLOR: #000000">0.000000000200</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">*</span><span style="COLOR: #000000"> Splitseconds;<br></span><span style="COLOR: #008080">78</span><span style="COLOR: #000000"><img src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>    Splitseconds</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">(</span><span style="COLOR: #0000ff">float</span><span style="COLOR: #000000">)</span><span style="COLOR: #000000">1000.0</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">*</span><span style="COLOR: #000000"> Splitseconds;<br></span><span style="COLOR: #008080">79</span><span style="COLOR: #000000"><img src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>    newtime.wMilliseconds   </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">   (unsigned   </span><span style="COLOR: #0000ff">short</span><span style="COLOR: #000000">)Splitseconds;<br></span><span style="COLOR: #008080">80</span><span style="COLOR: #000000"><img src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>    <br></span><span style="COLOR: #008080">81</span><span style="COLOR: #000000"><img src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>    </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000"> 修改本机pȝ旉</span><span style="COLOR: #008000"><br></span><span style="COLOR: #008080">82</span><span style="COLOR: #008000"><img src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top></span><span style="COLOR: #000000">    SetLocalTime(</span><span style="COLOR: #000000">&</span><span style="COLOR: #000000">newtime);<br></span><span style="COLOR: #008080">83</span><span style="COLOR: #000000"><img src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>    </span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000"> TRUE;<br></span><span style="COLOR: #008080">84</span><span style="COLOR: #000000"><img src="http://www.shnenglu.com/Images/OutliningIndicators/ExpandedBlockEnd.gif" align=top>}</span></span></div> <img src ="http://www.shnenglu.com/AutomateProgram/aggbug/118031.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.shnenglu.com/AutomateProgram/" target="_blank">漂漂</a> 2010-06-16 18:09 <a href="http://www.shnenglu.com/AutomateProgram/archive/2010/06/16/118031.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>使用CInternetSession和CHttpFiled|页内容http://www.shnenglu.com/AutomateProgram/archive/2010/06/16/118030.html漂漂漂漂Wed, 16 Jun 2010 10:03:00 GMThttp://www.shnenglu.com/AutomateProgram/archive/2010/06/16/118030.htmlhttp://www.shnenglu.com/AutomateProgram/comments/118030.htmlhttp://www.shnenglu.com/AutomateProgram/archive/2010/06/16/118030.html#Feedback0http://www.shnenglu.com/AutomateProgram/comments/commentRss/118030.htmlhttp://www.shnenglu.com/AutomateProgram/services/trackbacks/118030.htmlhttp://www.398588.com/Article/T9/78.aspx
d|页的内容可以类比本地的文g一下,代码如下Q?br>#include   <afxinet.h>
CString url;
    GetDlgItemText(IDC_EDIT1,url);
    CInternetSession Sess;
    CHttpFile
* cFile = (CHttpFile*)Sess.OpenURL(url,1,INTERNET_FLAG_TRANSFER_ASCII||INTERNET_FLAG_RELOAD,NULL,0);
    
    DWORD dwStatusCode;
    cFile
->QueryInfoStatusCode(dwStatusCode);
    
if(dwStatusCode == HTTP_STATUS_OK)
    
{
        CString szData,szAllData;
        
while(cFile->ReadString(szData))
        
{
            szAllData 
+= szData;
            szAllData 
+= "\r\n";
        }

        
        cFile
->Close();
        Sess.Close();
        CString name 
;
        name 
= "sd.htm";
        CFile file(name, CFile::modeCreate 
| CFile::modeWrite);
        file.Write(szAllData,szAllData.GetLength());
        file.Close();
    }

    
else
    
{
        MessageBox(
"hp|。。。?/span>");
    }

用CInternetSession打开url得到一个CHttpFileQ用ReadStringdCHttpFile中的内容?br>

漂漂 2010-06-16 18:03 发表评论
]]>
vc中几个源代码|站http://www.shnenglu.com/AutomateProgram/archive/2010/06/04/117176.html漂漂漂漂Fri, 04 Jun 2010 09:34:00 GMThttp://www.shnenglu.com/AutomateProgram/archive/2010/06/04/117176.htmlhttp://www.shnenglu.com/AutomateProgram/comments/117176.htmlhttp://www.shnenglu.com/AutomateProgram/archive/2010/06/04/117176.html#Feedback0http://www.shnenglu.com/AutomateProgram/comments/commentRss/117176.htmlhttp://www.shnenglu.com/AutomateProgram/services/trackbacks/117176.htmlhttp://www.codefans.net/
源码|:http://www.51kyuan.com/
vc驿站Q?a >http://www.cctry.com/
hellocpp中国的codeprojectQ?a >http://www.hellocpp.net/
E序员小辉:http://www.xiaohui.com/dev/
源码搜搜Q?a >http://www.codesoso.com/default.aspx
中国黑客Q?a >http://www.hackchina.com/

漂漂 2010-06-04 17:34 发表评论
]]>
P2P原理的解释与实现http://www.shnenglu.com/AutomateProgram/archive/2010/03/29/110785.html漂漂漂漂Mon, 29 Mar 2010 01:05:00 GMThttp://www.shnenglu.com/AutomateProgram/archive/2010/03/29/110785.htmlhttp://www.shnenglu.com/AutomateProgram/comments/110785.htmlhttp://www.shnenglu.com/AutomateProgram/archive/2010/03/29/110785.html#Feedback0http://www.shnenglu.com/AutomateProgram/comments/commentRss/110785.htmlhttp://www.shnenglu.com/AutomateProgram/services/trackbacks/110785.htmlhttp://www.shnenglu.com/converse/archive/2006/09/02/11969.html
关于采用UDP协议q行打洞以进行P2P会话的原?我本来想写一文章作说明,但是现在已经有一文章把原理性的东西解释清楚?我在q里不再作这部分的重?可以参见q里:
P2P ?UDPIKNAT的原理与实现Q附源代码)--http://www.cnpaf.net/Class/hack/0512182034513804825.htm

下面解释一下上面的文章中没有提及或者说我觉得比较欠~的地方.
U有地址/端口和公有地址/端口:我们知道,现在大部分网l采用的都是NAPT(Network Address/Port Translator)?q个东东的作用是一个对外的对话在经qNAT之后IP地址和端口号都会被改?在这里把一ơ会话中客户自己认ؓ在用的IP地址和端口号成ؓU有地址/端口,而把l过NAPT之后被改写的IP地址和端口号UCؓ公有地址/端口.或者可以这么理?U有地址/端口是你安人对你的늧而公有地址/端口则是你真正对外公开的名?如何获得用户的私用地址/端口?q个很简单了,而要得到公有地址/端口号就要在q接上另一台机器之后由那台机器看到的IP地址和端口号来表C?

如果明白了上面的东西,下面q入我们的代?在这里解释一下关键部分的实现:

客户端首先得到自qU有地址/l端,然后向server端发送登陆请?server端在得到q个h之后可以知道这个client端的公有地址/l端,server会ؓ每一个登陆的client保存它们的私有地址/端口和公有地址/端口.

OK,下面开始关键的打洞程.假设client A要向client B对话,但是A不知道B的地址,即知道ҎNAT的原理这个对话在W一ơ会被拒l?因ؓclient B的NAT认ؓq是一个从没有q的外部发来的请?q个时?A如果发现自己没有保存B的地址,或者说发送给B的会话请求失败了,它会要求server端让B向A打一个洞,q个B->A的会话意义在于它使NAT B认ؓA的地址/端口是可以通过的地址/端口,q样A再向B发送对话的时候就不会再被NAT B拒绝?打一个比Ҏ说明打洞的过E?AxB家做?但是遭到了B的管家NAT B的拒l?理由?我从来没有听我家B提过你的名字,q时A扑ֈ了A,B都认识的朋友server,要求serverlB报一个信,让B去跟家说A是我的朋?于是,B跟管家NAT B?A是我认识的朋?q样A的访问请求就不会再被家NAT B所拒绝?而言?UDP打洞是一个通过server保存下来的地址使得彼此之间能够直接通信的过E?server只管帮助建立q接,在徏立间接之后就不再介入?

好了,原理性的东西解释到这?附g中有一个完整的P2P演示E序,命o行模式下,包括server端和client?在运行的时候首先启动server?然后打开几个client端分别登?之后彼此之间可以相互通信?E序在本Z试通过,也就是测试的环境server和client都是一台机?q没有在不同的机器上试q?不知道会不会有问?)
下蝲地址:
http://www.shnenglu.com/Files/converse/P2PDemo.rar
参考资?
1)P2P ?UDPIKNAT的原理与实现Q附源代码)-http://www.cnpaf.net/Class/hack/0512182034513804825.htm
2)王艳q?lt;<Windows|络与通信E序设计>>


漂漂 2010-03-29 09:05 发表评论
]]>
C++ TCP htons INADDR_ANY SOCKADDR_INl构http://www.shnenglu.com/AutomateProgram/archive/2010/03/28/110720.html漂漂漂漂Sun, 28 Mar 2010 02:44:00 GMThttp://www.shnenglu.com/AutomateProgram/archive/2010/03/28/110720.htmlhttp://www.shnenglu.com/AutomateProgram/comments/110720.htmlhttp://www.shnenglu.com/AutomateProgram/archive/2010/03/28/110720.html#Feedback0http://www.shnenglu.com/AutomateProgram/comments/commentRss/110720.htmlhttp://www.shnenglu.com/AutomateProgram/services/trackbacks/110720.html

1 htons

htons(1986)得到的结果是49671
如何得到49671q个l果?
1986转换?6q制?7C2
L序转换成网l顺序之后是C207
C207转换为十q制?9671
例子:0x12 34 56 78l过htons?x78 56 34 12
在给SOCKADDR_INl构中的端口L接赋?客户端和服务端全部都直接赋?br style="line-height: 22px; ">SOCKADDR_IN addrSrv;   
addrSrv.sin_addr.S_un.S_addr = htonl(INADDR_ANY);
addrSrv.sin_family = AF_INET;
addrSrv.sin_port = 1986;
q样也可以通讯,但绑定的端口号已l不?986,而是49671

2 INADDR_ANY
#define INADDR_ANY  (u_long)0x00000000
INADDR_ANY表示 所有的IP,如果自己的主机有多个IP,在给SOCKADDR_IN l构的IP地址赋值时qINADDR_ANY.
如果只有一个IP且是固定?也可以不用INADDR_ANY,使用如下语句
addrSrv.sin_addr.S_un.S_addr = inet_addr("221.217.218.111");
inet_addr函数是将一个点分十q制的IP转换成无W号长整?/p>

3 SOCKADDR_INl构
struct sockaddr_in
 {
    short   sin_family;
    u_short sin_port;
    struct  in_addr sin_addr;
    char    sin_zero[8];
};
sin_family:|络cd
sin_port:|络端口?br style="line-height: 22px; ">in_addr:IP地址
sin_zero:保证l构的大?br style="line-height: 22px; ">in_addr的结构ؓ
struct in_addr {
        union {
                struct { u_char s_b1,s_b2,s_b3,s_b4; } S_un_b;
                struct { u_short s_w1,s_w2; } S_un_w;
                u_long S_addr;
        } S_un;

对于IP地址?21.217.218.111,addrSrv.sin_addr.S_un的赋值方法如?br style="line-height: 22px; ">W一U?
addrSrv.sin_addr.S_un.S_addr =111<<24|218<<16|217<<8|221;
W二U?
addrSrv.sin_addr.S_un.S_addr =htonl(221<<24|217<<16|218<<8|111);
W三U?
addrSrv.sin_addr.S_un.S_addr = inet_addr("221.217.218.111");
W四U?
addrSrv.sin_addr .S_un.S_un_w.s_w1 =217<<8|221;
addrSrv.sin_addr .S_un.S_un_w.s_w2 =111<<8|218;
W五U?
addrSrv.sin_addr .S_un.S_un_b.s_b1=221;
addrSrv.sin_addr .S_un.S_un_b.s_b2=217;
addrSrv.sin_addr .S_un.S_un_b.s_b3=218;
addrSrv.sin_addr .S_un.S_un_b.s_b4=111;



漂漂 2010-03-28 10:44 发表评论
]]>
直接用socket完成HTTP下蝲Q{Q?/title><link>http://www.shnenglu.com/AutomateProgram/archive/2010/03/27/110666.html</link><dc:creator>漂漂</dc:creator><author>漂漂</author><pubDate>Sat, 27 Mar 2010 04:39:00 GMT</pubDate><guid>http://www.shnenglu.com/AutomateProgram/archive/2010/03/27/110666.html</guid><wfw:comment>http://www.shnenglu.com/AutomateProgram/comments/110666.html</wfw:comment><comments>http://www.shnenglu.com/AutomateProgram/archive/2010/03/27/110666.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.shnenglu.com/AutomateProgram/comments/commentRss/110666.html</wfw:commentRss><trackback:ping>http://www.shnenglu.com/AutomateProgram/services/trackbacks/110666.html</trackback:ping><description><![CDATA[原文地址Q?a >http://www.cnoffice.info/Program/VC/VCnet/Program_55135.html</a> <img src ="http://www.shnenglu.com/AutomateProgram/aggbug/110666.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.shnenglu.com/AutomateProgram/" target="_blank">漂漂</a> 2010-03-27 12:39 <a href="http://www.shnenglu.com/AutomateProgram/archive/2010/03/27/110666.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>http 下蝲http://www.shnenglu.com/AutomateProgram/archive/2010/03/27/110662.html漂漂漂漂Sat, 27 Mar 2010 03:01:00 GMThttp://www.shnenglu.com/AutomateProgram/archive/2010/03/27/110662.htmlhttp://www.shnenglu.com/AutomateProgram/comments/110662.htmlhttp://www.shnenglu.com/AutomateProgram/archive/2010/03/27/110662.html#Feedback0http://www.shnenglu.com/AutomateProgram/comments/commentRss/110662.htmlhttp://www.shnenglu.com/AutomateProgram/services/trackbacks/110662.htmlvisual c++|络通讯~程实用案例_N中

 DWORD dwFlags;
 InternetGetConnectedState(&dwFlags, 0);
 CHAR strAgent[64];
 sprintf(strAgent, "Agent%ld", timeGetTime());
 HINTERNET hOpen;
 if(!(dwFlags & INTERNET_CONNECTION_PROXY))
  hOpen = InternetOpenA(strAgent, INTERNET_OPEN_TYPE_PRECONFIG_WITH_NO_AUTOPROXY, NULL, NULL, 0);
 else
  hOpen = InternetOpenA(strAgent, INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
 if(!hOpen)
 {
  AfxMessageBox("Internetq接错误!");
  return -1;
 }

 DWORD dwSize;
 CHAR   szHead[] = "Accept: */*\r\n\r\n";
 VOID* szTemp[16384];
 HINTERNET  hConnect;
 CFile file;

 if ( !(hConnect = InternetOpenUrlA ( hOpen, szUrl, szHead,
   lstrlenA (szHead), INTERNET_FLAG_DONT_CACHE | INTERNET_FLAG_PRAGMA_NOCACHE | INTERNET_FLAG_RELOAD, 0)))
 {
    AfxMessageBox("不能打开该URL!");
    return -1;
 }

 if  (file.Open(szFileName,CFile::modeWrite|CFile::modeCreate)==FALSE )
 {
    AfxMessageBox("不能打开本地的文?");
   return -1;
 }

 DWORD dwByteToRead = 0;
 DWORD dwSizeOfRq = 4;
 DWORD dwBytes = 0;

    if (!HttpQueryInfo(hConnect, HTTP_QUERY_CONTENT_LENGTH | HTTP_QUERY_FLAG_NUMBER,
                  (LPVOID)&dwByteToRead, &dwSizeOfRq, NULL))
 {
  dwByteToRead = 0;
 }

 DWORD start;
 DWORD end;
 DWORD time;
 CString tempstring;
 time = 10;
 start = timeGetTime();
 do
 {
  if (!InternetReadFile (hConnect, szTemp, 16384,  &dwSize))
  {
   AfxMessageBox("L件出?");
   file.Close();
   return -1;
  }
  if (dwSize==0)
   break;
  else
   file.Write(szTemp,dwSize);
  dwBytes+=dwSize;
  if(dwByteToRead)
  {
   tempstring.Format("%d%%",(dwBytes*100)/dwByteToRead);
   SetDlgItemText(IDC_PERCENT_TEXT,tempstring);
  }
  FLOAT fSpeed = 0;
  fSpeed = (float)dwBytes;
  fSpeed /= ((float)time)/1000.0f;
  fSpeed /= 1024.0f;
  tempstring.Format("%dKB/s",fSpeed);
  SetDlgItemText(IDC_SPEED_TEXT,tempstring);
  end = timeGetTime();
  time = end - start;
  if(time == 0)
   time = 10;
 }while (TRUE);

 file.Close();
 InternetCloseHandle(hOpen);



漂漂 2010-03-27 11:01 发表评论
]]>
WinINet操作程http://www.shnenglu.com/AutomateProgram/archive/2010/03/26/110569.html漂漂漂漂Fri, 26 Mar 2010 01:22:00 GMThttp://www.shnenglu.com/AutomateProgram/archive/2010/03/26/110569.htmlhttp://www.shnenglu.com/AutomateProgram/comments/110569.htmlhttp://www.shnenglu.com/AutomateProgram/archive/2010/03/26/110569.html#Feedback0http://www.shnenglu.com/AutomateProgram/comments/commentRss/110569.htmlhttp://www.shnenglu.com/AutomateProgram/services/trackbacks/110569.html原文地址Q?a >http://blog.csdn.net/li_guotao/archive/2009/03/04/3956067.aspx
一 WinINet是干什么的Q?br>?如果完成一个基本的WinINet操作程
?如何异步完成Q异步操作的好处?br>?unicode以及mutilbyte
?代码样例

一 WinINet是微软开发的一个库Q可以完成http ftp客户端的工作。让E序员从复杂的协议中节省大量体力?/p>

?我会用了http, ftpcM。用WinINet完成一个http下蝲需要以下步??下面q个是同步操作(也就是一步步操作Q每个函数执行完才会q回一个结果的意思)

     1   InternetOpen      Initializes an application's use of the WinINet functions.
                   需要的?InternetSetOption 讄代理服务器地址以及端口?br>                   httpQ?nbsp;   ip:port 或?http=http://ip:port
                   socksQSOCKS=ip:port
     2   InternetConnect    兌目标地址或者域名以及服务ip

     3   HttpOpenRequest   兌要下载的内容名字
                   InternetSetOption 讄用户名密?/p>

     4   HttpSendRequest     q步是用HttpOpenRequest 的返回|已经兌了上面的所有信息)发送出去,W一ơ用了网l。向目标服务器或者代理服务器?/p>

     5   HttpQueryInfo
            该函数查询返回|不参与网l操作。可以查询服务器的返回信息,比如目标文g的大,该文件是否存在,代理服务q回了要求用户名Q密码等{(q几个最常用Q,q有很多信息?/p>

     6   InternetReadFile
            很普通的d敎ͼ是下蝲文g。不知道是否和底层网l同步,底层会不会提前下载呢Q?/p>

     7   InternetCloseHandle 释放资源

?nbsp;   异步操作Q比较复杂的?nbsp;  Z么需要异步操作呢Q?因ؓ涉及到网l操作,某些函数在操作中可能需要时_如果一直不q回Q比?U)Ӟq时ȝE要l束E序Q岂不就出现意想不到的结果了Q但是如果每个函数都能够瞬间q回Q然后通过WaitForMultipleObjects或者WaitForSingleObject{待l果的出玎ͼ此时׃会操作那个消?U的函数了,而这?U函数正是要用到pȝ资源HINTERNET的)?/p>

       异步操作的目的上如,原理呢? 其实原理是注册一个函敎ͼ在这里叫InternetStatusCallbackQ因为微软写得底层代码要用到Q所以必L式统一。一旦有l果来了通过事g通知我们QWaitForSingleObject函数可以走了。比如等CHINTERNET创徏或者命令发送成功等l果。然后我们就可以W一旉安全的用了?br>      API 函数如果名字最后可以带EXQ那么带ex的就是异步操作的?/p>

?一定要注意Q凡是有unicode和mutilbyte函数的一定要l一Q最好都用mutilbyte的?/p>


五: 代码如下Q?br>// crt_assert.c
// compile with: /c
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>


#include <iostream>

#include "winsock2.h"
#include <string>
#include <Wininet.h>
#include <windows.h>
#include <fstream> //要用文件输入输出流必须的头文g
using namespace std;
#define __HTTP_VERB_GET    "GET"
#define __HTTP_VERB_POST "POST"
#define __HTTP_ACCEPT_TYPE "*/*"
#define __HTTP_ACCEPT "Accept: */*\r\n"
#define __SIZE_HTTP_BUFFER    100000
#define __SIZE_HTTP_RESPONSE_BUFFER    100000
#define __SIZE_HTTP_HEAD_LINE    2048

void CALLBACK InternetStatusCallback(

                                    HINTERNET hInternet,
                                    DWORD dwContext,
                                    DWORD dwInternetStatus,
                                    LPVOID lpvStatusInformation,
                                    DWORD dwStatusInformationLength);
HANDLE hEvent[3];

HINTERNET hFile;
HINTERNET hNet;
HINTERNET hSession,hConnect,hRequest;
int WaitExitEvent()
{
    //return 1;
    DWORD dwRet = ::WaitForMultipleObjects(3, hEvent, FALSE, 30000);//INFINITE);
    int x=-1;
    switch (dwRet)
    {
        //句柄被创Z件或者读数据h成功完成事g
    case WAIT_OBJECT_0:
        x=0;
        cout<<"WAIT_OBJECT_0"<<endl;
        //句柄被关闭事?br>        break;
    case WAIT_OBJECT_0+1:
        x=1;
        cout<<"WAIT_OBJECT_1"<<endl;
        //用户要求l止子线E事件或者发生错误事?br>        break;
    case WAIT_OBJECT_0+2:
        x=2;
        cout<<"WAIT_OBJECT_2"<<endl;
       
        break;
    default:
        cout<<"WaitForMultipleObjects time out"<<endl;
        return -1;

    }
    return x;
}

// 支持代理讄Q?是否异步讄Q?采用事g驱动
void WinINet3(bool setProxy, bool ASYNC)
{
    hSession=NULL;
    hConnect=NULL;
    hRequest=NULL;
    for (int i = 0; i < 3; i++)
    {
        hEvent[i] = CreateEvent(
            NULL,   // default security attributes
            FALSE, // auto-reset event object
            FALSE, // initial state is nonsignaled
            NULL); // unnamed object

        if (hEvent[i] == NULL)
        {
            printf("CreateEvent error: %d\n", GetLastError() );
            ExitProcess(0);
        }
    }
    char *url = "    char *pip = "down.360safe.com";
    char *paim = "/setup.exe";

 

    //   step 1
    if(ASYNC)    cout<<"异步模式"<<endl;
   // setProxy =false;
    if(setProxy)
    {
        cout<<"代理模式"<<endl;
        if(ASYNC)
          hSession = InternetOpen("name",
        INTERNET_OPEN_TYPE_DIRECT,//|INTERNET_OPEN_TYPE_PROXY,// INTERNET_OPEN_TYPE_PROXY,
        NULL,NULL,INTERNET_FLAG_ASYNC); // 异步
        else
          hSession = InternetOpen("name",INTERNET_OPEN_TYPE_PROXY,NULL,NULL,0); // 同步
    }
    else
    {
        if(ASYNC)
            hSession = InternetOpen("name",INTERNET_OPEN_TYPE_PRECONFIG,NULL,NULL,INTERNET_FLAG_ASYNC); // 异步
        else
            hSession = InternetOpen("name",INTERNET_OPEN_TYPE_PRECONFIG,NULL,NULL,0); // 同步
    }
    if(!hSession){
        DWORD er = ::GetLastError();
        cout<<"InternetOpen error"<<endl;//, "Err", MB_OK);
        return;
    }
    if(ASYNC)
    {
        //Sleep(500);
        INTERNET_STATUS_CALLBACK res = ::InternetSetStatusCallback(hSession,InternetStatusCallback);
        if(res == INTERNET_INVALID_STATUS_CALLBACK)
        {
            cout<<"InternetSetStatusCallback failed, so return "<<endl;
            return ;   
        }
        else
        {
            cout<<"InternetSetStatusCallback succeed, so go on "<<endl;

        }
        //Sleep(500);
    }
   
    char   strProxyList[MAX_PATH],   strUsername[64],   strPassword[64];
    strcpy(strProxyList,   "SOCKS=58.56.87.2:1080"); //   写上socks怎么无效了呢?Q?SOCKS5=172.18.132.27:1080
    strcpy(strUsername,   "user01");
    strcpy(strPassword,   "baidu");
    INTERNET_PROXY_INFO proxy;
    proxy.dwAccessType = INTERNET_OPEN_TYPE_PROXY;
    proxy.lpszProxy    = strProxyList;
    proxy.lpszProxyBypass = NULL;
    if( setProxy &&!InternetSetOption(hSession,INTERNET_OPTION_PROXY ,&proxy,sizeof(INTERNET_PROXY_INFO)))
    {
        cout<<"InternetSetOption failed"<<endl;
        return ;
    }
   
    // step 2
    //如果明确知道需要认证,W?Q?个参数可以输入用户名Q密?administrator","password"
    //W?Q?个参Cؓ目标LIP、端口号(不是代理服务器的参数)
    hConnect = InternetConnect(hSession,pip,INTERNET_DEFAULT_HTTP_PORT,NULL,NULL,INTERNET_SERVICE_HTTP,INTERNET_FLAG_RELOAD,0);
    if(!ASYNC &&!hConnect){
        cout<<"同步QInternetConnect error"<<endl;//, "Err", MB_OK);
        return;
    }
    if( ASYNC&& hConnect== NULL)// 异步 需要等?nbsp;  竟然直接创徏好了
    {
        int er = GetLastError();
        DWORD dwError = ::GetLastError();
        if (dwError != ERROR_IO_PENDING)
        {
            cout<<"CHttpDownload::OpenInternetConnection| q接p|" <<endl;
            return ;
        }
        else //
        {
            cout<<"hConnect == NULL, so run WaitExitEvent"<<endl;
            WaitExitEvent(); // {待成功创徏 // q里应该{待   q里应该昄一ơ呀
            ::ResetEvent(hEvent[0]);
            ::ResetEvent(hEvent[1]);
            ::ResetEvent(hEvent[2]);
        }
    }
    cout<<"step 2 :InternetConnect secced"<<endl;

    // ::InternetSetStatusCallback(hConnect,InternetStatusCallback);
   
    // step 3!!!
    char   szHead[] = "Accept: */*\r\n\r\n";
    char **p = new char*[2];*p = szHead;*(p+1) = NULL;

    //hRequest = HttpOpenRequest(hConnect,"GET","download/BaiduHi_1.0_Beta2.exe",NULL,NULL,/*(const char **)p*/NULL,0/*INTERNET_FLAG_NO_COOKIES|INTERNET_FLAG_RELOAD*/,0); // no request;
    CONST TCHAR *szAcceptType=__HTTP_ACCEPT_TYPE;
    hRequest = ::HttpOpenRequest(hConnect,
        "GET",
        paim,
        HTTP_VERSION,
        "",
        &szAcceptType,
        INTERNET_FLAG_RELOAD|INTERNET_FLAG_KEEP_CONNECTION|INTERNET_FLAG_NO_CACHE_WRITE,
        0);
   
    //::HttpAddRequestHeaders( hRequest, __HTTP_ACCEPT, strlen(__HTTP_ACCEPT), HTTP_ADDREQ_FLAG_REPLACE);
    /*_hHTTPRequest=::HttpOpenRequest(    _hHTTPConnection,
        __HTTP_VERB_GET, // HTTP Verb
        szURI, // Object Name
        HTTP_VERSION, // Version
        "", // Reference
        &szAcceptType, // Accept Type
        INTERNET_FLAG_KEEP_CONNECTION | INTERNET_FLAG_NO_CACHE_WRITE,
        0); // context call-back point
    */
    if (!ASYNC&& !hRequest){
        cout<<"同步QHttpOpenRequest error"<<endl;//, "Err", MB_OK);
        return;
    }
    if( ASYNC&& hRequest== NULL)// 异步 需要等?br>    {
        int er = GetLastError();
        DWORD dwError = ::GetLastError();
        if (dwError != ERROR_IO_PENDING)
        {
            cout<<"CHttpDownload::OpenInternetConnection| q接p|" <<endl;
            return ;
        }
        else //
        {
            cout<<"hRequest == NULL, so run WaitExitEvent"<<endl;
            WaitExitEvent(); // {待成功创徏
            ::ResetEvent(hEvent[0]);
            ::ResetEvent(hEvent[1]);
            ::ResetEvent(hEvent[2]);
        }
    }
    //Sleep(10000);
    cout << "step 3 : HttpOpenRequest success"<<endl;
    //::InternetSetStatusCallback(hRequest,InternetStatusCallback);
    //////////////////////////////////////////////
    if (setProxy )
    {
        // InternetSetOption 不要异步{待
        if( !InternetSetOption(hRequest,INTERNET_OPTION_PROXY_USERNAME ,strUsername,strlen(strUsername)+1))
        {
            cout<<"InternetSetOption Username failed"<<endl;
            return ;
        }
        if( !InternetSetOption(hRequest,INTERNET_OPTION_PROXY_PASSWORD ,strPassword,strlen(strPassword)+1))
        {
            cout<<"InternetSetOption Password failed"<<endl;
            return ;
        }
    }
    // step 4
    //HttpSendRequest(hRequest,NULL,0,NULL,0);
    //Sleep(3000);
    ::ResetEvent(hEvent[0]);
    ::ResetEvent(hEvent[1]);
    ::ResetEvent(hEvent[2]);
    if(!::HttpSendRequest(hRequest,NULL,0,NULL,0)) // Z么失败?Q?
    {
        //Sleep(3000);
        if(!ASYNC)// 同步
        {
            DWORD dwError = ::GetLastError();
              cout<<"同步QHttpSendRequest failed, GetLastError=="<<dwError<<endl;
            return ;
       
        }
        else
        {
            Sleep(3000);
            DWORD dwError = ::GetLastError();
            cout<<"dwError =="<<dwError<<endl;
            if (dwError != ERROR_IO_PENDING)
            {
                cout<<"dwError != ERROR_IO_PENDING, so quit,dwError =="<<dwError<<endl;
                return ;
            }
            else //
            {
                cout<<"HttpSendRequest, so run WaitExitEvent"<<endl;
                Sleep(3000);
                //if(WaitExitEvent()!=2)//; // {待成功创徏 {待是否不对Q?Q?br>                {
                       cout<<"had not recv complete event, so quit"<<endl;
                    // return ;
                }
            }
        }
       
    }
    Sleep(3000);
    cout << "step 4: HttpSendRequest success!"<<endl;

    int bufh[1000];
    DWORD dwLen,dwIndex;
    /*if(!::HttpQueryInfo(hRequest, HTTP_QUERY_RAW_HEADERS_CRLF, bufh, &dwLen, &dwIndex))// q句话?Q?
    {
        //return E_FAIL;
        return;
    }
*/
    // 判断状态码Q?br>    char m_dwStatusCode[90];
    DWORD dwStatusSize = sizeof(m_dwStatusCode);
    /*if (FALSE == ::HttpQueryInfo(hRequest,   // 查询失效Q?
        HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER,
        &m_dwStatusCode,
        &dwStatusSize,
        NULL))   //获取q回状态码
    {
        return ;
    }
    //判断状态码是不?200
    //if (HTTP_STATUS_OK != m_dwStatusCode)
    {
        //return ;
    }
*/

    DWORD dwByteToRead = 0;
    DWORD dwSizeOfRq = 4;
    DWORD dwBytes = 0;
    //q三个值分别存储文件的大小QHttpQueryInfo内容的大和dd的字节数?br>    //HttpQueryInfo(hRequest, HTTP_QUERY_CONTENT_LENGTH | HTTP_QUERY_FLAG_NUMBER, (LPVOID)&dwByteToRead, &dwSizeOfRq, NULL);
    //需要说明的?HttpQueryInfo q不q行|络操作Q因此它不需要进行异步操作的处理?
    if (!HttpQueryInfo(hRequest, HTTP_QUERY_CONTENT_LENGTH | HTTP_QUERY_FLAG_NUMBER, (LPVOID)&dwByteToRead, &dwSizeOfRq, NULL))
    { // q里p|了?Q?
       
        DWORD dwError = ::GetLastError();
        cout<<"HttpQueryInfo failed, so returnQ?GetLastError() =="<<dwError<<endl;
        return ;
    }
    FILE * pFile = fopen("e://baidu01.exe", "wb" );
    //ofstream mfile("out.txt");//定义文g输出oufQƈ兌到out.txt
    int i=0;
    DWORD leftB = dwByteToRead;
    cout<<"开始下?<<endl;
    if( !ASYNC) // 同步下蝲
    {
        while(true)
        {
            const int MAX_BUFFER_SIZE = 65536;
            unsigned long nSize = 0;
            char szBuffer[MAX_BUFFER_SIZE+2];
            int num = MAX_BUFFER_SIZE;
            if( leftB < num);
            num = leftB;
            BOOL bRet = ::InternetReadFile(hRequest, szBuffer, num, &nSize); // 异步 需要等?/p>

            leftB -= nSize;
            cout<<i++<<" size: "<<nSize<<endl;
            if(!bRet || nSize <= 0)
                break;
            fwrite(szBuffer, sizeof(char), nSize, pFile);
        }
    }
    else // 异步下蝲
    {
        INTERNET_BUFFERS i_buf = {0};
        i_buf.dwStructSize = sizeof(INTERNET_BUFFERS);
        i_buf.lpvBuffer = new TCHAR[10242];
        i_buf.dwBufferLength = 10240;
         for( DWORD i=0;i<dwByteToRead;)
        {
            //重置L据事?br>            ::ResetEvent( hEvent[0]);
            int num = 10240;
            if(dwByteToRead-i<10240)
            {
                num = dwByteToRead-i;
                   i_buf.dwBufferLength = dwByteToRead-i;
            }
            if (FALSE == ::InternetReadFileEx(hRequest,
                &i_buf,
                IRF_ASYNC,
                NULL))
            {
                if (ERROR_IO_PENDING == ::GetLastError())
                {
                    if ( NULL)//WaitExitEvent()!=2)
                    {
                        delete[] i_buf.lpvBuffer;
                        return ;
                    }
                }
                else
                {
                    cout<<"down failed,so return"<<endl;
                    delete[] i_buf.lpvBuffer;
                    return ;
                }
            }
            else
            {
                //在网l传输速度快,步长较小的情况下Q?br>                //InternetReadFileEx l常会直接返回成功,
                //因此要判断是否发生了用户要求l止子线E事件?br>                cout<<"|络很好QInternetReadFileExq回true"<<endl;

                // 暂不考虑用户退?br>            }
            i += i_buf.dwBufferLength; // 最后一ơ写多了Q!Q?br>            fwrite(i_buf.lpvBuffer, sizeof(char), i_buf.dwBufferLength, pFile);
            cout<<"i== "<<i<<endl;
            //保存数据
             //通知ȝE下载进?br>                   
        }
    }
    InternetCloseHandle(hRequest);
    InternetCloseHandle(hConnect);
    InternetCloseHandle(hSession);
    cout<<"success download file"<<endl;
   
    return;
}

int main( void )
{
   
    WinINet3(true,true);
    return 1;
}

void OnInternetHandleCreated(HINTERNET hInternet, LPINTERNET_ASYNC_RESULT lpInetStatusResult)
{
    if(NULL == lpInetStatusResult)
    {
        //ATLASSERT( 0 );
        return;
    }
    hFile = HINTERNET(lpInetStatusResult->dwResult);
    HINTERNET    hInet = HINTERNET(lpInetStatusResult->dwResult);
    DWORD        dwInetHandleType;
    DWORD        dwTypeLen = sizeof(dwInetHandleType);

    InternetQueryOption( hInet, INTERNET_OPTION_HANDLE_TYPE, &dwInetHandleType, &dwTypeLen);
    switch(dwInetHandleType)
    {
    case INTERNET_HANDLE_TYPE_CONNECT_HTTP:
        //CloseInternetConnection(); //   q里是何意?Q?Q?通过回调 讄httpConnect
        hConnect = hInet;     //
        break;
    case INTERNET_HANDLE_TYPE_HTTP_REQUEST:
        //CloseInternetFile();    //    q里是何意?Q?nbsp;   通过回调 讄httpFile
        hRequest = hInet;    //
        break;
    default:
        break;
    }
    cout<<"OnInternetHandleCreated, so ::SetEvent(hEvent[0])"<<endl;
    // HANDLE已创ZӞ异步控制Q?br>    ::SetEvent(hEvent[0]);
}
void OnInternetRequestComplete(HINTERNET hInternet, LPINTERNET_ASYNC_RESULT lpInetStatusResult)
{

    if( lpInetStatusResult == NULL )
    {
        //ATLASSERT( 0 );
        return;
    }
    cout<<"OnInternetRequestComplete, so ::SetEvent(hEvent[2])"<<endl;
    // Ȁ发请求完成事Ӟ异步控制Q?br>    ::SetEvent(hEvent[0]);
}

void CALLBACK InternetStatusCallback(
                                    HINTERNET hInternet,
                                    DWORD_PTR dwContext,
                                    DWORD dwInternetStatus,
                                    LPVOID lpvStatusInformation,
                                    DWORD dwStatusInformationLength
                                    )
{
    cout<<"q入回调"<<endl;
    switch (dwInternetStatus)
    {
    case INTERNET_STATUS_RESOLVING_NAME:
        break;
    case INTERNET_STATUS_NAME_RESOLVED:
        break;
    case INTERNET_STATUS_CONNECTING_TO_SERVER:
        break;
    case INTERNET_STATUS_CONNECTED_TO_SERVER:
        break;
    case INTERNET_STATUS_SENDING_REQUEST:
        break;
    case INTERNET_STATUS_REQUEST_SENT:
        break;
    case INTERNET_STATUS_RECEIVING_RESPONSE:
        break;
    case INTERNET_STATUS_RESPONSE_RECEIVED:
        break;
    case INTERNET_STATUS_CLOSING_CONNECTION:
        break;
    case INTERNET_STATUS_CONNECTION_CLOSED:
        break;
    case INTERNET_STATUS_HANDLE_CREATED:
        cout<<"回调是INTERNET_STATUS_HANDLE_CREATED"<<endl;
        OnInternetHandleCreated(hInternet, LPINTERNET_ASYNC_RESULT(lpvStatusInformation)); // 传递了HINTERNET q是_N呀
        break;
    case INTERNET_STATUS_HANDLE_CLOSING:
        break;
    case INTERNET_STATUS_REQUEST_COMPLETE:
        cout<<"回调是INTERNET_STATUS_REQUEST_COMPLETE"<<endl;
        OnInternetRequestComplete(hInternet, LPINTERNET_ASYNC_RESULT(lpvStatusInformation));
        break;
    case INTERNET_STATUS_REDIRECT:
    case INTERNET_STATUS_INTERMEDIATE_RESPONSE:
    case INTERNET_STATUS_STATE_CHANGE:
    default:
        break;
    }
}



漂漂 2010-03-26 09:22 发表评论
]]>
DrawFrameControl l制控ghttp://www.shnenglu.com/AutomateProgram/archive/2010/03/23/110335.html漂漂漂漂Tue, 23 Mar 2010 02:36:00 GMThttp://www.shnenglu.com/AutomateProgram/archive/2010/03/23/110335.htmlhttp://www.shnenglu.com/AutomateProgram/comments/110335.htmlhttp://www.shnenglu.com/AutomateProgram/archive/2010/03/23/110335.html#Feedback0http://www.shnenglu.com/AutomateProgram/comments/commentRss/110335.htmlhttp://www.shnenglu.com/AutomateProgram/services/trackbacks/110335.htmlDrawFrameControl 的效果:
RECT rect;
    rect.top = 0;
    rect.left = 0;
    rect.bottom = 40;    
    rect.right = 40;
    pDC->DrawFrameControl(&rect, DFC_CAPTION, DFCS_CAPTIONCLOSE);
    rect.left = 50;
    rect.right = 90;
    pDC->DrawFrameControl(&rect, DFC_CAPTION, DFCS_CAPTIONMIN);
    rect.left = 100;
    rect.right = 140;
    pDC->DrawFrameControl(&rect, DFC_CAPTION, DFCS_CAPTIONMAX);
    rect.left = 150;
    rect.right = 190;
    pDC->DrawFrameControl(&rect, DFC_CAPTION, DFCS_CAPTIONRESTORE);
    rect.left = 200;
    rect.right = 240;
    pDC->DrawFrameControl(&rect, DFC_CAPTION, DFCS_CAPTIONHELP);
 // TODO: Menu bar
    rect.left = 250;   
    rect.right = 290;
    pDC->DrawFrameControl(&rect, DFC_MENU, DFCS_MENUARROW);
    rect.left = 300;
    rect.right = 340;
    pDC->DrawFrameControl(&rect, DFC_MENU, DFCS_MENUCHECK);
    rect.left = 350;
    rect.right = 390;
    pDC->DrawFrameControl(&rect, DFC_MENU, DFCS_MENUBULLET);
    rect.left = 400;
    rect.right = 440;
    pDC->DrawFrameControl(&rect, DFC_MENU, DFCS_MENUARROWRIGHT);
 // TODO: Scroll bar
    rect.top = 50;
    rect.left = 0;
    rect.bottom = 90;    
    rect.right = 40;
    pDC->DrawFrameControl(&rect, DFC_SCROLL, DFCS_SCROLLUP);
    rect.left = 50;
    rect.right = 90;
    pDC->DrawFrameControl(&rect, DFC_SCROLL, DFCS_SCROLLDOWN);
    rect.left = 100;
    rect.right = 140;
    pDC->DrawFrameControl(&rect, DFC_SCROLL, DFCS_SCROLLLEFT);
    rect.left = 150;
    rect.right = 190;
    pDC->DrawFrameControl(&rect, DFC_SCROLL, DFCS_SCROLLRIGHT);
    rect.left = 200;
    rect.right = 240;
    pDC->DrawFrameControl(&rect, DFC_SCROLL, DFCS_SCROLLCOMBOBOX);
    rect.left = 250;
    rect.right = 290;
    pDC->DrawFrameControl(&rect, DFC_SCROLL, DFCS_SCROLLSIZEGRIP);
    rect.left = 300;
    rect.right = 340;
    pDC->DrawFrameControl(&rect, DFC_SCROLL, DFCS_SCROLLSIZEGRIPRIGHT);
 // TODO: Standard button
    rect.top = 100;
    rect.left = 0;
    rect.bottom = 140;    
    rect.right = 40;
    pDC->DrawFrameControl(&rect, DFC_BUTTON, DFCS_BUTTONCHECK);
    rect.left = 50;
    rect.right = 90;
    pDC->DrawFrameControl(&rect, DFC_BUTTON, DFCS_BUTTONRADIOIMAGE);
    rect.left = 100;
    rect.right = 140;
    pDC->DrawFrameControl(&rect, DFC_BUTTON, DFCS_BUTTONRADIOMASK);
    rect.left = 150;
    rect.right = 190;
    pDC->DrawFrameControl(&rect, DFC_BUTTON, DFCS_BUTTONRADIO);
    rect.left = 200;
    rect.right = 240;
    pDC->DrawFrameControl(&rect, DFC_BUTTON, DFCS_BUTTON3STATE);
    rect.left = 250;
    rect.right = 290;
    pDC->DrawFrameControl(&rect, DFC_BUTTON, DFCS_BUTTONPUSH);

使用DrawFrameControl 定制自己的check按钮
void CMyCheckButton::OnPaint()
{
 CPaintDC dc(this); // device context for painting
 CRect rect;
 GetClientRect(&rect);

 CRect BoxRect;
 BoxRect=rect;
 BoxRect.right =BoxRect.left +15;
 dc.DrawFrameControl(BoxRect,DFC_BUTTON,DFCS_BUTTONCHECK|GetCheck()?DFCS_CHECKED :0);

 CFont   myFont; 
 myFont.CreatePointFont (100,_T("宋体"));
 CFont   *pOldFont=(CFont   *)dc.SelectObject(&myFont);
 dc.SetBkMode(TRANSPARENT);  

 CString StrWndText;
 GetWindowText(StrWndText);

 rect.OffsetRect (20,0);
 dc.SetTextColor(RGB(0,0,0));
 dc.DrawText(StrWndText,   rect,   DT_LEFT|DT_VCENTER|DT_SINGLELINE);
 dc.SelectObject   (pOldFont); 
 myFont.DeleteObject   (); 
}



漂漂 2010-03-23 10:36 发表评论
]]>
CFile操作详解http://www.shnenglu.com/AutomateProgram/archive/2010/03/22/110281.html漂漂漂漂Mon, 22 Mar 2010 03:09:00 GMThttp://www.shnenglu.com/AutomateProgram/archive/2010/03/22/110281.htmlhttp://www.shnenglu.com/AutomateProgram/comments/110281.htmlhttp://www.shnenglu.com/AutomateProgram/archive/2010/03/22/110281.html#Feedback0http://www.shnenglu.com/AutomateProgram/comments/commentRss/110281.htmlhttp://www.shnenglu.com/AutomateProgram/services/trackbacks/110281.html原文地址Q?a >http://blog.csdn.net/zhuang_rui/archive/2007/09/28/1804875.aspx
各种关于文g的操作在E序设计中是十分常见Q如果能对其各种操作都了如指掌,可以根据实际情冉|到最佳的解决ҎQ从而在较短的时间内~写出高效的代码Q因而熟l的掌握文g操作是十分重要的。本文将对Visual C++中有x件操作进行全面的介绍Qƈ对在文g操作中经帔R到的一些疑N题进行详l的分析?br>  1Q文件的查找
  当对一个文件操作时Q如果不知道该文件是否存在,p首先q行查找。MFC中有一个专门用来进行文件查扄cCFileFindQ用它可以方便快捷地进行文件的查找。下面这D代码演CZq个cȝ最基本使用Ҏ?
  CString strFileTitle;
  CFileFind finder;
  BOOL bWorking = finder.FindFile("C:\\windows\\sysbkup\\*.cab");
  while(bWorking)
  {
  bWorking=finder.FindNextFile();
  strFileTitle=finder.GetFileTitle();
  }
  2Q文件的打开/保存对话?
  让用户选择文gq行打开和存储操作时Q就要用到文件打开/保存对话框。MFC的类CFileDialog用于实现q种功能。用CFileDialog声明一个对象时Q第一个BOOL型参数用于指定文件的打开或保存,当ؓTRUE时将构造一个文件打开对话框,为FALSE时构造一个文件保存对话框?
  在构造CFileDialog对象Ӟ如果在参C指定了OFN_ALLOWMULTISELECT风格Q则在此对话框中可以q行多选操作。此时要重点注意为此CFileDialog对象的m_ofn.lpstrFile分配一块内存,用于存储多选操作所q回的所有文件\径名Q如果不q行分配或分配的内存q小׃D操作p|。下面这D늨序演CZ文g打开对话框的使用Ҏ?
  CFileDialog mFileDlg(TRUE,NULL,NULL,
  OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT|OFN_ALLOWMULTISELECT,
  "All Files (*.*)|*.*||",AfxGetMainWnd());
  CString str(" ",10000);
  mFileDlg.m_ofn.lpstrFile=str.GetBuffer(10000);
  str.ReleaseBuffer();
  POSITION mPos=mFileDlg.GetStartPosition();
  CString pathName(" ",128);
  CFileStatus status;
  while(mPos!=NULL)
  {
  pathName=mFileDlg.GetNextPathName(mPos);
  CFile::GetStatus( pathName, status );
  }
  3Q文件的d
  文g的读写非帔R要,下面重点进行介l。文件读写的最普通的Ҏ是直接用CFileq行Q如文g的读写可以用下面的ҎQ?
  //Ҏ件进行读操作
  char sRead[2];
  CFile mFile(_T("user.txt"),CFile::modeRead);
  if(mFile.GetLength()<2)
  return;
  mFile.Read(sRead,2);
  mFile.Close();
  //Ҏ件进行写操作
  CFile mFile(_T("user.txt "), CFile::modeWrite|CFile::modeCreate);
  mFile.Write(sRead,2);
  mFile.Flush();
  mFile.Close();
  虽然q种Ҏ最为基本,但是它的使用J琐Q而且功能非常单。我向你推荐的是使用CArchiveQ它的用方法简单且功能十分强大。首先还是用CFile声明一个对象,然后用这个对象的指针做参数声明一个CArchive对象Q你可以非常方便地存储各种复杂的数据类型了。它的用方法见下例?
  //Ҏ件进行写操作
  CString strTemp;
  CFile mFile;
  mFile.Open("d:\\dd\\try.TRY",CFile::modeCreate|CFile::modeNoTruncate|CFile::modeWrite);
  CArchive ar(&mFile,CArchive::store);
  ar<<  ar.Close();
  mFile.Close();
  //Ҏ件进行读操作
  CFile mFile;
  if(mFile.Open("d:\\dd\\try.TRY",CFile::modeRead)==0)
  return;
  CArchive ar(&mFile,CArchive::load);
   ar>>strTemp;
      ar.Close();
  mFile.Close();
  CArchive?<< ?gt;> 操作W用于简单数据类型的dQ对于CObjectzcȝ对象的存取要使用ReadObject()和WriteObject()。用CArchive的ReadClass()和WriteClass()q可以进行类的读写,如:
  //存储CAboutDlgc?
  ar.WriteClass(RUNTIME_CLASS(CAboutDlg));
  //dCAboutDlgc?
  CRuntimeClass* mRunClass=ar.ReadClass();
  //使用CAboutDlgc?
  CObject* pObject=mRunClass->CreateObject();
      ((CDialog* )pObject)->DoModal();
  虽然VC提供的文?视结构中的文档也可进行这些操作,但是不容易理解、用和理Q因此虽然很多VC入门的书上花费大量篇q讲q文?视结构,但我你最好不要用它的文档。关于如何进行文?视的分离有很多书介绍Q包括非常著名的《Visual C++ 技术内q》?
  如果你要q行的文件操作只是简单的d整行的字W串Q我你用CStdioFileQ用它来q行此类操作非常方便Q如下例?
  CStdioFile mFile;
  CFileException mExcept;
  mFile.Open( "d:\\temp\\aa.bat", CFile::modeWrite, &mExcept);
  CString string="I am a string.";
  mFile.WriteString(string);
  mFile.Close();
 4Q时文件的使用

  正规软gl常用到临时文gQ你l常可以会看到C:\Windows\Temp目录下有大量的扩展名为tmp的文Ӟq些是E序q行是徏立的临时文g。时文件的使用Ҏ基本与常规文件一P只是文g名应该调用函数GetTempFileName()获得。它的第一个参数是建立此时文件的路径Q第二个参数是徏立时文件名的前~Q第四个参数用于得到建立的时文件名。得到此临时文g名以后,你就可以用它来徏立ƈ操作文g了,如:
  char szTempPath[_MAX_PATH],szTempfile[_MAX_PATH];
  GetTempPath(_MAX_PATH, szTempPath);
  GetTempFileName(szTempPath,_T ("my_"),0,szTempfile);
  CFile m_tempFile(szTempfile,CFile:: modeCreate|CFile:: modeWrite);
  char m_char='a';
  m_tempFile.Write(&m_char,2);
  m_tempFile.Close();
  5Q文件的复制、删除等
  MFC中没有提供直接进行这些操作的功能Q因而要使用SDK。SDK中的文g相关函数常用的有CopyFile()、CreateDirectory()、DeleteFile()、MoveFile()。它们的用法很简单,可参考MSDN?

1,判断文g是否存在
    access(filename,mode);
2,对于不同用途又不同的文件操?其中API函数CreateFile()也是比较有用处理方式,对于巨型文g很合适的其他的楼上的大都说了,不重复了.

[1]昄对话框,取得文g?/p>

CString FilePathName;
CFileDialog dlg(TRUE);///TRUE为OPEN对话框,FALSE为S***E AS对话?br>if (dlg.DoModal() == IDOK)
    FilePathName=dlg.GetPathName();

相关信息QCFileDialog 用于取文件名的几个成员函敎ͼ
假如选择的文件是C:\WINDOWS\TEST.EXE
?1)GetPathName();取文件名全称Q包括完整\径。取回C:\WINDOWS\TEST.EXE
(2)GetFileTitle();取文件全名:TEST.EXE
(3)GetFileName();取回TEST
(4)GetFileExt();取扩展名EXE

[2]打开文g
CFile file("C:\HELLO.TXT",CFile::modeRead);//只读方式打开
//CFile::modeRead可改?CFile::modeWrite(只写),
//CFile::modeReadWrite(d),CFile::modeCreate(新徏)
例子Q?br>{
CFile file;
file.Open("C:\HELLO.TXT",CFile::modeCreate|Cfile::modeWrite);
.
.
.
}

[3]Ud文g指针
file.Seek(100,CFile::begin);///从文件头开始往下移?00字节
file.Seek(-50,CFile::end);///从文件末־上移?0字节
file.Seek(-30,CFile::current);///从当前位|往上移?0字节
file.SeekToBegin();///Ud文g?br>file.SeekToEnd();///Ud文g?/p>

[4]d文g

char buffer[1000];
file.Read(buffer,1000);
写文Ӟ
CString string("自强不息");
file.Write(string,8);

[5]关闭文g
file.Close();


本文来自CSDN博客Q{载请标明出处Q?a >http://blog.csdn.net/zhuang_rui/archive/2007/09/28/1804875.aspx



漂漂 2010-03-22 11:09 发表评论
]]>
使用OnCtlColor函数来改变控仉?/title><link>http://www.shnenglu.com/AutomateProgram/archive/2010/03/20/110180.html</link><dc:creator>漂漂</dc:creator><author>漂漂</author><pubDate>Sat, 20 Mar 2010 12:29:00 GMT</pubDate><guid>http://www.shnenglu.com/AutomateProgram/archive/2010/03/20/110180.html</guid><wfw:comment>http://www.shnenglu.com/AutomateProgram/comments/110180.html</wfw:comment><comments>http://www.shnenglu.com/AutomateProgram/archive/2010/03/20/110180.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.shnenglu.com/AutomateProgram/comments/commentRss/110180.html</wfw:commentRss><trackback:ping>http://www.shnenglu.com/AutomateProgram/services/trackbacks/110180.html</trackback:ping><description><![CDATA[<p>原文地址Qhttp://lixikeke.blog.hexun.com/25486166_d.html</p><p>在MFCcd提供?strong><font color="#0000ff">CWnd</font>::<font color="#993300">OnCtlColor</font></strong>函数,在工作框架的子窗口被重画时将调用该成员函?因此可以重蝲<strong>WM_CTLCOLOR</strong>消息的响应函?此函数的原型:<br>  <strong>afx_msg HBRUSH OnCtlColor(CDC *pDC,CWnd *pWnd,UINT nCtlColor);<br></strong>           参数<strong>nCtlColor</strong>用于指定控g的类?可以?<br>           .<strong><font color="#993300">CTLCOLOR_BTN</font></strong>                按钮控g<br>           .<strong><font color="#993300">CTLCOLOR_DLG</font></strong>                对话?br>           .<font color="#993300"><strong>CTLCOLOR</strong>_<strong>EDIT</strong></font>               ~辑?br>           .<strong><font color="#993300">CTLCOLOR_LISTBOX</font></strong>            列表控g<br>           .<strong><font color="#993300">CTLCOLOR_MSGBOX</font></strong>             消息控g<br>           .<strong><font color="#993300">CTLCOLOR_SCROLLBAR</font></strong> 滚动条控?br>           .<strong><font color="#993300">CTLCOLOR_STATIC</font></strong>             静态控?br><strong>[E序实现]</strong><br>           假设你已有了名ؓ<strong>My</strong>的对话框工程.你有了一?strong>STATIC</strong>的控ӞID?strong><font color="#000000">IDC_STATIC1</font></strong>.<br>  <strong><font color="#0000ff">HBRUSH CMyDlg</font>::<font color="#993300">OnCtlColor</font>(<font color="#0000ff">CDC</font>* pDC, <font color="#0000ff">CWnd</font>* pWnd, <font color="#0000ff">UINT</font> nCtlColor) <br>           {<br>        <font color="#0000ff">HBRUSH</font> hbr = <font color="#0000ff">CDialog</font>::<font color="#993300">OnCtlColor</font>(pDC, pWnd, nCtlColor);<br>  <br>       <font color="#008080"> </font><font color="#339966">// TODO: Change any attributes of the DC here</font><br>           <font color="#0000ff">if </font>(nCtlColor==<font color="#993300">CTLCOLOR_</font><font color="#993300">STATIC</font>)</strong></p> <p><strong>              {<br>                    pDC-><font color="#993300">SetTextColor</font>(<font color="#993300">RGB</font>(255,0,0));</strong>  <font color="#339966">//字体颜色<br></font><strong>                    pDC-><font color="#993300">SetBkColor</font>(<font color="#993300">RGB</font>(0, 0, 255));</strong>   <font color="#339966">//字体背景?/font><strong>  </strong></p> <p><strong>                }<br>       </strong><strong><font color="#339966"> // TODO: Return a different brush if the default is not desired<br></font>        <font color="#0000ff">return</font> hbr;<br>           }</strong></p> <p><br>如果要指定某个特定控件可以这样写Q?strong><font color="#000000">ID为IDC_STATIC1</font></strong></p> <p><font color="#0000ff"><strong>if </strong></font><strong><font color="#000000">(pWnd-><font color="#993300">GetDlgCtrlID</font>()==<font color="#ff00ff">IDC_STATIC1</font>)<br>{<br>       pDC-><font color="#993300">SetTextColor</font></font><font color="#000000">(</font></strong><font color="#000000"><strong><font color="#ff00ff">RGB</font>(255,0,0));  <font color="#339966">//讄字体颜色</font><br>       pDC-><font color="#993300">SetBkMode</font>(<font color="#ff00ff">TRANSPARENT</font>); <font color="#339966">//讄字体背景为透明</font><br></strong><font color="#339966"><strong>// TODO: Return a different brush if the default is not desired</strong><br></font><strong>  <font color="#0000ff">return</font> (<font color="#0000ff">HBRUSH</font>)::<font color="#993300">GetStockObject</font>(<font color="#ff00ff">BLACK_BRUSH</font>);  <font color="#339966">// 讄背景?br></font>}<br><font color="#0000ff">else</font><br><font color="#0000ff">return</font> hbr;</strong></font></p> <p><font color="#000000"><strong>【注?/strong></font></p> <p><font color="#000000"><strong>BLACK_BRUSH:</strong>黑色</font></p> <p><font color="#000000"><strong>WHITE_BRUSH:</strong>白色</font></p> <p><font color="#000000"><strong>GRAY_BRUSH:</strong>灰色</font></p> <p><font color="#000000"><strong>NULL_BRUSH:</strong>透明</font></p> <p><font color="#000000"><font size="2"><strong><font color="#000000">HOLLOW_BRUSH</font></strong> Q透明</font></font></p><p><span style="font-size: small;">注:如果在窗体中使用q种ҎQ会引v代码的臃肿,自定义自qcR?/span></p><img src ="http://www.shnenglu.com/AutomateProgram/aggbug/110180.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.shnenglu.com/AutomateProgram/" target="_blank">漂漂</a> 2010-03-20 20:29 <a href="http://www.shnenglu.com/AutomateProgram/archive/2010/03/20/110180.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>VC之美化界面篇Q{Q?/title><link>http://www.shnenglu.com/AutomateProgram/archive/2010/03/20/110179.html</link><dc:creator>漂漂</dc:creator><author>漂漂</author><pubDate>Sat, 20 Mar 2010 12:12:00 GMT</pubDate><guid>http://www.shnenglu.com/AutomateProgram/archive/2010/03/20/110179.html</guid><wfw:comment>http://www.shnenglu.com/AutomateProgram/comments/110179.html</wfw:comment><comments>http://www.shnenglu.com/AutomateProgram/archive/2010/03/20/110179.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.shnenglu.com/AutomateProgram/comments/commentRss/110179.html</wfw:commentRss><trackback:ping>http://www.shnenglu.com/AutomateProgram/services/trackbacks/110179.html</trackback:ping><description><![CDATA[原文地址Q?a >http://blog.csdn.net/bluejoe2000/archive/2009/06/15/4269540.aspx</a> <img src ="http://www.shnenglu.com/AutomateProgram/aggbug/110179.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.shnenglu.com/AutomateProgram/" target="_blank">漂漂</a> 2010-03-20 20:12 <a href="http://www.shnenglu.com/AutomateProgram/archive/2010/03/20/110179.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>VS2005下如何安装WTLhttp://www.shnenglu.com/AutomateProgram/archive/2010/03/20/110153.html漂漂漂漂Sat, 20 Mar 2010 02:21:00 GMThttp://www.shnenglu.com/AutomateProgram/archive/2010/03/20/110153.htmlhttp://www.shnenglu.com/AutomateProgram/comments/110153.htmlhttp://www.shnenglu.com/AutomateProgram/archive/2010/03/20/110153.html#Feedback0http://www.shnenglu.com/AutomateProgram/comments/commentRss/110153.htmlhttp://www.shnenglu.com/AutomateProgram/services/trackbacks/110153.html原文地址Q?a >http://www.cnblogs.com/weitianshui/articles/1017178.html

Windows Template Library - WTL的安装指南满地都是,而且源程序也附带了安装说明,g没有必要再多写一个。但是,事情q没有想象的那么单,本h有过近4q的C++开发经验,在两q前刚{入C#Q有很长旉没碰C++了(最后用的工具是VC++6.0Q。今天突发兴,惌玩玩VC++ 2005和WTLQ谁知,以我q种l验Q折腾这看是单的玩意Q也׃大半天的旉Ҏ搞出W一个WTL的Hello WorldQ个中曲折,说来实在惭愧Q只好把正确的安装过E写一下,如果能给别h一ҎC,那是最好的了。至,可以做个W记Q两三年以后如果又忘了,q可以回来翻R?/font>

1、从http://sourceforge.net/projects/wtl下蝲最新版本的WTLQ当时是8.0
2、下载得到的是一个自解压文gQ可以解压到L目录。比如d:\wtl80Q下面有几个子目录samples, include, AppWizMobile, AppWizCE, AppWizQ还有一个txt文g和htm文g。其中include是头文gQsamples是几个示例,AppWiz*是各U向导文件?br>3、在命o行里执行Qwscript.exe "D:\WTL80\AppWiz\setup80.js"Q如果是vs2005 express、vs2003{,分别执行setup80x.js, setup70.js,可以看看里面的文档说明?br>   注:q是一个很Ҏ让h困惑的地方,|上的文档全都说解压后执行setup80.js文gQ但都没说如何执行。后来在代码里看到有WScriptQ才惛_用WSCript.exe来执行?/font>

4、打开VS2005, 选择VC++目Q就可以看到WTL目的向g?/font>

5、在VS2005里,打开菜单“工具”——选项——项目和解决Ҏ——VC++目录Q在“昄以下内容的目?#8221;下拉框中Q选择“包含文g”Q加入刚才解压的wtl下的includeQ本例中?d:\wtl80\include"?/font>

6、按照向导创Z个程序,~译Q执行,OK?/font>



漂漂 2010-03-20 10:21 发表评论
]]>
TransparentBlt的?/title><link>http://www.shnenglu.com/AutomateProgram/archive/2010/03/18/110001.html</link><dc:creator>漂漂</dc:creator><author>漂漂</author><pubDate>Thu, 18 Mar 2010 07:27:00 GMT</pubDate><guid>http://www.shnenglu.com/AutomateProgram/archive/2010/03/18/110001.html</guid><wfw:comment>http://www.shnenglu.com/AutomateProgram/comments/110001.html</wfw:comment><comments>http://www.shnenglu.com/AutomateProgram/archive/2010/03/18/110001.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.shnenglu.com/AutomateProgram/comments/commentRss/110001.html</wfw:commentRss><trackback:ping>http://www.shnenglu.com/AutomateProgram/services/trackbacks/110001.html</trackback:ping><description><![CDATA[  //自定义绘?br>  CDC* cdc = GetDC();     //前台DC<br>  CDC bufferDC;           //后台DC<br>  CDC tempDC;    //临时DC<br>  CBitmap bufferBMP;  //后台DC位图<br>  //DC兌<br>  bufferDC.CreateCompatibleDC(cdc);<br>  tempDC.CreateCompatibleDC(cdc);<br>  //后台DC位图兌        <br>  bufferBMP.CreateCompatibleBitmap(cdc,278,127);        <br>  bufferDC.SelectObject(bufferBMP);<br>  tempDC.SelectObject(m_bmp);<br>  bufferDC.TransparentBlt(0,0,278,127,&tempDC,0,0,278,127,RGB(255,255,255));<br>  //l制到前台DC<br>  cdc->BitBlt(0,0,278,127,&bufferDC,0,0,SRCCOPY);<br>  //释放资源<br>  bufferBMP.DeleteObject();<br>  tempDC.DeleteDC();<br>  bufferDC.DeleteDC();<br>  this->ReleaseDC(cdc);<br> <img src ="http://www.shnenglu.com/AutomateProgram/aggbug/110001.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.shnenglu.com/AutomateProgram/" target="_blank">漂漂</a> 2010-03-18 15:27 <a href="http://www.shnenglu.com/AutomateProgram/archive/2010/03/18/110001.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>透明位图的显C?/title><link>http://www.shnenglu.com/AutomateProgram/archive/2010/03/18/109999.html</link><dc:creator>漂漂</dc:creator><author>漂漂</author><pubDate>Thu, 18 Mar 2010 07:18:00 GMT</pubDate><guid>http://www.shnenglu.com/AutomateProgram/archive/2010/03/18/109999.html</guid><wfw:comment>http://www.shnenglu.com/AutomateProgram/comments/109999.html</wfw:comment><comments>http://www.shnenglu.com/AutomateProgram/archive/2010/03/18/109999.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.shnenglu.com/AutomateProgram/comments/commentRss/109999.html</wfw:commentRss><trackback:ping>http://www.shnenglu.com/AutomateProgram/services/trackbacks/109999.html</trackback:ping><description><![CDATA[     摘要: 原文地址Qhttp://www.vckbase.com/document/viewdoc/?id=532 包含透明色的位图的绘制方法有多种Q最单的Ҏ是调用现成的函数QTransparentBlt,也可以通过自己的代码实现类似TransparentBlt的功能,实现q程也有两种形式Q一U是事先做一张掩码位图,另一U是动态生成掩码位图。本文将介绍动态生成掩码位囄制具有透明区域位图的方法。一...  <a href='http://www.shnenglu.com/AutomateProgram/archive/2010/03/18/109999.html'>阅读全文</a><img src ="http://www.shnenglu.com/AutomateProgram/aggbug/109999.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.shnenglu.com/AutomateProgram/" target="_blank">漂漂</a> 2010-03-18 15:18 <a href="http://www.shnenglu.com/AutomateProgram/archive/2010/03/18/109999.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>E序控制桌面切换http://www.shnenglu.com/AutomateProgram/archive/2010/03/18/109977.html漂漂漂漂Thu, 18 Mar 2010 05:41:00 GMThttp://www.shnenglu.com/AutomateProgram/archive/2010/03/18/109977.htmlhttp://www.shnenglu.com/AutomateProgram/comments/109977.htmlhttp://www.shnenglu.com/AutomateProgram/archive/2010/03/18/109977.html#Feedback0http://www.shnenglu.com/AutomateProgram/comments/commentRss/109977.htmlhttp://www.shnenglu.com/AutomateProgram/services/trackbacks/109977.html原文地址Q?a >http://blog.csdn.net/goingup/archive/2006/03/07/618323.aspx

用向导徏立一个Win32E序“hello, world!”Q然后用下面的内Ҏ换整个CPP文g内容可以了


#include "stdafx.h"
#include "resource.h"

#include "ShellApi.h"

HINSTANCE hInst = NULL;

HDESK hDesktopCurrent;
HDESK hDesktopLlx;

LONG APIENTRY WndProc(
    HWND hWnd,
    UINT message,      // type of message
    WPARAM wParam,     // additional information
    LPARAM lParam)     // additional information
{
 int wmId, wmEvent;
 PAINTSTRUCT ps;
 HDC hdc;

 switch (message)
 {
  case WM_COMMAND:
   wmId    = LOWORD(wParam);
   wmEvent = HIWORD(wParam);
   // Parse the menu selections:
   switch (wmId)
   {
    case IDM_ABOUT:
       //DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About);
       break;
    case IDM_EXIT:
       DestroyWindow(hWnd);
       break;
    default:
       return DefWindowProc(hWnd, message, wParam, lParam);
   }
   break;
  case WM_PAINT:
   hdc = BeginPaint(hWnd, &ps);
   // TODO: Add any drawing code here...
   RECT rt;
   GetClientRect(hWnd, &rt);
   //DrawText(hdc, szHello, strlen(szHello), &rt, DT_CENTER);
   EndPaint(hWnd, &ps);
   break;
  /*
  case WM_DESTROY:
   PostQuitMessage(0);
   break;
  //*/
  case WM_LBUTTONDOWN:
   break;
  case WM_HOTKEY:
   if(7777 == wParam)
   {
    PostQuitMessage(0);
   }
   else if(7778 == wParam)
   {
    SwitchDesktop(hDesktopCurrent);
   }
   else if(7779 == wParam)
   {
    SwitchDesktop(hDesktopLlx);
   }
   break;
  case WM_QUIT:
  case WM_DESTROY:
   SwitchDesktop(hDesktopCurrent);
   return DefWindowProc(hWnd, message, wParam, lParam);
  default:
   return DefWindowProc(hWnd, message, wParam, lParam);
   }
   return 0;
}

void StartMyExplore(void)
{
 STARTUPINFO sui;         // Process startup info
 PROCESS_INFORMATION pi;  // info returned from CreateProcess
 //
 // Most sui members will be 0
 //
 ZeroMemory ((PVOID)&sui, sizeof(sui));

 sui.cb = sizeof (sui);
 //
 // Need the lpDesktop member so the new process runs on this desktop
 // The lpDesktop member was reserved in previous versions of NT
 //
 sui.lpDesktop = _T("llx");
 CreateProcess (NULL,   // image name
      "explorer", // command line
      NULL,   // process security attributes
      NULL,   // thread security attributes
      TRUE,   // inherit handles
      CREATE_DEFAULT_ERROR_MODE|CREATE_SEPARATE_WOW_VDM,
      NULL,   // environment block
      NULL,   // current directory
      &sui,   // STARTUPINFO
      &pi);   // PROCESS_INFORMATION
}

int CALLBACK WinMain( HINSTANCE hInstance,
      HINSTANCE hPrevInstance,
      LPSTR lpCmdLine,
      int nCmdShow)
{
 WNDCLASS wc;
 wc.style = CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW;
 wc.lpfnWndProc = WndProc;
 wc.cbClsExtra = 0;
 wc.cbWndExtra = 0;
 wc.hInstance = hInstance;
 wc.hIcon = LoadIcon (NULL, "IDI_SETTHREADDESKTOP");
 wc.hCursor = LoadCursor (NULL, IDC_ARROW);
 wc.hbrBackground = (HBRUSH)GetStockObject (WHITE_BRUSH);
 wc.lpszMenuName = NULL;
 wc.lpszClassName = "lilinxiang";
 if(!RegisterClass(&wc))
 {
  return TRUE;
 }

 hDesktopCurrent = NULL;
 hDesktopCurrent = GetThreadDesktop(GetCurrentThreadId());

 hDesktopLlx = NULL;
 hDesktopLlx = OpenDesktop("llx", 0, FALSE, NULL);
    if (hDesktopLlx != NULL)
 {
  CloseDesktop(hDesktopLlx);
 }

 SECURITY_ATTRIBUTES sa;
 sa.bInheritHandle = TRUE;
 sa.nLength = sizeof(SECURITY_ATTRIBUTES);
 sa.lpSecurityDescriptor = NULL;

 hDesktopLlx = CreateDesktop("llx", NULL,
                                    NULL,0,MAXIMUM_ALLOWED,
                                    NULL);
 if(hDesktopLlx == NULL)
 {
  return 0;
 }

 if(!SetThreadDesktop(hDesktopLlx))
 {
  char szError[256] = {0};
  ltoa( (long)(GetLastError()) , szError, 10);
 }
 SwitchDesktop(hDesktopLlx);

 HWND hWnd = NULL;
 hWnd = CreateWindow ("lilinxiang",
        "hello, world!",
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT,
        0,
        CW_USEDEFAULT,
        0,
        NULL,
        NULL,
        hInstance,
        NULL
        );
 if(NULL == hWnd)
 {
  return TRUE;
 }

 //register hotkey for exit this desktop or switch to another desktop
 //ShowWindow(hWnd, SW_SHOW);
 //UpdateWindow(hWnd);
 if(!RegisterHotKey(hWnd, 7777, MOD_CONTROL, 'Q'))
 {//exit process
  return TRUE;
 }
 if(!RegisterHotKey(hWnd, 7778, MOD_CONTROL | MOD_SHIFT, 'Q'))
 {//switch to new desktop
  return TRUE;
 }
 if(!RegisterHotKey(hWnd, 7779, MOD_CONTROL | MOD_SHIFT, 'W'))
 {//switch to original desktop
  return TRUE;
 }
 
 StartMyExplore();

 MSG msg;
 while (GetMessage(&msg, NULL,  0, 0))
    {
                 TranslateMessage(&msg);// Translates virtual key codes
                 DispatchMessage(&msg); // Dispatches message to window
    }

 SwitchDesktop(hDesktopCurrent);
 return TRUE;
}


//   : )  好东西噢

用CreateDesktop新徏一个桌面,什么都没有的桌面,然后在新桌面环境中运行了explorer所以就有了和windows一样功能的桌面Q这个时候你有了两个桌面了Q下面要做的是用SwitchDesktop切换不同的桌面了

在不同桌面中打开的程序在其他桌面的Q务栏包括pȝ托盘上不可见Q不qQ务管理器中还是可见的q样你就非常方便的做很多事了Q很?..  而且q可以非常快的{到正常状态上可以让会责骂你的人比如老板什么的无话可说Q因Z们什么也看不?nbsp; : )

或者你也可以给你用户定制一个个性化的桌面。改改上面的代码可以得到你要的效果的?/p>


本文来自CSDN博客Q{载请标明出处Q?a >http://blog.csdn.net/goingup/archive/2006/03/07/618323.aspx用向导徏立一个Win32E序“hello, world!”Q然后用下面的内Ҏ换整个CPP文g内容可以了


#include "stdafx.h"
#include "resource.h"

#include "ShellApi.h"

HINSTANCE hInst = NULL;

HDESK hDesktopCurrent;
HDESK hDesktopLlx;

LONG APIENTRY WndProc(
    HWND hWnd,
    UINT message,      // type of message
    WPARAM wParam,     // additional information
    LPARAM lParam)     // additional information
{
 int wmId, wmEvent;
 PAINTSTRUCT ps;
 HDC hdc;

 switch (message)
 {
  case WM_COMMAND:
   wmId    = LOWORD(wParam);
   wmEvent = HIWORD(wParam);
   // Parse the menu selections:
   switch (wmId)
   {
    case IDM_ABOUT:
       //DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About);
       break;
    case IDM_EXIT:
       DestroyWindow(hWnd);
       break;
    default:
       return DefWindowProc(hWnd, message, wParam, lParam);
   }
   break;
  case WM_PAINT:
   hdc = BeginPaint(hWnd, &ps);
   // TODO: Add any drawing code here...
   RECT rt;
   GetClientRect(hWnd, &rt);
   //DrawText(hdc, szHello, strlen(szHello), &rt, DT_CENTER);
   EndPaint(hWnd, &ps);
   break;
  /*
  case WM_DESTROY:
   PostQuitMessage(0);
   break;
  //*/
  case WM_LBUTTONDOWN:
   break;
  case WM_HOTKEY:
   if(7777 == wParam)
   {
    PostQuitMessage(0);
   }
   else if(7778 == wParam)
   {
    SwitchDesktop(hDesktopCurrent);
   }
   else if(7779 == wParam)
   {
    SwitchDesktop(hDesktopLlx);
   }
   break;
  case WM_QUIT:
  case WM_DESTROY:
   SwitchDesktop(hDesktopCurrent);
   return DefWindowProc(hWnd, message, wParam, lParam);
  default:
   return DefWindowProc(hWnd, message, wParam, lParam);
   }
   return 0;
}

void StartMyExplore(void)
{
 STARTUPINFO sui;         // Process startup info
 PROCESS_INFORMATION pi;  // info returned from CreateProcess
 //
 // Most sui members will be 0
 //
 ZeroMemory ((PVOID)&sui, sizeof(sui));

 sui.cb = sizeof (sui);
 //
 // Need the lpDesktop member so the new process runs on this desktop
 // The lpDesktop member was reserved in previous versions of NT
 //
 sui.lpDesktop = _T("llx");
 CreateProcess (NULL,   // image name
      "explorer", // command line
      NULL,   // process security attributes
      NULL,   // thread security attributes
      TRUE,   // inherit handles
      CREATE_DEFAULT_ERROR_MODE|CREATE_SEPARATE_WOW_VDM,
      NULL,   // environment block
      NULL,   // current directory
      &sui,   // STARTUPINFO
      &pi);   // PROCESS_INFORMATION
}

int CALLBACK WinMain( HINSTANCE hInstance,
      HINSTANCE hPrevInstance,
      LPSTR lpCmdLine,
      int nCmdShow)
{
 WNDCLASS wc;
 wc.style = CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW;
 wc.lpfnWndProc = WndProc;
 wc.cbClsExtra = 0;
 wc.cbWndExtra = 0;
 wc.hInstance = hInstance;
 wc.hIcon = LoadIcon (NULL, "IDI_SETTHREADDESKTOP");
 wc.hCursor = LoadCursor (NULL, IDC_ARROW);
 wc.hbrBackground = (HBRUSH)GetStockObject (WHITE_BRUSH);
 wc.lpszMenuName = NULL;
 wc.lpszClassName = "lilinxiang";
 if(!RegisterClass(&wc))
 {
  return TRUE;
 }

 hDesktopCurrent = NULL;
 hDesktopCurrent = GetThreadDesktop(GetCurrentThreadId());

 hDesktopLlx = NULL;
 hDesktopLlx = OpenDesktop("llx", 0, FALSE, NULL);
    if (hDesktopLlx != NULL)
 {
  CloseDesktop(hDesktopLlx);
 }

 SECURITY_ATTRIBUTES sa;
 sa.bInheritHandle = TRUE;
 sa.nLength = sizeof(SECURITY_ATTRIBUTES);
 sa.lpSecurityDescriptor = NULL;

 hDesktopLlx = CreateDesktop("llx", NULL,
                                    NULL,0,MAXIMUM_ALLOWED,
                                    NULL);
 if(hDesktopLlx == NULL)
 {
  return 0;
 }

 if(!SetThreadDesktop(hDesktopLlx))
 {
  char szError[256] = {0};
  ltoa( (long)(GetLastError()) , szError, 10);
 }
 SwitchDesktop(hDesktopLlx);

 HWND hWnd = NULL;
 hWnd = CreateWindow ("lilinxiang",
        "hello, world!",
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT,
        0,
        CW_USEDEFAULT,
        0,
        NULL,
        NULL,
        hInstance,
        NULL
        );
 if(NULL == hWnd)
 {
  return TRUE;
 }

 //register hotkey for exit this desktop or switch to another desktop
 //ShowWindow(hWnd, SW_SHOW);
 //UpdateWindow(hWnd);
 if(!RegisterHotKey(hWnd, 7777, MOD_CONTROL, 'Q'))
 {//exit process
  return TRUE;
 }
 if(!RegisterHotKey(hWnd, 7778, MOD_CONTROL | MOD_SHIFT, 'Q'))
 {//switch to new desktop
  return TRUE;
 }
 if(!RegisterHotKey(hWnd, 7779, MOD_CONTROL | MOD_SHIFT, 'W'))
 {//switch to original desktop
  return TRUE;
 }
 
 StartMyExplore();

 MSG msg;
 while (GetMessage(&msg, NULL,  0, 0))
    {
                 TranslateMessage(&msg);// Translates virtual key codes
                 DispatchMessage(&msg); // Dispatches message to window
    }

 SwitchDesktop(hDesktopCurrent);
 return TRUE;
}


//   : )  好东西噢

用CreateDesktop新徏一个桌面,什么都没有的桌面,然后在新桌面环境中运行了explorer所以就有了和windows一样功能的桌面Q这个时候你有了两个桌面了Q下面要做的是用SwitchDesktop切换不同的桌面了

在不同桌面中打开的程序在其他桌面的Q务栏包括pȝ托盘上不可见Q不qQ务管理器中还是可见的q样你就非常方便的做很多事了Q很?..  而且q可以非常快的{到正常状态上可以让会责骂你的人比如老板什么的无话可说Q因Z们什么也看不?nbsp; : )

或者你也可以给你用户定制一个个性化的桌面。改改上面的代码可以得到你要的效果的?/font>



漂漂 2010-03-18 13:41 发表评论
]]>
RegisterHotKey注册热键http://www.shnenglu.com/AutomateProgram/archive/2010/03/18/109970.html漂漂漂漂Thu, 18 Mar 2010 03:49:00 GMThttp://www.shnenglu.com/AutomateProgram/archive/2010/03/18/109970.htmlhttp://www.shnenglu.com/AutomateProgram/comments/109970.htmlhttp://www.shnenglu.com/AutomateProgram/archive/2010/03/18/109970.html#Feedback0http://www.shnenglu.com/AutomateProgram/comments/commentRss/109970.htmlhttp://www.shnenglu.com/AutomateProgram/services/trackbacks/109970.htmlhotKeyDlg.h
 afx_msg LONG OnHotKey(WPARAM wPARAM, LPARAM lPARAM);

hotKeyDlg.cpp
#define ID_EXIT   0x5000
#define ID_SHOW         0X6000 

BEGIN_MESSAGE_MAP(CHotKeyDlg, CDialog)
 //{{AFX_MSG_MAP(CHotKeyDlg)
 ON_WM_SYSCOMMAND()
 ON_WM_PAINT()
 ON_WM_QUERYDRAGICON()
 ON_MESSAGE(WM_HOTKEY, OnHotKey)

BOOL CHotKeyDlg::OnInitDialog()
{
... 
::RegisterHotKey(m_hWnd, ID_EXIT, MOD_ALT, '5');
 ::RegisterHotKey(m_hWnd, ID_SHOW, MOD_ALT, '6');
}


LONG CHotKeyDlg::OnHotKey(WPARAM wParam,LPARAM lParam)
{
 if (wParam == ID_EXIT)
 {
  PostMessage(WM_QUIT, NULL, NULL);
 }
 if(wParam == ID_SHOW)
 {
  AfxMessageBox("ALT + 6", 0, 0);
 }
 return 0;
}

void CHotKeyDlg::OnDestroy()
{
 CDialog::OnDestroy();
 UnregisterHotKey(m_hWnd, ID_EXIT);
 UnregisterHotKey(m_hWnd, ID_SHOW);
}

漂漂 2010-03-18 11:49 发表评论
]]>
WM_NCLBUTTONDOWN 消息http://www.shnenglu.com/AutomateProgram/archive/2010/03/18/109963.html漂漂漂漂Thu, 18 Mar 2010 02:40:00 GMThttp://www.shnenglu.com/AutomateProgram/archive/2010/03/18/109963.htmlhttp://www.shnenglu.com/AutomateProgram/comments/109963.htmlhttp://www.shnenglu.com/AutomateProgram/archive/2010/03/18/109963.html#Feedback0http://www.shnenglu.com/AutomateProgram/comments/commentRss/109963.htmlhttp://www.shnenglu.com/AutomateProgram/services/trackbacks/109963.html

The WM_NCLBUTTONDOWN message is posted when the user presses the left mouse button while the cursor is within the nonclient area of a window. This message is posted to the window that contains the cursor. If a window has captured the mouse, this message is not posted.

使用WM_NCLBUTTONDOWN 消息Q?br>利用标题栏的消息Q欺骗windows使得点击客户区可实现H体的移动。在打击客户区的时候假传消息给windowsQ告诉他现在左击标题栏?br>
void CDragWindowDlg::OnLButtonDown(UINT nFlags, CPoint point)
{
 // TODO: Add your message handler code here and/or call default
 PostMessage(WM_NCLBUTTONDOWN, HTCAPTION, MAKELPARAM(point.x, point.y));
 CDialog::OnLButtonDown(nFlags, point);
}

C# 使用WM_NCLBUTTONDOWN消息实现L位置UdH体

public const int WM_NCLBUTTONDOWN = 0xA1;
       public const int HT_CAPTION = 0x2;
    [DllImportAttribute("user32.dll")]
  public static extern int SendMessage(IntPtr hWnd,
                int Msg, int wParam, int lParam);
[DllImportAttribute("user32.dll")]
 public static extern bool ReleaseCapture();

  private void Form1_MouseDown(object sender, MouseEventArgs e)
  {
    if (e.Button == MouseButtons.Left)
{
  if ((e.Clicks == 1))
{
ReleaseCapture();

SendMessage(this.Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
 }

 }

 }


漂漂 2010-03-18 10:40 发表评论
]]>
COM聚合 用到的c++的一些东?/title><link>http://www.shnenglu.com/AutomateProgram/archive/2010/03/16/109840.html</link><dc:creator>漂漂</dc:creator><author>漂漂</author><pubDate>Tue, 16 Mar 2010 10:49:00 GMT</pubDate><guid>http://www.shnenglu.com/AutomateProgram/archive/2010/03/16/109840.html</guid><wfw:comment>http://www.shnenglu.com/AutomateProgram/comments/109840.html</wfw:comment><comments>http://www.shnenglu.com/AutomateProgram/archive/2010/03/16/109840.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.shnenglu.com/AutomateProgram/comments/commentRss/109840.html</wfw:commentRss><trackback:ping>http://www.shnenglu.com/AutomateProgram/services/trackbacks/109840.html</trackback:ping><description><![CDATA[<p>#include <iostream><br>using namespace std;</p> <p>class A<br>{<br>public:<br> virtual void FA() = 0;<br>};<br>class B<br>{<br>public:<br> virtual void FB() = 0;<br>};<br>class C : public A , public B<br>{<br>public:<br> void FA(){cout<<"FA"<<endl;}<br> void FB(){cout<<"FB"<<endl;}<br>};<br>int main()<br>{<br> C* mc = new C();<br> B* mb = static_cast<B*>(mc);<br> ((A*)mb)->FA();<br> return 0;<br>}<br><br>输出的是FB, com中利用这机制实现了聚合?/p> <img src ="http://www.shnenglu.com/AutomateProgram/aggbug/109840.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.shnenglu.com/AutomateProgram/" target="_blank">漂漂</a> 2010-03-16 18:49 <a href="http://www.shnenglu.com/AutomateProgram/archive/2010/03/16/109840.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>DllMain详解和DLL 实践说明 (DllMain的?http://www.shnenglu.com/AutomateProgram/archive/2010/03/15/109718.html漂漂漂漂Mon, 15 Mar 2010 01:24:00 GMThttp://www.shnenglu.com/AutomateProgram/archive/2010/03/15/109718.htmlhttp://www.shnenglu.com/AutomateProgram/comments/109718.htmlhttp://www.shnenglu.com/AutomateProgram/archive/2010/03/15/109718.html#Feedback0http://www.shnenglu.com/AutomateProgram/comments/commentRss/109718.htmlhttp://www.shnenglu.com/AutomateProgram/services/trackbacks/109718.html原文地址Q?a >http://blog.csdn.net/benkaoya/archive/2008/06/02/2504781.aspx

1  DLL的进?退出函?br>1.1  DllMain?br>跟exe有个main或者WinMain入口函数一PDLL也有一个入口函敎ͼ是DllMain。以“DllMain”为关键字Q来看看MSDN帮助文档怎么介绍q个函数的?br>The DllMain function is an optional method of entry into a dynamic-link library (DLL)。(要翻译:对于一个Dll模块QDllMain函数是可选的。)q句话很重要Q很多初学者可能都认ؓ一个动态链接库肯定要有DllMain函数。其实不Ӟ像很多仅仅包含资源信息的DLL是没有DllMain函数的?br>1.2 何时调用DllMain
       pȝ是在什么时候调用DllMain函数的呢Q静态链接时Q或动态链接时调用LoadLibrary和FreeLibrary都会调用DllMain函数。DllMain的第三个参数fdwReason指明了系l调用Dll的原因,它可能是DLL_PROCESS_ATTACH、DLL_PROCESS_DETACH、DLL_THREAD_ATTACH和DLL_THREAD_DETACH。以下从q四U情冉|分析pȝ何时调用了DllMain?nbsp;           
1.2.1 DLL_PROCESS_ATTACH
       大家都知道,一个程序要调用Dll里的函数Q首先要先把DLL文g映射到进E的地址I间。要把一个DLL文g映射到进E的地址I间Q有两种ҎQ静态链接和动态链接的LoadLibrary或者LoadLibraryEx?br>       当一个DLL文g被映到q程的地址I间Ӟpȝ调用该DLL的DllMain函数Q传递的fdwReason参数为DLL_PROCESS_ATTACH。这U调用只会发生在W一ơ映时。如果同一个进E后来ؓ已经映射q来的DLL再次调用LoadLibrary或者LoadLibraryExQ操作系l只会增加DLL的用次敎ͼ它不会再用DLL_PROCESS_ATTACH调用DLL的DllMain函数。不同进E用LoadLibrary同一个DLLӞ每个q程的第一ơ映都会用DLL_PROCESS_ATTACH调用DLL的DllMain函数?br>       可参考DllMainTest的DLL_PROCESS_ATTACH_Test函数?br>1.2.2 DLL_PROCESS_DETACH
       当DLL被从q程的地址I间解除映射Ӟpȝ调用了它的DllMainQ传递的fdwReason值是DLL_PROCESS_DETACH。当DLL处理该值时Q它应该执行q程相关的清理工作?br>       那么什么时候DLL被从q程的地址I间解除映射呢?两种情况Q?br>       ◆FreeLibrary解除DLL映射Q有几个LoadLibraryQ就要有几个FreeLibraryQ?br>       ◆进E结束而解除DLL映射Q在q程l束前还没有解除DLL的映,q程l束后会解除DLL映射。(如果q程的终l是因ؓ调用了TerminateProcessQ系l就不会用DLL_PROCESS_DETACH来调用DLL的DllMain函数。这意味着DLL在进E结束前没有Z执行M清理工作。)
       注意Q当用DLL_PROCESS_ATTACH调用DLL的DllMain函数Ӟ如果q回FALSEQ说明没有初始化成功Q系l仍会用DLL_PROCESS_DETACH调用DLL的DllMain函数。因此,必须保没有清理那些没有成功初始化的东西?br>       可参考DllMainTest的DLL_PROCESS_DETACH_Test函数?br>1.2.3 DLL_THREAD_ATTACH
       当进E创ZU程Ӟpȝ查看当前映射到进E地址I间中的所有DLL文g映像Qƈ用值DLL_THREAD_ATTACH调用DLL的DllMain函数?br>新创建的U程负责执行q次的DLL的DllMain函数Q只有当所有的DLL都处理完q一通知后,pȝ才允许进E开始执行它的线E函数?br>注意跟DLL_PROCESS_ATTACH的区别,我们在前面说q,Wn(n>=2)ơ以后地把DLL映像文g映射到进E的地址I间Ӟ是不再用DLL_PROCESS_ATTACH调用DllMain的。而DLL_THREAD_ATTACH不同Q进E中的每ơ徏立线E,都会用值DLL_THREAD_ATTACH调用DllMain函数Q哪怕是U程中徏立线E也一栗?br>1.2.4 DLL_THREAD_DETACH
       如果U程调用了ExitThread来结束线E(U程函数q回Ӟpȝ也会自动调用ExitThreadQ,pȝ查看当前映射到进E空间中的所有DLL文g映像Qƈ用DLL_THREAD_DETACH来调用DllMain函数Q通知所有的DLLL行线E的清理工作?br>       注意Q如果线E的l束是因为系l中的一个线E调用了TerminateThreadQ系l就不会用值DLL_THREAD_DETACH来调用所有DLL的DllMain函数?br>1.3  为DllMain换名
在早期的SDK版本中,DllMain是叫做DllEntryPoint。其实有一仉Zh知的事:一个Dll的入口函数名是可以自己定义的。下面我以VC++6.0Z来演C如何更攏V首先要说明一点,虽然DllMain可以换成其他函数名,但函数的参数和返回值必dDllMain一栗而且q个函数要ؓ__stdcallcdQDllMain本n也是__stdcallcdQ?br>打开VC++菜单Project\Settings\Link tab\ Output in the Category boxQ如下图Q在Entry-point symbol中输入要替换DllMain的函数名Q当然这个函数名是你E序中已l实现的函数Q。Entry-point symbol是干么的呢?可以以关键字“Entry-point symbol”搜烦MSDN帮助文档查看Q搜索时Q打?#8220;仅搜索标?#8221;会更快定位?/p>

         按OK后,如果马上~译的话会出现如下错误:
LIBCMTD.lib(crt0.obj) : error LNK2001: unresolved external symbol _main
Debug/Dll.dll : fatal error LNK1120: 1 unresolved externals
打开VC++菜单Project\Settings\C/C++选项卡,如下图,在Project OptionsQ末地方d”/D”Q图中蓝色高亮的地方Q,要注意位|,我试了,要把/D攑ֈ/GZ后面也会链接错误Q我也不懂ؓ什么,^_^。按OKQ再ơ编译,成功。大家可以自己测试下到底有没有更Ҏ功,什么,如果试Q打式信息啊?/p>

1.4 DisableThreadLibraryCalls
看帮助就知道它是q么用的Q?br>The DisableThreadLibraryCalls function disables the DLL_THREAD_ATTACH and DLL_THREAD_DETACH notifications for the dynamic-link library (DLL) specified by hLibModule. This can reduce the size of the working code set for some applications.

原文地址Q?a >http://www.blogjava.net/Yipak/articles/182025.html

DllMain的用:
DllMain函数是DLL模块的默认入口点。当Windows加蝲DLL模块时调用这一函数。系l首先调用全局对象的构造函敎ͼ然后调用全局函数 DLLMain。DLLMain函数不仅在将DLL链接加蝲到进E时被调用,在DLL模块与进E分LQ以及其它时候)也被调用。下面是一个框?DLLMain函数的例子?br>
如果我们在DllMain中写入下面的代码Q在原来的gandll.c中添加下面的代码Q:
BOOL APIENTRY DllMain(HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
{
    printf("hModule.%p lpReserved.%p \n", hModule, lpReserved);

    switch (ul_reason_for_call)
    {
        case DLL_PROCESS_ATTACH:
            printf("Process attach. \n");
            break;

        case DLL_PROCESS_DETACH:
            printf("Process detach. \n");
            break;

        case DLL_THREAD_ATTACH:
            printf("Thread attach. \n");
            break;

        case DLL_THREAD_DETACH:
            printf("Thread detach. \n");
            break;
    }

    return (TRUE);
}

同时dlltest\dlltest.c修改为:
#include <stdio.h>
#include "dlltest.h"

int main(int argc, char **argv)
{
    printf("Simple DLL test start. \n");

    printf("Call DLL function: \n");
    printf("Test DLL values: %d \n", add2(1, 2));
    printf("Call DLL function end. \n");

    printf("Simple DLL test end. \n");

    return (0);
}

我简单的试一下输出结果ؓQ?br>C:\gandll\dlltest>dlltest
hModule.10000000 lpReserved.0012FD30
Process attach.
Simple DLL test start.
Call DLL function:
Test DLL values: 3
Call DLL function end.
Simple DLL test end.
hModule.10000000 lpReserved.00000001
Process detach.
也就是说DLL加蝲和应用程序退出的使用都会调用该函敎ͼDllMainQ的哦, 是应用程序一上来p用的Q不是用到该函数时才调用的!

好象有个问题Q?br>下面的话来源Q?a target=_blank>http://waiguai.blogdriver.com/waiguai/989918.html
采用隐式链接方式Q程序员在徏立一个DLL文gӞ链接E序会自动生成一个与之对应的LIB导入文g。该文g包含了每一个DLL导出函数的符号名和可选的标识P但是q不含有实际的代码。LIB文g作ؓDLL的替代文件被~译到应用程序项目中。当E序员通过静态链接方式编译生成应用程序时Q应用程序中的调用函CLIB文g中导出符L匚wQ这些符h标识可入到生成的EXE文g中。LIB文g中也包含了对应的DLL文g名(但不是完全的路径名)Q链接程序将其存储在EXE文g内部。当应用E序q行q程中需要加载DLL文gӞWindowsҎq些信息发现q加载DLLQ然后通过W号名或标识号实现对 DLL函数的动态链接?br>
我们看他说的“当应用程序运行过E中需要加载DLL文g?#8221;Q?我做的实验测试的是,在输?br>Simple DLL test start.
Call DLL function:
q两行应该是不需要DLL的啊Q?怎么应用E序在前面输ZQ?br>hModule.10000000 lpReserved.0012FD30
Process attach.
q个呢? q就说明其实应用E序一上来p用了DLL的(q有一U可能就是他是正的Q由于编译器优化的原因的该DLL一上来p调用了)?br>到地是ؓ什么? 再查Q再查。。。。。?


例子Q?br>创徏一个简单的DLL工程?br>MyDllMain.
修改MyDllMain.cpp
#include "stdafx.h"
#include <stdio.h>
BOOL APIENTRY DllMain( HANDLE hModule,
                       DWORD  ul_reason_for_call,
                       LPVOID lpReserved
      )
{
 printf("hModule.%p lpReserved.%p \n", hModule, lpReserved);
  switch (ul_reason_for_call)
    {
        case DLL_PROCESS_ATTACH:
            printf("Process attach. \n");
            break;
        case DLL_PROCESS_DETACH:
            printf("Process detach. \n");
            break;
        case DLL_THREAD_ATTACH:
            printf("Thread attach. \n");
            break;
        case DLL_THREAD_DETACH:
            printf("Thread detach. \n");
            break;
    }
    return TRUE;
}

创徏一?CPP文g
#include "StdAfx.h"
extern "C" int _declspec(dllexport) add(int a, int b)
{
 return a + b;
}

新徏一个测试工E:
TestMainDll
TestMainDll.cpp

#include "stdafx.h"
#include <iostream>
#include <windows.h>
using namespace std;
typedef int (*Fun)(int, int);
HINSTANCE hInstance;
Fun fun;
int main(int argc, char* argv[])
{
 hInstance = LoadLibrary("MyDllMain.dll");
    if (!hInstance)
  cout<<"Not Find this Dll"<<endl;
    fun = (Fun)GetProcAddress(hInstance, "add");
    cout<<fun(12,3)<<endl;;
    FreeLibrary(hInstance);
 
 printf("Hello World!\n");
 return 0;
}


漂漂 2010-03-15 09:24 发表评论
]]>
DLL ?.def文g的?/title><link>http://www.shnenglu.com/AutomateProgram/archive/2010/03/13/109614.html</link><dc:creator>漂漂</dc:creator><author>漂漂</author><pubDate>Sat, 13 Mar 2010 08:45:00 GMT</pubDate><guid>http://www.shnenglu.com/AutomateProgram/archive/2010/03/13/109614.html</guid><wfw:comment>http://www.shnenglu.com/AutomateProgram/comments/109614.html</wfw:comment><comments>http://www.shnenglu.com/AutomateProgram/archive/2010/03/13/109614.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.shnenglu.com/AutomateProgram/comments/commentRss/109614.html</wfw:commentRss><trackback:ping>http://www.shnenglu.com/AutomateProgram/services/trackbacks/109614.html</trackback:ping><description><![CDATA[原文地址Q?a href="http://www.shnenglu.com/amyvmiwei/archive/2008/01/02/40203.html">http://www.shnenglu.com/amyvmiwei/archive/2008/01/02/40203.html</a><br><br>DLL中导出函数的声明有两U方式:一Uؓ在函数声明中加上__declspec(dllexport)Q这里不再D例说明;另外一U方式是采用模块定义(.def) 文g声明Q?def文g为链接器提供了有兌链接E序的导出、属性及其他斚w的信息?br><br><br>            首先创徏 一个DLLE序Q?cpp?br>int __stdcall Add(int numa, int numb)<br>{<br>       return (numa + numb);<br>} <p>int __stdcall Sub(int numa, int numb)<br>{<br>        return (numa - numb);<br>}<br><br>然后创徏一?def的文Ӟ在里面加?br></p> <p class=code>;DllTestDef.lib : 导出DLL函数<br>;作者:----<br>LIBRARY defDll.dll<br>EXPORTS <br>Add @ 1<br>Sub @ 2<br><br>           最后创Z个测试程序:.cpp文g如下Q?br>#include <iostream><br>#include <windows.h><br>using namespace std;</p> <p class=code>typedef int (__stdcall *Fun)(int , int);<br>HINSTANCE hInstance;<br>Fun fun;</p> <p class=code>int main()<br>{<br> hInstance = LoadLibrary("defDll.dll");</p> <p class=code> if (!hInstance)<br>  cout<<"Not Find this Dll"<<endl;<br> fun = (Fun)GetProcAddress(hInstance, MAKEINTRESOURCE(1));<br> cout<<fun(12,3)<<endl;;<br> fun = (Fun)GetProcAddress(hInstance, "Sub");<br> cout<<fun(7, 3)<<endl;<br> FreeLibrary(hInstance);<br>}</p> <p class=code></p> <br><br>说明Q?br>.def文g的规则ؓQ?br><br>  (1)LIBRARY语句说明.def文g相应的DLLQ?br><br>  (2)EXPORTS语句后列导出函数的名U。可以在.def文g中的导出函数名后加@nQ表C导出函数的序号ؓnQ在q行函数调用Ӟq个序号发挥其作用Q;<br><br>  (3).def 文g中的注释由每个注释行开始处的分?(;) 指定Q且注释不能与语句共享一行?br> <img src ="http://www.shnenglu.com/AutomateProgram/aggbug/109614.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.shnenglu.com/AutomateProgram/" target="_blank">漂漂</a> 2010-03-13 16:45 <a href="http://www.shnenglu.com/AutomateProgram/archive/2010/03/13/109614.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>CBitmap, HBITMAP和LoadImagehttp://www.shnenglu.com/AutomateProgram/archive/2010/01/24/106337.html漂漂漂漂Sun, 24 Jan 2010 07:40:00 GMThttp://www.shnenglu.com/AutomateProgram/archive/2010/01/24/106337.htmlhttp://www.shnenglu.com/AutomateProgram/comments/106337.htmlhttp://www.shnenglu.com/AutomateProgram/archive/2010/01/24/106337.html#Feedback0http://www.shnenglu.com/AutomateProgram/comments/commentRss/106337.htmlhttp://www.shnenglu.com/AutomateProgram/services/trackbacks/106337.htmlhttp://blog.csdn.net/newstarao/archive/2009/05/22/4209656.aspx

hbitmap是bitmap的指针,

msdn中:

Handle to a bitmap.typedef HANDLE HBITMAP;

cbitmap是mfc中封装bitmap的类Q?/p>

msdn中:

EncapsulatesQ囊括) a Windows graphics device interface (GDI) bitmap and provides member functions to manipulateQ操作) the bitmap.

 
class CBitmap : public CGdiObject转化Ҏ两种Q?br>Attach和FromHandleQ?br>CBitmap bmp;
bmp.LoadBitmap(IDB_BITMAP1);
HBITMAP hbm = (HBITMAP)bmp;
CBitmap bmp1;
bmp1.Attach(hbm);
HBITMAP = CBitmap.m_hObject;
CBitmap* = CBitmap.FromHandle(HBITMAP)//已知
HBITMAP hbit;
CBitmap cb;
cb.FromHandle(hbit);//已知
CBitmap cb;
HBITMAP hbit=(HBITMAP)cb;
CBitmap bmp;HBITMAP hBmp;
怺转换QhBmp=(HBITMAP)bmp.GetSafeHandle();bmp.Attach(hBmp);
注意点:Attach和FromHandle的区别FromHandle得到的指针是临时变量Q?通过Attachq接的句柄可以长久保?但通过FromHandle得到的只是暂时的,大概只在一个消息区间内有效,很快便会被删?所以基本上不能用。我用了FromHandle然后一直出错!Q!

CBitmap 无法从文件中加蝲位图Q可以用下面的方法解冟?/p>  HBITMAP hBitmap;
 //hBitmap = (HBITMAP)::LoadImage(::AfxGetInstanceHandle(), "NetFriend.bmp", IMAGE_BITMAP, 0,0,LR_LOADFROMFILE|LR_CREATEDIBSECTION);//从文件中加蝲Q?br> hBitmap = (HBITMAP)::LoadImage(::AfxGetInstanceHandle(), MAKEINTRESOURCE(IDB_BITMAP1), IMAGE_BITMAP, 0,0,LR_CREATEDIBSECTION);//从资源文件中加蝲

漂漂 2010-01-24 15:40 发表评论
]]>
VC下显CZ囄几种ҎQ{Q?/title><link>http://www.shnenglu.com/AutomateProgram/archive/2010/01/24/106336.html</link><dc:creator>漂漂</dc:creator><author>漂漂</author><pubDate>Sun, 24 Jan 2010 07:36:00 GMT</pubDate><guid>http://www.shnenglu.com/AutomateProgram/archive/2010/01/24/106336.html</guid><wfw:comment>http://www.shnenglu.com/AutomateProgram/comments/106336.html</wfw:comment><comments>http://www.shnenglu.com/AutomateProgram/archive/2010/01/24/106336.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.shnenglu.com/AutomateProgram/comments/commentRss/106336.html</wfw:commentRss><trackback:ping>http://www.shnenglu.com/AutomateProgram/services/trackbacks/106336.html</trackback:ping><description><![CDATA[VC下显CZ囄几种Ҏ<br>ȝ一下关于位囄几种用法?br>说到q还是开头给大家介绍一下,有关位图的知识吧Q以及对于我们比较有用的几个关于位图的结构体?br>一.位图l构如下Q? <div> </div> <div>---- 一、BMP文gl构 <br>---- 1. BMP文gl成 <br>---- BMP文g由文件头、位图信息头、颜色信息和囑Ş数据四部分组成?nbsp;<br>---- 2. BMP文g?nbsp;<br>---- BMP文g头数据结构含有BMP文g的类型、文件大和位图起始位置{信息?nbsp;<br>---- 其结构定义如?  <br>typedef struct tagBITMAPFILEHEADER<br>{<br>WORDbfType;   // 位图文g的类型,必须为BM<br>DWORD   bfSize;   // 位图文g的大,以字节ؓ单位<br>WORDbfReserved1;  // 位图文g保留字,必须?<br>WORDbfReserved2;  // 位图文g保留字,必须?<br>DWORD   bfOffBits; // 位图数据的v始位|,以相对于位图<br>// 文g头的偏移量表C,以字节ؓ单位<br>} BITMAPFILEHEADER;<br>---- 3. 位图信息?nbsp; <br>BMP位图信息头数据用于说明位囄寸{信息?br>typedef struct tagBITMAPINFOHEADER{<br>   DWORD  biSize;   // 本结构所占用字节?br>   LONGbiWidth;  // 位图的宽度,以像素ؓ单位<br>   LONGbiHeight; // 位图的高度,以像素ؓ单位<br>   WORD   biPlanes; // 目标讑֤的别,必须?<br>   WORD   biBitCount// 每个像素所需的位敎ͼ必须?(双色),<br>  // 4(16?Q?(256??4(真彩?之一<br>   DWORD  biCompression;   // 位图压羃cdQ必L 0(不压~?,<br>  // 1(BI_RLE8压羃cd)?(BI_RLE4压羃cd)之一<br>   DWORD  biSizeImage; // 位图的大,以字节ؓ单位<br>   LONGbiXPelsPerMeter; // 位图水^分L率,每米像素?br>   LONGbiYPelsPerMeter;  // 位图垂直分L率,每米像素?br>   DWORD  biClrUsed;// 位图实际使用的颜色表中的颜色?br>   DWORD  biClrImportant;// 位图昄q程中重要的颜色?br>} BITMAPINFOHEADER; <br>---- 4. 颜色?nbsp; <br>    颜色表用于说明位图中的颜Ԍ它有若干个表,每一个表Ҏ一个RGBQUADcd的结构,定义一U颜艌ӀRGBQUADl构的定义如? <br>typedef struct tagRGBQUAD {</div> <div>BYTErgbBlue;// 蓝色的亮?D围ؓ0-255)<br>BYTErgbGreen;   // l色的亮?D围ؓ0-255)<br>BYTErgbRed; // U色的亮?D围ؓ0-255)<br>BYTErgbReserved;// 保留Q必Mؓ0<br>} RGBQUAD;<br>颜色表中RGBQUADl构数据的个数有biBitCount来确?<br>当biBitCount=1,4,8Ӟ分别?,16,256个表?<br>当biBitCount=24Ӟ没有颜色表项?br>   位图信息头和颜色表组成位图信息,BITMAPINFOl构定义如下:<br>typedef struct tagBITMAPINFO {<br>   BITMAPINFOHEADER bmiHeader;   // 位图信息?br>   RGBQUAD  bmiColors[1];  // 颜色?br>} BITMAPINFO;<br>下面我们介绍W一U办法:<br>//W一U方法,使用WM__CTRLCOLOR来实?br>/*******************OnCtlColor中添加的代码*****************/<br>HBRUSH CDemoBitmapDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) <br>{<br> HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);<br> <br> // TODO: Change any attributes of the DC here<br> //W一U实现方?br> if(nCtlColor=CTLCOLOR_DLG)<br> {<br>  return brush;<br> }<br> // TODO: Return a different brush if the default is not desired<br> return hbr;<br>}</div> <div>BOOL CDemoBitmapDlg::OnInitDialog()<br>{<br> //W一U方法,使用wm_ctlcolor来实?br> CBitmap bitmap;<br> bitmap.LoadBitmap(IDB_BITMAP1);<br> brush.CreatePatternBrush(&bitmap);//brush定义在头文g中CBrush burush<br> bitmap.DeleteObject();<br>}<br>W二U方法用资源文件中导入的IDB_BITMAP来实?br>W一步先在资源中导入一个位?IDB_BITMAP1<br>void CDemoBitmapDlg::OnBtnBitmap() <br>{<br> // TODO: Add your control notification handler code here<br> /***************昄BITMAP的第二种Ҏ**********************/<br> HBITMAP hBitmap;<br> CBitmap bitmap;<br> CClientDC *pDC;<br> pDC=new CClientDC(this);<br> hBitmap=(HBITMAP)::LoadImage(::AfxGetInstanceHandle(),"NetFriend.bmp",IMAGE_BITMAP,0,0,LR_LOADFROMFILE|LR_CREATEDIBSECTION);<br> bitmap.Attach(hBitmap);<br> CDC CDCompatible;<br> CDCompatible.CreateCompatibleDC(pDC);//创徏兼容DC<br> CDCompatible.SelectObject(&bitmap);<br> /*********其它几种昄方式********/<br>/* pDC->BitBlt(0,0,200,200,&CDCompatible,0,0,SRCCOPY);<br> CDCompatible.SetBkMode(TRANSPARENT);<br> pDC->StretchBlt(0,0,352,288,&CDCompatible,0,0,352,288,SRCCOPY);<br>    CDCompatible.CreateCompatibleDC(this->GetDC());<br>    CDCompatible.SelectObject(&bitmap); */ //选择l图对象<br> BITMAP bmp;<br> bitmap.GetBitmap(&bmp);<br>    pDC->StretchBlt(0,0,bmp.bmWidth,bmp.bmHeight,&CDCompatible,0,0,bmp.bmWidth,bmp.bmHeight,SRCCOPY); <br>    CDCompatible.DeleteDC();  <br>    ::DeleteObject(&bitmap); <br>}<br>W三U方法是通过d指定目录中的位图文g来显C?q里分别把它们显C在Picture<a ><font color=#0000ff><u>控g</u></font></a>和Static上面q也是大安常要用到的)Q?br>void CDemoBitmapDlg::OnBtnPic() <br>{<br> // TODO: Add your control notification handler code here<br> /****************向PICTURE<a ><font color=#0000ff><u>控g</u></font></a>中动态添加位?***************************/<br> HBITMAP hBitmap;<br> CBitmap bitmap;<br> hBitmap=(HBITMAP)::LoadImage(::AfxGetInstanceHandle(),"NetFriend.bmp",IMAGE_BITMAP,0,0,LR_LOADFROMFILE|LR_CREATEDIBSECTION);<br> bitmap.Attach(hBitmap);<br> m_Pic.SetBitmap(hBitmap);</div> <div>}<br>/****************在STATIC中显CZ?********************/<br>void CDemoBitmapDlg::OnBtnStatic() <br>{<br> // TODO: Add your control notification handler code here<br> CBitmap hbmp;<br> HBITMAP hbitmap;<br> //pStatic指向要显C的地方<br> CStatic *pStaic;<br> pStaic=(CStatic*)GetDlgItem(IDC_IMAGE);<br>// pStaic=(CStatic*)GetDlgItem(IDC_BUTTON1);<br> //装蝲资源 MM.bmp是我的一个文件名Q用你的替换<br> hbitmap=(HBITMAP)::LoadImage (::AfxGetInstanceHandle(),"NetFriend.bmp",<br>  IMAGE_BITMAP,0,0,LR_LOADFROMFILE|LR_CREATEDIBSECTION); <br> <br> hbmp.Attach(hbitmap);<br> //获取囄格式<br> BITMAP bm;<br> hbmp.GetBitmap(&bm);<br> CDC dcMem;<br> dcMem.CreateCompatibleDC(GetDC());<br> CBitmap *poldBitmap=(CBitmap*)dcMem.SelectObject(hbmp);<br> CRect lRect;<br> pStaic->GetClientRect(&lRect);<br> //昄位图<br> pStaic->GetDC()->StretchBlt(lRect.left ,lRect.top ,lRect.Width(),lRect.Height(),  &dcMem,0 ,0,bm.bmWidth,bm.bmHeight,SRCCOPY);<br> dcMem.SelectObject(&poldBitmap);<br>}<br>/*****************向BUTTON中添加ICON*****************************/<br>//Ҏ和导入其它位囄|在这里就只做一个说明一下就是了?nbsp;<br>HICON hIco;<br> hIco=AfxGetApp()->LoadIcon(IDI_ICON1);<br> m_BtnBitmap.SetIcon(hIco);<br>W四U读取位图文件来昄位图:<br>void CDemoBitmapDlg::OnBtnBtmfile() <br>{<br> // TODO: Add your control notification handler code here<br> /*******************通过d位图文g来显CZ?*********************/<br> CString strFileName;<br> strFileName.Format("%s","NetFriend.bmp");<br> //成位图信息BITMAPINFO<br> BITMAPINFO *bitmapinfo=NULL;<br> BYTE *BmpData=NULL;<br> CFile file;//d位图信息<br> //讄文g的打开方式<br> if(!file.Open(strFileName,C<em>file:</em>:modeRead|C<em>file:</em>:typeBinary))<br> {<br>  return;<br> }<br> //BITMAP文g头结构体<br> BITMAPFILEHEADER BitmapHead;<br> //d位图文g?br> if(file.Read(&BitmapHead,sizeof(BITMAPFILEHEADER))!=sizeof(BITMAPFILEHEADER))<br> {<br>  MessageBox("d文g头失?");<br>  return;<br> }<br> if(BitmapHead.bfType!=0x4d42)<br> {<br>  MessageBox("对不?您读取的不是位图文g!");<br>  return;<br> }<br> //位图信息<br> BITMAPINFOHEADER  BitmapInfo;<br> if(file.Read(&BitmapInfo,sizeof(BITMAPINFOHEADER))!=sizeof(BITMAPINFOHEADER))<br> {<br>  MessageBox("d位图信息p|!");<br>  return;<br> }<br> if(BitmapInfo.biBitCount!=24)<br> {<br>  MessageBox("对不?当前<a ><font color=#0000ff><u>E序</u></font></a>只支?4位位图信?");<br>  return;<br> }<br> bitmapinfo=(BITMAPINFO*)new char[sizeof(BITMAPINFOHEADER)];<br> if(!bitmapinfo)<br> {<br>  MessageBox("内存分配p|!");<br>  return;<br> }<br> /*把BMP位图信息头中的数据读取到位图信息l构中去.*/<br> memcpy(bitmapinfo,&BitmapInfo,sizeof(BITMAPINFOHEADER));<br> /*用来得到位图文g的大?/<br> DWORD dataByte=BitmapHead.bfSize-BitmapHead.bfOffBits;<br> BmpData=(BYTE*)new char[dataByte];<br> if(!BmpData)<br> {<br>  MessageBox("内存分配p|!");<br>  delete bitmapinfo;<br>  delete BmpData;<br>  return;<br> }<br> if(file.Read(BmpData,dataByte)!=dataByte)<br> {<br>  MessageBox("d位图数据p|!");<br>  return;<br> }<br> file.Close();<br> CClientDC *pDC=new CClientDC(this);<br> pDC->SetStretchBltMode(COLORONCOLOR);<br> StretchDIBits(pDC->GetSafeHdc(),200,0,BitmapInfo.biWidth,BitmapInfo.biHeight,  0,0,BitmapInfo.biWidth,BitmapInfo.biHeight,BmpData,bitmapinfo,DIB_RGB_COLORS,SRCCOPY);<br>}<br>最后给大家介绍两个昄位图的特效的例子更多的特效显C?/div> <div>1> 水^交错昄位图<br>void CListBmap::OnButton1() <br>{<br> // TODO: Add your control notification handler code here<br> HBITMAP hBitmap;<br> CBitmap cBitmap;<br> hBitmap=(HBITMAP)::LoadImage(::AfxGetInstanceHandle(),"NetFriend.bmp",  IMAGE_BITMAP,0,0,LR_LOADFROMFILE|LR_CREATEDIBSECTION);<br> cBitmap.Attach(hBitmap);<br> cBitmap.GetBitmap(&bitmap);<br> int i=0,j=0;<br> CClientDC *pDC=new CClientDC(this);<br> CDC memDC;<br> memDC.CreateCompatibleDC(GetDC());<br> memDC.SelectObject(&cBitmap);<br> for(i=0;i<bitmap.bmHeight;i+=2)<br> {<br>  j=i;<br>  while(j>0)<br>  {<br>   pDC->StretchBlt(0,j-1,bitmap.bmWidth,1,&memDC,0,bitmap.bmHeight-(i-j+1),    bitmap.bmWidth,1,SRCCOPY);<br>   pDC->StretchBlt(0,bitmap.bmHeight-j,bitmap.bmWidth,1,&memDC,0,i-j,    bitmap.bmWidth,1,SRCCOPY);<br>   j-=2;<br>  }<br>  Sleep(50);<br> }</div> <div>}</div> <div>2>雨点昄Q?br>void CListBmap::OnButton2() <br>{<br> // TODO: Add your control notification handler code here</div> <div> HBITMAP hBitmap;<br> CBitmap cBitmap;<br> hBitmap=(HBITMAP)::LoadImage(::AfxGetInstanceHandle(),"NetFriend.bmp",  IMAGE_BITMAP,0,0,LR_LOADFROMFILE|LR_CREATEDIBSECTION);<br> cBitmap.Attach(hBitmap);<br> cBitmap.GetBitmap(&bitmap);<br> int i=0,j=0;<br> CClientDC *pDC=new CClientDC(this);<br> CDC memDC;<br> memDC.CreateCompatibleDC(GetDC());<br> memDC.SelectObject(&cBitmap);<br> for ( i=0; i<=bitmap.bmHeight; i++ )   <br> {<br>  for ( j=0; j<=bitmap.bmHeight-i; j++ )  <br>   pDC->StretchBlt(  <br>   0,j,  <br>   //目标讑֤逻辑横、纵坐标  <br>   bitmap.bmWidth,1,  <br>   //昄位图的像素宽、高?nbsp;<br>   &memDC,  <br>   //源位图设备情境对?nbsp;<br>   0,bitmap.bmHeight-i,  <br>   //源位囄起始横、纵坐标  <br>   bitmap.bmWidth,1,  <br>   //源位囄像素宽、高?nbsp; <br>   SRCCOPY);  <br>  Sleep(20);  <br> }<br>}<br></div> <img src ="http://www.shnenglu.com/AutomateProgram/aggbug/106336.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.shnenglu.com/AutomateProgram/" target="_blank">漂漂</a> 2010-01-24 15:36 <a href="http://www.shnenglu.com/AutomateProgram/archive/2010/01/24/106336.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>动态的创徏CStaticq显CZ图?/title><link>http://www.shnenglu.com/AutomateProgram/archive/2010/01/24/106335.html</link><dc:creator>漂漂</dc:creator><author>漂漂</author><pubDate>Sun, 24 Jan 2010 07:34:00 GMT</pubDate><guid>http://www.shnenglu.com/AutomateProgram/archive/2010/01/24/106335.html</guid><wfw:comment>http://www.shnenglu.com/AutomateProgram/comments/106335.html</wfw:comment><comments>http://www.shnenglu.com/AutomateProgram/archive/2010/01/24/106335.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.shnenglu.com/AutomateProgram/comments/commentRss/106335.html</wfw:commentRss><trackback:ping>http://www.shnenglu.com/AutomateProgram/services/trackbacks/106335.html</trackback:ping><description><![CDATA[ 注意风格的设定否则无法显CZ图?br><br>if(m_createPic.m_hWnd == NULL)<br>  m_createPic.Create("", WS_CHILD | <span style="COLOR: red">WS_VISIBLE</span> | WS_TABSTOP | WS_CLIPSIBLINGS | SS_NOTIFY | <span style="COLOR: red">SS_BITMAP</span> , CRect(0,0,150,220),this);<br> CBitmap bmp;<br> bmp.LoadBitmap(IDB_BITMAP1);<br> m_createPic.SetBitmap((HBITMAP)bmp.GetSafeHandle()); <img src ="http://www.shnenglu.com/AutomateProgram/aggbug/106335.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.shnenglu.com/AutomateProgram/" target="_blank">漂漂</a> 2010-01-24 15:34 <a href="http://www.shnenglu.com/AutomateProgram/archive/2010/01/24/106335.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>PreTranslateMessage作用和用方??http://www.shnenglu.com/AutomateProgram/archive/2010/01/18/105917.html漂漂漂漂Mon, 18 Jan 2010 01:22:00 GMThttp://www.shnenglu.com/AutomateProgram/archive/2010/01/18/105917.htmlhttp://www.shnenglu.com/AutomateProgram/comments/105917.htmlhttp://www.shnenglu.com/AutomateProgram/archive/2010/01/18/105917.html#Feedback0http://www.shnenglu.com/AutomateProgram/comments/commentRss/105917.htmlhttp://www.shnenglu.com/AutomateProgram/services/trackbacks/105917.html原文地址Q?a >http://blog.csdn.net/wzyzb/archive/2009/03/05/3959564.aspx
PreTranslateMessage是消息在送给TranslateMessage函数之前被调用的Q绝大多数本H口的消息都要通过q里Q比较常用,当需要在MFC之前处理某些消息Ӟ常常要在q里d代码. 
      

       MFC 消息控制最L色的地方是CWndcȝ虚拟函数PreTranslateMessage()Q通过重蝲q个函数Q可以改变MFC的消息控制流E,甚至可以作一个全新的控制出来。只有穿q消息队列的消息才受PreTranslateMessage()影响Q采用SendMessage()或其他类似的方式向窗口直接发送的而不l过消息队列的消息根本不会理睬PreTranslateMessage()的存在?nbsp;

       是否调用TranslateMessage()和DispatchMessage()是由一个名UCؓPreTranslateMessage()函数的返回值决定的Q如果该函数q回TRUEQ则不会把该消息分发l窗口函数处理?

传给PreTranslateMessage()的消息是未经译q的消息Q它没有l过TranslateMessage()处理。可以在该函C使用(pMsg->wParam==VK_RETURN)来拦截回车键。wParam中存攄是键盘上字符的虚拟码?

PeekMessage和GetMessage的区别:

GetMessage在没有消息的时候等待消息,cpu当然?

PeekMessage没有消息的时候立刻返回,所以cpu占用率高?

因ؓ游戏不能靠windows消息驱动Q所以要用PeekMessage();

     PretranslateMessage 的实玎ͼ不得不谈到MFC消息循环的实现。MFC通过CWinAppcM的Pumpmessage函数实现消息循环Q但是实际的消息循环代码位于 CWinThread中,CWinApp只是从CWinThreadl承q来。其化后的代码大概如下:
  BOOL CWinThread::PumpMessage()
  {
  _AFX_THREAD_STATE *pState = AfxGetThreadState();
  
  ::GetMessage(&(pState->m_msgCur), NULL, NULL, NULL))
  
  if (!AfxPreTranslateMessage(&(pState->m_msgCur)))
  {
  ::TranslateMessage(&(pState->m_msgCur));
  ::DispatchMessage(&(pState->m_msgCur));
  }
  return TRUE;
  }
  可以看到QPumpMessage在实际的TranslateMessage和DispatchMessage发生之前会调?AfxPreTranslateMessageQAfxPreTranslateMessage又会调用 CWnd::WalkPreTranslateTree(虽然也会调用其他函数Q但是这个最为关键)Q其代码如下Q?
  BOOL PASCAL CWnd::WalkPreTranslateTree(HWND hWndStop, MSG* pMsg)
  {
  ASSERT(hWndStop == NULL || ::IsWindow(hWndStop));
  ASSERT(pMsg != NULL);
  
  // walk from the target window up to the hWndStop window checking
  // if any window wants to translate this message
  
  for (HWND hWnd = pMsg->hwnd; hWnd != NULL; hWnd = ::GetParent(hWnd))
  {
  CWnd* pWnd = CWnd::FromHandlePermanent(hWnd);
  if (pWnd != NULL)
  {
  // target window is a C window
  if (pWnd->PreTranslateMessage(pMsg))
  return TRUE; // trapped by target window (eg: accelerators)
  }
  
  // got to hWndStop window without interest
  if (hWnd == hWndStop)
  break;
  }
  return FALSE; // no special processing
  }
  
  可以看到Q代码还是很直接的。从接受到消息的H口层层往上遍历,q调用PretranslateMessage看是否返回TRUEQ是则结束,否则l箋?
  q里有一个地斚w常关键:CWnd *pWnd = CWnd::FromHandlePermanent(hWnd) q一句代码从当前AfxModuleThreadState拿到Permanent句柄表,从而找到hWnd对应的CWnd


MFC 中PreTranslateMessage是GetMessage(...)函数的下一U操作,即GetMessage(...)从消息队列中获取消息后,交由PreTranslateMessageQ)处理Q若其返回FALSE则再交给TranslateMessage?DispatchMessage处理Q进入WindowProcQ;  
如果用SendMessage,   则消息直接交到WindowProc处理Q所以GetMessage不会取得SendMessage的消息,当然PreTranslateMessage也就不会被调用?nbsp;  [Page]
如果用PostMessage,则消息进入消息队列,由GetMessage取得QPreTranslateMessage有Zq行处理?

例子Q?br>按Enter,ESC按是不会退Z

BOOL CComboBoxExDlg::PreTranslateMessage(MSG* pMsg)
{
if(WM_KEYDOWN == pMsg->message )
{
UINT nKey = (int) pMsg->wParam; 
if( VK_RETURN == nKey || VK_ESCAPE == nKey )
return TRUE ;
}

return CDialog::PreTranslateMessage(pMsg);
}

~辑框,如何响应的这个回车的信息
如果你的~辑框定义如? 
  CEdidt   m_cName;       //ID号ؓIDC_E_NAME  
   
  BOOL   CSecondDlg::PreTranslateMessage(MSG*   pMsg)    
  {  
  //   TODO:   Add   your   specialized   code   here   and/or   call   class  
   
                if   (pMsg->message==WM_KEYDOWN)  
  {  
          //CWnd   *p=GetDlgItem(IDC_E_NAME);  
  CWnd   *   hWnd=GetFocus();  
  //if(pMsg->wParam==13   &&   pMsg->hwnd==m_cName)  
  if(pMsg->wParam==13   &&   hWnd==&m_cName)  
  {  
  处理函数  
  return   TRUE;  
  }  
  else  
  {  
                                                          处理函数  
  }  
  }  
  return   CDialog::PreTranslateMessage(pMsg);  
  }



漂漂 2010-01-18 09:22 发表评论
]]>
þۺɫ99žak| þ99ۺϾƷҳ| þþþƷձһ| ŷҹƷþþþ| ȾþùþƷ| wwwþ| ˾þþƷ鶹һ| 99þۺϺݺۺϾþֹ| ɫݺݾþAVۺ| ɫۺϾþ| þþƷһAV| ھƷ˾þþþø| ޹Ʒ۲ӰԺþ| ҹƵþþþһ| ˾þۺӰԺ| þֻǾƷ66| ƷۺϾþþþþ98| þù¶ƷӰ| vaþþþ | Ʒѿþþ| þۺϸϾþúݺݺ97ɫ | þþþӰԺŮ| ۲ӰԺþþƷ| þ޹ҹƷƬ| ˾þ777777| ޾Ʒһþ| ҹƷþþþþӰriav| ԸߺþþþþþþAAAAA| þAV뾫Ʒɫҹ鶹| ƷŮ߳׾þþ| þþƷþý | þۺ| ƷŮþþþ| ƷžžþƵ | þù| þݺҹҹ| ˾ƷۺϾþþþ | ĻӰӾþþ| ˾þô߽AV| ĻƷѾþ5151 | þþþø߳ۺӰԺ|