2021SC@SDUSC
今天继续分析代码的后续部分,具体代码如下所示:
if (!wcscmp(L"/userdir", lpstrCmdLine))
{
CreateDirectory(WeaselUserDataPath().c_str(), NULL);
WeaselServerApp::explore(WeaselUserDataPath());
return 0;
}
if (!wcscmp(L"/weaseldir", lpstrCmdLine))
{
WeaselServerApp::explore(WeaselServerApp::install_dir());
return 0;
}
// command line option /q stops the running server
bool quit = !wcscmp(L"/q", lpstrCmdLine) || !wcscmp(L"/quit", lpstrCmdLine);
// restart if already running
{
weasel::Client client;
if (client.Connect()) // try to connect to running server
{
client.ShutdownServer();
}
if (quit)
return 0;
}
bool check_updates = !wcscmp(L"/update", lpstrCmdLine);
if (check_updates)
{
WeaselServerApp::check_update();
}
CreateDirectory(WeaselUserDataPath().c_str(), NULL);
int nRet = 0;
try
{
WeaselServerApp app;
if (IsWindowsVistaOrGreater())
{
PRAR RegisterApplicationRestart = (PRAR)::GetProcAddress(::GetModuleHandle(_T("kernel32.dll")), "RegisterApplicationRestart");
RegisterApplicationRestart(NULL, 0);
}
nRet = app.Run();
}
catch (...)
{
// bad luck...
nRet = -1;
}
_Module.Term();
::CoUninitialize();
return nRet;
}
1.DefWindowProc
1)简介
函数功能:该调用DefWindowProc函数时使用窗口过程接收的相同参数。
函数原型:LRESULT DefWindowProc(HWND hWnd,UINT Msg,WPARAM wParam,LPARAM IParam)
2)功能
DefWindowProc这个函数是默认的窗口处理函数,我们可以把不关心的消息都丢给它来处理。这个函数在处理关闭窗口消息WM_CLOSE时,是调用DestroyWindow函数关闭窗口并且发WM_DESTROY消息给应用程序;而它对WM_DESTROY这个消息是不处理的(考虑为什么?);我们在应用程序中对这个消息的处理是发出WM_QUIT消息。因此WM_CLOSE、WM_DESTROY、WM_QUIT这三个消息是先后产生的。
Defwindowproc是wind32的缺省消息处理函数。要知道win32程序是消息驱动的,即程序员接收消息(主要来自于用户的界面操作如按键、鼠标点击),并处理消息。如果程序员没处理某个消息,则系统会最后替你处理:就是调用Defwindowproc。(你也能调用它) MFC将win32底层包装了起来,消息处理在MFC中就是一个个On...函数。这个onlbuttondown就是处理用户点击鼠标左键所产生的消息。 以下是MSDN中DefWindowProc原型: LRESULT DefWindowProc( HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam ); MFC包装了底层,因此MFC版的DefWindowProc没有hWnd这个参数。 0xa1 -- 表示消息类型,在winuser.h中有定义, #define WM_NCLBUTTONDOWN 0xa1 表示鼠标左键点击。 2是什么呢?它是wParam参数,表示鼠标点到了窗口什么地方。同样在winuser.h中有定义:#define HTCAPTION 2 表示鼠标点到了窗口标题栏上(这样才允许托动) 类似 #define HTCLIENT 1 点到了窗口客户区
3)参数
hWnd:指向接收消息的窗口过程的句柄。
Msg:指定消息类型。
wParam:指定其余的、消息特定的信息。该参数的内容与Msg参数值有关。
IParam:指定其余的、消息特定的信息。该参数的内容与Msg参数值有关。
返回值:返回值就是消息处理结果,它与发送的消息有关。
备注:对于Windows CE;如果Msg为WM_SETTEXT那么返回0。
当DefWindowProc处理WM_DESTROY消息时,它不自动调用PostQuitMessage。
速查:Windows NT 3.1以上版本;Windows:95以上版本:Windows CE以上版本;头文件;winuser.h;库文件:user32.lib;Unicode:在Windows NT环境中以Unicode和ANSI版本实现。
=============================================================================================================================
DefWindowProc Function
--------------------------------------------------------------------------------
The DefWindowProc function calls the default window procedure to provide default processing for any window messages that an application does not process. This function ensures that every message is processed. DefWindowProc is called with the same parameters received by the window procedure.
Syntax
LRESULT DefWindowProc( HWND hWnd,
UINT Msg,
WPARAM wParam,
LPARAM lParam
);
Parameters
hWnd
[in] Handle to the window procedure that received the message.
Msg
[in] Specifies the message.
wParam
[in] Specifies additional message information. The content of this parameter depends on the value of the Msg parameter.
lParam
[in] Specifies additional message information. The content of this parameter depends on the value of the Msg parameter.
Return Value
The return value is the result of the message processing and depends on the message.
Remarks
Windows 95/98/Me: DefWindowProcW is supported by the Microsoft Layer for Unicode (MSLU). To use this, you must add certain files to your application, as outlined in Microsoft Layer for Unicode on Windows 95/98/Me Systems .
Example
For an example, see Designing a Window Procedure.
Function Information
Minimum DLL Version user32.dll
Header Declared in Winuser.h, include Windows.h
Import library User32.lib
Minimum operating systems Windows 95, Windows NT 3.1
Unicode Implemented as ANSI and Unicode versions
// this resolves ATL window thunking problem when Microsoft Layer for Unicode (MSLU) is used
::DefWindowProc(NULL, 0, 0, 0L);
Windows应用程序是消息驱动的,这里的DefWindowProc就是对消息的默认处理,用来实现窗口的形实默认转化。
2.AtlInitCommonControls
AtlInitCommonControls(),这个函数是对InitCommonControlsEx()的封装,因此具体的使用方法要参考InitCommonControlEx()的具体用法
1)作用
InitCommonControlsEx()函数主要用于通用控件初始化,InitCommonControlsex,函数名称,lpInitCtrls参数指向一个INITCOMMONCONTROLSEX结构。如果一个运行在 Windows XP 上的应用程序清单指定要使用 ComCtl32.dll 版本 6 或更高版本来启用可视化方式,则需要 InitCommonControlsEx()。否则,将无法创建窗口。
2)函数原型
BOOL WINAPI InitCommonControlsEx(LPINITCOMMONCONTROLSEX lpInitCtrls);
3)数据结构
lpInitCtrls参数指向一个INITCOMMONCONTROLSEX结构:
typedef struct tagINITCOMMONCONTROLSEX {
DWORD dwSize;
DWORD dwICC;
} INITCOMMONCONTROLSEX, *LPINITCOMMONCONTROLSEX;
4)参数含义
dwSize
数据类型:DWORD
本结构体的长度。
dwICC
数据类型:DWORD
需要初始化的类的标示符。
dwICC字段指定了需要注册的扩展通用控件类,与InitCommonControls注册所有它支持的通用控件类不同,InitCommonControlsEx函数只注册dwICC字段指明的扩展通用控件类,字段可以是下面取值的组合:
● ICC_BAR_CLASSES——注册工具栏、状态栏、Trackbar和Tooltip类。
● ICC_COOL_CLASSES——注册Rebar类。
● ICC_DATE_CLASSES——注册Date and Time Picker类。
● ICC_HOTKEY_CLASS——注册Hot Key类。
● ICC_INTERNET_CLASSES——注册IP Address Picker类。
● ICC_LISTVIEW_CLASSES——注册ListView和Header类。
● ICC_PAGESCROLLER_CLASS——注册Pager类。
● ICC_PROGRESS_CLASS——注册Progress Bar类。
● ICC_TAB_CLASSES——注册Tab和Tooltip类。
● ICC_TREEVIEW_CLASSES——注册TreeView和Tooltip类。
● ICC_UPDOWN_CLASS——注册Up-Down类。
● ICC_USEREX_CLASSES——注册ComboBoxEx类。
● ICC_WIN95_CLASSES——注册InitCommonControls函数注册的所有类。
InitCommonControlsEx函数是InitCommonControls函数的扩充
AtlInitCommonControls(ICC_BAR_CLASSES); // add flags to support other controls
因此,这段代码的具体含义是对通用组件进行初始化,方便后续对具体组件的应用