Board index » Visual Studio » Why I couldn't use this function?

Why I couldn't use this function?

Visual Studio49
Hello all:

I create a single document,and a modelless dialog is called by menu,look:

void CSampleView::OnTest()

if(!D11.m_hWnd)

{

D11.Create(IDD_D11,this);

D11.ShowWindow(SW_SHOW);

}

and now I want to get the printer of dialog,look:

void CSampleView::OnLButtonDown(UINT nFlags, CPoint point)

{

CD11 *p=(CD11*)this->GetDlgItem(IDD_D11);

if(p) MessageBox("you");

else MessageBox("null");

}

but it always display "null",

I know I can use the variable "D11" to get the dialog's function and

member,but I want to know why I couldn't use this function GetDlgItem?

(Maybe I want to get the pointer in class CMainFrame or other class,should I

do)

Thank you very much.


-
 

Re:Why I couldn't use this function?

Lee Tow wrote:

Quote
Hello all:

I create a single document,and a modelless dialog is called by menu,look:

void CSampleView::OnTest()

if(!D11.m_hWnd)

{

D11.Create(IDD_D11,this);

D11.ShowWindow(SW_SHOW);

}

and now I want to get the printer of dialog,look:

void CSampleView::OnLButtonDown(UINT nFlags, CPoint point)

{

CD11 *p=(CD11*)this->GetDlgItem(IDD_D11);

if(p) MessageBox("you");

else MessageBox("null");

}

but it always display "null",

I know I can use the variable "D11" to get the dialog's function and

member,but I want to know why I couldn't use this function GetDlgItem?

(Maybe I want to get the pointer in class CMainFrame or other class,should I

do)

Thank you very much.







When it returns NULL you can call GetLastError for additional

information. Maybe IDD_D11 does not have a unique value?



But your reasons for using GetDlgItem, even from CMainFrame or another

class, are not valid. GetDlgItem can only work from the child's parent

window, not from other classes. Since you already have an object

attached to the child window, using anything other than that object to

access the window is poor coding and obscures whatever you are trying to do.



--

Scott McPhillips [VC++ MVP]



-

Re:Why I couldn't use this function?

I use function GetLastError(),look:

DWORD dd=GetLastError();

LPVOID lpMsgBuf;

char *ErrMsg;

FormatMessage(

FORMAT_MESSAGE_ALLOCATE_BUFFER |

FORMAT_MESSAGE_FROM_SYSTEM |

FORMAT_MESSAGE_IGNORE_INSERTS,

NULL,

dd,

MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),

(LPTSTR) &lpMsgBuf,

0,

NULL

);

ErrMsg=(char*)&lpMsgBuf;

LocalFree(lpMsgBuf);

MessageBox(ErrMsg);

but don't display error information,why?



"Scott McPhillips [MVP]" <org-dot-mvps-at-scottmcp>????

Quote
Lee Tow wrote:

>Hello all:

>I create a single document,and a modelless dialog is called by

menu,look:

>void CSampleView::OnTest()

>if(!D11.m_hWnd)

>{

>D11.Create(IDD_D11,this);

>D11.ShowWindow(SW_SHOW);

>}

>and now I want to get the printer of dialog,look:

>void CSampleView::OnLButtonDown(UINT nFlags, CPoint point)

>{

>CD11 *p=(CD11*)this->GetDlgItem(IDD_D11);

>if(p) MessageBox("you");

>else MessageBox("null");

>}

>but it always display "null",

>I know I can use the variable "D11" to get the dialog's function and

>member,but I want to know why I couldn't use this function GetDlgItem?

>(Maybe I want to get the pointer in class CMainFrame or other

class,should I

>do)

>Thank you very much.

>

>



When it returns NULL you can call GetLastError for additional

information. Maybe IDD_D11 does not have a unique value?



But your reasons for using GetDlgItem, even from CMainFrame or another

class, are not valid. GetDlgItem can only work from the child's parent

window, not from other classes. Since you already have an object

attached to the child window, using anything other than that object to

access the window is poor coding and obscures whatever you are trying to

do.



--

Scott McPhillips [VC++ MVP]







-

Re:Why I couldn't use this function?

Look,

{

DWORD dd=GetLastError();

CString ss;

ss.Format("%d",dd);

MessageBox(ss);

}

I find the error ID is 0.

"Scott McPhillips [MVP]" <org-dot-mvps-at-scottmcp>????

Quote
Lee Tow wrote:

>Hello all:

>I create a single document,and a modelless dialog is called by

menu,look:

>void CSampleView::OnTest()

>if(!D11.m_hWnd)

>{

>D11.Create(IDD_D11,this);

>D11.ShowWindow(SW_SHOW);

>}

>and now I want to get the printer of dialog,look:

>void CSampleView::OnLButtonDown(UINT nFlags, CPoint point)

>{

>CD11 *p=(CD11*)this->GetDlgItem(IDD_D11);

>if(p) MessageBox("you");

>else MessageBox("null");

>}

>but it always display "null",

>I know I can use the variable "D11" to get the dialog's function and

>member,but I want to know why I couldn't use this function GetDlgItem?

>(Maybe I want to get the pointer in class CMainFrame or other

class,should I

>do)

>Thank you very much.

>

>



When it returns NULL you can call GetLastError for additional

information. Maybe IDD_D11 does not have a unique value?



But your reasons for using GetDlgItem, even from CMainFrame or another

class, are not valid. GetDlgItem can only work from the child's parent

window, not from other classes. Since you already have an object

attached to the child window, using anything other than that object to

access the window is poor coding and obscures whatever you are trying to

do.



--

Scott McPhillips [VC++ MVP]







-

Re:Why I couldn't use this function?

if you want to get the pointer to the modeless dialog instead of

calling the GetDlgItem you can use the following code



CD11 *p=(CD11*) &D11;



Thanks,

Chary.



-

Re:Why I couldn't use this function?

But I want to know why I could not use the function GetDlgItem?

Thanks.

<s.s.chary@gmail.com>????

Quote
if you want to get the pointer to the modeless dialog instead of

calling the GetDlgItem you can use the following code



CD11 *p=(CD11*) &D11;



Thanks,

Chary.







-

Re:Why I couldn't use this function?

Lee Tow wrote:



Quote
Hello all:

I create a single document,and a modelless dialog is called by menu,look:

void CSampleView::OnTest()

if(!D11.m_hWnd)

{

D11.Create(IDD_D11,this);

D11.ShowWindow(SW_SHOW);

}

and now I want to get the printer of dialog,look:

void CSampleView::OnLButtonDown(UINT nFlags, CPoint point)

{

CD11 *p=(CD11*)this->GetDlgItem(IDD_D11);

if(p) MessageBox("you");

else MessageBox("null");

}

but it always display "null",

I know I can use the variable "D11" to get the dialog's function and

member,but I want to know why I couldn't use this function GetDlgItem?

(Maybe I want to get the pointer in class CMainFrame or other class,should I

do)

Thank you very much.







Lee Tow:



I think IDD_D11 is used only to identify the template, not as the child

ID of the created dialog. Try calling



int nTest = D11.GetDlgCtrlID();



after creating the dialog. Is it equal to IDD_D11 ? I don't think so.



David Wilkinson



-

Re:Why I couldn't use this function?

I insert resoruce and select Dialog,and then I rename the dailog's

IDD_DIALOG1 to

IDD_D11,so I think IDD_D11 is only unique.

"David Wilkinson" <no-reply@effisols.com>????

Quote
Lee Tow wrote:



>Hello all:

>I create a single document,and a modelless dialog is called by

menu,look:

>void CSampleView::OnTest()

>if(!D11.m_hWnd)

>{

>D11.Create(IDD_D11,this);

>D11.ShowWindow(SW_SHOW);

>}

>and now I want to get the printer of dialog,look:

>void CSampleView::OnLButtonDown(UINT nFlags, CPoint point)

>{

>CD11 *p=(CD11*)this->GetDlgItem(IDD_D11);

>if(p) MessageBox("you");

>else MessageBox("null");

>}

>but it always display "null",

>I know I can use the variable "D11" to get the dialog's function and

>member,but I want to know why I couldn't use this function GetDlgItem?

>(Maybe I want to get the pointer in class CMainFrame or other

class,should I

>do)

>Thank you very much.

>

>



Lee Tow:



I think IDD_D11 is used only to identify the template, not as the child

ID of the created dialog. Try calling



int nTest = D11.GetDlgCtrlID();



after creating the dialog. Is it equal to IDD_D11 ? I don't think so.



David Wilkinson







-

Re:Why I couldn't use this function?

Lee Tow wrote:



Quote
I insert resoruce and select Dialog,and then I rename the dailog's

IDD_DIALOG1 to

IDD_D11,so I think IDD_D11 is only unique.



Lee Tow:



What I said has nothing to do with the template ID being unique. Rather

it has to do with the template ID not being used as the child ID. Did

you try what I suggested in order to check?



David Wilkinson

-

Re:Why I couldn't use this function?

The ID that you pass to GetDlgItem has to be the control's id, not the

resource ID of the dialog template.

You are passing in the Resource ID of the dialog to find a control. If

things worked the way you are hoping it would, what would happen if you were

to have two IDD_D11 dialogs in the same parent dialog?



AliR.





"Lee Tow" <fbjlt@pub3.fz.fj.cn>wrote in message

Quote
But I want to know why I could not use the function GetDlgItem?

Thanks.

<s.s.chary@gmail.com>????

news:1140784953.505262.156750@i39g2000cwa.googlegroups.com...

>if you want to get the pointer to the modeless dialog instead of

>calling the GetDlgItem you can use the following code

>

>CD11 *p=(CD11*) &D11;

>

>Thanks,

>Chary.

>









-

Re:Why I couldn't use this function?



"Lee Tow" <fbjlt@pub3.fz.fj.cn>wrote in message

Quote
ErrMsg=(char*)&lpMsgBuf;

LocalFree(lpMsgBuf);

MessageBox(ErrMsg);

but don't display error information,why?





1.You're passing wrong address to MessageBox.

2. You're deallocating the string before you want to display the string

contents.



ErrMsg=(LPCTSTR)lpMsgBuf;

MessageBox(ErrMsg);

LocalFree(lpMsgBuf);







-