How Create derived from CWnd POP UP Window? - c++

I created class CSurfaceWnd from CWnd by Class Wizard. I tried to create window but getting error.
That's my code of creating:
if(!m_pSurfaceWnd)
{
CString m_NameClass = AfxRegisterWndClass(
CS_VREDRAW | CS_HREDRAW,
::LoadCursor(NULL, IDC_ARROW),
(HBRUSH) ::GetStockObject(WHITE_BRUSH),
::LoadIcon(NULL, IDI_APPLICATION));
m_pSurfaceWnd = new CSurfaceWnd;
CRect rcTemp;
GetWindowRect(rcTemp);
VERIFY(m_pSurfaceWnd->CreateEx(WS_EX_CLIENTEDGE, m_NameClass, NULL, WS_POPUP | WS_VISIBLE, rcTemp, mpWnd, 1));
//DWORD dw =GetLastError();
m_pSurfaceWnd->ShowWindow(SW_SHOW);
}
else
m_pSurfaceWnd->ShowWindow(SW_SHOW);
How can You see I'm creating pop up window that's why I'm using CreateEx. I have registered class and in debug mode I see a number of new class in m_NameClass. But CreateEx returned false.
Please help me. Probably you will see some error that I can't see.Please Don't send me to MSDN I have read it a lot of times.
Thank you

First of all, you have to check whether mpWnd is valid object.
BOOL isValid = ::IsWindow(mpWnd->GetSafeHwnd());
If mpWnd is invalid value, CreateEx function will return 0 because of WS_POPUP style.

Related

Winapi . TrackPopupMenuEx equivalent in CreateWindow

When we create a pop up menu we call a function TrackPopupMenuEx . It displays a shortcut menu at the specified location and tracks the selection of items on the shortcut menu .
If you specify TPM_RETURNCMD in the fuFlags parameter, the return value is the menu-item identifier of the item that the user selected. If the user cancels the menu without making a selection, or if an error occurs, the return value is zero.
This command is blocking ie unless we click on menu or outside we are blocked.
I have created a window which has various static items which acts like a popup menu. Is there some function equalient to (TrackPopupMenuEx with TPM_RETURNCMD ) which can tell me which was the last WM_COMMAND CLICKED which is blocking . If a function does not exist , how can we create a good blocking in the view that it doesn't eat up the CPU in wait cycle.
What I did :
In the WM_COMAND I initialize the global variable which is whatIsLastClicked. In my function i wait till this variable is set but it seems to eat cpu.
Some code
WNDCLASSW wc = { 0 };
wc.hbrBackground = (HBRUSH)COLOR_WINDOW;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hInstance = hInst;
wc.lpszClassName = L"mywindowsclass";
wc.lpfnWndProc = windowprocedure;
//<== all handlers WM_COMMAND defined in this fucntion
if (!RegisterClassW(&wc))
{
return -1; // registration failed
}
HWND hWnd = CreateWindowW(L"mywindowsclass",
L"My window", WS_OVERLAPPEDWINDOW | WS_VISIBLE,
100, 100, 500, 500, NULL, NULL, NULL, NULL);
//POINT : need some blocking code here which is efficient to find what i clicked

winapi: removing decoration

This looks like a duplicate but hear me first. This is more on the debugging side.
I'm trying to remove the borders of my window using the method here.
What are some things that will make these functions not work? Hiding windows using ShowWindow(Handle, SW_HIDE) doesn't work also. I've made my own Window class with many functions so I don't wanna paste my whole code here.
Here's my Initialization function for the window:
HRESULT SampleWindow::InitializeSimple(SampleWindow* win)
{
HRESULT hr;
HWND hWnd;
SampleWindow* sampleWin;
sampleWin = new SampleWindow();
aMWindowProps->Center();
hWnd = CreateWindowEx(
NULL,
aMWindowProps->aWindowClass,
aMWindowProps->aWindowTitle,
WS_OVERLAPPEDWINDOW,
aMWindowProps->aRealLeft,
aMWindowProps->aRealTop,
aMWindowProps->GetRelativePosWidth(),
aMWindowProps->GetRelativePosHeight(),
HWND_DESKTOP,
NULL,
*aMWindowProps->aHInstance,
sampleWin);
aMWindowProps->aHwnd = &hWnd;
hr = hWnd ? S_OK : E_FAIL;
win->aHwnd = &hWnd;
//ShowWindow(hWnd, SW_SHOWNORMAL);
return hr;
}
WindowProps as you can see contains various info about the window being created.
I also have a HWND pointer variable in my class which points to the window handler.
Here are some things I've tried on my main, where sw2 is a pointer to my window class:
ShowWindow(*sw2->aHwnd, SW_SHOW);
//ShowWindow(*sw2->aHwnd, nCmdShow);
LONG lStyle = GetWindowLong(*sw2->aHwnd, GWL_STYLE);
lStyle &= WS_POPUP;
//lStyle &= ~(WS_CAPTION | WS_THICKFRAME | WS_MINIMIZE | WS_MAXIMIZE | WS_SYSMENU);
SetWindowLong(*sw2->aHwnd, GWL_STYLE, lStyle);
//ShowWindow(*sw2->aHwnd, SW_MINIMIZE);
//ShowWindow(*sw2->aHwnd, SW_HIDE);
//ShowWindow(*sw2->aHwnd, SW_HIDE);
//SetWindowLong(*sw2->aHwnd, GWL_STYLE, GetWindowLong(*sw2->aHwnd, GWL_STYLE) && ~ WS_BORDER && ~ WS_SIZEBOX && ~ WS_DLGFRAME);
SetWindowPos(*sw2->aHwnd, HWND_TOP, sw2->aMWindowProps->aRealLeft, sw2->aMWindowProps->aRealTop, sw2->aMWindowProps->aRealWidth, sw2->aMWindowProps->aRealHeight, SWP_FRAMECHANGED);
//LONG lExStyle = GetWindowLong(*sw2->aHwnd, GWL_EXSTYLE);
//lExStyle &= ~(WS_EX_DLGMODALFRAME | WS_EX_CLIENTEDGE | WS_EX_STATICEDGE);
//SetWindowLong(*sw2->aHwnd, GWL_EXSTYLE, lExStyle);
//SetWindowPos(*sw2->aHwnd, NULL, 0, 0, 0, 0, SWP_FRAMECHANGED | SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_NOOWNERZORDER);
I'd just like some suggestions on where to debug my code. I know the functions work as I've tested it on a much simpler window project (sample project from Microsoft).
As Jonathan Potter already pointed out in his comment, your mistake is:
aMWindowProps->aHwnd = &hWnd;
hr = hWnd ? S_OK : E_FAIL;
win->aHwnd = &hWnd;
where HWND hWnd is only valid in the scope of the current methode. I guess that you defined aHwnd in your class as something like:
HWND *aHwnd;
which is at least unnecessary. You seem to mistake a HANDLE (a HWND is nothing else) as a kind of instance/object itself. It isn't, it is more like a pointer or reference. You can always safely write:
HWND myAttribute=hWnd;
As long as you use it in the same process. (Window handles are even valid across process boundaries, but do not tell anyone about that).
I fact I know no situation where you keep a pointer to a handle instead of the handle itself.
Notice, I explicit wrote about handles, as HWND are a kind of standard window handles.

create a CAxWindow inside a BHO (C++)

I'm having trouble opening a new CAxWindow inside my BHO, I can see the request to "microsoft.com" being fired but no window is shown.
I tried many different ways, this is my last, anyone has a clue what's wrong?
thanks.
CAxWindow m_axWindow;
CRect rc;
HWND wndIE = NULL;
m_pWebBrowser->get_HWND((SHANDLE_PTR*)&wndIE);
GetWindowRect(wndIE, &rc);
CSize sz = CSize(100, 200);
CRect rcPage = new CRect(10, 10, 10, 10);
m_axWindow.Create(wndIE, rcPage, _TEXT("http://www.microsoft.com"), WS_POPUP | WS_TABSTOP, 0, 0U, 0);
HRESULT hRet = m_axWindow.QueryControl(IID_IWebBrowser2, (void**)&m_pWebBrowser);
I think m_axWindow.Create creates a child window. Check its style for WS_CHILD after that call. You probably need to create a plain popup top-level window first, then create a CAxWindow using that popup window as parent, not the wndIE. Make sure to do ShowWindow on the pop-up, too.

MFC CEdit object in a CDialogEx object

I'd be very grateful if anyone could help me with this? I'm trying to create a dialog box with a text box in it for receiving error messages. I've added ON_WM_CREATE to the message map, and written this function which the debug goes through, but the object doesn't display.
int CImportDatatoAPMDlg::OnCreate(LPCREATESTRUCT LpCreateStruct)
{
if(CWnd::OnCreate(LpCreateStruct) == -1)
{
return -1;
}
CEdit *MessageBox = new CEdit;
MessageBox->Create(WS_CHILD | WS_VISIBLE | ES_MULTILINE | ES_AUTOVSCROLL,CRect(100, 200, 450, 150), this, 0x1552);
return 0;
}
Do I have to make a dummy box when I'm designing the dialog box. I've already done this for the rest of the controls? I'm also wondering where I give this object a number ID combination?
Thanks,
James
You normally should use VisualStudio resource editor to add controls to your dialog. If you want to do it manually then create and add controls in your overriden OnInitDialog method:
BOOL CImportDatatoAPMDlg::OnInitDialog() {
BOOL bRes = CDialog::OnInitDialog();
CEdit *MessageBox; // !!! put it into class definition
MessageBox = new CEdit
MessageBox->Create(WS_CHILD | WS_VISIBLE | ES_MULTILINE | ES_AUTOVSCROLL,CRect(100, 200, 450, 150), this, 0x1552);
return bRes;
}

BringWindowToTop is Not working even if I get the handle to Class Window

I am registering my Class in the following method:
BOOL CNDSClientDlg::InitInstance()
{
//Register Window Updated on 16th Nov 2010, #Subhen
// Register our unique class name that we wish to use
WNDCLASS wndcls;
memset(&wndcls, 0, sizeof(WNDCLASS));
wndcls.style = CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW;
wndcls.lpfnWndProc = ::DefWindowProc;
wndcls.hInstance = AfxGetInstanceHandle();
wndcls.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wndcls.lpszMenuName = NULL;
//Class name for using FindWindow later
wndcls.lpszClassName = _T("CNDSClientDlg");
// Register new class and exit if it fails
if(!AfxRegisterClass(&wndcls)) // [C]
{
return FALSE;
}
}
and then calling the InitInstance method and creating the window in constructor of the Class:
CNDSClientDlg::CNDSClientDlg(CWnd* pParent /*=NULL*/)
: CDialog(CNDSClientDlg::IDD, pParent)
{
InitInstance();
HWND hWnd;
hInst = AfxGetInstanceHandle(); // Store instance handle in our global variable
hWnd = CreateWindow(_T("CNDSClientDlg"), "NDS", WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInst, NULL);
}
Now in my other application I am finding the window and trying to bring to top:
Edit
Able to bring newlyCreated Windows with below code
CWnd *pWndPrev = NULL;
CWnd *FirstChildhWnd = NULL;
pWndPrev = CWnd::FindWindow(_T("CNDSClientDlg"),NULL);
if(pWndPrev != NULL)
{
//pWndPrev->BringWindowToTop();
WINDOWPLACEMENT wndplacement;
pWndPrev->GetWindowPlacement(&wndplacement);
wndplacement.showCmd = SW_RESTORE;
pWndPrev->SetWindowPlacement(&wndplacement);
pWndPrev->SetForegroundWindow();
FirstChildhWnd = pWndPrev->GetLastActivePopup();
if (pWndPrev != FirstChildhWnd)
{
// a pop-up window is active, bring it to the top too
FirstChildhWnd->GetWindowPlacement(&wndplacement);
wndplacement.showCmd = SW_RESTORE;
FirstChildhWnd->SetWindowPlacement(&wndplacement);
FirstChildhWnd->SetForegroundWindow();
}
I am able to find the window as pWndPrev is not NULL , but It is not bringing up my application to front. Do I need to register any other class Instead of CNDSClientDlg. I want to bring my MFC application to top.
A few things to look at...
1) Try SetForegroundWindow() instead of BringWindowToTop(). It's been awhile since I've done Win32 programming, but I seem to recall that BringWindowToTop() has some limitations (especially when working with windows in different processes).
2) There are some rules that Microsoft put in place regarding SetForegroundWindow() starting with Windows 2000. The short version is that only the front-most application can change the foreground window. The idea is that an application that is not front-most cannot "jump in front of" the active application. If a background application calls SetForegroundWindow(), Windows will flash the taskbar button for the app, but will not actually bring the app to the front. The user must do that. I'm oversimplifying the rules, but this may be something to look at depending on your specific scenario.
BringWindowToTop() only works if the calling process is the foreground process or if it received the last input event.
Call CWnd::SetForegroundWindow() instead.
You may need to call AllowSetForegroundWindow in your "other" application before calling SetForegroundWindow.
That is assuming your other application is the foreground app and is trying to pass on its foreground status to the application with the window.
If neither app is the foreground app then you're not supposed to be able to bring a window to the front, although there are ways to do it (both accidentally and on purpose).
SetWindowPos(&wndTopMost, -1, -1, -1, -1, SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW);
SetForegroundWindow();