forked from walbourn/directx-sdk-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WaitDlg.h
149 lines (118 loc) · 4.15 KB
/
WaitDlg.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
//--------------------------------------------------------------------------------------
// File: WaitDlg.h
//
// Wait dialog for shader compilation
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//--------------------------------------------------------------------------------------
#pragma once
#include <process.h>
//--------------------------------------------------------------------------------------
INT_PTR CALLBACK WaitDialogProc( HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam );
unsigned int __stdcall WaitThread( void* pArg );
//--------------------------------------------------------------------------------------
class CWaitDlg
{
private:
HWND m_hDialogWnd;
HANDLE m_hThread;
HWND m_hProgressWnd;
int m_iProgress;
bool m_bDone;
RECT m_AppRect;
WCHAR m_szText[MAX_PATH];
public:
CWaitDlg() :
m_hDialogWnd( nullptr ),
m_hThread( nullptr ),
m_hProgressWnd( nullptr ),
m_iProgress( 0 ),
m_bDone( false )
{
}
~CWaitDlg() { DestroyDialog(); }
bool IsRunning() const { return !m_bDone; }
void UpdateProgressBar()
{
m_iProgress ++;
if( m_iProgress > 110 )
m_iProgress = 0;
SendMessage( m_hProgressWnd, PBM_SETPOS, m_iProgress, 0 );
InvalidateRect( m_hDialogWnd, nullptr, FALSE );
UpdateWindow( m_hDialogWnd );
}
bool GetDialogControls()
{
m_bDone = false;
m_hDialogWnd = CreateDialog( DXUTGetHINSTANCE(), MAKEINTRESOURCE( IDD_COMPILINGSHADERS ), nullptr, WaitDialogProc );
if( !m_hDialogWnd )
return false;
SetWindowLongPtr( m_hDialogWnd, GWLP_USERDATA, (LONG_PTR)this );
// Set the position
int left = ( m_AppRect.left + m_AppRect.right ) / 2;
int up = ( m_AppRect.top + m_AppRect.bottom ) / 2;
SetWindowPos( m_hDialogWnd, nullptr, left, up, 0, 0, SWP_NOSIZE );
ShowWindow( m_hDialogWnd, SW_SHOW );
// Get the progress bar
m_hProgressWnd = GetDlgItem( m_hDialogWnd, IDC_PROGRESSBAR );
SendMessage( m_hProgressWnd, PBM_SETRANGE, 0, MAKELPARAM( 0, 100 ) );
// Update the static text
HWND hMessage = GetDlgItem( m_hDialogWnd, IDC_MESSAGE );
SetWindowText( hMessage, m_szText );
return true;
}
bool ShowDialog( WCHAR* pszInputText )
{
if ( !DXUTIsWindowed() )
return false;
// Get the window rect
GetWindowRect( DXUTGetHWND(), &m_AppRect );
wcscpy_s( m_szText, MAX_PATH, pszInputText );
// spawn a thread that does nothing but update the progress bar
unsigned int threadAddr;
m_hThread = (HANDLE)_beginthreadex( nullptr, 0, WaitThread, this, 0, &threadAddr );
return true;
}
void DestroyDialog()
{
m_bDone = true;
if ( !DXUTIsWindowed() )
return;
WaitForSingleObject( m_hThread, INFINITE );
if( m_hDialogWnd )
DestroyWindow( m_hDialogWnd );
m_hDialogWnd = nullptr;
if ( IsWindow( DXUTGetHWND() ) )
SetForegroundWindow( DXUTGetHWND() );
}
};
//--------------------------------------------------------------------------------------
INT_PTR CALLBACK WaitDialogProc( HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam )
{
UNREFERENCED_PARAMETER(wParam);
UNREFERENCED_PARAMETER(lParam);
auto pThisDialog = reinterpret_cast<CWaitDlg*>( GetWindowLongPtr( hwndDlg, GWLP_USERDATA ) );
switch( uMsg )
{
case WM_INITDIALOG:
return TRUE;
case WM_CLOSE:
pThisDialog->DestroyDialog();
return TRUE;
}
return FALSE;
}
//--------------------------------------------------------------------------------------
unsigned int __stdcall WaitThread( void* pArg )
{
auto pThisDialog = reinterpret_cast<CWaitDlg*>( pArg );
// We create the dialog in this thread, so we can call SendMessage without blocking on the
// main thread's message pump
pThisDialog->GetDialogControls();
while( pThisDialog->IsRunning() )
{
pThisDialog->UpdateProgressBar();
Sleep(100);
}
return 0;
}