How to change RibbonStatusBarPane text color? - visual-c++

My application is using MFC Ribbon(VS2008 + Feature pack9).
I'm not able to change RibbonStatus Bar Pane text color.I override the virtual int DrawPaneText(CDC* pDC, const CString& strText, CRect rectText, UINT uiDTFlags) function but things are not working.
In non Ribbon Status Bar there is a function void SetPaneTextColor(int nIndex, COLORREF clrText = (COLORREF)-1 , BOOL bUpdate) in order to change pane text color.
how to do this in MFCRibbonStatusBarPane ?

class MyPane : public CMFCRibbonStatusBarPane
{
public:
MyPane(){};
MyPane(
UINT nCmdID,
LPCTSTR lpszText,
BOOL bIsStatic = FALSE,
HICON hIcon = NULL,
LPCTSTR lpszAlmostLargeText = NULL)
:CMFCRibbonStatusBarPane(nCmdID,
lpszText,bIsStatic,hIcon,lpszAlmostLargeText){}
MyPane(
UINT nCmdID,
LPCTSTR lpszText,
HBITMAP hBmpAnimationList, list
int cxAnimation = 16,
COLORREF clrTrnsp= RGB(192,192,192),
HICON hIcon = NULL,
BOOL bIsStatic = FALSE)
:CMFCRibbonStatusBarPane(nCmdID,lpszText,
hBmpAnimationList,cxAnimation,clrTrnsp,hIcon,bIsStatic){}
MyPane(
UINT nCmdID,
LPCTSTR lpszText,
UINT uiAnimationListResID,
int cxAnimation = 16,
COLORREF clrTrnsp= RGB(192,192,192),
HICON hIcon = NULL,
BOOL bIsStatic = FALSE)
:CMFCRibbonStatusBarPane(nCmdID,lpszText,
uiAnimationListResID,cxAnimation,clrTrnsp, hIcon,bIsStatic){}
~MyPane(){};
virtual COLORREF OnFillBackground(CDC* pDC)override
{
return RGB(255,0,0); //return whatever new color you want
}
};
int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
[...]
m_wndStatusBar.AddElement(new MyPane(ID_STATUSBAR_PANE1, strTitlePane1, TRUE), strTitlePane1);
m_wndStatusBar.AddExtendedElement(new MyPane(ID_STATUSBAR_PANE2, strTitlePane2, TRUE), strTitlePane2);
[...]
}

Related

CMFCEditBrowseCtrl is cropping the file name

I select an image using the CMFCEditBrowseCtrl which has the name:
D:\WhatsApp Image 2020-04-02 at 13.03.48.jpeg
So it is now selected in the control:
Now, I decide to hit the Browse button again:
See?
D:\WhatsApp Image 2020-04-02 at 13.03.48.jpeg appears to be truncated to to at 13.03.48.jpeg. But the moment I click the mouse into the filename control it then shows correct:
It doesn't always show the full name again if you click the edit box. But guaranteed, if you click OK it will be correct and complete.
This is going to be confusing for the user.
Update 1
If I click in the filename and click the HOME button on the keyboard then the rest of the file name comes into view.
Update 2
I have delved into the MFC source code for this bit and this is what it looks like:
case BrowseMode_File:
{
CString strFile;
GetWindowText(strFile);
if (!strFile.IsEmpty())
{
TCHAR fname [_MAX_FNAME];
_tsplitpath_s(strFile, NULL, 0, NULL, 0, fname, _MAX_FNAME, NULL, 0);
CString strFileName = fname;
strFileName.TrimLeft();
strFileName.TrimRight();
if (strFileName.IsEmpty())
{
strFile.Empty();
}
const CString strInvalidChars = _T("*?<>|");
if (strFile.FindOneOf(strInvalidChars) >= 0)
{
if (!OnIllegalFileName(strFile))
{
SetFocus();
return;
}
}
}
CFileDialog dlg(TRUE, !m_strDefFileExt.IsEmpty() ? (LPCTSTR)m_strDefFileExt : (LPCTSTR)NULL, strFile, m_dwFileDialogFlags, !m_strFileFilter.IsEmpty() ? (LPCTSTR)m_strFileFilter : (LPCTSTR)NULL, NULL);
if (dlg.DoModal() == IDOK && strFile != dlg.GetPathName())
{
SetWindowText(dlg.GetPathName());
SetModify(TRUE);
OnAfterUpdate();
}
if (GetParent() != NULL)
{
GetParent()->RedrawWindow(NULL, NULL, RDW_FRAME | RDW_INVALIDATE | RDW_ALLCHILDREN);
}
}
break;
}
Update 3
I have tried to roll out my own class that overrides the OnBrowse handler. It has improved logic for setting the default filter index and default file extension:
#include "stdafx.h"
#include "MyMFCEditBrowseFileCtrl.h"
IMPLEMENT_DYNAMIC(CMyMFCEditBrowseFileCtrl, CMFCEditBrowseCtrl)
BEGIN_MESSAGE_MAP(CMyMFCEditBrowseFileCtrl, CMFCEditBrowseCtrl)
END_MESSAGE_MAP()
void CMyMFCEditBrowseFileCtrl::OnBrowse()
{
CString strFile, strFileExtension;
GetWindowText(strFile);
if (!strFile.IsEmpty())
{
TCHAR fname[_MAX_FNAME];
TCHAR ext[_MAX_EXT];
_tsplitpath_s(strFile, NULL, 0, NULL, 0, fname, _MAX_FNAME, ext, _MAX_EXT);
CString strFileName = fname;
strFileName.TrimLeft();
strFileName.TrimRight();
if (strFileName.IsEmpty())
{
strFile.Empty();
}
strFileExtension = ext;
strFileExtension.Trim();
strFileExtension.MakeLower();
const CString strInvalidChars = _T("*?<>|");
if (strFile.FindOneOf(strInvalidChars) >= 0)
{
if (!OnIllegalFileName(strFile))
{
SetFocus();
return;
}
}
}
int iFilterIndex = 2; // jpg - fallback
m_strDefFileExt = _T("jpg");
if (strFileExtension == _T(".gif"))
{
iFilterIndex = 1;
m_strDefFileExt = _T("gif");
}
else if (strFileExtension == _T(".jpeg") || strFileExtension == _T(".jpg"))
{
iFilterIndex = 2;
m_strDefFileExt = _T("jpg");
}
else if (strFileExtension == _T(".png"))
{
iFilterIndex = 3;
m_strDefFileExt = _T("png");
}
else if (strFileExtension == _T(".tif") || strFileExtension == _T(".tiff"))
{
iFilterIndex = 4;
m_strDefFileExt = _T("tif");
}
else if (strFileExtension == _T(".bmp"))
{
iFilterIndex = 5;
m_strDefFileExt = _T("bmp");
}
CFileDialog dlg(TRUE, (LPCTSTR)m_strDefFileExt,
strFile,
m_dwFileDialogFlags,
!m_strFileFilter.IsEmpty() ? (LPCTSTR)m_strFileFilter : (LPCTSTR)NULL, NULL);
dlg.m_pOFN->nFilterIndex = iFilterIndex;
if (dlg.DoModal() == IDOK && strFile != dlg.GetPathName())
{
SetWindowText(dlg.GetPathName());
SetModify(TRUE);
OnAfterUpdate();
}
if (GetParent() != NULL)
{
GetParent()->RedrawWindow(NULL, NULL, RDW_FRAME | RDW_INVALIDATE | RDW_ALLCHILDREN);
}
}
But, it still have this odd behaviour I described.
Update 4
This issue is technically related to CFileDialog. If I simply try:
CFileDialog dlgOpen(TRUE, _T("MWB"), _T("123456789abcdefghijklmnopqrstuvwxyz.mwb"), OFN_PATHMUSTEXIST | OFN_HIDEREADONLY, strFilter, this);
Then all that is visibly selected is "rstuvwxyz.mwb".
Update 5
One of the replies here states:
This really isn't an MFC issue. The bad actor is the shell's COM object that implements the IFileDialog interface that is used by MFC under the hood. The following minimal example reproduces the problem using COM without any MFC code.
#include <Windows.h>
#include <ShlObj.h>
int WINAPI wWinMain(HINSTANCE hInst, HINSTANCE hPrevious, LPWSTR szCommandline, INT nShow)
{
HRESULT hr = CoInitialize(nullptr);
if (SUCCEEDED(hr))
{
IFileDialog *pfd = nullptr;
hr = CoCreateInstance(CLSID_FileOpenDialog,
nullptr,
CLSCTX_INPROC_SERVER,
IID_PPV_ARGS(&pfd));
if (SUCCEEDED(hr))
{
COMDLG_FILTERSPEC rgFileSpec[] = {
{L"MWB Files (*.mwb)", L"*.mwb"},
{L"All Files (*.*)", L"*.*"}
};
hr = pfd->SetFileTypes(ARRAYSIZE(rgFileSpec), rgFileSpec);
hr = pfd->SetFileName(L"123456789abcdefghijklmnopqrstuvwxyz.mwb");
hr = pfd->Show(NULL);
pfd->Release();
}
}
CoUninitialize();
return 0;
}
This class solves it
FileDialogHack.h:
#pragma once
#include <afxdlgs.h>
/////////////////////////////////////////////////////////////////////////////
// CFileDialogHack dialog
//
// solves bug in filedialog:
// https://developercommunity.visualstudio.com/t/problem-with-using-the-cfiledialog-class/1225634
//
class CFileDialogHack : public CFileDialog
{
public:
CFileDialogHack(BOOL bOpenFileDialog, LPCTSTR lpszDefExt = NULL, LPCTSTR lpszFileName = NULL,
DWORD dwFlags = OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
LPCTSTR lpszFilter = NULL, CWnd* pParentWnd = NULL);
virtual INT_PTR DoModal();
protected:
HWINEVENTHOOK wineventhook();
public:
void _WinEventProcCallback(HWINEVENTHOOK hWinEventHook,
DWORD dwEvent, HWND hwnd, LONG idObject, LONG idChild, DWORD dwEventThread, DWORD dwmsEventTime);
LRESULT _CallWndProc(int code, WPARAM wParam, LPARAM lParam);
protected:
HWINEVENTHOOK m_hHook;
HHOOK m_hWndProcHook;
HWND m_hDialog;
HWND m_hCombo;
HWND m_hEdit;
BOOL m_bHook;
HWND m_hParent;
};
FileDialogHack.cpp:
#include "stdafx.h"
#include "FileDialogHack.h"
/////////////////////////////////////////////////////////////////////////////
// CMyFileDialogHack dialog
static CFileDialogHack* g_ptrfiledaloghack = nullptr;
CFileDialogHack::CFileDialogHack(BOOL bOpenFileDialog, LPCTSTR lpszDefExt, LPCTSTR lpszFileName,
DWORD dwFlags, LPCTSTR lpszFilter, CWnd* pParentWnd)
: CFileDialog(bOpenFileDialog, lpszDefExt, lpszFileName, dwFlags, lpszFilter, pParentWnd, 0, TRUE),
m_hHook(0), m_hWndProcHook(0), m_hParent(0), m_hDialog(0), m_hCombo(0), m_hEdit(0), m_bHook(0)
{
}
LRESULT CFileDialogHack::_CallWndProc(int code, WPARAM wParam, LPARAM lParam)
{
PCWPRETSTRUCT data = (PCWPRETSTRUCT)lParam;
if (data->hwnd == m_hCombo && data->message == CBEM_GETEDITCONTROL)
{
m_hEdit = (HWND)data->lResult;
return CallNextHookEx(0, code, wParam, lParam);
}
if (m_bHook)
{
if (data->hwnd == m_hEdit && data->message == EM_SETSEL &&
data->wParam == 0 && data->lParam == -1)
::SendMessage(m_hEdit, EM_SETSEL, (WPARAM)0, (LPARAM)0);
else if (data->hwnd == m_hParent && data->message == WM_ENTERIDLE)
{
m_bHook = 0;
TCHAR text[MAX_PATH];
::GetWindowText(m_hEdit, text, MAX_PATH - 1);
auto len = _tcslen(text);
::SendMessage(m_hEdit, EM_SETSEL, (WPARAM)0, (LPARAM)0);
::SendMessage(m_hEdit, EM_SETSEL, (WPARAM)0, (LPARAM)len);
::SetFocus(m_hEdit);
}
}
return CallNextHookEx(0, code, wParam, lParam);
}
static LRESULT CALLBACK CallWndProc(int code, WPARAM wParam, LPARAM lParam)
{
if (g_ptrfiledaloghack)
return g_ptrfiledaloghack->_CallWndProc(code, wParam, lParam);
return CallNextHookEx(0, code, wParam, lParam);
}
VOID CALLBACK CFileDialogHack::_WinEventProcCallback(HWINEVENTHOOK /*hWinEventHook*/,
DWORD dwEvent, HWND hwnd, LONG idObject, LONG /*idChild*/,
DWORD /*dwEventThread*/, DWORD /*dwmsEventTime*/)
{
if (idObject == OBJID_WINDOW && dwEvent == EVENT_OBJECT_CREATE)
{
if (IsWindow(hwnd) && ::GetDlgCtrlID(hwnd) == 1148)
{
m_hCombo = hwnd; // We have the file dialog's filename combobox field.
m_hDialog = ::GetParent(hwnd);
m_hParent = ::GetParent(m_hDialog);
m_hEdit = (HWND) ::SendMessage(m_hCombo, CBEM_GETEDITCONTROL, 0, 0);
DWORD threadID = GetWindowThreadProcessId(m_hDialog, NULL);
// Hook messages to the file dialog.
m_hWndProcHook = SetWindowsHookEx(WH_CALLWNDPROCRET, CallWndProc, NULL, threadID);
}
}
}
static VOID CALLBACK WinEventProcCallback(HWINEVENTHOOK hWinEventHook,
DWORD dwEvent, HWND hwnd, LONG idObject, LONG idChild, DWORD dwEventThread,
DWORD dwmsEventTime)
{
if (g_ptrfiledaloghack)
g_ptrfiledaloghack->_WinEventProcCallback(hWinEventHook,
dwEvent, hwnd, idObject, idChild, dwEventThread, dwmsEventTime);
}
HWINEVENTHOOK CFileDialogHack::wineventhook()
{
// Hook creation of the Open File Dialog.
g_ptrfiledaloghack = this;
m_bHook = 1;
return SetWinEventHook(
EVENT_OBJECT_CREATE, EVENT_OBJECT_CREATE,
NULL, WinEventProcCallback, GetCurrentProcessId(), 0,
WINEVENT_OUTOFCONTEXT);
}
INT_PTR CFileDialogHack::DoModal()
{
m_hHook = wineventhook();
auto r = __super::DoModal();
UnhookWinEvent(m_hHook);
UnhookWindowsHookEx(m_hWndProcHook);
g_ptrfiledaloghack = nullptr;
return r;
}

How to change the background properties of an mfc application

CURRENT UI
NEW UI
I want to change the background color of a button in MFC application. I have created my user interface(UI) in MFC .I have added every controls from the toolbox.But the problem is that i want to change the background and foreground properties of a button and window.How it is possible?
please help me to change the properties of controls in MFC.In windows application we can directly change the properties in the property window.
But in the case of MFC application that is not possible.
please help me..i have not enough experience in MFC application development.....
Thanks in advance......................
code from dialer.h
class CButtonDialer : public CButton
{
// Construction
public:
CButtonDialer();
// Attributes
public:
CButton m_button;
// CButton IDC_KEY_1;
CBrush m_brush;
// Operations
public:
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CButtonDialer)
public:
virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct);
// Implementation
public:
virtual ~CButtonDialer();
// Generated message map functions
protected:
//{{AFX_MSG(CButtonDialer)
// NOTE - the ClassWizard will add and remove member functions here.
//}}AFX_MSG
afx_msg BOOL OnEraseBkgnd(CDC* pDC);
CFont m_FontLetters;
CMapStringToString m_map;
HTHEME m_hTheme;
void OpenTheme() { m_hTheme = OpenThemeData(m_hWnd, L"Button"); }
void CloseTheme() {
if (m_hTheme) { CloseThemeData(m_hTheme); m_hTheme = NULL; }
}
DECLARE_MESSAGE_MAP()
virtual void PreSubclassWindow();
afx_msg LRESULT OnThemeChanged();
afx_msg void OnMouseMove(UINT,CPoint);
afx_msg void OnSize(UINT type, int w, int h);
};
code from dialer.cpp file
#include "stdafx.h"
#include "ButtonDialer.h"
#include "Strsafe.h"
#include "const.h"
/////////////////////////////////////////////////////////////////////////////
// CButtonDialer
CButtonDialer::CButtonDialer()
{
//255,255,255
m_brush.CreateSolidBrush(
(173, 41, 41));
m_map.SetAt(_T("1"),_T(""));
m_map.SetAt(_T("2"),_T("ABC"));
m_map.SetAt(_T("3"),_T("DEF"));
m_map.SetAt(_T("4"),_T("GHI"));
m_map.SetAt(_T("5"),_T("JKL"));
m_map.SetAt(_T("6"),_T("MNO"));
m_map.SetAt(_T("7"),_T("PQRS"));
m_map.SetAt(_T("8"),_T("TUV"));
m_map.SetAt(_T("9"),_T("WXYZ"));
m_map.SetAt(_T("0"),_T(""));
m_map.SetAt(_T("*"),_T(""));
m_map.SetAt(_T("#"),_T(""));
}
CButtonDialer::~CButtonDialer()
{
CloseTheme();
}
BEGIN_MESSAGE_MAP(CButtonDialer, CButton)
ON_WM_THEMECHANGED()
ON_WM_MOUSEMOVE()
ON_WM_SIZE()
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CButtonDialer message handlers
void CButtonDialer::PreSubclassWindow()
{
OpenTheme();
HFONT hFont = (HFONT)GetStockObject(DEFAULT_GUI_FONT);
LOGFONT lf;
GetObject(hFont, sizeof(LOGFONT), &lf);
lf.lfHeight = 14;
StringCchCopy(lf.lfFaceName,LF_FACESIZE,_T("Microsoft Sans Serif"));
m_FontLetters.CreateFontIndirect(&lf);
DWORD dwStyle = ::GetClassLong(m_hWnd, GCL_STYLE);
dwStyle &= ~CS_DBLCLKS;
::SetClassLong(m_hWnd, GCL_STYLE, dwStyle);
}
LRESULT CButtonDialer::OnThemeChanged()
{
CloseTheme();
OpenTheme();
return 0L;
}
void CButtonDialer::OnSize(UINT type, int w, int h)
{
CButton::OnSize(type, w, h);
}
void CButtonDialer::OnMouseMove(UINT nFlags,CPoint point)
{
CRect rect;
GetClientRect(&rect);
if (rect.PtInRect(point)) {
if (GetCapture() != this) {
SetCapture();
Invalidate();
}
}
else {
ReleaseCapture();
Invalidate();
}
CButton::OnMouseMove(nFlags, point);
}
void CButtonDialer::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
CDC dc;
dc.Attach(lpDrawItemStruct->hDC); //Get device context object
CRect rt;
rt = lpDrawItemStruct->rcItem; //Get button rect
dc.FillSolidRect(rt,dc.GetBkColor());
dc.SetBkMode(TRANSPARENT);
CRect rtl = rt;
UINT state = lpDrawItemStruct->itemState; //Get state of the button
if (!m_hTheme) {
UINT uStyle = DFCS_BUTTONPUSH;
if ( (state & ODS_SELECTED) ) {
uStyle |= DFCS_PUSHED;
rtl.left+=1;
rtl.top+=1;
}
dc.DrawFrameControl(rt, DFC_BUTTON, uStyle);
} else {
UINT uStyleTheme = RBS_NORMAL;
if ( (state & ODS_SELECTED) ) {
uStyleTheme = PBS_PRESSED;
} else if (GetCapture()==this) {
uStyleTheme = PBS_HOT;
}
DrawThemeBackground(m_hTheme, dc.m_hDC,
BP_PUSHBUTTON, uStyleTheme,
rt, NULL);
}
CString strTemp;
GetWindowText(strTemp); // Get the caption which have been set
rtl.top += 4;
CString letters;
COLORREF crOldColor;
if (m_map.Lookup(strTemp,letters)) {
rtl.left+=15;
dc.DrawText(strTemp,rtl,DT_LEFT|DT_TOP|DT_SINGLELINE); // Draw out the caption
HFONT hOldFont = (HFONT)SelectObject(dc.m_hDC, m_FontLetters);
// Do your text drawing
rtl.left += 13;
rtl.top += 4;
rtl.right -=4;
crOldColor = dc.SetTextColor(RGB(148, 167, 70));
dc.DrawText(letters,rtl,DT_LEFT | DT_TOP | DT_SINGLELINE);
dc.SetTextColor(crOldColor);
// Always select the old font back into the DC
SelectObject(dc.m_hDC, hOldFont);
} else {
//127,127,127
crOldColor = dc.SetTextColor(RGB(148, 167, 70));
dc.DrawText(strTemp,rtl,DT_CENTER|DT_TOP|DT_SINGLELINE); // Draw out the caption
dc.SetTextColor(crOldColor);
}
if ( (state & ODS_FOCUS ) ) // If the button is focused
{
int iChange = 3;
rt.top += iChange;
rt.left += iChange;
rt.right -= iChange;
rt.bottom -= iChange;
dc.DrawFocusRect(rt);
}
dc.Detach();
}
BOOL CButtonDialer::OnEraseBkgnd(CDC* pDC)
{
CRect rect;
GetClientRect(&rect);
//255,255,255
CBrush myBrush(RGB(173, 41, 41)); // dialog background color
CBrush *pOld = pDC->SelectObject(&myBrush);
BOOL bRes = pDC->PatBlt(0, 0, rect.Width(), rect.Height(), PATCOPY);
pDC->SelectObject(pOld); // restore old brush
return bRes; // CDialog::OnEraseBkgnd(pDC);
}
You need to provide a message handler for the WM_ERASEBKGND message and paint the background yourself.
Put a normal button on the Resource Editor.
On the .h file of the form where you use it, declare a CMFCButton variable like m_btnRead.
In the DoDataExchange method of your form append a line
DDX_Control(pDX, IDC_BUTTON_READ, m_btnRead);
On the method where you initialize your form (OnInitDialog, Create, etc.) append a line
m_btnRead.SetFaceColor(RGB(0, 255, 255));
and now you are done!

Tool tip for disabled check boxes in Check List Box control in MFC

I am working in an MFC windows application. I am using check boxes in Check List Box control. Some of the check boxes are disabled. How can I implement the tool tips for disabled check boxes?
Ran Wainstein was implemented the tool tip for each item in the list box control. This can be extended to the Check List Box control also.
MyCheckListBox.h
class CMyCheckListBox : public CCheckListBox
{
DECLARE_DYNAMIC(CMyCheckListBox)
public:
CMyCheckListBox(){};
virtual ~CMyCheckListBox(){};
afx_msg int OnToolHitTest(CPoint point, TOOLINFO * pTI) const;
UINT ItemFromPoint2(CPoint pt, BOOL& bOutside) const;
BOOL OnToolTipText( UINT id, NMHDR * pNMHDR, LRESULT * pResult );
protected:
virtual void PreSubclassWindow();
DECLARE_MESSAGE_MAP()
};
MyCheckListBox.cpp
This will work only for Unicode strings.
IMPLEMENT_DYNAMIC(CMyCheckListBox, CCheckListBox)
BEGIN_MESSAGE_MAP(CMyCheckListBox, CCheckListBox)
ON_NOTIFY_EX_RANGE(TTN_NEEDTEXT, 0, 0xFFFF, OnToolTipText)
END_MESSAGE_MAP()
void CMyCheckListBox::PreSubclassWindow() {
CCheckListBox::PreSubclassWindow();
EnableToolTips(TRUE);
}
int CMyCheckListBox::OnToolHitTest(CPoint point, TOOLINFO * pTI) const{
int row;
RECT cellrect;
BOOL tmp = FALSE;
row = ItemFromPoint(point,tmp);
if ( row == -1 )
return -1;
GetItemRect(row,&cellrect);
pTI->rect = cellrect;
pTI->hwnd = m_hWnd;
pTI->uId = (UINT)((row));
pTI->lpszText = LPSTR_TEXTCALLBACK;
return pTI->uId;
}
BOOL CMyCheckListBox::OnToolTipText( UINT id, NMHDR * pNMHDR, LRESULT * pResult ){
TOOLTIPTEXTW* pTTTW = (TOOLTIPTEXTW*)pNMHDR;
CString strTipText;
UINT nID = pNMHDR->idFrom;
GetText( nID ,strTipText);
lstrcpyn(pTTTW->szText, strTipText, 80);
*pResult = 0;
return TRUE;
}
UINT CMyCheckListBox::ItemFromPoint2(CPoint pt, BOOL& bOutside) const{
int nFirstIndex, nLastIndex;
nFirstIndex = GetTopIndex();
nLastIndex = nFirstIndex + GetCount();
bOutside = TRUE;
CRect Rect;
int nResult = -1;
for (int i = nFirstIndex; nResult == -1 && i <= nLastIndex; i++){
if (GetItemRect(i, &Rect) != LB_ERR){
if (Rect.PtInRect(pt)){
nResult = i;
bOutside = FALSE;
}
}
}
return nResult;
}
Finally implement Check List Box control in the corresponding dialog box.The output is

Hiding window in process.start() and moving this window

I wrote this code, but I cannot hide and move the window.
How can I hide the window in Process.Start() and then move it and show it to the user?
[DllImport("USER32.dll")]
//[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool MoveWindow(IntPtr hwnd, int x, int y, int cx, int cy, bool repaint);
[DllImport("USER32.dll")]
private static extern bool UpdateWindow(IntPtr hWnd);
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = Application.StartupPath + #"\award_star_gold_2.png";
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.RedirectStandardOutput = false;
startInfo.RedirectStandardError = false;
startInfo.UseShellExecute = true;
startInfo.CreateNoWindow = true;
Process processTemp = new Process();
processTemp.StartInfo = startInfo;
processTemp.EnableRaisingEvents = true;
processTemp.Start();
processTemp.WaitForInputIdle();
IntPtr id = processTemp.MainWindowHandle;
Console.Write(id);
System.Threading.Thread.Sleep(1000);
MoveWindow(processTemp.MainWindowHandle, 10, 200, 200, 300, true);
UpdateWindow(processTemp.MainWindowHandle);
}

Mfc application crash in CWnd::DefWindowProc while creating Progress Control from within Worker Thread after 34 repetitive cycles on 64 bit windows

I am trying to figure out the exact reason for the crash happening in my 32 bit MFC application which is running on 64 bit system.
Actually this is a multithreaded MFC SDI application and can do cyclic execution which includes Inspection and outputting inspection results as reports.
After an inspection finishes it show a Custom Alert Window with a progress control until the reports are generated.The Alert Window is created from a Worker Thread and the Main Thread waits until the window is created.
Below is the coded representation of one cycle of Displaying the Alert Window With Progress Bar:
static const __int64 POPUPWND_POLLPERIOD = 10 * 10000LL;
static const __int64 POPUPWND_POLLTIMEOUT = 1000 * POPUPWND_POLLPERIOD;
class CCallFunc
{
public:
class Queue;
public:
typedef int(*Call)(const CCallFunc &cf);
public:
CCallFunc(Call call, LPVOID lpData) :
m_call(call),
m_lpData(lpData)
{}
int Run() { m_call(*this); }
LPVOID GetData() const { return m_lpData; }
private:
Call m_call;
LPVOID m_lpData;
};
class CCallFunc::Queue
{
public:
int SetQueue(const CCallFunc &cf, const __int64 &timeout = INFINITE)
{
m_pcf = &cf;
m_timeout = timeout;
}
public:
int Run(const __int64 &timeout = 0)
{
CCallFunc cf(*m_pcf);
cf.Run();
}
private:
const CCallFunc* m_pcf;
__int64 m_timeout;
};
class CWorkThread
{
private:
static DWORD WINAPI SystemThread(LPVOID lpData)
{
CWorkThread* pThread = (CWorkThread*)lpData;
__int64 timeout = pThread->m_timeout;
try {
pThread->m_queue.Run(timeout);
}
catch (const CCallFunc &cf) {
pThread->m_queue.SetQueue(cf, timeout);
}
}
public:
static int Aquire(CWorkThread *pThread)
{
pThread = &thisThread;
return S_OK;
}
static void Sleep(const __int64 &period)
{
__int64 current;
__int64 final = period;
switch (final) {
case INFINITE:
while (true)
::SleepEx(INFINITE, TRUE);
throw;
case 0:
::SleepEx(DWORD(0), TRUE);
return;
default:
::GetSystemTimeAsFileTime(reinterpret_cast<FILETIME*>(&current));
if ((final += current) < 0)
final = current;
while (current < final) {
if (::SleepEx(DWORD((final - current) / __int64(10000)), TRUE) == 0)
return;
::GetSystemTimeAsFileTime((FILETIME*)&current);
}
}
}
int Start(CCallFunc::Call call, LPVOID lpData)
{
return Start(CCallFunc(call, lpData));
}
int Start(const CCallFunc &fc)
{
DWORD dwID = 0;
::CreateThread(0, 0, &SystemThread, this, 0, &dwID);
}
public:
CCallFunc::Queue m_queue;
private:
__int64 m_timeout;
static CWorkThread thisThread;
};
class CPopupWindow;
struct PopupWndCreateContext : public CCreateContext {
CPopupWindow* popup;
CString clsname;
CString wndname;
DWORD style;
DWORD exstyle;
CRect rc;
HWND parent;
UINT id;
};
class CPopupWindow : public CWnd
{
public:
int Show()
{
HWND hParent = 0;
CWinApp* pApp = NULL;
CWnd* pMain;
if ((pApp = ::AfxGetApp()) != 0 && (pMain = pApp->GetMainWnd()) != 0) {
hParent = pMain->m_hWnd;
}
Create(800, 600, hParent);
}
private:
int Create(int iWidth, int iHeight, HWND parent)
{
PopupWndCreateContext ctxt;
ctxt.popup = this;
ctxt.clsname = "AlertCtrl";
ctxt.wndname = "Alert Control";
ctxt.style = WS_VISIBLE | WS_POPUP;
ctxt.exstyle = 0;
ctxt.rc = CRect(0, 0, iWidth, iHeight);
ctxt.parent = parent;
ctxt.id = 10000;
CWorkThread* pThread;
int e;
if (SUCCEEDED(e = CWorkThread::Aquire(pThread)) && SUCCEEDED(e = pThread->Start(&Run, &ctxt))) {
for (__int64 t = 0; t < POPUPWND_POLLTIMEOUT; t += POPUPWND_POLLPERIOD) {
if (::IsWindow(*this))
return 0;
CWorkThread::Sleep(POPUPWND_POLLPERIOD);
}
}
}
static int Run(const CCallFunc &cf)
{
int e = 0;
PopupWndCreateContext& ctxt = *(static_cast<PopupWndCreateContext*>(cf.GetData()));
ASSERT(&ctxt != 0);
CPopupWindow &wnd = *ctxt.popup;
static const DWORD clsstyle = CS_BYTEALIGNCLIENT | CS_BYTEALIGNWINDOW;
static const HCURSOR clscursor = ::LoadCursor(0, IDC_WAIT);
static const HICON clsicon = 0;
static LPCTSTR clsname = ::AfxRegisterWndClass(clsstyle, clscursor, NULL, clsicon);
if (wnd.CreateEx(DWORD(ctxt.exstyle), ctxt.clsname, ctxt.wndname, DWORD(ctxt.style), ctxt.rc.left, ctxt.rc.top, ctxt.rc.Width(), ctxt.rc.Height(), ctxt.parent, HMENU(ctxt.id), 0) != 0) {
HWND hwnd = wnd.GetSafeHwnd();
::UpdateWindow(hwnd);
MSG msg;
while ((::GetMessage(&msg, 0, 0, 0))) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
wnd.DestroyWindow();
}
return e;
}
};
class CAlertCtrl : CPopupWindow
{
CProgressCtrl m_progctrl;
DECLARE_MESSAGE_MAP();
int OnCreate(LPCREATESTRUCT cs)
{
int e = 0; //< error code / return value
if ((e = __super::OnCreate(cs)) != 0)
return e;
if (!::IsWindow(m_progctrl))
{
CRect rc;
GetClientRect(rc);
if (m_progctrl.Create(WS_CHILD | WS_VISIBLE | PBS_SMOOTH, rc, this, 100000))
m_progctrl.SetRange(0, 10000);
}
return e;
}
};
BEGIN_MESSAGE_MAP(CAlertCtrl, CPopupWindow)
ON_WM_CREATE()
END_MESSAGE_MAP()
So while executing m_progctrl.Create it crashes in the Wincore.cpp
at the method CWnd::DefWindowProc trying to execute callWindowProc after calling the method CPopupWindow::Show for the 35th Cycle.
/////////////////////////////////////////////////////////////////////////////
// Default CWnd implementation
LRESULT CWnd::DefWindowProc(UINT nMsg, WPARAM wParam, LPARAM lParam)
{
if (m_pfnSuper != NULL)
return ::CallWindowProc(m_pfnSuper, m_hWnd, nMsg, wParam, lParam);

Resources