MFC drawing
In my MFC fucntion(below). When the Alpha option is selected I want a rectangle to be drawn with Alpha inside it.
I've tried with the code below but doesn't work for some reason? :confused:
ANY ideas?
Thanks!!!!!
afx_msg void CMainWin::OnAlpha()
{
hPen.CreatePen(PS_SOLID,6,RGB(0,0,255));
r.top = 100;
r.left = 100;
r.bottom = 400;
r.right = 400;
CPaintDC dc(this);
dc.SelectObject(hPen);
dc.Rectangle(&r);
dc.DrawText("ALPHA!",-1,&r,DT_SINGLELINE | DT_CENTER | DT_VCENTER);
dc.SelectObject(hPenOld);}
# 1 Re: MFC drawing
Maybe because you didn't initialize CRect r; unless it's a member or global which I can't tell from the code.
Write what happens when you run it, for example "it doesnt compile", "the rectangle shows up but not the text", "the text shows up but not the rectangle", or "it's just an empty window".
# 2 Re: MFC drawing
the code from the .cpp file is below
It doesnt do anything at all when i choose the Alpha option from the menu(nothing shows up): :confused:
#include <afxwin.h>
#include "menu.h"
#include "ids.h"
CRect r;
CMainWin::CMainWin()
{
Create(NULL, "Using Menus", WS_OVERLAPPEDWINDOW,
rectDefault, NULL, "MYMENU");
}
// Initialize the application.
BOOL CApp::InitInstance()
{
m_pMainWnd = new CMainWin;
m_pMainWnd->ShowWindow(m_nCmdShow);
m_pMainWnd->UpdateWindow();
return TRUE;
}
// This is the application's message map.
BEGIN_MESSAGE_MAP(CMainWin, CFrameWnd)
ON_COMMAND(IDM_ALPHA, OnAlpha)
END_MESSAGE_MAP()
afx_msg void CMainWin::OnAlpha()
{
hPen.CreatePen(PS_SOLID,6,RGB(0,0,255));
r.top = 100;
r.left = 100;
r.bottom = 400;
r.right = 400;
CPaintDC dc(this);
dc.SelectObject(hPen);
dc.Rectangle(&r);
dc.DrawText("ALPHA!",-1,&r,DT_SINGLELINE | DT_CENTER | DT_VCENTER);
dc.SelectObject(hPenOld);
}
CApp App;
# 3 Re: MFC drawing
It could be because in you're passing &r instead of r. &r is the address of the rectangle, whereas r is the rectangle itself. The program is converting an address to a CRect without you knowing it, and so the computer is interpreting the data wrong. Replace &r with r and see what happens.
# 4 Re: MFC drawing
You had me there for a min.
i did try that but :( :( :( had no luck!
It did the same as before.
Runs the program but doesnt do anything when I choose the alpha option!
# 5 Re: MFC drawing
did you initialize hPen? how about try calling SelectPen() instead of SelectObject()?
# 6 Re: MFC drawing
Iam not entirely sure but isnt:
hPen.CreatePen(PS_SOLID,6,RGB(0,0,255));
initializing the hPen? :confused:
What else would initialize it?
I did try SelectPen instead but same thing happened again :(
# 7 Re: MFC drawing
Originally posted by londondevil
Iam not entirely sure but isnt:
hPen.CreatePen(PS_SOLID,6,RGB(0,0,255));
initializing the hPen? :confused:
What else would initialize it?
I think you haven't told the compiler that hPen is a CPen. Try this:
CPen hPen(PS_SOLID,6,RGB(0,0,255));
...
dc.SelectObject(&hPen);
# 8 Re: MFC drawing
Thanks!!! :D that did it for the intializing!!
now I got some weird Debug Assertion failed Error, so I am gona try work through that!
Once again, thanks for your help, MUCH appreciated!
# 9 Re: MFC drawing
Originally posted by londondevil
Thanks!!! :D that did it for the intializing!!
now I got some weird Debug Assertion failed Error, so I am gona try work through that!
Once again, thanks for your help, MUCH appreciated!
Glad I could help you out :)