how to change background color of a static text control (when a button is pushed or in a timer) in mfc? - visual-c++

I know it can be done with OnCtlColor(), but it changes colors when the form is being loaded and the static texts are to be drawn, I want to do it after form is loaded, with a timer maybe, I searched for a solution but I didn't find a clear one, this is what I wrote:
void CTabFive::OnBnClickedButton1()
{
// TODO: Add your control notification handler code here
CWnd* pWnd = this->GetDlgItem(IDC_Chromosome1);
CDC* dc = pWnd->GetDC();
dc->SetBkColor(RGB(200,0,0));
pWnd->Invalidate();
pWnd->UpdateWindow();
Invalidate();
UpdateWindow();
//flag = true;
}

No timer is needed. Here I have a bool m_coloured member of the class initialized to false, and toggled in the button press. The OnCtlColor will draw in red or in the system colour depending on the value of m_coloured. Works nicely.
HBRUSH Cmfcvs2010Dlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
HBRUSH hbr = CDialogEx::OnCtlColor(pDC, pWnd, nCtlColor);
if (nCtlColor == CTLCOLOR_STATIC && pWnd->GetDlgCtrlID() == IDC_LABEL)
{
DWORD d = GetSysColor(COLOR_BTNFACE);
COLORREF normal = RGB(GetRValue(d), GetGValue(d), GetBValue(d));
COLORREF red = RGB(255, 0, 0);
pDC->SetBkColor(m_coloured ? red : normal);
}
return hbr;
}
void Cmfcvs2010Dlg::OnBnClickedButton1()
{
m_coloured = !m_coloured;
Invalidate();
}

Related

Why is my stretchblt images losing colours?

I am having some difficulties in correctly populating a CListCtrl with thumbnails of monitor displays.
On the right of my CDialog I have a static control and I render the image on a white canvas like this:
void CCenterCursorOnScreenDlg::OnDrawItem(int nIDCtl, LPDRAWITEMSTRUCT lpDrawItemStruct)
{
if (nIDCtl == IDC_STATIC_MONITOR && !m_imgPreview.IsNull())
{
// Set the mode
SetStretchBltMode(lpDrawItemStruct->hDC, HALFTONE);
// Wipe the canvas
FillRect(lpDrawItemStruct->hDC, &lpDrawItemStruct->rcItem, static_cast<HBRUSH>(GetStockObject(WHITE_BRUSH)));
// Get canvas rectangle
const CRect rectCanvas(lpDrawItemStruct->rcItem);
// Calculate ratio factors
const float nRatioImage = m_imgPreview.GetWidth() / static_cast<float>(m_imgPreview.GetHeight());
const float nRatioCanvas = rectCanvas.Width() / static_cast<float>(rectCanvas.Height());
// Calculate new rectangle size
// Account for portrait images (negative values)
CRect rectDraw = rectCanvas;
if (nRatioImage > nRatioCanvas)
rectDraw.SetRect(0, 0, rectDraw.right, static_cast<int>(rectDraw.right / nRatioImage));
else if (nRatioImage < nRatioCanvas)
rectDraw.SetRect(0, 0, static_cast<int>((rectDraw.bottom * nRatioImage)), rectDraw.bottom);
// Add a margin
rectDraw.DeflateRect(5, 5);
// Move to center
const CSize ptOffset = rectCanvas.CenterPoint() - rectDraw.CenterPoint();
rectDraw.OffsetRect(ptOffset);
// Add a black frame
FrameRect(lpDrawItemStruct->hDC, &lpDrawItemStruct->rcItem, static_cast<HBRUSH>(GetStockObject(BLACK_BRUSH)));
// Draw
m_imgPreview.Draw(lpDrawItemStruct->hDC, rectDraw);
return;
}
CDialogEx::OnDrawItem(nIDCtl, lpDrawItemStruct);
}
The above works beautifully:
But I have problems with the CListCtrl versions of the images. For instance, I am losing the colouring as you can see.
My CImageList is created like this:
m_ImageListThumb.Create(THUMBNAIL_WIDTH, THUMBNAIL_HEIGHT, ILC_COLOR32, 0, 1);
m_ListThumbnail.SetImageList(&m_ImageListThumb, LVSIL_NORMAL);
I then create all the thumbnails by calling DrawThumbnails() in OnInitDialog:
void CCenterCursorOnScreenDlg::DrawThumbnails()
{
int monitorIndex = 0;
m_ListThumbnail.SetRedraw(FALSE);
for (auto& strMonitor : m_monitors.strMonitorNames)
{
CImage img;
CreateMonitorThumbnail(monitorIndex, img, true);
CBitmap* pImage = new CBitmap();
pImage->Attach((HBITMAP)img);
m_ImageListThumb.Add(pImage, nullptr);
CString strMonitorDesc = m_monitors.strMonitorNames.at(monitorIndex);
strMonitorDesc.AppendFormat(L" (Screen %d)", monitorIndex + 1);
m_ListThumbnail.InsertItem(monitorIndex, strMonitorDesc, monitorIndex);
monitorIndex++;
delete pImage;
}
m_ListThumbnail.SetRedraw(TRUE);
}
The CreateMonitorThumbnail function:
BOOL CCenterCursorOnScreenDlg::CreateMonitorThumbnail(const int iMonitorIndex, CImage &rImage, bool bSmall)
{
const CRect rcCapture = m_monitors.rcMonitors.at(iMonitorIndex);
// destroy the currently contained bitmap to create a new one
rImage.Destroy();
auto nWidth = rcCapture.Width();
auto nHeight = rcCapture.Height();
if (bSmall)
{
nWidth = THUMBNAIL_WIDTH;
nHeight = THUMBNAIL_HEIGHT;
}
// create bitmap and attach it to this object
if (!rImage.Create(nWidth, nHeight, 32, 0))
{
AfxMessageBox(L"Cannot create image!", MB_ICONERROR);
return FALSE;
}
// create virtual screen DC
CDC dcScreen;
dcScreen.CreateDC(_T("DISPLAY"), nullptr, nullptr, nullptr);
// copy the contents from the virtual screen DC
BOOL bRet = FALSE;
if (bSmall)
{
CRect rt(0, 0, nWidth, nHeight);
//::FillRect(rImage.GetDC(), rt, static_cast<HBRUSH>(GetStockObject(WHITE_BRUSH)));
bRet = ::StretchBlt(rImage.GetDC(), 0, 0,
nWidth,
nHeight,
dcScreen.m_hDC,
rcCapture.left,
rcCapture.top,
rcCapture.Width(),
rcCapture.Height(), SRCCOPY | CAPTUREBLT);
}
else
{
bRet = ::BitBlt(rImage.GetDC(), 0, 0,
rcCapture.Width(),
rcCapture.Height(),
dcScreen.m_hDC,
rcCapture.left,
rcCapture.top, SRCCOPY | CAPTUREBLT);
}
// do cleanup and return
dcScreen.DeleteDC();
rImage.ReleaseDC();
return bRet;
}
Ideally I want to have exactly the same kind of visual image as on the right, but obviously resized down. How do I fix this?
I simplified the converting from CImage to CBitmap but it made no difference:
void CCenterCursorOnScreenDlg::DrawThumbnails()
{
int monitorIndex = 0;
// Stop redrawing the CListCtrl
m_ListThumbnail.SetRedraw(FALSE);
// Loop monitor info
for (auto& strMonitor : m_monitors.strMonitorNames)
{
// Create the thumbnail image
CImage monitorThumbnail;
CreateMonitorThumbnail(monitorIndex, monitorThumbnail, true);
// Convert it to a CBitmap
CBitmap* pMonitorThumbnailBitmap = CBitmap::FromHandle(monitorThumbnail);
// Add the CBitmap to the CImageList
m_ImageListThumb.Add(pMonitorThumbnailBitmap, nullptr);
// Build the caption description
CString strMonitorDesc = m_monitors.strMonitorNames.at(monitorIndex);
strMonitorDesc.AppendFormat(L" (Screen %d)", monitorIndex + 1);
// Add the item to the CListCtrl
m_ListThumbnail.InsertItem(monitorIndex, strMonitorDesc, monitorIndex);
monitorIndex++;
}
// Start redrawiung the CListCtrl again
m_ListThumbnail.SetRedraw(TRUE);
}
If I change my code to pass false for the last parameter, so that it uses the original captured images without scaling down:
The colours are god there, so it is when I do:
if (bSmall)
{
CRect rt(0, 0, nWidth, nHeight);
//::FillRect(rImage.GetDC(), rt, static_cast<HBRUSH>(GetStockObject(WHITE_BRUSH)));
bRet = ::StretchBlt(rImage.GetDC(), 0, 0,
nWidth,
nHeight,
dcScreen.m_hDC,
rcCapture.left,
rcCapture.top,
rcCapture.Width(),
rcCapture.Height(), SRCCOPY | CAPTUREBLT);
}
that it messes up.
My issue was did not have anything to do with OnDrawItem. I simply included that to indicate how the image on the right was being rendered. I thought it may helped as background information. But it has probably confused the question and I may take it out in the long run!
Based on the comments I was reminded about SetStretchBltMode which was missing from CreateMonitorThumbnail. So, I now have this function:
BOOL CCenterCursorOnScreenDlg::CreateMonitorThumbnail(const int iMonitorIndex, CImage &rImage, bool bResizeAsThumbnail)
{
const CRect rcCapture = m_monitors.rcMonitors.at(iMonitorIndex);
// Destroy the currently contained bitmap to create a new one
rImage.Destroy();
// Massage the dimensions as we want a thumbnail
auto nWidth = rcCapture.Width();
auto nHeight = rcCapture.Height();
if (bResizeAsThumbnail)
{
nWidth = m_iThumbnailWidth;
auto dRatio = rcCapture.Width() / nWidth;
//nHeight = m_iThumbnailHeight;
nHeight = static_cast<int>(rcCapture.Height() / dRatio);
if (nHeight > m_iThumbnailHeight)
{
AfxMessageBox(L"Need to investigate!");
}
}
// Create bitmap and attach it to this object
if (!rImage.Create(nWidth, nHeight, 32, 0))
{
AfxMessageBox(L"Cannot create image!", MB_ICONERROR);
return FALSE;
}
// Create virtual screen DC
CDC dcScreen;
dcScreen.CreateDC(L"DISPLAY", nullptr, nullptr, nullptr);
// Copy (or resize) the contents from the virtual screen DC
BOOL bRet = FALSE;
auto dcImage = rImage.GetDC();
if (bResizeAsThumbnail)
{
// Set the mode first!
SetStretchBltMode(dcImage, COLORONCOLOR);
CPen penBlack;
penBlack.CreatePen(PS_SOLID, 3, RGB(0, 0, 0));
::Rectangle(dcImage, 0, 0, m_iThumbnailWidth, m_iThumbnailHeight);
int iTop = (m_iThumbnailHeight - nHeight) / 2;
// Copy (and resize)
bRet = ::StretchBlt(dcImage, 0, iTop,
nWidth,
nHeight,
dcScreen.m_hDC,
rcCapture.left,
rcCapture.top,
rcCapture.Width(),
rcCapture.Height(), SRCCOPY | CAPTUREBLT);
}
else
{
// Copy
bRet = ::BitBlt(dcImage, 0, 0,
rcCapture.Width(),
rcCapture.Height(),
dcScreen.m_hDC,
rcCapture.left,
rcCapture.top, SRCCOPY | CAPTUREBLT);
}
// Do cleanup and return
dcScreen.DeleteDC();
rImage.ReleaseDC();
return bRet;
}
That was the key to getting the thumbnail showing with the right colours:

Popup menu in MFC from button click handler not working

// CMFCApplication1Dlg dialog
CMFCApplication1Dlg::CMFCApplication1Dlg(CWnd* pParent /*=NULL*/)
: CDialogEx(CMFCApplication1Dlg::IDD, pParent)
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CMFCApplication1Dlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CMFCApplication1Dlg, CDialogEx)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON1, &CMFCApplication1Dlg::OnBnClickedButton1)
END_MESSAGE_MAP()
// CMFCApplication1Dlg message handlers
BOOL CMFCApplication1Dlg::OnInitDialog()
{
CDialogEx::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
BOOL bNameValid;
CString strAboutMenu;
bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
ASSERT(bNameValid);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
return TRUE; // return TRUE unless you set the focus to a control
}
void CMFCApplication1Dlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialogEx::OnSysCommand(nID, lParam);
}
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CMFCApplication1Dlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(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);
}
else
{
CDialogEx::OnPaint();
}
}
// The system calls this function to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CMFCApplication1Dlg::OnQueryDragIcon()
{
return static_cast<HCURSOR>(m_hIcon);
}
void CMFCApplication1Dlg::OnBnClickedButton1()
{
//Simulating the similar code as product
CFrameWnd *pFrame = GetParentFrame();
CMenu popup;
popup.CreatePopupMenu();
LPCWSTR pszMenuItem2 = L"Korean with wchar_t: 기존 운";
AppendMenuW(popup.m_hMenu, MF_STRING, 1, pszMenuItem2);
TCHAR* pszMenuItem3 = "|| Korean without wchar_t: 또는 차량 삽입";
AppendMenu(popup.m_hMenu, MF_STRING, 2, pszMenuItem3);
UINT nCmd = popup.TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON | TPM_RETURNCMD,
14, 20, pFrame, 0);
}
I have written above piece of code to generate popup menu in the screen on button click but it doesn't show any menu at all? Am I missing something?
As you asked here is the complete code. Actually I have placed one static button through GUI and on button click trying to generate the popup menu.
These button is not created dynamically. Also, the entire code here is generated by wizard except the last portion of the code about the button click.
The problem was in the handle, if I use the right API to get the handle, it shows the popup menu.
Here is the updated code.
void CMFCApplication1Dlg::OnBnClickedButton1()
{
//Simulating the similar code as product
HWND wnd= GetSafeHwnd();//<---- this had to change
CMenu popup;
popup.CreatePopupMenu();
LPCWSTR pszMenuItem2 = L"Korean with wchar_t: 기존 운";
AppendMenuW(popup.m_hMenu, MF_STRING, 1, pszMenuItem2);
TCHAR* pszMenuItem3 = "|| Korean without wchar_t: 또는 차량 삽입";
AppendMenu(popup.m_hMenu, MF_STRING, 2, pszMenuItem3);
UINT nCmd = popup.TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON | TPM_RETURNCMD,
14, 20, CWnd::FromHandle(wnd)/*need this api to get the CWnd from HWND*/, 0);

MFC: How to draw in a static text

I try to draw in a static text control named IDC_RESULT.When I click the OK button, the static text will display different picture according to the shape I selected.So I declare three bool variable: isLine ,is Rect and isEllipse, everytime when I choose Lien radio box, I make the bool variabel isLine be true and the others be false, the same as Rectangle and Ellipse.
This is my code:
void CDrawDlg::OnPaint()
{
CWnd* pWnd = (CWnd*)GetDlgItem(IDC_RESULT);
CPaintDC dcPaint(pWnd);
CPen pen(PS_SOLID,2,RGB(0,0,0));
dcPaint.SelectObject(&pen);
CRect rect;
pWnd->GetClientRect(&rect);
if(isLine)
{
dcPaint.MoveTo(10,150);
dcPaint.LineTo (350,150);
}
if(isRect)
{
dcPaint.Rectangle(50, 100, 300, 200);
}
if(isEllipse)
{
doSomething;
}
ReleaseDC(&dcPaint);
if (IsIconic())
{
CPaintDC dc(this);
SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
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;
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialogEx::OnPaint();
}
}
And the function below associate with the OK button:
void CDrawDlg::OnBnClickedOk()
{
Invalidate(false);
}
The question is: How to erase the line in the static text control when I select Rectangle and click the OK button to draw a rectangle,but I can't erase the line.
The parameter to Invalidate (bErase) controls whether or not the background within the update region should be erased. Since you passed false it isn't. Pass TRUE instead.
As an aside, you should use TRUE/FALSE instead of true/false as arguments to MFC methods. Even though the types are compatible they are still different.

How to prevent screen rotation in j2me?

I'm programming in j2me.
How can I prevent screen rotation in j2me for all phones that support screen rotation?
thanks.
If you use Canvas to draw your screen (not LCDUI, not LWUIT, not any other framework) you may implement sizeChanged method to be notified when the rotation happens.
In such case you may draw your screen to an Image and use Sprite to rotate it. For example, to support only Landscape mode I used below code at constructor:
int width = Math.max(super.getWidth(), super.getHeight());
int height = Math.min(super.getWidth(), super.getHeight());
// screen and sprite are attributes
screen = Image.createImage(width, height);
sprite = new Sprite(screen);
if (super.getWidth() < super.getHeight()) { // portrait screen
sprite.setTransform(Sprite.TRANS_ROT90);
sprite.setPosition(0, 0);
}
And following methods:
public void sizeChanged (int w, int h) {
// lastWidth and lastHeight are attributes
lastWidth = w;
lastHeight = h;
if (sprite == null) return;
if (super.getWidth() < super.getHeight()) { // portrait screen
sprite.setTransform(Sprite.TRANS_ROT90);
} else {
sprite.setTransform(Sprite.TRANS_NONE);
}
sprite.setPosition(0, 0);
}
protected void paint(Graphics g1) {
if (super.getWidth() != lastWidth
|| super.getHeight() != lastHeight) {
sizeChanged(super.getWidth(), super.getHeight());
}
Graphics g = screen.getGraphics();
// ... do your drawing on g
this.sprite.setImage(screen, screen.getWidth(), screen.getHeight());
sprite.paint(g1);
}
As seen at http://smallandadaptive.blogspot.com.br/2009/08/fullscreen-landscape.html and http://smallandadaptive.blogspot.com.br/2010/03/adapting-to-sizechanged.html and http://smallandadaptive.blogspot.com.br/2011/04/sizechanged-not-called.html
Add to manifest
For Nokia devices:
Nokia-MIDlet-App-Orientation : Landscape
For Samsung devices:
MIDlet-ScreenMode : Landscape

Crop area selection control(like photoshop's) in c# windows form

how to develop a crop selection control like photoshop's in c# 4.0 in widows form application.
I have a windows form application in c# 4.0 that can crop images. At first you have to draw a rectangle using mouse to select the cropped region.
private Point _pt;
private Point _pt2;
private void picBoxImageProcessing_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
int ix = (int)(e.X / _zoom);
int iy = (int)(e.Y / _zoom);
//reset _pt2
_pt2 = new Point(0, 0);
_pt = new Point(ix, iy);
// pictureBox1.Invalidate();
picBoxImageProcessing.Invalidate();
}
}
private void picBoxImageProcessing_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left && _selecting)
{
_selecting = false;
}
}
private void picBoxImageProcessing_Paint(object sender, PaintEventArgs e)
{
if (_selecting &&_pt.X >= 0 && _pt.Y >= 0 && _pt2.X >= 0 && _pt2.Y >= 0)
{
e.Graphics.DrawRectangle(pen, _pt.X * _zoom, _pt.Y * _zoom,
(_pt2.X - _pt.X) * _zoom, (_pt2.Y - _pt.Y) * _zoom);
}
}
private void picBoxImageProcessing_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
_selecting = true;
int ix = (int)(e.X / _zoom);
int iy = (int)(e.Y / _zoom);
_pt2 = new Point(ix, iy);
// pictureBox1.Invalidate();
picBoxImageProcessing.Invalidate();
}
}
there is no problem to draw the rectangle by mouse dragging. But if i want to change the height or width of the rectangle then I have to draw a new rectangle, that i don't want. I want to change the height and width of the rectangle by modifying the drawn rectangle instead of drawing a new rectangle.
I don’t want to know how to crop. I need to draw a resizable rectangle on the image as we can do in photoshop.
So I need a crop selection control like photoshop's crop control.

Resources