C++ WinApp Scroll bar undeclared global variables. - c++

hwnd = CreateWindowEx(
0, // no extended styles
g_szWindowClass, // global string containing name of window class
g_szTitle, // global string containing title bar text
WS_OVERLAPPEDWINDOW |
WS_HSCROLL | WS_VSCROLL, // window styles
CW_USEDEFAULT, // default horizontal position
CW_USEDEFAULT, // default vertical position
CW_USEDEFAULT, // default width
CW_USEDEFAULT, // default height
(HWND) NULL, // no parent for overlapped windows
(HMENU) NULL, // use the window class menu
g_hInst, // global instance handle
(PVOID) NULL // pointer not needed
);
I copied and pasted directly from http://msdn.microsoft.com/en-us/library/windows/desktop/hh298376%28v=vs.85%29.aspx
But, however the variables starting with 'g_' are undefined, how do I define them? I am not finding that info on Microsoft site.
Oh and My Goal is too create a scroll bar. (vertical)
Thanks

g_ indicates a global variable; Declare the 3 variables with a prefix of _g as global. Set
wchar_t g_szClassName[] = L"WindowClassName";
g_szTitle = L"Name"; // use this
g_hInst = thisInstance; // one of mains parameters
So something like this:
// This is unicode.
wchar_t szClassName[] = L"ClassRegisterName";
int WINAPI WinMain (HINSTANCE hThisInstance, HINSTANCE hPrevInstance, LPSTR lpszArgument, int nFunsterStil) {
...
hwnd = CreateWindow(szClassName, L"Your Title", WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX | WS_CLIPCHILDREN,
100, 50, 100, 100, NULL, NULL, hThisInstance, NULL);

Related

Why CreateWindowEx will add WS_CAPTION by default?

I'm trying to create an window by CreateWindowEx, but seams even I give both dwExStyle dwStyle value 0, the window still have WS_CAPTION style.
Code snippet as following:
_hWnd = CreateWindowExW(iExStyle, pszClassName, pszTitle, iStyle | WS_CLIPCHILDREN, dX, dY, dWidth, dHeight,
hWndParent, 0, hInstance, NULL);
ASSERT(GetWindowLong(_hWnd, GWL_STYLE) & WS_CAPTION == 0); //<---- This will failed.
dwStyle = 0x00000000L means WS_OVERLAPPED or WS_TILED, this window has a title bar and a border.
Window Styles
As emax says, WS_OVERLAPPED (0) is the default and results in:
The window is an overlapped window. An overlapped window has a title bar and a border. Same as the WS_TILED style.
If you are creating a child window you must specify WS_CHILD and if you are creating a "popup" window you must use WS_POPUP or WS_POPUPWINDOW.
A tooltip for example would use WS_POPUP and WS_EX_TOOLWINDOW + WS_EX_TOPMOST...

Creating two overlapped win32 windows simulatenously c++

I tried to create two overlapped windows but only one popped up. I plan to use 1 window to handle the buttons and another separate window(not child) to show images that changes every 1 second. Is it possible? I tried to use 1 window to handle both but the buttons went missing and are unable to click because the program is busy running the display. And what parameter to set for the HINSTANCE for the second window?
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;
HWND hWnd2;
hInst = hInstance; // Store instance handle in our global variable
hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
100, 0,1000, 700, NULL, NULL, hInstance, NULL);
hWnd2= CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
100, 0,1000, 700, NULL, NULL, NULL, NULL);
CreateWindow(TEXT("button"), TEXT("\t Start Scanning\n"),
WS_VISIBLE | WS_CHILD | WS_BORDER,
810, 320, 150, 150,
hWnd, (HMENU) IDM_BEGIN, NULL, NULL);
CreateWindow(TEXT("button"), TEXT("\t STOP \n"),
WS_VISIBLE | WS_CHILD | WS_BORDER,
810, 480, 150, 150,
hWnd, (HMENU) IDM_PERMASTOP, NULL, NULL);
if (!hWnd)
{
return FALSE;
}
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
return TRUE;
}
You are creating two overlapped windows, but you are calling ShowWindow() on only the first time. Simply call ShowWindow() on the other one as well.

Adding Tooltips to Checkboxes in ATL-based C++ Dialogs

I'm attempting to add a set of tooltips to a set of checkboxes on a dialog inheriting from CAxDialogImpl, where CSG32GridViewControlDlg is my dialog's class.
Using the example code on MSDN as a base, I've added the following code:
void CSG32GridViewControlDlg::AddCheckboxTooltip(const int toolId, PTSTR tooltipText)
{
HWND hCheckbox = GetDlgItem(toolId);
char label1[501];
::GetWindowText(hCheckbox, label1, 500);
HINSTANCE hInstance = _AtlBaseModule.GetResourceInstance( ); // This bit I'm not sure about...
// Need to create the ToolTip first
HWND hWndToolTip = ::CreateWindowEx(NULL, TOOLTIPS_CLASS, NULL,
WS_POPUP | TTS_ALWAYSTIP | TTS_BALLOON,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
hCheckbox,
NULL,
hInstance, 0);
if (!hCheckbox || !hWndToolTip)
{
return;
}
TOOLINFO toolInfo = { 0 };
toolInfo.cbSize = sizeof(toolInfo);
toolInfo.hwnd = hCheckbox;
toolInfo.uFlags = TTF_IDISHWND | TTF_SUBCLASS;
toolInfo.uId = (UINT_PTR)hWndToolTip;
toolInfo.lpszText = tooltipText;
LRESULT result = ::SendMessage(hWndToolTip, TTM_ADDTOOL, 0, (LPARAM) &toolInfo);
return hWndToolTip;
}
Debugging the code, I can see that the message gets sent, and the result comes back as "1", which would seem to suggest that everything's worked successfully. But when I mouseover the checkbox in question... no tooltip.
What could I do to check if the tooltip is successfully registered, and why might it not be appearing on Mouseover? Alternatively, is there a better way to approach this?
There are a few problems with your code. The starting point worth mentioning is obviosuly this: How to Create a Tooltip for a Control.
Parent window for the tooltip control should be the dialog, not the checkbox
uId member for the tool with TTF_IDISHWND flag needs to be the tool window, and not tooltip control window
This gives code:
HWND AddCheckboxTooltip(const int toolId, PTSTR tooltipText)
{
HWND hCheckbox = GetDlgItem(toolId);
// Need to create the ToolTip first
HWND hWndToolTip = ::CreateWindowEx(NULL, TOOLTIPS_CLASS, NULL,
WS_POPUP | TTS_ALWAYSTIP | TTS_BALLOON,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
m_hWnd,
NULL,
_AtlBaseModule.GetModuleInstance(), 0);
if (!hCheckbox || !hWndToolTip)
return NULL;
TOOLINFO toolInfo = { 0 };
toolInfo.cbSize = sizeof(toolInfo);
toolInfo.hwnd = hCheckbox;
toolInfo.uFlags = TTF_IDISHWND | TTF_SUBCLASS;
toolInfo.uId = (UINT_PTR)hCheckbox;
toolInfo.lpszText = tooltipText;
LRESULT result = ::SendMessage(hWndToolTip, TTM_ADDTOOL,
0, (LPARAM) &toolInfo);
return hWndToolTip;
}
LRESULT OnInitDialog(UINT, WPARAM, LPARAM, BOOL& /*bHandled*/)
{
AddCheckboxTooltip(IDC_CHECK1, _T("Checkbox Tooltip Test"));
//...
Which runs as:

CWnd::CreateEx() fails after replacing WS_CHILD with WS_POPUP

When user click a button, I would like to bring up another window. So in the OnBnClicked..() function, I added following code:
CWnd* window = new CWnd;
BOOL result = window->CreateEx(WS_EX_CLIENTEDGE, _T("STATIC"), _T("Hi"), WS_CHILD | WS_VISIBLE, 0, 0, 20, 20, m_hWnd, (HMENU)1234);
window->ShowWindow(SW_SHOW);
This works, except it created a child window. What I need is a separate window. So I modified the code by changing "WS_CHILD" to "WS_POPUP", and "m_hWnd" to "NULL".
CWnd* window = new CWnd;
BOOL result = window->CreateEx(WS_EX_CLIENTEDGE, _T("STATIC"), _T("Hi"), WS_POPUP | WS_VISIBLE, 0, 0, 20, 20, NULL, (HMENU)1234);
window->ShowWindow(SW_SHOW);
But now, the CreateEx() call returns FALSE. I would appreciate some pointer.
[Edit:]
I updated the code as following. But still seeing the same issue. Also, even when CreateEx() returns FALSE, GetLastError() actually returns 0.
CWnd* window = new CalibrationWindow;
LPCTSTR className = NULL;
if (!className) {
className = AfxRegisterWndClass(
CS_VREDRAW | CS_HREDRAW,
::LoadCursor(NULL, IDC_ARROW),
(HBRUSH) ::GetStockObject(WHITE_BRUSH),
::LoadIcon(NULL, IDI_APPLICATION));
}
BOOL isValid = ::IsWindow(GetSafeHwnd());
BOOL result = window->CreateEx(0, _T(className), _T("Hi"), WS_POPUP | WS_VISIBLE, 0, 0, 20, 20, m_hWnd, (HMENU)1234);
DWORD errorCode;
if (!result) {
errorCode = GetLastError();
}
window->ShowWindow(SW_SHOW);
The nID or nIDorHMenu parameter for CWnd::CreateEx is an 'overloaded' parameter that either specifies a control ID or a menu ID.
For a child window (WS_CHILD window styles) it is the control identifier. You can specify any value as long as it is unique among all child windows with the same parent window.
For a popup window on the other hand (a window that does have WS_CHILD window style) it is the menu identifier. In this case it has to be a valid HMENU. Passing a made up value will cause window creation to fail.
Here's how to create a window out of your CWnd-derived class using a different method:
CWndDerived myWnd;
LPCTSTR className = AfxRegisterWndClass(CS_VREDRAW | CS_HREDRAW,
nullptr,
(HBRUSH) ::GetStockObject(WHITE_BRUSH),
nullptr);
myWnd.CreateEx(
0, // WS_EX_TOPMOST
className,
_T("Title"),
WS_POPUP | WS_BORDER | WS_CAPTION | WS_MAXIMIZEBOX | WS_SYSMENU,
0, 0, 300, 200,
m_hWnd,
nullptr,
nullptr);
This allows you to have your CWndDerived myWnd as a member variable (should you need it) of your other window and retain all the goodies that come with MSVC's MFC class editor.
I hope this helps

Win32: How to create a bordless popup window

Win32 API provides many styles for window creating and I'm looking for a style that can remove a one-pixel border from the window that I created with this code:
DWORD dwExtStyle = 0;
DWORD dwStyle = WS_POPUPWINDOW;
m_hWnd = CreateWindowEx(
dwExtStyle,
className,
windowName,
dwStyle,
300,
300,
100,
100,
NULL,
NULL,
hInstance,
NULL);
ShowWindow(m_hWnd, SW_SHOW);
and I got the result:
What combination of flags can remove the black border from the window.
Just use WS_POPUP instead of WS_POPUPWINDOW.
The macro WS_POPUPWINDOW is actually a set of flags:
#define WS_POPUPWINDOW (WS_BORDER | WS_POPUP | WS_SYSMENU)
The WS_BORDER flag is the one responsible of your black square.