|
CControlBar is a derived class from CWnd. You can apply any function related to CWnd on it.
|
|
|
|
|
I tried calling SetWindowPos, but it didn't work???
How do I print my voice mail?
|
|
|
|
|
Hi all, ibe been coding for over a year now, and ibe never actually stopped to think about this (duh).
if i need to define a constant value like a string, a number, etc. for my app
should i:
#define MY_STRING _T("this is a string")
#define MY_NUMBER 100
or:
const TCHAR* MY_STRING = _T("this is a string");
const int MY_NUMBER = 100;
and why?
needless to say, both approaches work fine, and most code ibe seen uses the #define approach, however, a book i have (Beginning Visual C++ by Ivor Horton) sais that i should use the "const" approach, and that the #define one is obsolete and should be avoided (doesnt mention why tho)
Thanks!
|
|
|
|
|
You should not consider to use #define to create a constant.
#define define a macro. That mean, it will be used by preprocessor. There is not type checking here. The identifier, that appear after define keyword, exists only during preprocessing.
const define a constant. That mean, it will be used by compiler. Therefore, compiler do alot base on that define.
|
|
|
|
|
1) Regarding to the very purpose in your question, the main difference is ~ scope. A define lasts its definition till the end of the compilation module; but a const literal only lives within its scope. For example, try to compile the code below:
void Foo()
{
#define MAX_SIZE 192
}
bool Bar()
{
#define MAX_SIZE 512 // Oops!
int iSize = MAX_SIZE;
}
When your program is very large with a lot of source files, and the define(s) hide somewhere very deep in some header file(s) included by the other header files, you would find it annoying, because it needs to undef and ......
2) There are some other reasons about avoiding macros (not related to your question). Please read Section 7.8 - Macro in the book The C++ Programming Language, 3rd by Stroustrup.
"Macros are very important in C but have far fewer uses in C++. The first rule about macros is: Don't use them unless you have to. Almost every macro demonstrates a flaw in the programming language, in the program, or in the programmer. Because they rearrange the program text before the compiler proper sees it, macros are also a major problem for many programming tools. ...... " ~ Stroustrup.
The section demonstrates many flaws!
Maxwell Chen
|
|
|
|
|
Good points, ill stick with const from now on.
Thanks
|
|
|
|
|
Be aware that const may allocate memory.
To say #define is obsolete is idiotic. It's a tool that is extremely useful. I prefer it not be used for strings, but on occasion it's the best solution for a problem.
Anyone who thinks he has a better idea of what's good for people than people do is a swine.
- P.J. O'Rourke
|
|
|
|
|
Ok this is a really dasic dialog program but I can't figure what I'm donig wrong?? The problem I am having is get the dialog title to change to the caption of the button pressed. Help!!
--------------------Configuration: Retry - Win32 Debug--------------------
Compiling resources...
Compiling...
StdAfx.cpp
Compiling...
Retry.cpp
c:\c++ projects\retry\retrydlg.h(18) : error C2061: syntax error : identifier 'UNIT'
c:\c++ projects\retry\retrydlg.h(19) : error C2146: syntax error : missing ';' before identifier 'm_nIDofLastButton'
c:\c++ projects\retry\retrydlg.h(19) : error C2501: 'UNIT' : missing storage-class or type specifiers
c:\c++ projects\retry\retrydlg.h(19) : error C2501: 'm_nIDofLastButton' : missing storage-class or type specifiers
RetryDlg.cpp
c:\c++ projects\retry\retrydlg.h(18) : error C2061: syntax error : identifier 'UNIT'
c:\c++ projects\retry\retrydlg.h(19) : error C2146: syntax error : missing ';' before identifier 'm_nIDofLastButton'
c:\c++ projects\retry\retrydlg.h(19) : error C2501: 'UNIT' : missing storage-class or type specifiers
c:\c++ projects\retry\retrydlg.h(19) : error C2501: 'm_nIDofLastButton' : missing storage-class or type specifiers
C:\C++ Projects\Retry\RetryDlg.cpp(208) : error C2660: 'ChangeDialogTitle' : function does not take 1 parameters
C:\C++ Projects\Retry\RetryDlg.cpp(213) : error C2660: 'ChangeDialogTitle' : function does not take 1 parameters
C:\C++ Projects\Retry\RetryDlg.cpp(218) : error C2660: 'ChangeDialogTitle' : function does not take 1 parameters
C:\C++ Projects\Retry\RetryDlg.cpp(221) : error C2065: 'UNIT' : undeclared identifier
C:\C++ Projects\Retry\RetryDlg.cpp(221) : error C2146: syntax error : missing ')' before identifier 'nID'
C:\C++ Projects\Retry\RetryDlg.cpp(221) : error C2182: 'ChangeDialogTitle' : illegal use of type 'void'
C:\C++ Projects\Retry\RetryDlg.cpp(221) : error C2350: 'CRetryDlg::ChangeDialogTitle' is not a static member
C:\C++ Projects\Retry\RetryDlg.cpp(221) : error C2059: syntax error : ')'
C:\C++ Projects\Retry\RetryDlg.cpp(222) : error C2143: syntax error : missing ';' before '{'
C:\C++ Projects\Retry\RetryDlg.cpp(222) : error C2447: missing function header (old-style formal list?)
Generating Code...
Error executing cl.exe.
Retry.exe - 18 error(s), 0 warning(s)
// RetryDlg.h : header file
//
#if !defined(AFX_RETRYDLG_H__4A3C71B3_E316_48A5_B38E_7F3E778D47DD__INCLUDED_)
#define AFX_RETRYDLG_H__4A3C71B3_E316_48A5_B38E_7F3E778D47DD__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
/////////////////////////////////////////////////////////////////////////////
// CRetryDlg dialog
class CRetryDlg : public CDialog
{
// Construction
public:
// Trouble Rigth Here Line 18
void ChangeDialogTitle(UNIT nID);
// Trouble Rigth Here Line 19
UNIT m_nIDofLastButton;
CRetryDlg(CWnd* pParent = NULL); // standard constructor
// Dialog Data
//{{AFX_DATA(CRetryDlg)
enum { IDD = IDD_RETRY_DIALOG };
// NOTE: the ClassWizard will add data members here
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CRetryDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
HICON m_hIcon;
// Generated message map functions
//{{AFX_MSG(CRetryDlg)
virtual BOOL OnInitDialog();
afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
afx_msg void OnPaint();
afx_msg HCURSOR OnQueryDragIcon();
afx_msg void OnSh();
afx_msg void OnEd();
afx_msg void OnL();
afx_msg void OnC();
afx_msg void OnR();
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.
#endif // !defined(AFX_RETRYDLG_H__4A3C71B3_E316_48A5_B38E_7F3E778D47DD__INCLUDED_)
// RetryDlg.cpp : implementation file
//
#include "stdafx.h"
#include "Retry.h"
#include "RetryDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
afx_msg void OnPlus();
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
ON_BN_CLICKED(IDC_Plus, OnPlus)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CRetryDlg dialog
CRetryDlg::CRetryDlg(CWnd* pParent /*=NULL*/)
: CDialog(CRetryDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CRetryDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CRetryDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CRetryDlg)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CRetryDlg, CDialog)
//{{AFX_MSG_MAP(CRetryDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_SH, OnSh)
ON_BN_CLICKED(IDC_ED, OnEd)
ON_BN_CLICKED(IDC_L, OnL)
ON_BN_CLICKED(IDC_C, OnC)
ON_BN_CLICKED(IDC_R, OnR)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CRetryDlg message handlers
BOOL CRetryDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
return TRUE; // return TRUE unless you set the focus to a control
}
void CRetryDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CRetryDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CRetryDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CRetryDlg::OnSh()
{
BOOL bVisible = GetDlgItem(IDC_L) ->IsWindowVisible();
GetDlgItem(IDC_L) ->ShowWindow(bVisible? SW_HIDE : SW_SHOW);
GetDlgItem(IDC_C) ->ShowWindow(bVisible? SW_HIDE : SW_SHOW);
GetDlgItem(IDC_R) ->ShowWindow(bVisible? SW_HIDE : SW_SHOW);
GetDlgItem(IDC_SH) ->SetWindowText(bVisible? "SHOW" : "HIDE");
}
void CRetryDlg::OnEd()
{
BOOL bState = GetDlgItem(IDC_L) ->IsWindowEnabled();
GetDlgItem(IDC_L) ->EnableWindow(!bState);
GetDlgItem(IDC_C) ->EnableWindow(!bState);
GetDlgItem(IDC_R) ->EnableWindow(!bState);
GetDlgItem(IDC_ED) ->SetWindowText(bState? "ENABLE" : "DISABLE");
}
void CAboutDlg::OnPlus()
{
MessageBox ("Please Register Your Shareware", "!!!!!!");
}
void CRetryDlg::OnL()
{
ChangeDialogTitle(IDC_L);
}
void CRetryDlg::OnC()
{
ChangeDialogTitle(IDC_C);
}
void CRetryDlg::OnR()
{
ChangeDialogTitle(IDC_R);
}
void CRetryDlg::ChangeDialogTitle(UNIT nID)
{
CString strCaption;
GetDlgItem(nID) ->GetWindowText(strCaption);
strCaption += " was pressed";
if(m_nIDofLastButton == nID)
strCaption += " again";
SetWindowText(strCaption);
|
|
|
|
|
---Mark---- wrote:
c:\c++ projects\retry\retrydlg.h(18) : error C2061: syntax error : identifier 'UNIT'
Mark, open the file, retrydlg.h, and correct the typename 'UNIT' with 'UINT'.
Maxwell Chen
|
|
|
|
|
Thanks!!!!
|
|
|
|
|
I am a noob and that wasn kind of a lame mistake
But how would I in the future narrow down and single out my misspelling
|
|
|
|
|
---Mark--- wrote:
But how would I in the future narrow down and single out my misspelling
When we are fixing the compilation errors, the 1st error is the most important one we have to look at. Basically the rest hundrads would probably be gone after we have fixed the first.
Regarding to the misspellings, hummm.... The only way, I guess, is to take a rest and have a cup of tea. Later you will see more clearer!
Maxwell Chen
|
|
|
|
|
|
Hello all,
I used to program Unix(C/C++) and now trying to learn program in Windows. I would appreciate if someone could kindly recommend me some resources, books, URLs, for learning Windows programming. I am espeacilly interested in system and network programming, and device driver development. These are the area I used to do when I programmed Unix.
Thank you very much.
I can be reached at vnm6@lycos.com
I am a Unix programmer and trying to learn Windows now.
Ken Ji
|
|
|
|
|
Try Programming Windows by Charles Petzold, Ms Press (C++)
Chris Maunder has a list of recommended books in one of his article. Do check on that also.
Sonork 100.41263:Anthony_Yio
Life is about experiencing ...
|
|
|
|
|
Thank you Anthony for your info.
|
|
|
|
|
Hello I was trying to debug this code (see below), because after a few minutes it crashes.
So I used a try{ }catch(...) to catch the exception but I dont know what type of exception it was. Is there a way to tell?
I tried using TRY{} CATCH_ALL() but it dod not catch anything??
try{<br />
DeleteObject(hbr);
hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);<br />
if (nCtlColor == CTLCOLOR_STATIC )<br />
{<br />
CWnd *Ctrl = GetDlgItem(pWnd->GetDlgCtrlID());<br />
<br />
if (Ctrl)<br />
{ <br />
CRect Rect; <br />
Ctrl->GetWindowRect(&Rect);<br />
this->ScreenToClient(&Rect);<br />
COLORREF Clr = GetDC()->GetPixel(Rect.left-1, Rect.top-1);<br />
hbr = CreateSolidBrush(Clr);<br />
ASSERT(pDC);<br />
if(!m_staticTextBgd)<br />
pDC->SetBkColor(Clr);<br />
pDC->SetTextColor(RGB(m_fntR,m_fntG,m_fntB));<br />
pDC->SetBkMode(m_staticTextBgd?OPAQUE:TRANSPARENT);<br />
}<br />
}<br />
}<br />
catch(...)<br />
{<br />
printf("Error");<br />
}<br />
I tried catching CResourceException and CMemoryException and many others but it did not catch anything?
How do I know what exception is being thrown?
---
|
|
|
|
|
Try catching a CException, that might work. If it does get the error message and it might help you figure out what type of exception is being thrown.
- Aaron
|
|
|
|
|
First thing I tried, it seems it does not throw a CException at all.
I am sure it is a memory resource overflow problem, even though I am deleting my HBRUSH object.
---
|
|
|
|
|
Appart from not knowing how to get infomation from an unknown exception, I have fixed my code problem.
COLORREF Clr = GetDC()->GetPixel(Rect.left-1, Rect.top-1);
This line does not like being called so often.(why??) So I just set Clr to the background color as it actually does not change after initilising it.
Clr = RGB(m_bkR, m_bkG, m_bkB);
cheers
---
|
|
|
|
|
There are a limited number of device contexts available at any one time. Every time this executes, you are locking one of these DCs with GetDC() but not releasing it with ReleaseDC() . Eventually, there will be no DCs available, and GetDC() will return NULL . Since you're not doing any error checking, you'll get a NULL-pointer exception while trying to call GetPixel() .
Ryan "Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"
|
|
|
|
|
Two questions:
One, can interface methods have default parameters? i.e.
HRESULT MyMethod ([in] int size = 0) Two, can a class that implements and interface have a constructor with paramaters?
Thanks!
- Aaron
|
|
|
|
|
hi
One - Yes, An interface method can have parameters with default values provided the parameter shud be optional too. The parameter settings while creating the function defenition in IDL is as fllows:
[in] short i, [in] short j, [in, out, optional, defaultvalue(117)] short* k, [out, retval] short* l
and the c++ function signature will be:
STDMETHODIMP MyClass::Test(short i, short j, short *k, short *l)
Two - A class that implimets an interface can have constructor with parameters. But no way to use that . If you use CoCreateInstance to create the client then we can't use the c++ class name directly and so cunstructor parameter also. Only way where we can use the c++ class name to create the com class is CComObject<MyClass> . But even this CComObject does not accept constructor initializers.
rgds...mil10
|
|
|
|
|
In a support library I'm developing which is serving as the foundation of an application I'm working on, I've provided a whole bunch of augmented debug functionality. For example, if I hit an assert, the replacement assert implementation logs this sort of thing to a file and to OutputDebugString:
---- ASSERT LOG OPENED ----
Version 0.7.4.1
assertion failure 5/12/04, 16:49:42.353738
File: btlocalclient.cpp
Line: 1086
Expression: Unrecognized command in BTLocalClient_t::AsynchMessageReceived
Call stack: 0x00417B93 (BTLocalClient_t::AsynchMessageReceived +0x542, btlocalclient.cpp line 1086)
0x004F15D1 (AsynchMessageTrigger_t::Triggered +0x60, asynchmessagetrigger.cpp line 93)
0x004F6E88 (EventServer_t::ThreadFunction +0x387, eventserver.cpp line 284)
0x0042F8BF (BTServer_t::ThreadFunction +0x2E, btserver.cpp line 93)
0x004F6A01 (EventServer_t::ThreadFunctionEntryPoint +0x70, eventserver.cpp line 174)
0x1020BFD2 (UNKNOWN)
0x7C57B382 (UNKNOWN)
I'm providing that detail so no one asks "why are you replacing assert" - the utility should be clear and MS should have done a better job of providing this sort of info.
Anyway... One part of this support library is an EventServer object running in a thread, servicing EventTrigger objects by WaitForMultipleObjects - EventTriggers have an associated triggler HANDLE of one kind or another - could be an event, could be a TCP/IP socket, could be a device handle doing asynchronous I/O, that sort of thing. The server thread is servicing a remote client server and device objects controlling Bluetooth ASICs. An EventServer running at critical priority is a necessary part of the application I'm working on. Anyway every once in a while during the development process I introduce a bug where the code gets stuck in a loop. At critical priority, that brings my system to its knees and depending on how bad it is, I can't get over to Visual Studio to hit the pause button on the debugger. So I just added a feature to the event server where, optionally, another thread watches it and if the event server stops punching a watchdog (to borrow a metaphor from hardware engineering) then the watcher thread suspends the event server and pops up a dialog indicating that the event server stalled, abort retry ignore, all that.
I caught the case where the event server has hit an assert and is therefore legitimately not punching the watchdog...assert puts the watchdog to sleep. But what if I set a breakpoint? Once I start stepping through code, the watcher thread fires almost immediately.
What I wanted to do was catch EXCEPTION_BREAKPOINT and put the watchdog to sleep in that situation. I already have a __try exception handler so that address exceptions for example get turned into asserts and log the info shown up above, the call stack and everything. I augmented it as follows:
int
ExceptionFilter( unsigned int code, struct _EXCEPTION_POINTERS *ep )
{
g_exception_context = ep->ContextRecord;
if(code == EXCEPTION_ACCESS_VIOLATION)
release_error("Access violation");
else if(code == EXCEPTION_INT_DIVIDE_BY_ZERO)
release_error("Integer divide by zero");
else if(code == EXCEPTION_PRIV_INSTRUCTION)
release_error("Privileged instruction");
else if(code == EXCEPTION_ILLEGAL_INSTRUCTION)
release_error("Illegal instruction");
else if(code == EXCEPTION_NONCONTINUABLE_EXCEPTION)
release_error("Noncontinuable exception");
else if(code == EXCEPTION_GUARD_PAGE)
release_error("Guard page violation");
ADD else if(code == EXCEPTION_BREAKPOINT)
ADD {
ADD debug_printf("Why isn't this working???\n");
ADD EventServer_t::PauseCurrentThreadStallWatcher();
ADD }
g_exception_context = NULL;
return EXCEPTION_CONTINUE_SEARCH;
}
But it didn't work...EXCEPTION_BREAKPOINT isn't raised, or at least isn't able to be caught using __try.
Does anyone know what EXCEPTION_BREAKPOINT is for, will it work for what I'm trying to do if I do something a bit different?
Alternatively, does anyone know of a way that the watcher thread can determine whether the debugger has been active recently, like having just set a breakpoint and released the debuggee to do a "step out of function"? IsDebuggerPresent() isn't good enough because that returns TRUE whenever I'm in the debugger, that says nothing as to whether I'm stepping through code.
Thanks,
Brian
|
|
|
|
|
Breakpoint exceptions are not raised when the debugger is single-stepping. Try checking for EXCEPTION_SINGLE_STEP instead. If that doesn't work, I'm as stumped as you are...
Ryan "Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"
|
|
|
|
|