|
|
|
Dear all..
How can I use GetDC() in a dialog from an MDI CView?
I mean, I have image data in CMyMDIView, and I want to show it in a dialog (CMyDialog).
I've tried this:
CMyDialog *pMyDialog = (CMyDialog*) AfxGetMainWnd();<br />
CDC *pDC = pMyDialog->m_StaticImage.GetDC();
but the second line doesn't work...
I also tried to call the function in CMyDialog that calls CDC from CMyMDIView, like this:
void MyMDIView::OnDraw(CDC* pDC)<br />
{<br />
CMyDialog *pMyDialog = (CMyDialog*) AfxGetMainWnd();<br />
pMyDialog->DoLoadImage();<br />
...<br />
}
where DoLoadImage() contains CDC *pDC = m_StaticImage.GetDC();
but it also doesn't work..
I really bad at these pointer things ..
Thank you in advance..
-Houari
|
|
|
|
|
There are (at least) three things worth pointing out here.
CMyDialog *pMyDialog = (CMyDialog*) AfxGetMainWnd();
This won't work because (I assume) your application's window is a mainframe window, not a dialog. Forcing the cast just tells the compiler to assume your main window is indeed a CMyDialog (which is why it compiles), but any attempt to treat the cast object as such will only cause you a lot of pain (because you've made an incorrect assumption about the structure of the dereferenced object).
CDC *pDC = pMyDialog->m_StaticImage.GetDC();
pMyDialog is really a pointer to your application's mainframe window, so trying to reference its m_StaticImage member's DC is going to hurt. A lot.
- You're better off defining a method in your view class that exposes pertinent data required by any class who wishes to display the image. The displaying class (i.e. an instance of
CMyDialog ) can be passed a pointer to the view class - or can be so bold as to cast its GetParent() to your view class (assuming the dialog is and will always be only displayed by that view class) - in order to get at the image data, which it can then display in a suitable control. /ravi
|
|
|
|
|
thank you mr ravi...
I just get your point about referencing from view class. so instead, I just use OnPaint() function in the CMyDialog to call the function to display the image, and it works fine..
thanks a lot...
-Houari
|
|
|
|
|
Excellent - good work!
/ravi
|
|
|
|
|
Can someone tell me how can I get registry keys values in C++(just to read it from the registry
not write it) and if possible to explain the steps because I've tried to search for this on the Internet and can't make sense of the information/source code provided there because no one explains it (ex:why is this instead of that).
|
|
|
|
|
|
This[^] handy dandy class should be of help.
/ravi
|
|
|
|
|
|
The problem is as follows: I had to make a program that allows the user to enter some text. Then, I had to allow the user to drag and drop the text over the window. Up to that, everything works. The problem is in changing the color of the text. When the user selects the new color, we can't see it until we move the text. What should I do if I wanted to change the color right away?
This is the part of my code:
CColorDialog colorDlg;
if (colorDlg.DoModal() == IDOK) // The user selected the "OK" button
{
m_crColor = colorDlg.GetColor();
m_bFirstTime = TRUE;
}
and
if (m_bFirstTime == TRUE)
{
pDC->SetTextColor(RGB(GetRValue(m_crColor), GetGValue(m_crColor),
GetBValue(m_crColor)));
}
else
pDC->SetTextColor(m_txtColor);
pDC->TextOut(pDoc->m_pos.x,pDoc->m_pos.y,pDoc->m_text);
Thanks
Alessandra
|
|
|
|
|
I don't think you need the m_bFirstTime flag. Just refreshing/repainting the controls should
do...
CColorDialog colorDlg;
if (colorDlg.DoModal() == IDOK) // The user selected the "OK" button
{
m_crColor = colorDlg.GetColor();
Invalidate(FALSE); // force repaint of child controls
}
|
|
|
|
|
OK, I'll try it.
Thanks a lot
Alessandra
|
|
|
|
|
Thanks again.
I tried it and it works
Can you please help me with one more thing? It's just that when I first open my program, the text should not move immediately, but only when the user clickes on it. But in my case it starts to follow the mouse as soon as the mouse gets on the window (not on the text). Do you know how can I solve it?
Thanks.
Alessandra
|
|
|
|
|
The code is as follows:
CSize ts;<br />
ts = pDC->GetTextExtent(pDoc->m_text);<br />
pDoc->m_textRectangle.left = ts.cy;<br />
pDoc->m_textRectangle.right = (pDoc->m_textRectangle.left + ts.cx);<br />
pDoc->m_textRectangle.bottom = ts.cx;<br />
pDoc->m_textRectangle.top = (pDoc->m_textRectangle.bottom - ts.cy);<br />
pDoc->m_textRectangle.MoveToXY (pDoc->m_pos.x, pDoc->m_pos.y);
and
if((pDoc->m_mouse_captured))<br />
{<br />
pDoc->m_pos = point;
Invalidate();<br />
}
Alessandra
|
|
|
|
|
Ha you beat me to it.
Do you want the user to be able to drag the text by clicking on the text or do you move the text
when the user moves the mouse over it?
When does pDoc->m_mouse_captured get set?
|
|
|
|
|
Ok, I want the user to click on the text and to drag it over the window. When he realeses the left mouse button, the text should stop moving.
Alessandra
|
|
|
|
|
You may be doing something similar already...
This is pseudocode for one way to handle it
Member variables:
CPoint BasePoint;
bool fInCapture;
CRect TextRect;
void CMyWnd::OnLButtonDown(UINT nFlags, CPoint point)
{
if TextRect.PtInRect(point)
{
BasePoint = point;
::SetCapture(*this);
fInCapture = true;
}
}
void CMyWnd::OnMouseMove(UINT nFlags, CPoint point)
{
if (fInCapture)
{
if (point != BasePoint)
{
TextRect.OffsetRect(point.x - BasePoint.x, point.y - BasePoint.y)
Invalidate(TRUE);
UpdateWindow();
BasePoint = point;
}
}
}
void CMyWnd::OnLButtonUp(UINT nFlags, CPoint point)
{
if (fInCapture)
{
fInCapture = false;
::ReleaseCapture();
}
}
|
|
|
|
|
I haven't seen the code Are you moving the text in response to WM_MOUSEMOVE messages?
|
|
|
|
|
And yes, I am moving the text in response to WM_MOUSEMOVE messages
Alessandra
|
|
|
|
|
Hey, I am part of a project which is being coded in Visual C++; however, I am a Java programmer. Thus I need some serious help (A fully written class would be wonderful!) with connecting to a URL via an HTTP connection, and then reading from the URL. The idea is to read from the URL in order to get a PHP script running.
I wrote a bit of code in Java to accomplish this task, but the project is being created in Visual C++.
Here is the java code I wrote:
<br />
public static void main(String[] args) {<br />
String key = "/*insert key here*/";<br />
try {<br />
URL myURL = new URL("http", "/*insert url here*/", "/*script location here*/" + key);
System.out.println(myURL);
URLConnection appendConnect = myURL.openConnection();<br />
BufferedReader in = new BufferedReader( <br />
new InputStreamReader(<br />
appendConnect.getInputStream()));<br />
in.close();
<br />
} catch (MalformedURLException e) {
System.err.println("LOL!");<br />
} catch (IOException e) {<br />
System.err.println("LOL!!");<br />
}<br />
}<br />
The key is just a random string of numbers and characters which is read by the PHP script on the server and compared against the script's key. The key can be combined with the URL if that makes it any simpler.
Help would be really wonderful!
|
|
|
|
|
|
http://www.codeproject.com/internet/amhttputils.asp
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
cheers,
Alok Gupta
VC Forum Q&A :- I/ IV
Support CRY- Child Relief and you
|
|
|
|
|
I have an assignment that requires me to code a petty cash program in C++. I did this assignment a while back when it was required to code it in python. Is there a simple way to convert a python code into C++. I managed to get the menu working. I am trying to get it to do the math needed to adjust the account.
Here is the python code that works:
<br />
class Account:<br />
def __init__(self, initial):<br />
self.balance = initial<br />
<br />
def deposit(self, amt):<br />
self.balance = self.balance + amt<br />
<br />
def withdraw(self, amt):<br />
self.balance = self.balance - amt<br />
if self.balance < 0:<br />
print 'Not enough here'<br />
else:<br />
return self.balance<br />
<br />
def getbalance(self):<br />
return self.balance<br />
<br />
<br />
from datetime import date<br />
now = date.today()<br />
a = Account(0)<br />
print 'Please select a number'<br />
print '1 Deposit'<br />
print '2 Withdraw'<br />
print '3 Inquiry'<br />
number = input('Please choose an option: ')<br />
while True:<br />
if number == 1:<br />
dep = input('Enter amount deposited: $')<br />
a.deposit(dep)<br />
print 'Deposit of $', dep, 'makes account balance $', a.getbalance(), 'as of', now<br />
elif number == 2:<br />
wit = input('Enter amount withdrawn: $')<br />
a.withdraw(wit)<br />
print 'Withdraw of $', wit, 'makes account balance $', a.getbalance(), 'as of', now<br />
elif number == 3:<br />
print 'Account balance is $', a.getbalance(), now<br />
<br />
else:<br />
break<br />
print<br />
number = int(raw_input('Do you want something else? or enter 4 to exit '))<br />
<br />
print 'Goodbye'<br />
this is what I have so far in C++:
<br />
#include <iostream><br />
#include <string><br />
#include <sstream><br />
using namespace std;<br />
<br />
void DisplayMenu();<br />
int GetSelection();<br />
<br />
int main()<br />
{<br />
int selection = 0;<br />
while (selection != 4) {<br />
DisplayMenu();<br />
selection = GetSelection();<br />
switch (selection){<br />
case 1:<br />
cout << "Deposit" << endl;<br />
break;<br />
case 2: <br />
cout << "Withdraw" << endl;<br />
break;<br />
case 3:<br />
cout << "Inquiry" << endl;<br />
break;<br />
case 4:<br />
cout << "Quitting" << endl;<br />
break;<br />
default:<br />
cout << "\aInvalid selection" << endl;<br />
break;<br />
}<br />
} <br />
return 0;<br />
}<br />
void DisplayMenu()<br />
{<br />
cout << " ---Menu---" << endl;<br />
cout << "1=Deposit" << endl;<br />
cout << "2=Withdraw" << endl;<br />
cout << "3=Inquiry" << endl;<br />
cout << "4=Quit" << endl;<br />
}<br />
int GetSelection()<br />
{<br />
int selection;<br />
cout << "Enter selection: ";<br />
cin >> selection;<br />
return selection;<br />
}
I want to make the class Account work in C++. I don't want someone to do the assignment, just give me a clue as to how to go about it. Thanks in advance for any help.
Elaine
|
|
|
|
|
kerthy_29 wrote: I want to make the class Account work in C++.
Ok, so what is an account (object)? What are its properties? What can you tell it (settor methods)? What can it tell you about itself (getter methods). Knowing this will go a long way towards designing the class.
"Approved Workmen Are Not Ashamed" - 2 Timothy 2:15
"Judge not by the eye but by the heart." - Native American Proverb
|
|
|
|