VC 2010工程中加入SplashScreen

2022-02-06 20:28:01 字數 4609 閱讀 4584

作者資訊:羅樹鵬

由於筆者在實踐過程中走了一些彎路,所以把這些情況記錄下來,希望為後來者提供一些經驗。

在vc6.0

時代,可以通過元件為工程加入

splashscreen

,具體方法是

通過ide

中的選單

project->add to project->components

and controls

,就可以從

visual c++ components

中選擇splash screen

這個元件插入工程。

但進入到

vc. net

時代,這種方法就不行了,需要程式作者自己加入

splashscreen

類,自己處理一些系統訊息。下面筆者就把實踐過程記錄下來,並指出需要注意的地方。

一、新建乙個

splashscreen

類,並宣告成員和方法

新建基類為

cwnd

的csplashwnd

類(當然類名可以自由書寫),並宣告如下成員:

cbitmap m_bitmap;

//載入

splashscreen

用static

csplashwnd*

c_psplashwnd;

//csplashwnd

類的控制代碼,可以用來判定

csplashwnd

是否已經建立或消毀

static

bool

c_bshowsplashwnd;

//標識是否顯示顯示用來,靜態成員需要在類外進行初始化

類成員一般宣告為保護型別

(protected)

。接下來宣告幾個靜態方法來處理這些成員:

static

bool

c_bshowsplashwnd;

//標識是否顯示顯示用來,靜態成員需要在類外進行初始化

static

void showsplashscreen(cwnd* pparentwnd = null);

//用來顯示

splashscreen

視窗static bool

//用來處理一些訊息

注意這裡不是繼承於

cwnd

類的pretranslatemessage

方法這些方法一定要宣告成公開型別的

(public)

,因為要在外部呼叫這些方法。接下來再宣告兩個自定義方法:

bool create(cwnd*

pparentwnd = null);

//建立視窗

void

hidesplashscreen(void);

//隱藏視窗

我把這些方法宣告為保護型別

(protected)

。接下來再宣告一些處理系統訊息的函式:

virtual

void postncdestroy();

afx_msg int oncreate(lpcreatestruct lpcreatestruct);

afx_msg void ontimer(uint_ptr nidevent);

afx_msg void onpaint();

至此,類已經設計完畢。完整的標頭檔案如下:

#pragma

once

#include

"afxwin.h"

//csplashwnd

class

csplashwnd : public cwnd;二、

csplashwnd

類方法的實現

這裡需要注意的事項是在析構函式中一定要把

c_psplashwnd

成員置為

null

型別,否則程式收到訊息後會出現異常。其它沒有什麼了,直接看**吧。

: implementation file

//#include

"stdafx.h"

#include

"splash.h"

csplashwnd*

csplashwnd::c_psplashwnd;

bool

csplashwnd::c_bshowsplashwnd;

//csplashwnd

implement_dynamic(csplashwnd,

cwnd)

csplashwnd::csplashwnd()

csplashwnd::~csplashwnd()

begin_message_map(csplashwnd,

cwnd)

on_wm_create()

on_wm_timer()

on_wm_paint()

end_message_map()

//csplashwnd message handlers

void

csplashwnd::enablesplashscreen(bool benable/* =

true*/)

void

csplashwnd::showsplashscreen(cwnd* pparentwnd/* =

null*/)

c_psplashwnd = new

csplashwnd;

if ( !

c_psplashwnd->create(pparentwnd))

else

}bool

return

false;

}void

csplashwnd::postncdestroy()

intcsplashwnd::oncreate(lpcreatestruct lpcreatestruct)

void

csplashwnd::ontimer(uint_ptr nidevent)

/*cwnd::ontimer(nidevent);*/

}void

csplashwnd::onpaint()

bitmap bm;

m_bitmap.getbitmap(&bm);

// paint the

image

cbitmap* poldbit =

dcimg.selectobject(&m_bitmap);

dc.bitblt(0,0,bm.bmwidth,bm.bmheight,&dcimg,0,0,srccopy);

dcimg.selectobject(poldbit);

}bool

csplashwnd::create(cwnd* pparentwnd)

bitmap bm;

m_bitmap.getbitmap(&bm);

return

createex(0,

null,

ws_popup|ws_visible,

0,0,

bm.bmwidth,

bm.bmheight,

pparentwnd->getsafehwnd(),

null);

}void

csplashwnd::hidesplashscreen(void)三、在

sdi或者

mdi中使用

csplashwnd

類(1)

在中呼叫

csplashwnd::enablesplashscreen()

設定c_bshowsplashwnd;在

pretranslatemessage()

中呼叫,將鍵盤和滑鼠訊息傳遞給

csplashwnd

物件(2)

(2)在

cmainframe

物件的oncreate()

中呼叫csplashwnd::showsplashscreen()

建立乙個靜態的

splashscreen

視窗物件

c_psplashwnd

,並設定其父視窗為

cmainframe

.**如下:

bool

………..

}bool

intcmainframe::oncreate(lpcreatestruct lpcreatestruct)

idb_splash

沒有定義。這需要在資源中加入個位圖資源,並將

」resource.h」

標頭檔案包含到

cslashwnd.cpp

檔案中。

四、在對話方塊中使用

csplashwnd

類在對話方塊中使用和在

sdi或

mdi中使用基本相似,首先在

的繼承類中處理

initinstance()

和pretranslatemessage(msg* pmsg)

兩個訊息函式,然後在對話方塊類的

wm_create

響應函式中顯示

splashscreen

。bool

……….

}bool

return

}int

cdlgdlg::oncreate(lpcreatestruct lpcreatestruct)

檢視示例

VC2010除錯DLL工程的方法

如果有用於建立可執行檔案的專案,則從該專案開始除錯。然後可以開啟 dll 的原始檔,並在該檔案中設定斷點,即使它不是用於建立可執行檔案的專案的一部分。如果從建立 dll 的專案開始除錯,則必須指定在除錯 dll 時要使用的可執行檔案。為除錯會話指定可執行檔案 在 解決方案資源管理器 中,選擇用於建立...

VC2010工程依賴不再自動鏈結

發現 vc2010 express 設定了 project dependencies 之後並沒有自動鏈結 而在vc2008中工程依賴不僅影響構建順序,也會自動鏈結依賴項 具體說明見 現在應該使用 references 來設定依賴並自動鏈結 但是vc2010express存在乙個錯誤,buildall...

vc2010呼叫儲存過程

coinitialize null connectionptr pmyconnect null hresult hr pmyconnect.createinstance uuidof connection if failed hr return bstr t strconnect provider ...