主窗口
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
#ifndef NASIABOUT_MAINWND_H #define NASIABOUT_MAINWND_H #include "define.h" #include <atlstr.h> class CMainPage : public CNotifyPump { public: CMainPage(); void SetPaintMagager(CPaintManagerUI* pPaintMgr); void set_resource_zip(LPCWSTR path); CPaintManagerUI* get_manager_ui(); DUI_DECLARE_MESSAGE_MAP() virtual void OnClick(TNotifyUI& msg); //virtual void OnSelectChanged(TNotifyUI& msg); //virtual void OnItemClick(TNotifyUI& msg); private: CPaintManagerUI* m_pPaintManager; }; class CInstallWnd : public WindowImplBase { public: CInstallWnd(); ~CInstallWnd(); virtual LRESULT HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam) override; virtual CDuiString GetSkinFile() override { return _T("install.xml"); } virtual LPCTSTR GetWindowClassName(void) const override { return _T("InstallWnd"); } virtual LRESULT OnSysCommand(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled) override; virtual void InitWindow() override; virtual void Notify(TNotifyUI& msg) override; DUI_DECLARE_MESSAGE_MAP() virtual void OnClick(TNotifyUI& msg); virtual LRESULT OnClose(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled); virtual LRESULT OnDestroy(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled); public: void set_resource_zip(); CPaintManagerUI* GetPaintManagerUI(); private: CButtonUI* m_pCloseBtn; CCheckBoxUI* m_pCheckBox; CCheckBoxUI* m_pCheckBoxShortcut; CCheckBoxUI* m_pCheckBoxStartMenu; CButtonUI* m_pMainInstallBtn; CButtonUI* m_pUnInstallBtn; CButtonUI* m_pCloseUnInstall; CButtonUI* m_pUninstalled; CButtonUI* m_pRun; CButtonUI* m_pPrivacy; CButtonUI* m_pSelectDir; CRichEditUI* m_pEdit; CMainPage m_MainPage; CString m_OldPath; CString m_FinalInstPath; bool flag_ = false; }; #endif // NASIABOUT_MAINWND_H |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 |
#include "MainWnd.h" #include <string> #include "ns_helper.h" #include "common/Statistics.h" #include "ns_utils.h" #include "include/gflags/gflags.h" #include "MsgWnd.h" //extern std::string g_src_path; extern extra_parameters* g_pluginParms; extern HWND g_hwndParent; extern bool g_msgloop_flag; extern int g_desktop_shortcut; extern bool g_autorun; extern bool g_start_tray; extern bool g_create_tray_icon; extern bool g_start_up_main; extern bool g_enable_tray; std::once_flag flag_close; // cmd about DECLARE_bool(nostart); DECLARE_bool(no_deskshort); DECLARE_bool(no_startmenu); DECLARE_bool(autorun); // cmd about DUI_BEGIN_MESSAGE_MAP(CMainPage, CNotifyPump) DUI_ON_MSGTYPE(DUI_MSGTYPE_CLICK, OnClick) DUI_END_MESSAGE_MAP() CMainPage::CMainPage() { m_pPaintManager = NULL; } void CMainPage::SetPaintMagager(CPaintManagerUI* pPaintMgr) { m_pPaintManager = pPaintMgr; } void CMainPage::set_resource_zip(LPCWSTR path) { if (m_pPaintManager) { //m_pPaintManager->SetResourceZip(path); } } DuiLib::CPaintManagerUI* CMainPage::get_manager_ui() { return m_pPaintManager; } void CMainPage::OnClick(TNotifyUI& msg) { if (msg.pSender->GetName() == _T("down_ico")) { } else if (msg.pSender->GetName() == _T("down_del")) { } else if (msg.pSender->GetName() == _T("down_new")) { } } DUI_BEGIN_MESSAGE_MAP(CInstallWnd, WindowImplBase) DUI_ON_MSGTYPE(DUI_MSGTYPE_CLICK, OnClick) DUI_END_MESSAGE_MAP() CInstallWnd::CInstallWnd() { m_MainPage.SetPaintMagager(&m_pm); AddVirtualWnd(_T("mainpage"), &m_MainPage); } CInstallWnd::~CInstallWnd() { RemoveVirtualWnd(_T("mainpage")); } LRESULT CInstallWnd::HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam) { if (uMsg == WM_NCLBUTTONDBLCLK) { return 0; } return __super::HandleMessage(uMsg, wParam, lParam); } LRESULT CInstallWnd::OnSysCommand(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled) { // 有时会在收到WM_NCDESTROY后收到wParam为SC_CLOSE的WM_SYSCOMMAND if (wParam == SC_CLOSE) { ::PostQuitMessage(0L); bHandled = TRUE; return 0; } BOOL bZoomed = ::IsZoomed(*this); LRESULT lRes = CWindowWnd::HandleMessage(uMsg, wParam, lParam); if (::IsZoomed(*this) != bZoomed) { if (!bZoomed) { CControlUI* pControl = static_cast<CControlUI*>(m_pm.FindControl(_T("maxbtn"))); if (pControl) pControl->SetVisible(false); pControl = static_cast<CControlUI*>(m_pm.FindControl(_T("restorebtn"))); if (pControl) pControl->SetVisible(true); } else { CControlUI* pControl = static_cast<CControlUI*>(m_pm.FindControl(_T("maxbtn"))); if (pControl) pControl->SetVisible(true); pControl = static_cast<CControlUI*>(m_pm.FindControl(_T("restorebtn"))); if (pControl) pControl->SetVisible(false); } } return lRes; } void CInstallWnd::InitWindow() { m_pCloseBtn = static_cast<CButtonUI*>(m_pm.FindControl(_T("btnClose"))); m_pCheckBox = static_cast<CCheckBoxUI*>(m_pm.FindControl(_T("chkAgree"))); m_pCheckBoxShortcut = static_cast<CCheckBoxUI*>(m_pm.FindControl(_T("chkbox_desktop_ico"))); m_pCheckBoxStartMenu = static_cast<CCheckBoxUI*>(m_pm.FindControl(_T("chkbox_start_menu"))); m_pMainInstallBtn = static_cast<CButtonUI*>(m_pm.FindControl(_T("btnMainInstall"))); m_pEdit = static_cast<CRichEditUI*>(m_pm.FindControl(_T("editDir"))); m_pUnInstallBtn = static_cast<CButtonUI*>(m_pm.FindControl(_T("btnUnInstall"))); m_pCloseUnInstall = static_cast<CButtonUI*>(m_pm.FindControl(_T("btnCloseUnInstall"))); m_pUninstalled = static_cast<CButtonUI*>(m_pm.FindControl(_T("btnUninstalled"))); m_pRun = static_cast<CButtonUI*>(m_pm.FindControl(_T("btnRun"))); m_pPrivacy= static_cast<CButtonUI*>(m_pm.FindControl(_T("btnPrivacy"))); m_pSelectDir = static_cast<CButtonUI*>(m_pm.FindControl(_T("btnSelectDir"))); } void CInstallWnd::Notify(TNotifyUI& msg) { if (wcscmp(msg.sType, L"textchanged") == 0) { if (msg.pSender == m_pEdit) { auto str = m_pEdit->GetText(); m_OldPath = str; auto ctrl_name = m_pEdit->GetName(); CallCallback(ctrl_name.GetData(), GetHWND(), g_pluginParms); } } else { WindowImplBase::Notify(msg); } } void CInstallWnd::OnClick(TNotifyUI& msg) { CDuiString sName = msg.pSender->GetName(); sName.MakeLower(); if (msg.pSender == m_pCloseBtn) { CControlUI* pCtrl = m_pm.FindControl(L"wizardTab"); CTabLayoutUI* pTab = static_cast<CTabLayoutUI*>(pCtrl); int index = pTab->GetCurSel(); if (index == 0) { CMsgDlg msg; msg.Create(this->m_hWnd, _T("Win清理优化"), UI_WNDSTYLE_DIALOG, WS_EX_TOPMOST); CString tmp = L"确认放弃安装Windows清理优化吗?"; msg.set_msg_tip(tmp); msg.CenterWindow(); msg.ShowModal(); flag_ = true; int clk = msg.get_btn_click(); if (clk == 2) { CStatistic::GetInstance()->SendStatAction(L"click_exit"); Close(); return; } CStatistic::GetInstance()->SendStatAction(L"abandon_no"); return; } else if (index == 2) { Active(m_FinalInstPath); Close(); return; } else if (index == 3) { CStatistic::GetInstance()->SendStatAction(L"uninstall_close"); Close(); return; } else if (index == 4) { CStatistic::GetInstance()->SendStatAction(L"uninstall_close"); Close(); return; } else if (index == 5) { CStatistic::GetInstance()->SendStatAction(L"uninstall_close"); Close(); return; } else { Close(); return; } } else if (msg.pSender == m_pCheckBox) { auto tmp = m_pCheckBox->IsSelected(); if (tmp) { m_pMainInstallBtn->SetAttribute(L"enabled", L"false"); } else { m_pMainInstallBtn->SetAttribute(L"enabled", L"true"); } } else if (msg.pSender == m_pMainInstallBtn) { // dot deskshortcut_enable deskshortcut_disable // SetDeskShortCut m_bDesktopIconRe CStatistic::GetInstance()->SendStatAction(L"click"); CDuiString cur_path; if (m_pEdit) { cur_path = m_pEdit->GetText(); } if (!m_OldPath.IsEmpty() && cur_path.CompareNoCase(m_OldPath) != 0) { CStatistic::GetInstance()->SendStatAction(L"non_default"); } if (cur_path.IsEmpty()) { // show msgbox todo: return; } std::wstring tmp = cur_path.GetData(); auto space = ns::get_disk_freespace(tmp[0]); if (space == 0) { // msgbox 磁盘盘符无效,请检查后再试 return; } if (space < install_disk_freespace) { // msgbox 磁盘空间不足,Windows系统优化需要120MB return; } if (ns::is_invalidchar_in_path_new(tmp)) { // msgbox 输入的路径中包含非法字符 return; } if (!::PathFileExists(cur_path)) { DWORD dwErr = 0; if (!ns::is_can_create_dir(tmp, dwErr)) { // msgbox 您选择的路径无法安装Windows系统优化,\n请重新选择 return; } } if (!ns::is_target_writable(tmp)) { // msgbox 您选择的路径无法安装Windows系统优化,\n请重新选择 return; } // 改成桌面快捷方式的m_pCheckBoxShortcut if (m_pCheckBoxShortcut) { auto sel = m_pCheckBoxShortcut->IsSelected(); if (sel) { CStatistic::GetInstance()->SendStatAction(L"deskshortcut_enable"); } else { CStatistic::GetInstance()->SendStatAction(L"deskshortcut_disable"); } g_desktop_shortcut = (sel) ? TRUE : FALSE; FLAGS_no_deskshort = sel; } // 改成桌面快捷方式的m_pCheckBoxStartMenu if (m_pCheckBoxStartMenu) { auto sel = m_pCheckBoxStartMenu->IsSelected(); if (sel) { CStatistic::GetInstance()->SendStatAction(L"startmenu_enable"); } else { CStatistic::GetInstance()->SendStatAction(L"startmenu_disable"); } // gflags::SetCommandLineOption("no_deskshort", ); FLAGS_no_startmenu = sel; } // 改成桌面快捷方式的m_pCheckBoxAutorun /*if (m_pCheckBox) { auto sel = m_pCheckBox->IsSelected(); if (sel) { CStatistic::GetInstance()->SendStatAction(L"autorun_enable"); } else { CStatistic::GetInstance()->SendStatAction(L"autorun_disable"); } FLAGS_autorun = sel; // to do g_autorun = true; }*/ m_FinalInstPath = cur_path; auto ctrl_name = m_pMainInstallBtn->GetName(); CallCallback(ctrl_name.GetData(), GetHWND(), g_pluginParms); } else if (msg.pSender == m_pUnInstallBtn) { CStatistic::GetInstance()->SendStatAction(L"uninstall_continue"); auto ctrl_name = m_pUnInstallBtn->GetName(); CallCallback(ctrl_name.GetData(), GetHWND(), g_pluginParms); } else if (msg.pSender == m_pCloseUnInstall) { CStatistic::GetInstance()->SendStatAction(L"uninstall_use_again"); auto ctrl_name = m_pCloseUnInstall->GetName(); CallCallback(ctrl_name.GetData(), GetHWND(), g_pluginParms); } else if (msg.pSender == m_pUninstalled) { auto ctrl_name = m_pUninstalled->GetName(); CallCallback(ctrl_name.GetData(), GetHWND(), g_pluginParms); } else if (msg.pSender == m_pRun) { g_start_tray = true; g_create_tray_icon = true; g_start_up_main = true; g_enable_tray = true; Active(m_FinalInstPath); auto ctrl_name = m_pRun->GetName(); CallCallback(ctrl_name.GetData(), GetHWND(), g_pluginParms); } else if (msg.pSender == m_pPrivacy) { auto ctrl_name = m_pPrivacy->GetName(); CallCallback(ctrl_name.GetData(), GetHWND(), g_pluginParms); } else if (msg.pSender == m_pSelectDir){ auto ctrl_name = m_pSelectDir->GetName(); CallCallback(ctrl_name.GetData(), GetHWND(), g_pluginParms); } } LRESULT CInstallWnd::OnClose(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled) { return __super::OnClose(uMsg, wParam, lParam, bHandled); } LRESULT CInstallWnd::OnDestroy(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled) { PostQuitMessage(0); return __super::OnDestroy(uMsg, wParam, lParam, bHandled); } DuiLib::CPaintManagerUI* CInstallWnd::GetPaintManagerUI() { return m_MainPage.get_manager_ui(); } void CInstallWnd::set_resource_zip() { //if (!g_src_path.empty()) { // // wx g_src_path // m_MainPage.set_resource_zip(L"skin.zip"); //} } |
提示弹窗
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
#ifndef NASIABOUT_MSGWND_H #define NASIABOUT_MSGWND_H #include "define.h" #include <atlstr.h> enum btn_click { bc_close = 0, bc_cancle = 1, bc_ok = 2, }; class CMsgDlg : public WindowImplBase { public: CMsgDlg(void); ~CMsgDlg(void); virtual LRESULT HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam) override; //virtual void OnFinalMessage(HWND hWnd) override; virtual CDuiString GetSkinFile() override { return _T("MsgBox.xml"); } virtual LPCTSTR GetWindowClassName(void) const override { return _T("tipMsgWnd"); } virtual void InitWindow() override; virtual void Notify(DuiLib::TNotifyUI& msg) override; virtual void OnClick(TNotifyUI& msg); virtual LRESULT OnDestroy(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled); DUI_DECLARE_MESSAGE_MAP() public: int get_btn_click(); void set_msg_tip(CString& tip); private: int btn_click_; CButtonUI* m_pCloseBtn; CButtonUI* m_pCancelBtn; CButtonUI* m_pOkBtn; CLabelUI* m_pMsgtip; }; #endif // NASIABOUT_MSGWND_H |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
#include "MsgWnd.h" DUI_BEGIN_MESSAGE_MAP(CMsgDlg, WindowImplBase) DUI_ON_MSGTYPE(DUI_MSGTYPE_CLICK, OnClick) DUI_END_MESSAGE_MAP() CMsgDlg::CMsgDlg(void) : btn_click_(0) { } CMsgDlg::~CMsgDlg(void) { } LRESULT CMsgDlg::HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam) { if (uMsg == WM_ERASEBKGND) { return 1; } else if (uMsg == WM_NCLBUTTONDBLCLK) { return 0; } return __super::HandleMessage(uMsg, wParam, lParam); } //void CMsgDlg::OnFinalMessage(HWND hWnd) { // __super::OnFinalMessage(hWnd); //} void CMsgDlg::InitWindow() { m_pCloseBtn = static_cast<CButtonUI*>(m_pm.FindControl(_T("btnClose"))); m_pCancelBtn = static_cast<CButtonUI*>(m_pm.FindControl(_T("btnCancle"))); m_pOkBtn = static_cast<CButtonUI*>(m_pm.FindControl(_T("btnOk"))); m_pMsgtip = static_cast<CLabelUI*>(m_pm.FindControl(_T("msg_tips"))); } void CMsgDlg::Notify(DuiLib::TNotifyUI& msg) { if (msg.sType == L"click") { auto btn_tmp = msg.pSender->GetName(); if (btn_tmp == L"btnClose") { btn_click_ = bc_close; Close(); } else if (btn_tmp == L"btnCancle") { btn_click_ = bc_cancle; Close(); } else if (btn_tmp == L"btnOk") { btn_click_ = bc_ok; Close(); } } else { __super::Notify(msg); } } void CMsgDlg::OnClick(TNotifyUI& msg) { CDuiString sName = msg.pSender->GetName(); sName.MakeLower(); if (msg.pSender == m_pCloseBtn) { Close(); return; } else if (msg.pSender == m_pCancelBtn) { Close(); return; } else if (msg.pSender == m_pOkBtn) { Close(); return; } } LRESULT CMsgDlg::OnDestroy(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled) { //PostQuitMessage(0); return __super::OnDestroy(uMsg, wParam, lParam, bHandled); } int CMsgDlg::get_btn_click() { return btn_click_; } void CMsgDlg::set_msg_tip(CString& tip) { if (m_pMsgtip) { m_pMsgtip->SetText(tip); } } |
其他
关于进度条实现
- 实际上在
xml
里面使用了Slider
- 经验是:
- 这个
test1.png
,test2.png
图片,得使用Slider
的尺寸,这样,如果图片使用了圆角,才不会模糊 - 如果是直角进度条,应该就不需要
- 这个
1 2 3 4 5 6 7 8 9 10 11 |
<Slider name="slrProgress" height="8" width="400" float="true" pos="100,304,500,312" mouse="false" bkimage="form\test1.png" foreimage="form\test2.png" thumbsize="0,0" value="0" /> |
点击
1 2 3 4 5 6 7 |
void CInstallWnd::OnClick(TNotifyUI& msg) { CDuiString sName = msg.pSender->GetName(); sName.MakeLower(); if (msg.pSender == m_pCloseBtn) { } } |
关闭
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
LRESULT CInstallWnd::OnClose(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled) { return __super::OnClose(uMsg, wParam, lParam, bHandled); } LRESULT CInstallWnd::OnDestroy(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled) { PostQuitMessage(0); return __super::OnDestroy(uMsg, wParam, lParam, bHandled); } |
事件处理
- 比如
CRichEditUI
控件有textchanged
事件,处理如下
1 2 3 4 5 6 7 8 9 10 11 12 13 |
void CInstallWnd::Notify(TNotifyUI& msg) { if (wcscmp(msg.sType, L"textchanged") == 0) { if (msg.pSender == m_pEdit) { auto str = m_pEdit->GetText(); m_OldPath = str; auto ctrl_name = m_pEdit->GetName(); CallCallback(ctrl_name.GetData(), GetHWND(), g_pluginParms); } } else { WindowImplBase::Notify(msg); } } |
单独处理某些消息
- 如屏蔽鼠标左键双击
1 2 3 4 5 6 7 8 9 |
LRESULT CInstallWnd::HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam) { if (uMsg == WM_NCLBUTTONDBLCLK) { return 0; } return __super::HandleMessage(uMsg, wParam, lParam); } |
关于消息循环
duilib
需要在cpp
文件这里加这些
1 2 3 |
DUI_BEGIN_MESSAGE_MAP(CInstallWnd, WindowImplBase) DUI_ON_MSGTYPE(DUI_MSGTYPE_CLICK, OnClick) DUI_END_MESSAGE_MAP() |
模态子窗口展示提示弹窗
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
void NS_CALL InitSkinSubPage(HWND hwndParent, int string_size, wchar_t* variables, stack_t** stacktop, extra_parameters* extra) { NSMETHOD_INIT(hwndParent); TCHAR dir[MAX_PATH] = {0}; TCHAR tip[MAX_PATH] = {0}; TCHAR title[MAX_PATH] = {0}; if (popstringn(dir, MAX_PATH) != 0) { pushstring(TEXT("错误:缺少参数")); if (logger) { logger->info("InitSkinSubPage-> nsis popstringn 1 failed."); logger->flush(); } return; } if (popstringn(tip, MAX_PATH) != 0) { pushstring(TEXT("错误:缺少参数")); if (logger) { logger->info("InitSkinSubPage-> nsis popstringn 2 failed."); logger->flush(); } return; } if (popstringn(title, MAX_PATH) != 0) { pushstring(TEXT("错误:缺少参数")); if (logger) { logger->info("InitSkinSubPage-> nsis popstringn 3 failed."); logger->flush(); } return; } CPaintManagerUI::SetInstance(g_hInstance); CPaintManagerUI::SetResourcePath(dir); CPaintManagerUI::SetResourceZip(L"skin.zip"); CMsgDlg msg; HWND thwnd; HWND hWndMain = FindWindow(L"InstallWnd", title); if (hWndMain) { msg.Create(hWndMain, _T("Win清理优化"), UI_WNDSTYLE_DIALOG, WS_EX_TOPMOST); } else { msg.Create(g_hwndParent, _T("Win清理优化"), UI_WNDSTYLE_DIALOG, WS_EX_TOPMOST); } CString tmp = tip; msg.set_msg_tip(tmp); msg.CenterWindow(); //::EnableWindow(g_hwndParent, FALSE); msg.ShowModal(); //::EnableWindow(g_hwndParent, TRUE); int clk = msg.get_btn_click(); pushint(clk); } |
请求相关
- 实现
win_http.hpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 |
#ifndef SRC_PLUGIN_INSTALLER_WIN_HTTP_H_ #define SRC_PLUGIN_INSTALLER_WIN_HTTP_H_ #include <windows.h> #include <wininet.h> #include <string> #include <queue> #include <memory> #include <CommonHelper\LdsThread.h> #pragma comment(lib, "Wininet.lib") namespace win_http { // 请求任务: 一般信息 和 文件, 都会放入队列中 // RequestThread 从队列中获取任务逐一处理 struct RequestTask { std::string request_url; std::string save_file_path; HANDLE wait_event; }; static void CloseInternetHandle(HINTERNET* h) { if (h && *h) { InternetCloseHandle(*h); *h = NULL; } } using RequestTaskPtr = std::unique_ptr<RequestTask>; class WinHttpCallback { public: // 如果是文件完成, info 为 null // kErrMsg为空表示成功 virtual void RequestFinished(const char* kUrl, const char* kInfo, const char* kErrMsg) = 0; }; class WinHttp { public: WinHttp(WinHttpCallback* callback) : callback_(callback) { exist_request_thread_ = CreateEvent(NULL, TRUE, FALSE, NULL); } ~WinHttp() { Stop(); }; // 如果 kSavePath 可以为 null, 如果非null则认为是请求文件 void Request(const char* kUrl, const char* kSavePath, DWORD wait_timeout = 0) { RequestTaskPtr request_task = std::make_unique<RequestTask>(); request_task->request_url = kUrl; HANDLE wait_event = wait_timeout ? CreateEvent(NULL, FALSE, FALSE, NULL) : NULL; if (wait_event) { request_task->wait_event = wait_event; } if (kSavePath) { request_task->save_file_path = kSavePath; } { LdsAutoLocker lock(request_tasks_mutex_); request_tasks_.push(std::move(request_task)); StartThread(); } if (wait_event) { WaitForSingleObject(wait_event, wait_timeout); CloseHandle(wait_event); } } void Stop() { stop_task_ = true; StopThread(); } private: void GetData(const char* kUrl) { std::string text; const std::string kErrMsg = GetData( kUrl, [&text](const char* data, size_t size) -> std::string { text.append(data, size); return ""; }); if (callback_) { callback_->RequestFinished(kUrl, text.c_str(), kErrMsg.c_str()); } } void GetData(const char* kUrl, const char* kSavePath) { HANDLE file = NULL; size_t write_size = 0; std::string kErrMsg = GetData(kUrl, [&file, &write_size, kSavePath]( const char* data, size_t size) -> std::string { if (!file) { file = CreateFileA(kSavePath, GENERIC_WRITE, 0, 0, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0); if (INVALID_HANDLE_VALUE == file) { return UpdateErrMsg("create file error", GetLastError()); } } DWORD writeen_size(0); if (!WriteFile(file, data, size, &writeen_size, NULL)) { return UpdateErrMsg("write file error", GetLastError()); } if (size != writeen_size) { return UpdateErrMsg("write file error(size)", GetLastError()); } write_size += writeen_size; return ""; }); if (file && file != INVALID_HANDLE_VALUE) { CloseHandle(file); file = NULL; } if (callback_) { callback_->RequestFinished(kUrl, NULL, kErrMsg.c_str()); } } // 返回执行结果描述, 成功返回空 std::string GetData( const char* kUrl, std::function<std::string(const char*, size_t)> putout_data) { std::string err_msg; HINTERNET session = NULL; HINTERNET url = NULL; do { session = InternetOpenA("WinInet", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, NULL); if (!session) { err_msg = UpdateErrMsg("open session error", GetLastError()); break; } url = InternetOpenUrlA(session, kUrl, NULL, 0, INTERNET_FLAG_RELOAD | INTERNET_FLAG_NO_CACHE_WRITE, 0); if (!url) { err_msg = UpdateErrMsg("open url error", GetLastError()); break; } constexpr size_t kBufSize = 256; char read_buffer[kBufSize]{ 0 }; DWORD read_size = 0; while (true) { if (IsStopTask()) { break; } memset(read_buffer, 0, kBufSize); if (!InternetReadFile(url, read_buffer, kBufSize, &read_size)) { err_msg = UpdateErrMsg("read url file error", GetLastError()); break; } // printf("read url file size: %d \n", read_size); if (read_size == 0) { // complete break; } if (putout_data) { err_msg = putout_data(read_buffer, read_size); if (!err_msg.empty()) { break; } } } } while (false); CloseInternetHandle(&session); CloseInternetHandle(&url); return err_msg; } // 停止线程即时停止任务 bool IsStopTask() { return stop_task_; } RequestTaskPtr GetRequestTask() { RequestTaskPtr request_task; { LdsAutoLocker lock(request_tasks_mutex_); if (!request_tasks_.empty()) { request_task = std::move(request_tasks_.front()); request_tasks_.pop(); } } return request_task; } void DoThread() { while (!IsExitThread()) { if (RequestTaskPtr request_task = GetRequestTask()) { if (request_task->save_file_path.empty()) { GetData(request_task->request_url.c_str()); } else { GetData(request_task->request_url.c_str(), request_task->save_file_path.c_str()); } SetEvent(request_task->wait_event); } } } void StartThread() { if (!request_thread_) { unsigned int thread_id = 0; request_thread_ = (HANDLE)_beginthreadex( NULL, NULL, &WinHttp::RequestThread, this, NULL, &thread_id); } } void StopThread() { if (exist_request_thread_) { SetEvent(exist_request_thread_); } if (request_thread_) { WaitForSingleObject(request_thread_, 3000); CloseHandle(request_thread_); request_thread_ = NULL; } if (exist_request_thread_) { CloseHandle(exist_request_thread_); exist_request_thread_ = NULL; } } bool IsExitThread() { return WaitForSingleObject(exist_request_thread_, 100) == WAIT_OBJECT_0; } static std::string UpdateErrMsg(const char* kMsg, DWORD err_code) { constexpr size_t kSize = 128; char buf[kSize]{ 0 }; snprintf(buf, kSize, "%s(%d)", kMsg ? kMsg : "", err_code); return std::string(buf); } static unsigned int __stdcall RequestThread(LPVOID param) { WinHttp* pthis = reinterpret_cast<WinHttp*>(param); if (!pthis) return 0; pthis->DoThread(); return 0; } private: WinHttpCallback* callback_ = nullptr; HANDLE request_thread_ = NULL; HANDLE exist_request_thread_ = NULL; LdsMutex request_tasks_mutex_; std::queue<RequestTaskPtr> request_tasks_; bool stop_task_ = false; }; } // namespace plugin_installer_http #endif |
- 使用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
#pragma once #include "common/win_http.hpp" #include <string> class http_helper : public win_http::WinHttpCallback { public: http_helper(); ~http_helper(); void RequestFinished(const char* kUrl, const char* kInfo, const char* kErrMsg) override; std::string GetInfo(); bool is_ok(); void set_event(HANDLE ev); private: bool is_ok_{false}; std::string info_; HANDLE ev_; }; |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
#include "http_helper.h" http_helper::http_helper() { } http_helper::~http_helper() { if (ev_) { CloseHandle(ev_); } } void http_helper::RequestFinished(const char* kUrl, const char* kInfo, const char* kErrMsg) { if (strcmp(kErrMsg, "") == 0) { info_ = kInfo; if (!info_.empty()) { is_ok_ = true; } } else { is_ok_ = false; } if (ev_) { SetEvent(ev_); } } std::string http_helper::GetInfo() { return info_; } bool http_helper::is_ok() { return is_ok_; } void http_helper::set_event(HANDLE ev) { ev_ = ev; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
std::string str_url = "http://www1.chixiatech.com/pc/PopIntf/GetPreFix"; str_url.append("?channel="); str_url.append(tmp_pid); HANDLE hEvent = CreateEvent(NULL, TRUE, FALSE, NULL); if (hEvent == NULL) { return false; } http_helper helper; helper.set_event(hEvent); win_http::WinHttp http(&helper); http.Request(str_url.c_str(), NULL, 30000); WaitForSingleObject(hEvent, INFINITE); std::string res; if (!helper.is_ok()) { CStatistic::GetInstance()->SendStatAction(L"bc_filter_get_fail"); return false; } res = helper.GetInfo(); if (res.empty()) { return false; } |
选择目录
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
void NS_CALL SelectInstallDir(HWND hwndParent, int string_size, wchar_t* variables, stack_t** stacktop, extra_parameters* extra) { NSMETHOD_INIT(hwndParent); HWND hwnd; hwnd = (HWND)popint(); TCHAR sel_title[MAX_PATH] = {0}; if (popstringn(sel_title, MAX_PATH) != 0) { pushstring(TEXT("错误:缺少参数")); if (logger) { logger->info("SelectInstallDir->nsis popstringn 2 failed."); logger->flush(); } return; } BROWSEINFO brInfo = {0}; brInfo.lpszTitle = sel_title; LPITEMIDLIST pIdList = SHBrowseForFolder(&brInfo); if (pIdList != NULL) { TCHAR szSelectedPath[MAX_PATH]; SHGetPathFromIDList(pIdList, szSelectedPath); pushstring(szSelectedPath); CoTaskMemFree(pIdList); // 释放内存 return; } pushstring(L""); } |
Nsis
初始化主窗口
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
void NS_CALL InitDUISetup(HWND hwndParent, int string_size, wchar_t* variables, stack_t** stacktop, extra_parameters* extra) { NSMETHOD_INIT(hwndParent); int from = popint(); TCHAR param2[MAX_PATH] = {0}; if (popstringn(param2, MAX_PATH) != 0) { pushstring(TEXT("错误:缺少参数")); if (logger) { logger->info("InitDUISetup->nsis popstringn 2 failed."); logger->flush(); } return; } extra->RegisterPluginCallback(g_hInstance, PluginCallback); { CPaintManagerUI::SetInstance(g_hInstance); CPaintManagerUI::SetResourcePath(param2); CPaintManagerUI::SetResourceZip(L"skin.zip"); g_pMainDlg = new CInstallWnd(); g_pMainDlg->Create(NULL, _T("test"), UI_WNDSTYLE_FRAME, WS_EX_STATICEDGE | WS_EX_APPWINDOW, 0, 0, 600, 400); g_pMainDlg->CenterWindow(); if (from == 0) { // inst CStatistic::GetInstance()->SendStatAction(L"start"); } else if (from == 1) { // uninst if (!FLAGS_s) { CStatistic::GetInstance()->SendStatAction(L"uninstall_show"); } } g_pMainDlg->ShowWindow(FALSE); pushint(int(g_pMainDlg->GetHWND())); } if (logger) { logger->info("InitDUISetup->init dui."); logger->flush(); } } |
声明:本文为原创文章,版权归Aet所有,欢迎分享本文,转载请保留出处!
你可能也喜欢
- ♥ Windows 核心编程 _ 线程内幕07/06
- ♥ Windows 核心编程 _ 进程三06/19
- ♥ WinDbg相关01/12
- ♥ x86_64汇编学习记述二08/07
- ♥ Windows API11/11
- ♥ Soui应用 动画二06/27