Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Displaying Bitmap on a button [modified]

Displaying Bitmap on a button [modified]

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++graphicslearning
5 Posts 4 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    safigh
    wrote on last edited by
    #1

    Hi all. I am using MFC, and I want to display a bitmap on a button. I created the resource and it appeared in the bitmap resources as IDB_BITMAP1. Now I added a button named m_opaque and added the followin code to the OnPaint function: void HelloDlg::OnPaint() { if (IsIconic()) { CPaintDC dc(this); // device context for painting SendMessage(WM_ICONERASEBKGND, reinterpret_cast(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); m_opaque.ModifyStyle(0, WS_CHILD|WS_VISIBLE|BS_BITMAP); m_opaque.SetBitmap( ::LoadBitmap(NULL, MAKEINTRESOURCE(IDB_BITMAP1)) ); // UpdateData(FALSE); } else { CDialog::OnPaint(); } } The code compiles but the image doesnt appear. what is the reason? -- modified at 11:27 Tuesday 11th July, 2006

    L L D 3 Replies Last reply
    0
    • S safigh

      Hi all. I am using MFC, and I want to display a bitmap on a button. I created the resource and it appeared in the bitmap resources as IDB_BITMAP1. Now I added a button named m_opaque and added the followin code to the OnPaint function: void HelloDlg::OnPaint() { if (IsIconic()) { CPaintDC dc(this); // device context for painting SendMessage(WM_ICONERASEBKGND, reinterpret_cast(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); m_opaque.ModifyStyle(0, WS_CHILD|WS_VISIBLE|BS_BITMAP); m_opaque.SetBitmap( ::LoadBitmap(NULL, MAKEINTRESOURCE(IDB_BITMAP1)) ); // UpdateData(FALSE); } else { CDialog::OnPaint(); } } The code compiles but the image doesnt appear. what is the reason? -- modified at 11:27 Tuesday 11th July, 2006

      L Offline
      L Offline
      led mike
      wrote on last edited by
      #2

      safigh wrote:

      if (IsIconic())

      You are painting only when the window is minimized! :omg: Also setting the bitmap is not a "painting" function. That should be done during initialization not painting.

      Last modified: Tuesday, July 11, 2006 10:26:23 AM --

      S 1 Reply Last reply
      0
      • S safigh

        Hi all. I am using MFC, and I want to display a bitmap on a button. I created the resource and it appeared in the bitmap resources as IDB_BITMAP1. Now I added a button named m_opaque and added the followin code to the OnPaint function: void HelloDlg::OnPaint() { if (IsIconic()) { CPaintDC dc(this); // device context for painting SendMessage(WM_ICONERASEBKGND, reinterpret_cast(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); m_opaque.ModifyStyle(0, WS_CHILD|WS_VISIBLE|BS_BITMAP); m_opaque.SetBitmap( ::LoadBitmap(NULL, MAKEINTRESOURCE(IDB_BITMAP1)) ); // UpdateData(FALSE); } else { CDialog::OnPaint(); } } The code compiles but the image doesnt appear. what is the reason? -- modified at 11:27 Tuesday 11th July, 2006

        L Offline
        L Offline
        ldaoust
        wrote on last edited by
        #3

        You are painting in the OnPaint message of the dialog when the dialog is minimized. If you want to draw the bitmap on a button that is child control of the dialog, you are not painting at the right place. To draw a bitmap on a button, you can use the CBitmapButton class.


        Louis * google is your friend *

        1 Reply Last reply
        0
        • L led mike

          safigh wrote:

          if (IsIconic())

          You are painting only when the window is minimized! :omg: Also setting the bitmap is not a "painting" function. That should be done during initialization not painting.

          Last modified: Tuesday, July 11, 2006 10:26:23 AM --

          S Offline
          S Offline
          safigh
          wrote on last edited by
          #4

          Adding these two line to the init dialog also doesnt show the picture???? m_opaque.ModifyStyle(0, WS_CHILD|WS_VISIBLE|BS_BITMAP); m_opaque.SetBitmap( ::LoadBitmap(NULL, MAKEINTRESOURCE(IDB_BITMAP1)) );

          1 Reply Last reply
          0
          • S safigh

            Hi all. I am using MFC, and I want to display a bitmap on a button. I created the resource and it appeared in the bitmap resources as IDB_BITMAP1. Now I added a button named m_opaque and added the followin code to the OnPaint function: void HelloDlg::OnPaint() { if (IsIconic()) { CPaintDC dc(this); // device context for painting SendMessage(WM_ICONERASEBKGND, reinterpret_cast(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); m_opaque.ModifyStyle(0, WS_CHILD|WS_VISIBLE|BS_BITMAP); m_opaque.SetBitmap( ::LoadBitmap(NULL, MAKEINTRESOURCE(IDB_BITMAP1)) ); // UpdateData(FALSE); } else { CDialog::OnPaint(); } } The code compiles but the image doesnt appear. what is the reason? -- modified at 11:27 Tuesday 11th July, 2006

            D Offline
            D Offline
            David Crow
            wrote on last edited by
            #5

            See here.


            "The largest fire starts but with the smallest spark." - David Crow

            "Judge not by the eye but by the heart." - Native American Proverb

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups