Bonjour,
J'aurais souhaiter savoir comment puis je afficher une image d'accueil au démarrage de mon application avant de lancer ma dialogue principale.
Je précise que je suis sur un projet type dialog-based.
Merci d'avance.
Kemanke
Bonjour,
J'aurais souhaiter savoir comment puis je afficher une image d'accueil au démarrage de mon application avant de lancer ma dialogue principale.
Je précise que je suis sur un projet type dialog-based.
Merci d'avance.
Kemanke
salut,
ça s'appelle un "splash-screen".
voici mon implémentation perso :
fichier SplashWnd.h:
fichier SplashWnd.cpp
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 #pragma once // CSplashWnd #define SPLASHWND(pObject) (STATIC_DOWNCAST(CSplashWnd,(CObject*)(pObject))) class AFX_EXT_CLASS CSplashWnd : public CWnd { DECLARE_DYNAMIC(CSplashWnd) public: // fonctions publiques BOOL Create(LPCTSTR pResourceName); BOOL DrawText( LPCTSTR pString,LPCRECT pClipRect, UINT format=DT_END_ELLIPSIS|DT_NOPREFIX|DT_SINGLELINE, const LOGFONT *pLogFont=NULL, COLORREF textcolor=RGB(0,0,0) ); protected: virtual void PostNcDestroy(); DECLARE_MESSAGE_MAP() afx_msg void OnPaint(); afx_msg void OnTimer(UINT_PTR nIDEvent); // variable membre CBitmap m_Bitmap; };
il faut juste rajouter dans ton CWinApp::InitInstance(), avant de faire le DoModal() sur ta boite de dialogue :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 // SplashWnd.cpp*: fichier d'implémentation // #include "stdafx.h" #include "SplashWnd.h" // CSplashWnd IMPLEMENT_DYNAMIC(CSplashWnd, CWnd) BOOL CSplashWnd::Create(LPCTSTR pResourceName) { if (!m_Bitmap.LoadBitmap(pResourceName)) { PostNcDestroy(); return FALSE; } LPCTSTR pClassName; TRY { pClassName=AfxRegisterWndClass(0,LoadCursor(NULL,MAKEINTRESOURCE(IDC_ARROW))); } CATCH_ALL(e) { PostNcDestroy(); return FALSE; } END_CATCH_ALL BITMAP bm={}; VERIFY(m_Bitmap.GetBitmap(&bm)); if (!CreateEx( WS_EX_TOPMOST,pClassName,NULL,WS_POPUP|WS_VISIBLE|WS_DISABLED,0,0, bm.bmWidth,bm.bmHeight,AfxGetMainWnd()->GetSafeHwnd(),NULL,NULL )) return FALSE; CenterWindow(); VERIFY(SetTimer(1234,2000,NULL)); return TRUE; } BOOL CSplashWnd::DrawText( LPCTSTR pString,LPCRECT pClipRect, UINT format,const LOGFONT *pLogFont, COLORREF textcolor ) { CDC memdc; if (memdc.CreateCompatibleDC(NULL)) { if (pLogFont==NULL) { static const LOGFONT lf={ -16,0,0,0,FW_BOLD,FALSE,FALSE,FALSE, ANSI_CHARSET,OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,DEFAULT_QUALITY, VARIABLE_PITCH|FF_SWISS, _T("Arial Narrow") }; pLogFont=&lf; } CFont font; if (font.CreateFontIndirect(pLogFont)) { CFont *pOldFont=memdc.SelectObject(&font); CBitmap *pOldBitmap=memdc.SelectObject(&m_Bitmap); memdc.SetBkMode(TRANSPARENT); memdc.SetTextColor(textcolor); memdc.DrawText(pString,-1,(LPRECT)pClipRect,format); memdc.SelectObject(pOldBitmap); memdc.SelectObject(pOldFont); return TRUE; } } return FALSE; } void CSplashWnd::PostNcDestroy() { delete this; } BEGIN_MESSAGE_MAP(CSplashWnd, CWnd) ON_WM_PAINT() ON_WM_TIMER() END_MESSAGE_MAP() // Gestionnaires de messages de CSplashWnd void CSplashWnd::OnPaint() { CPaintDC dc(this); BITMAP bm={}; VERIFY(m_Bitmap.GetBitmap(&bm)); CDC memdc; if (memdc.CreateCompatibleDC(&dc)) { CBitmap *pOldBitmap=memdc.SelectObject(&m_Bitmap); dc.BitBlt(0,0,bm.bmWidth,bm.bmHeight,&memdc,0,0,SRCCOPY); memdc.SelectObject(pOldBitmap); } } void CSplashWnd::OnTimer(UINT_PTR nIDEvent) { DestroyWindow(); }
dans l'exemple ci dessus, j'écris du texte sur le splash-screen avec la fonction DrawText() ; à enlever si tu n'en as pas besoin
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7 CSplashWnd *pSplashWnd=new CSplashWnd; if (pSplashWnd->Create(MAKEINTRESOURCE(IDB_SPLASH))) { VERIFY(pSplashWnd->DrawText(m_LicenceName,CRect(12,150,388,180))); pSplashWnd->UpdateWindow(); }
@+
Salut
En implémentant la solution proposée, une erreur survient lors de la compilation :
Description : error C2491: 'CSplashWnd::classCSplashWnd' : définition de données membres static dllimport non autorisée
Fichier : f:\...\splashwnd.cpp
Ligne : 10
et un curseur qui indique l'emplacement de l'erreur se position devant :
IMPLEMENT_DYNAMIC(CSplashWnd, CWnd)
dans le fichier: 'splashwnd.cpp'
Notez bien que je travail sous VS2005 pour réaliser une application MFC MDI
Y a t il qq qui peut rectifier le code ou nous indiquer comment resoudre ce probleme
Merci pour votre attention
salut,
enlève le symbole AFX_EXT_CLASS dans le fichier .h
c'etait un copier / coller d'une DLL d'extension que j'avais fait.
donc si tu ne fais pas ça dans une DLL, il faut enlever ce symbole.
@+
Tout d'abord merci pour votre rapidité
et encore merci pour la remarque, ça résolut le problème
Maintenant, je me demande, pourquoi le splash-screen n'apparait pas, est ce que c'est du au durée d'affichage définit (si oui, comment la modifier).
Merci encore
dans ton InitInstance(), tu dois faire ça:
vérifies que la fonction Create renvoie bien TRUE, sinon la cause la plus probable c'est qu'il n'arrive pas à charger le bitmap depuis les ressources... (ici IDB_SPLASH qui est un identifieur)
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4 CSplashWnd *pSplashWnd=new CSplashWnd; if (pSplashWnd->Create(MAKEINTRESOURCE(IDB_SPLASH))) pSplashWnd->UpdateWindow();
@+
Merci infinément
Ca marché cette fois ci
Merci encore![]()
Partager