How do you output a Variable?

I want to see the values that are being passed...Just for my own curiosity I would like to see what the value for WM_CREATE is...I would like to output that to the screen.....

for command line c++ for example cout << WM_CREATE << endl;

What is windows equivalent?

Here is my winProc

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
WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	HDC hdc;
	PAINTSTRUCT ps;
	RECT rect;

	switch (message)
	{
	case WM_CREATE:
		
		
		return 0;
	case WM_PAINT:
		hdc = BeginPaint(hwnd, &ps);
		GetClientRect(hwnd, &rect);
		DrawText(hdc, TEXT("Hello, Windows 7!"), -1, &rect, DT_SINGLELINE | DT_CENTER | DT_VCENTER);
		EndPaint(hwnd, &ps);
		return 0;
	case WM_DESTROY:
		PostQuitMessage(0);
		return 0;
	}

	return DefWindowProc(hwnd, message, wParam,lParam);
}
Last edited on
Two ways to go about it Novellof.

1) Open an output/debug text file and use whatever functions you use to output to that - likely for you something from iostream, but I use old fashioned stdio;

2) Open a console window using AllocConsole() in your WM_CREATE handler and write your output to the console screen in the same manner as #1 above.

If you want I can demo both of these techniques for you.

Fred
Thanks for the reply but is there anyway to use the DrawText function to display these variables...like use draw text as a cout << var << endl;
You'll need to convert the variable to a string. If you are using C++11 then you'll have access to the to_string functions:

1
2
3
4
5
6
7
8
// Note:  I use DrawTextA instead of DrawText because I'm using a normal string and not a
//    TCHAR string.
std::string s = std::to_string(WM_CREATE);
DrawTextA( hdc, s.c_str(), ...

// ...or...  cram it into one line:

DrawTextA( hdc, std::to_string(WM_CREATE).c_str(), ...



If you do not have C++11 available and you do not want to update your super-old outdated compiler for whatever reason... you can use stringstreams:

1
2
3
4
5
#include <sstream>

std::stringstream s;
s << WM_CREATE;
DrawTextA( hdc, s.str().c_str(), ...


or C-style sprintf:

1
2
3
4
5
#include <cstdio>

char buffer[20];
sprintf(buffer, "%d", WM_CREATE);
DrawTextA( hdc, buffer, ...
Thanks Disch...is there a way to just print the number itself without conversion? ....looks like sprintf does that prints out as a digit?
Last edited on
Thanks Disch...is there a way to just print the number itself without conversion?


No.

Numbers cannot be printed. Only text can be printed.

The only reason it works with cout is because ostream automatically does the conversion behind the scenes for you. DrawText has no such functionality built into it -- so you have to do it yourself.

....looks like sprintf does that prints out as a digit?


For the record... sprintf was listed last for a reason -- it is my least favorite suggestion of all of those. =P


But what do you mean by "prints out as a digit"? Isn't that what you want?
thanks man

DrawTextA( hdc, std::to_string(WM_CREATE).c_str(), ...



worked beautifully!!

so what happens is wm_create gets turned into a string...then turned into a character string with c_str()?

can you please explain how that works?

Why cant DT_VCENTER work by itself? Why does it need another DT_SINGLELINE Flag?
Last edited on
so what happens is wm_create gets turned into a string...then turned into a character string with c_str()?

can you please explain how that works?



Your summary is correct. to_string takes the given value and formats it into a string. It then returns the std::string.

c_str() returns a pointer to the data within that std::string. c_str is available for this very reason: ability to use std::string in libraries that use c-style strings.

Why cant DT_VCENTER work by itself? Why does it need another DT_SINGLELINE Flag?


I don't have an answer other than "Because that's what DrawText requires".

If you read the reference page, it says that explicitly:

http://msdn.microsoft.com/en-us/library/windows/desktop/dd162498%28v=vs.85%29.aspx
That page wrote:
DT_VCENTER Centers text vertically. This value is used only with the DT_SINGLELINE value.
Really, if you are wanting to learn Windows Programming, DrawText(), TextOut(), and several other variants are useful and wonderful to know, but your original question seemed to concern debugging and learning how to determine the contents of your variables. For that, those GUI output routines mentioned above are not the most useful. Other than a stepping debugger, my original suggestions are still valid. The vast majority of your coding will involve Windows with various child window controls decorating them. In those situations DrawText() will be of no use. Here is the output you seem to be requesting ...

1
2
3
4
5
6
7
8
9
10
11
12
13
Entering WinMain()
  Entering fnWndProc_OnCreate()
    Wea.hWnd  = 0x0000000000140850
    WM_CREATE = 1
  Leaving fnWndProc_OnCreate()

  hWnd        = 0x0000000000140850
  szClassName = Form1

  Entering fnWndProc_OnDestroy()
    Wea.hWnd = 0x0000000000140850
  Leaving fnWndProc_OnDestroy()
Leaving WinMain()


And here is an example of a program that produces that ...

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
//Form1.cpp
#ifndef  UNICODE
#define  UNICODE
#endif
#ifndef  _UNICODE
#define  _UNICODE
#endif
#define  MyDebug
#include <windows.h>
#include <cstdio>
#include <tchar.h>
#include "Form1.h"
#ifdef MyDebug
FILE*    fp=NULL;
#endif


long fnWndProc_OnCreate(WndEventArgs& Wea)
{
 #ifdef MyDebug
 fprintf(fp,"  Entering fnWndProc_OnCreate()\n");
 fprintf(fp,"    Wea.hWnd  = 0x%p\n",Wea.hWnd);
 fprintf(fp,"    WM_CREATE = %d\n",WM_CREATE);
 #endif
 Wea.hIns=((LPCREATESTRUCT)Wea.lParam)->hInstance;
 #ifdef MyDebug
 fprintf(fp,"  Leaving fnWndProc_OnCreate()\n\n");
 #endif

 return 0;
}


long fnWndProc_OnDestroy(WndEventArgs& Wea)
{
 #ifdef MyDebug
 fprintf(fp,"  Entering fnWndProc_OnDestroy()\n");
 fprintf(fp,"    Wea.hWnd = 0x%p\n",Wea.hWnd);
 #endif
 PostQuitMessage(0);
 #ifdef MyDebug
 fprintf(fp,"  Leaving fnWndProc_OnDestroy()\n");
 #endif

 return 0;
}



LRESULT CALLBACK fnWndProc(HWND hwnd, unsigned int msg, WPARAM wParam, LPARAM lParam)
{
 WndEventArgs Wea;

 for(unsigned int i=0; i<dim(EventHandler); i++)
 {
     if(EventHandler[i].iMsg==msg)
     {
        Wea.hWnd=hwnd, Wea.lParam=lParam, Wea.wParam=wParam;
        return (EventHandler[i].fnPtr)(Wea);
     }
 }

 return (DefWindowProc(hwnd, msg, wParam, lParam));
}


int WINAPI WinMain(HINSTANCE hIns, HINSTANCE hPrevIns, LPSTR lpszArgument, int iShow)
{
 TCHAR szClassName[]=_T("Form1");
 WNDCLASSEX wc={};
 MSG messages;
 HWND hWnd;

 #ifdef MyDebug
 fp=fopen("Output.txt","w");
 if(fp)
    fprintf(fp,"Entering WinMain()\n");
 #endif
 wc.lpszClassName=szClassName;                wc.lpfnWndProc=fnWndProc;
 wc.cbSize=sizeof (WNDCLASSEX);               wc.hIcon=LoadIcon(NULL,IDI_APPLICATION);
 wc.hInstance=hIns,                           wc.hCursor=LoadCursor(NULL,IDC_ARROW);
 wc.hbrBackground=(HBRUSH)COLOR_BTNSHADOW;
 RegisterClassEx(&wc);
 hWnd=CreateWindowEx(0,szClassName,szClassName,WS_OVERLAPPEDWINDOW,75,75,320,305,HWND_DESKTOP,0,hIns,0);
 #ifdef MyDebug
 fprintf(fp,"  hWnd        = 0x%p\n",hWnd);
 _ftprintf(fp,_T("  szClassName = %S\n\n"),szClassName);
 #endif
 ShowWindow(hWnd,iShow);
 while(GetMessage(&messages,NULL,0,0))
 {
    TranslateMessage(&messages);
    DispatchMessage(&messages);
 }
 #ifdef MyDebug
 fprintf(fp,"Leaving WinMain()\n");
 fclose(fp);
 #endif

 return messages.wParam;
}


And Form1.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
//Main.h
#ifndef Form1_h
#define Form1_h

#define dim(x) (sizeof(x) / sizeof(x[0]))

struct WndEventArgs
{
 HWND                         hWnd;
 WPARAM                       wParam;
 LPARAM                       lParam;
 HINSTANCE                    hIns;
};

long fnWndProc_OnCreate       (WndEventArgs& Wea);
long fnWndProc_OnDestroy      (WndEventArgs& Wea);

struct EVENTHANDLER
{
 unsigned int                 iMsg;
 long                         (*fnPtr)(WndEventArgs&);
};

const EVENTHANDLER EventHandler[]=
{
 {WM_CREATE,                  fnWndProc_OnCreate},
 {WM_DESTROY,                 fnWndProc_OnDestroy}
};
#endif 
Topic archived. No new replies allowed.