Displaying Bitmap on a button [modified]
-
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 -
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 -
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, 2006You 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 *
-
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 --
-
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, 2006See here.
"The largest fire starts but with the smallest spark." - David Crow
"Judge not by the eye but by the heart." - Native American Proverb