MFC how to change the background color of a disabled CEdit - visual-c++

I want to change the background color of a CEdit which is set to disabled.
I tried to change it in OnCtlColor. But the CEdit is still a gray background:
m_hEditBrush = ::CreateSolidBrush(RGB(255, 255, 255));
...
if (nCtlColor == CTLCOLOR_EDIT)
{
CString ss;
pWnd->GetWindowText(ss);
switch(pWnd->GetDlgCtrlID())
{
case IDC_EDIT_USERNAME:
{
pDC->SetBkColor(EDIT_BK_COLOR);
return m_hEditBrush;
break;
}
default:
break;
}
}

For the disabled edit control, you need to respond to CTLCOLOR_STATIC:
if (nCtlColor == CTLCOLOR_STATIC)
{
switch (pWnd->GetDlgCtrlID())
{
case IDC_EDIT_USERNAME:
{
pDC->SetBkColor(EDIT_BK_COLOR));
return m_hEditBrush;
break;
}
default:
break;
}
}

Related

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 Dialog controls goes invisible while running

I created a MFC application.Sometimes the controls(Button,Label etc) in the Dialog goes invisible at run time.The Dialog form remains.This is a random issue.The screenshots of the dialog in normal time and when goes blank are attached.Can anyone help me to find the solution for this ?
http://i.stack.imgur.com/KTDGV.png
http://i.stack.imgur.com/3PqUb.png
for displaying/hiding the Dialog i used the following code
void CVideoConverter::PopUpDlg(BOOL bValue)
{
try
{
if(bValue) // show
{
CVideoConverterApp::m_pCVideoConverterDlg->ShowWindow(SW_SHOWNORMAL);
CVideoConverterApp::m_pCVideoConverterDlg->UpdateWindow();
}
else
{// hide
CVideoConverterApp::m_pCVideoConverterDlg->ShowWindow(SW_MINIMIZE);
}
}
catch(...)
{}
}
The following code was used to position the dialog to bottom right corner of the window.This is called before the PopupDlg()
void CVideoConverter::SetWindowToBottomRightCorner()
{
try
{
CRect rcScreen;
SystemParametersInfo(SPI_GETWORKAREA, 0, (void *) &rcScreen, 0);
CRect rcWindow;
GetWindowRect(&rcWindow);
MoveWindow(rcScreen.right - rcWindow.Width(), rcScreen.bottom - rcWindow.Height(), rcWindow.Width(), rcWindow.Height(), TRUE);
}
catch(...)
{}
}

Display color in a JavaFX TableRow

I would like to change the color of the text displayed in a TableRow.
The instruction setStyle("-fx-background-color: green"); is working well,
but the instruction setTextFill doesn't work. Is it normal ?
tableView.setRowFactory(new Callback<TableView<Person>, TableRow<Person>>() {
#Override
public TableRow<Person> call(TableView<Person> param) {
final TableRow<Person> row = new TableRow<Person>() {
#Override
protected void updateItem(Person person, boolean empty) {
super.updateItem(person, empty);
setTextFill(Color.RED);
//setStyle("-fx-background-color: green");
}
};
return row;
}
});
The easiest way is to set the default CSS file in your application with:
.cell {
-fx-background-color: #FFCCAA;
-fx-text-fill: #000000;
}
/* if you want more different colours for even and odds: */
.cell:odd {
-fx-background-color: #FFDDDD;
-fx-text-fill: green;
}
You can add this file.css to your scene:
scene.getStylesheets().add("file.css");

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

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();
}

handling NSStream events when using EASession in MonoTouch

Does anyone have an example of how to handle read and write NSStream events in Monotouch when working with accessories via EASession?
It looks like there isn't a strongly typed delegate for this and I'm having trouble figuring out what selectors I need to handle on the delegates of my InputStream and OutputStream and what I actually need to do with each selector in order to properly fill and empty the buffers belonging to the EASession object.
Basically, I'm trying to port Apple's EADemo app to Monotouch right now.
Here's the Objective-C source that I think is relevant to this problem:
/
/ asynchronous NSStream handleEvent method
- (void)stream:(NSStream *)aStream handleEvent:(NSStreamEvent)eventCode
{
switch (eventCode) {
case NSStreamEventNone:
break;
case NSStreamEventOpenCompleted:
break;
case NSStreamEventHasBytesAvailable:
[self _readData];
break;
case NSStreamEventHasSpaceAvailable:
[self _writeData];
break;
case NSStreamEventErrorOccurred:
break;
case NSStreamEventEndEncountered:
break;
default:
break;
}
}
/ low level write method - write data to the accessory while there is space available and data to write
- (void)_writeData {
while (([[_session outputStream] hasSpaceAvailable]) && ([_writeData length] > 0))
{
NSInteger bytesWritten = [[_session outputStream] write:[_writeData bytes] maxLength:[_writeData length]];
if (bytesWritten == -1)
{
NSLog(#"write error");
break;
}
else if (bytesWritten > 0)
{
[_writeData replaceBytesInRange:NSMakeRange(0, bytesWritten) withBytes:NULL length:0];
}
}
}
// low level read method - read data while there is data and space available in the input buffer
- (void)_readData {
#define EAD_INPUT_BUFFER_SIZE 128
uint8_t buf[EAD_INPUT_BUFFER_SIZE];
while ([[_session inputStream] hasBytesAvailable])
{
NSInteger bytesRead = [[_session inputStream] read:buf maxLength:EAD_INPUT_BUFFER_SIZE];
if (_readData == nil) {
_readData = [[NSMutableData alloc] init];
}
[_readData appendBytes:(void *)buf length:bytesRead];
//NSLog(#"read %d bytes from input stream", bytesRead);
}
[[NSNotificationCenter defaultCenter] postNotificationName:EADSessionDataReceivedNotification object:self userInfo:nil];
}
I'd also appreciate any architectural recommendations on how to best implement this in monotouch. For example, in the Objective C implementation these functions are not contained in any class--but in Monotouch would it make sense to make them members of my
It looks like the latest version of MonoTouch (I had to upgrade to iOS 4.2 first to get it) now implements a strongly typed delegate for HandleEvent and a new NSStreamEvent type so it can be more easily handled:
// asynchronous NSStream handleEvent method
public override void HandleEvent (NSStream theStream, NSStreamEvent streamEvent)
{
switch (streamEvent)
{
case NSStreamEvent.None:
break;
case NSStreamEvent.OpenCompleted:
break;
case NSStreamEvent.HasBytesAvailable:
LowReadData((NSInputStream)theStream);
break;
case NSStreamEvent.HasSpaceAvailable:
LowWriteData((NSOutputStream)theStream);
break;
case NSStreamEvent.ErrorOccurred:
break;
case NSStreamEvent.EndEncountered:
break;
default:
break;
}
}

Resources