bitmap to draw in a dialog windows



Hi guys , Im a total noob when it comes to windows programing ,and only have basic skills when it comes to C++, I have programed a few simple apps that ran in the dos console but thats about it below is my code.I'm using VS2010 to complie the code and would be happy to Email the project to anyone that would like to have a look.

right now Im trying to get this code working but Im having problems with drawing a bitmap in my Dialog window,when I run my app it draws the black background in the dialog window as it should but its also placing one on the desktop and then draws the green bars there and not in the dialog window ??,
below is most of my code but I had to cut out a few functions to be able to post it up here.

could someone please tell me what Im doing wrong as Ive been banging my head over this all weekend.


Thanks for reading and I hope someone can help.

Screen shot showing the error
http://img.photobucket.com/albums/v11/Dannymax/Screen_shot.jpg

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
#define _CRT_SECURE_NO_WARNINGS

#include <windows.h>
#include <stdio.h>
#include <math.h>
#include <malloc.h>
#include <new.h>
#include "main.h"
#include "bass.h"

HWND win=NULL;
HDC dc;
DWORD timer=0;
HFX fx[4];
BYTE *specbuf;
HINSTANCE TheInstance = 0;
OPENFILENAME ofn={0};
char file[MAX_PATH]="";
DWORD chan,level;
int specmode =  1; 
HDC specdc=0;
HBITMAP specbmp=0;
#define SPECWIDTH 730	// display width
#define SPECHEIGHT 110	// height (changing requires palette adjustments too)

int NewHandler (size_t size)
{
    throw WinException ( "Out of memory" );
    return 0;
}


// display error messages
void Error(const char *es)
{
	char mes[200];
	sprintf(mes,"%s\n(error code: %d)",es,BASS_ErrorGetCode());
	MessageBox(win,mes,0,0);
}

///////////////////////////
void CALLBACK UpdateSpectrum(UINT uTimerID, UINT uMsg, DWORD dwUser, DWORD dw1, DWORD dw2)
{
//	HDC dc;
	int x,y,y1;
	float fft[2048];
		
		BASS_ChannelGetData(chan,fft,BASS_DATA_FFT4096); // get the FFT data

			
			if (specmode==1) { // logarithmic, acumulate & average bins
			int b0=0;
			memset(specbuf,0,SPECWIDTH*SPECHEIGHT);

#define BANDS 26
			for (x=0;x<BANDS;x++) {
				float peak=0;
				int b1=pow(2,x*10.0/(BANDS-1));
				if (b1>1023) b1=1023;
				if (b1<=b0) b1=b0+1; // make sure it uses at least 1 FFT bin
				for (;b0<b1;b0++)
					if (peak<fft[1+b0]) peak=fft[1+b0];
				y=sqrt(peak)*3*SPECHEIGHT-4; // scale it (sqrt to make low values more visible)
				if (y>SPECHEIGHT) y=SPECHEIGHT; // cap it
				while (--y>=0)
					memset(specbuf+y*SPECWIDTH+x*(SPECWIDTH/BANDS),y+1,SPECWIDTH/BANDS-2); // draw bar
			} 
	}
		
	

	// update the display
	dc=GetDC(win);
	BitBlt(dc,0,0,SPECWIDTH,SPECHEIGHT,specdc,0,0,SRCCOPY);
	ReleaseDC(win,dc);
}

Controller::Controller (HWND hwnd) {code cut out for space}

BOOL PlayFile(){code cut out for space}


void Controller::Command (HWND hwnd, int controlID, int command)
{
    char statusMessage[64];

    switch (controlID)

    {
	case WM_CREATE:
		
			// initialize BASS
			if (!BASS_Init(-1,44100,0,win,NULL)) {
				Error("Can't initialize device");
				
			}
			
			


        case IDC_EDIT:
            if (_edit.IsChanged(command))
            {
                if (_edit.GetLength())
                    _clear.Enable();
                else
                    _clear.Disable();
            }
            break;
        case IDC_CLEAR:
            _edit.SetString("");
            _clear.Disable();
            _edit.SetFocus();
            _status.SetString("Edit field cleared.");
            break;
        case IDC_LEFT:
            if (command == BN_CLICKED)
            {
                strcpy(statusMessage, "Left checkbox clicked");
                if (_leftCheck.IsChecked())
                    strcat(statusMessage, ": CHECKED");
                _status.SetString(statusMessage);
            }
            break;
        case IDC_RIGHT:
            if (command == BN_CLICKED)
            {
                strcpy(statusMessage, "Right checkbox clicked");
                if (_rightCheck.IsChecked())
                    strcat(statusMessage, ": CHECKED");
                _status.SetString(statusMessage);
            }
            break;
        case IDC_TOP:
            if (command == BN_CLICKED)
                _status.SetString("Top radio button selected.");
            break;
        case IDC_MIDDLE:
            if (command == BN_CLICKED)
                _status.SetString("Middle radio button selected.");
            break;
        case IDC_BOTTOM:
            if (command == BN_CLICKED)
                _status.SetString("Bottom radio button selected.");
            break;
        case IDC_BUTTON1:
            if (command == BN_CLICKED)
                _status.SetString("Button1 clicked.");
			 BASS_Free();
			 PlayFile();
            break;
        case IDC_BUTTON2:
            if (command == BN_CLICKED)
            {
				BASS_ChannelPlay(chan,FALSE);
              
            }
            break;
        case IDC_BUTTON3:
            if (command == BN_CLICKED)
				
			BASS_ChannelStop(chan);
                _status.SetString("Button3 clicked.");
            break;
    }

}

BOOL CALLBACK DialogProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    static Controller* control = 0;
    switch (message)
    {
		/////////////////
		case WM_PAINT:
			if (GetUpdateRect(hwnd,0,0)) {
				PAINTSTRUCT p;
				HDC dc;
				if (!(dc=BeginPaint(hwnd,&p))) return 0;
				BitBlt(dc,0,0,SPECWIDTH,SPECHEIGHT,specdc,0,0,SRCCOPY);
				EndPaint(hwnd,&p);
			}
			return 0;		

    
    }
	{ // create bitmap to draw spectrum in (8 bit for easy updating)
				
				BYTE data[2000]={0};
				BITMAPINFOHEADER *bh=(BITMAPINFOHEADER*)data;
				RGBQUAD *pal=(RGBQUAD*)(data+sizeof(*bh));
				int a;
				bh->biSize=sizeof(*bh);
				bh->biWidth=SPECWIDTH;
				bh->biHeight=SPECHEIGHT; // upside down (line 0=bottom)
				bh->biPlanes=1;
				bh->biBitCount=8;
				bh->biClrUsed=bh->biClrImportant=256;
				// setup palette
				for (a=1;a<128;a++) {
					pal[a].rgbGreen=255;
					pal[a].rgbRed=0;
					pal[a].rgbBlue=0;
				}
			
				// create the bitmap
				specbmp=CreateDIBSection(0,(BITMAPINFO*)bh,DIB_RGB_COLORS,(void**)&specbuf,NULL,0);
				specdc=CreateCompatibleDC(0);
				SelectObject(specdc,specbmp);
			}
	
	timer=timeSetEvent(25,25,(LPTIMECALLBACK)&UpdateSpectrum,0,TIME_PERIODIC);
    return FALSE;
	
}

int WINAPI WinMain
   (HINSTANCE hInst, HINSTANCE hPrevInst, char * cmdParam, int cmdShow)
{	
    TheInstance = hInst;
    _set_new_handler (&NewHandler);

    HWND hDialog = 0;

    hDialog = CreateDialog (hInst, MAKEINTRESOURCE (DLG_MAIN), 0, (DLGPROC)DialogProc);
    if (!hDialog)
    {
        char buf [100];
        wsprintf (buf, "Error x%x", GetLastError ());
        MessageBox (0, buf, "CreateDialog", MB_ICONEXCLAMATION | MB_OK);
        return 1;
    }

    MSG  msg;
    int status;
    while ((status = GetMessage (&msg, 0, 0, 0)) != 0)
    {
        if (status == -1)
            return -1;
        if (!IsDialogMessage (hDialog, &msg))
        {
            TranslateMessage ( &msg );
            DispatchMessage ( &msg );

        }
    }

    return msg.wParam;




}
This point here where you update:
1
2
3
4
    // update the display
    dc=GetDC(win);
    BitBlt(dc,0,0,SPECWIDTH,SPECHEIGHT,specdc,0,0,SRCCOPY);
    ReleaseDC(win,dc);


At which point in the program do you set win to something other than NULL ??

dc=GetDC(win); with win having a value of NULL will get the
display context for the whole screen and you will end up painting on the desktop area.
Thanks for the reply guestgulkan , I will look into it , I know I need it to point to my dialog window but Im not sure how to go about it, everything I try gives me an error. so theres something Im not understanding here, this is what I added ...

HWND win= hDialog;

am I on the right track here ?
thanks again for your help
well win is a global variable (see line 11) so try inserting
win =hDialog at about line 234 - just after the creation of the dialog.
Thanks so much for your help guestgulkan its now working :), now I can move on to the next part of the code.
Topic archived. No new replies allowed.