We help IT Professionals succeed at work.

Create CStatic not working with SetBitmap function

Axter
Axter asked
on
2,066 Views
Last Modified: 2013-11-20
I'm having problems creating a dynamic CStatic field.  When I created a dynamic CStatic field the SetBitmap function doesn't work.
if (!Create("",WS_CHILD|WS_VISIBLE|BS_BITMAP|SS_NOTIFY, MyRect, PWnd, 16969+intRoomNumber))//WS_CHILD|SS_BITMAP|WM_CTLCOLOR|WS_VISIBLE  
{
      MessageBox("Create failed","",0);
      return;
}
SetBitmap((HBITMAP)LoadImage(NULL,szFilename,IMAGE_BITMAP,0,0,LR_LOADFROMFILE|LR_CREATEDIBSECTION));

I know the problem is not in the LoadImage function, because I have the exact same statement on a field that is not dynamic, and it works with no problem.
I'm pretty sure the problem is with the Create function because that is the only difference between the two.

Please do not give me an answer by referring me to a previously answered questioned.  I've already wasted about 100 points looking at previous similar questions, and non of them had the right answer for my particular problem.

Thanks in advanceā€¦..
Comment
Watch Question

Wyn

Commented:
Try
Create("",WS_CHILD|WS_VISIBLE|SS_BITMAP|SS_NOTIFY, MyRect, ....)
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
Wyn

Commented:
Besides,although less chance,you'd not subclass it.
AxterSenior Software Engineer
CERTIFIED EXPERT

Author

Commented:
Wyn,
I tried that setting, but it only works if I remove the "OnPaint" member.
I need it to work with the OnPaint.
Wyn

Commented:
No,you'd not reject me,at least It works for adding bitmap.

I bet you'v got to add code to your OnPaint for bitmap,I suggest you check the source code of CStatic.
AxterSenior Software Engineer
CERTIFIED EXPERT

Author

Commented:
I'm sorry Wyn, but I can't accept the answer if I have to remove required code from the program.  Your answer does me no good if I have to remove the OnPaint function to get it to work.
I need a working solution that let's me keep my OnPaint function.
I'll take a look at the source code for CStatic, if this helps me figure it out, I'll give you the points, but if it doesn't, I have to reject your answer.
Wyn

Commented:
->I'm sorry Wyn, but I can't accept the answer if I have to remove required code from the program.  Your answer does me no good if I have to remove the OnPaint function to get it to work.
====================================
Oh,not at all.
You misundertand me.You should reject me.I just want to say you'd better ask more because I dont know OnPaint matter when you ask this quesiotn,Right:)
 
->I need a working solution that let's me keep my OnPaint function.
====================
You can do this.All in all,you can blit the bitmap mannually and setbkmode to transparent and write text onto it.That's all.
AxterSenior Software Engineer
CERTIFIED EXPERT

Author

Commented:
Wyn,
I finally figured out the problem with the OnPaint problem, and your answer definitely help me focus on the right direction.  When the OnPaint function is created by the Class-Wizard, it creates the following:
void RoomIconGroup::OnPaint()
{
      CPaintDC dc(this); // device context for painting

      // Do not call CStatic::OnPaint() for painting messages
}

This is very misleading and it inappropriately put together.  If the above function is left as-is, the "CStatic" bitmap will not appear.  What has to be added to the above function is a call to the base class OnPaint() function.

void RoomIconGroup::OnPaint()
{
      CPaintDC dc(this); // device context for painting

      // Do not call CStatic::OnPaint() for painting messages
      CStatic::OnPaint();
}

The commented line that the Class-Wizard puts in the function, makes it look like you're not supposed to make a call to the base function.  Further more, in most cases where the base class function has to be called, the class-wizard automatically puts the call into the function.  This proves to me that you can't always rely on the class-wizard to do everything.  You have to double check it.

Just to info anyone else that may have a similar problem with "CbitmapButton" it also has the same problem with OnPaint.

Thanks for your help Wyn.

P.S.
Wyn,
I'm not sure if you can add comments after I award you the points.  So, could you please tell me what you're talking about "blit the bitmap mannually", and if it's not to much trouble, could you give me a short example?
Any additional information would be appreciated.... Thanks!
Wyn

Commented:
I dont know why you give me a C degree.I havent gotten this degree for a time:)

Regarding your problem,i'm surprised that you solve it by adding CStatic:OnPaint() to it:)
What does CStatic::OnPaint do here?
I think it's just CWnd::OnPaint(),correct me if I'm wrong.I'm not very familiar with MFC.

Try call this sequence:
SetBitmap();
Invalidate();
UpdateWindow();

Regards
W.Yinan

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.