Windows.h input [duplicate] - c++

This question already has answers here:
How to parse a string to an int in C++?
(17 answers)
Closed 7 years ago.
For the last 2 weeks i`ve been trying to make a program using windows.h library (for the simple use of GUI ). I managed to make a textbox that you can write in , but my problem is that you can only introduce data as char* . The programm is ment for my workers to be able to calculate some formulas easier & faster. How can i transform the char to an int , without having trouble with ASCII codes?
*I have also tried using strings. This is my actual code :
#if defined(UNICODE) && !defined(_UNICODE)
#define _UNICODE
#elif defined(_UNICODE) && !defined(UNICODE)
#define UNICODE
#endif
#include <tchar.h>
#include <windows.h>
#include <string>
using namespace std;
/* Declare Windows procedure */
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
HWND textstatic,texteditz1,texteditz2,textedit2,buttoncalculate,button2;
/* Make the class name into a global variable */
string inputZ1,inputZ2;
char *cinputZ1 = &inputZ1[0u];
TCHAR szClassName[ ] = _T("CodeBlocksWindowsApp");
int WINAPI WinMain (HINSTANCE hThisInstance,
HINSTANCE hPrevInstance,
LPSTR lpszArgument,
int nCmdShow)
{
HWND hwnd; /* This is the handle for our window */
MSG messages; /* Here messages to the application are saved */
WNDCLASSEX wincl; /* Data structure for the windowclass */
/* The Window structure */
wincl.hInstance = hThisInstance;
wincl.lpszClassName = szClassName;
wincl.lpfnWndProc = WindowProcedure; /* This function is called by windows */
wincl.style = CS_DBLCLKS; /* Catch double-clicks */
wincl.cbSize = sizeof (WNDCLASSEX);
/* Use default icon and mouse-pointer */
wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
wincl.lpszMenuName = NULL; /* No menu */
wincl.cbClsExtra = 0; /* No extra bytes after the window class */
wincl.cbWndExtra = 0; /* structure or the window instance */
/* Use Windows's default colour as the background of the window */
wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;
/* Register the window class, and if it fails quit the program */
if (!RegisterClassEx (&wincl))
return 0;
/* The class is registered, let's create the program*/
hwnd = CreateWindowEx (
0, /* Extended possibilites for variation */
szClassName, /* Classname */
_T("Code::Blocks Template Windows App"), /* Title Text */
WS_OVERLAPPEDWINDOW, /* default window */
CW_USEDEFAULT, /* Windows decides the position */
CW_USEDEFAULT, /* where the window ends up on the screen */
500, /* The programs width */
500, /* and height in pixels */
HWND_DESKTOP, /* The window is a child-window to desktop */
NULL, /* No menu */
hThisInstance, /* Program Instance handler */
NULL /* No Window Creation data */
);
/* Make the window visible on the screen */
ShowWindow (hwnd, nCmdShow);
/* Run the message loop. It will run until GetMessage() returns 0 */
while (GetMessage (&messages, NULL, 0, 0))
{
/* Translate virtual-key messages into character messages */
TranslateMessage(&messages);
/* Send message to WindowProcedure */
DispatchMessage(&messages);
}
/* The program return-value is 0 - The value that PostQuitMessage() gave */
return messages.wParam;
}
/* This function is called by the Windows function DispatchMessage() */
LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message) /* handle the messages */
{
case WM_CREATE:
texteditz1 = CreateWindow ("EDIT","",
WS_VISIBLE | WS_CHILD,
60,50,45,14,
hwnd, (HMENU) 4,NULL,NULL);
texteditz2 = CreateWindow ("EDIT","",
WS_VISIBLE | WS_CHILD,
60,30,45,14,
hwnd, (HMENU) 5 ,NULL , NULL);
textstatic = CreateWindow ("STATIC",
"Z2=",
WS_VISIBLE | WS_CHILD,
30,50,30,15,
hwnd, NULL, NULL, NULL);
textstatic = CreateWindow ("STATIC",
"Z1=",
WS_VISIBLE | WS_CHILD,
30,30,30,15,
hwnd, NULL, NULL, NULL);
textstatic = CreateWindow ("STATIC",
"x1min=",
WS_VISIBLE | WS_CHILD,
30,70,50,15,
hwnd, NULL, NULL, NULL);
buttoncalculate = CreateWindow ("BUTTON",
"Calculate",
WS_VISIBLE | WS_CHILD,
150, 300, 70, 15,
hwnd, (HMENU) 1, NULL, NULL);
button2 = CreateWindow ("BUTTON","apasa",
WS_VISIBLE | WS_CHILD,
300, 300, 70, 15,
hwnd, (HMENU) 2, NULL, NULL);
break;
case WM_COMMAND:
switch (LOWORD(wParam))
{
case 1:
{
int text=0 ,text2=0 ;
text=GetWindowText(texteditz1,inputZ1,5);
text2=GetWindowText(texteditz2,inputZ2,5);
::MessageBox(hwnd, inputZ1, "Button",MB_OK);
::MessageBox(hwnd, inputZ2, "Text2",MB_OK);
}
break;
case 2:
::MessageBox (hwnd, "251", "button",NULL);
break;
}
break;
case WM_DESTROY:
PostQuitMessage (0); /* send a WM_QUIT to the message queue */
break;
default: /* for messages that we don't deal with */
return DefWindowProc (hwnd, message, wParam, lParam);
}
return 0;
}

you can use atoi() get int from a char*,
and _wtoi() get int from a wchar_t*.
so uauslly use _ttoi(),it will replacesed atoi or _wtoi.
int a = _ttoi("123");
and you need add stdlib.h

Related

Window.h c++ how to get index of a button from an array inside switch

I have one question for you. I just started to learn C++ GUI (windows.h) and I got a problem. I have to do for my mother a program that record some information so I designed a GuI that add a new set of empty field every time I click on "Adaugare" button . Near every set of fields, on right, there is a button named "Scrie" that should get the information from textBoxes and store it into a string. The problem is when I press the button to get the info, I don t know how to make my switch to figure out what field I want to get . Example : I writed 5 records, but I want to modify the first one, so how do I get the index of it even thought I have another 5 set of fields after ?
PLEASE HELP ME GUYS !
#if defined(UNICODE) && !defined(_UNICODE)
#define _UNICODE
#elif defined(_UNICODE) && !defined(UNICODE)
#define UNICODE
#endif
#include <tchar.h>
#include <windows.h>
#include <iostream>
using namespace std;
/* Declare Windows procedure */
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
HWND hwnd; /* This is the handle for our window */
HWND button[5];
HWND banda[100];
HWND tip[100];
HWND lungime[100];
HWND latime[100];
HWND data[100];
HWND button_valideaza[100];
int i = 1;
char textSaved[20];
/* Make the class name into a global variable */
TCHAR szClassName[ ] = _T("CodeBlocksWindowsApp");
int WINAPI WinMain (HINSTANCE hThisInstance,
HINSTANCE hPrevInstance,
LPSTR lpszArgument,
int nCmdShow)
{
MSG messages; /* Here messages to the application are saved */
WNDCLASSEX wincl; /* Data structure for the windowclass */
/* The Window structure */
wincl.hInstance = hThisInstance;
wincl.lpszClassName = szClassName;
wincl.lpfnWndProc = WindowProcedure; /* This function is called by windows */
wincl.style = CS_DBLCLKS; /* Catch double-clicks */
wincl.cbSize = sizeof (WNDCLASSEX);
/* Use default icon and mouse-pointer */
wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
wincl.lpszMenuName = NULL; /* No menu */
wincl.cbClsExtra = 0; /* No extra bytes after the window class */
wincl.cbWndExtra = 0; /* structure or the window instance */
/* Use Windows's default colour as the background of the window */
wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;
/* Register the window class, and if it fails quit the program */
if (!RegisterClassEx (&wincl))
return 0;
/* The class is registered, let's create the program*/
hwnd = CreateWindowEx (
0, /* Extended possibilites for variation */
szClassName, /* Classname */
_T("Flux Artego"), /* Title Text */
WS_OVERLAPPEDWINDOW, /* default window */
CW_USEDEFAULT, /* Windows decides the position */
CW_USEDEFAULT, /* where the window ends up on the screen */
700, /* The programs width */
300, /* and height in pixels */
HWND_DESKTOP, /* The window is a child-window to desktop */
NULL, /* No menu */
hThisInstance, /* Program Instance handler */
NULL /* No Window Creation data */
);
/* Make the window visible on the screen */
ShowWindow (hwnd, nCmdShow);
/* Run the message loop. It will run until GetMessage() returns 0 */
while (GetMessage (&messages, NULL, 0, 0))
{
/* Translate virtual-key messages into character messages */
TranslateMessage(&messages);
/* Send message to WindowProcedure */
DispatchMessage(&messages);
}
/* The program return-value is 0 - The value that PostQuitMessage() gave */
return messages.wParam;
}
/* This function is called by the Windows function DispatchMessage() */
void scrie() {
}
LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message) /* handle the messages */
{
case WM_CREATE: // fac butoane, label etc
button[0] = CreateWindow("BUTTON",
"Iesire",
WS_VISIBLE | WS_CHILD | WS_BORDER,
560, 220, 100, 20,
hwnd, (HMENU) 1, NULL, NULL); // (HMENU) 1 reprezinta care case din switch se executa
button[1] = CreateWindow("BUTTON",
"Adauga",
WS_VISIBLE | WS_CHILD | WS_BORDER,
450, 220, 100, 20,
hwnd, (HMENU) 2, NULL, NULL);
break;
case WM_COMMAND: // fac instructiuni butoane
switch(LOWORD(wParam) )
{
case 1:
//::MessageBeep(MB_ICONERROR);
//::MessageBox(hwnd, "Ai salvat ?", "atentie", MB_OKCANCEL);
cout << "GoodBye!";
PostQuitMessage (0);
break;
case 2: // Adaug nou record
banda[i] = CreateWindow("EDIT",
"EP",
WS_BORDER | WS_CHILD | WS_VISIBLE,
20, 30 * i, 30, 25,
hwnd, NULL, NULL, NULL);
tip[i] = CreateWindow("EDIT",
"100",
WS_BORDER | WS_CHILD | WS_VISIBLE,
55, 30 * i, 100, 25,
hwnd, NULL, NULL, NULL);
lungime[i] = CreateWindow("EDIT",
"Lungime",
WS_BORDER | WS_CHILD | WS_VISIBLE,
160, 30 * i, 100, 25,
hwnd, NULL, NULL, NULL);
latime[i] = CreateWindow("EDIT",
"Latime",
WS_BORDER | WS_CHILD | WS_VISIBLE,
265, 30 * i, 100, 25,
hwnd, NULL, NULL, NULL);
data[i] = CreateWindow("EDIT",
"Data",
WS_BORDER | WS_CHILD | WS_VISIBLE,
370, 30 * i, 100, 25,
hwnd, NULL, NULL, NULL);
button_valideaza[i] = CreateWindow("BUTTON",
"Scrie",
WS_VISIBLE | WS_CHILD | WS_BORDER,
475, 30 * i, 80, 20,
hwnd, NULL, NULL, NULL);
i++;
break;
case 3: // scriu record
int gwtstat = 0;
gwtstat = GetWindowText(banda[i], &textSaved[0], 20);
break;
}
break;
case WM_DESTROY:
PostQuitMessage (0); /* send a WM_QUIT to the message queue */
break;
default: /* for messages that we don't deal with */
return DefWindowProc (hwnd, message, wParam, lParam);
}
return 0;
}
Use the different command for each "Scrie" Button , set its Menu like that:
#if defined(UNICODE) && !defined(_UNICODE)
#define _UNICODE
#elif defined(_UNICODE) && !defined(UNICODE)
#define UNICODE
#endif
#include <tchar.h>
#include <windows.h>
#include <iostream>
using namespace std;
/* Declare Windows procedure */
LRESULT CALLBACK WindowProcedure(HWND, UINT, WPARAM, LPARAM);
HWND hwnd; /* This is the handle for our window */
HWND button[5];
HWND banda[100];
HWND tip[100];
HWND lungime[100];
HWND latime[100];
HWND data1[100];
HWND button_valideaza[100];
int i = 0;
char textSaved[20];
/* Make the class name into a global variable */
TCHAR szClassName[] = _T("CodeBlocksWindowsApp");
int WINAPI WinMain(HINSTANCE hThisInstance,
HINSTANCE hPrevInstance,
LPSTR lpszArgument,
int nCmdShow)
{
MSG messages; /* Here messages to the application are saved */
WNDCLASSEX wincl; /* Data structure for the windowclass */
/* The Window structure */
wincl.hInstance = hThisInstance;
wincl.lpszClassName = szClassName;
wincl.lpfnWndProc = WindowProcedure; /* This function is called by windows */
wincl.style = CS_DBLCLKS; /* Catch double-clicks */
wincl.cbSize = sizeof(WNDCLASSEX);
/* Use default icon and mouse-pointer */
wincl.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wincl.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
wincl.hCursor = LoadCursor(NULL, IDC_ARROW);
wincl.lpszMenuName = NULL; /* No menu */
wincl.cbClsExtra = 0; /* No extra bytes after the window class */
wincl.cbWndExtra = 0; /* structure or the window instance */
/* Use Windows's default colour as the background of the window */
wincl.hbrBackground = (HBRUSH)COLOR_BACKGROUND;
/* Register the window class, and if it fails quit the program */
if (!RegisterClassEx(&wincl))
return 0;
/* The class is registered, let's create the program*/
hwnd = CreateWindowEx(
0, /* Extended possibilites for variation */
szClassName, /* Classname */
_T("Flux Artego"), /* Title Text */
WS_OVERLAPPEDWINDOW, /* default window */
CW_USEDEFAULT, /* Windows decides the position */
CW_USEDEFAULT, /* where the window ends up on the screen */
700, /* The programs width */
300, /* and height in pixels */
HWND_DESKTOP, /* The window is a child-window to desktop */
NULL, /* No menu */
hThisInstance, /* Program Instance handler */
NULL /* No Window Creation data */
);
/* Make the window visible on the screen */
ShowWindow(hwnd, nCmdShow);
/* Run the message loop. It will run until GetMessage() returns 0 */
while (GetMessage(&messages, NULL, 0, 0))
{
/* Translate virtual-key messages into character messages */
TranslateMessage(&messages);
/* Send message to WindowProcedure */
DispatchMessage(&messages);
}
/* The program return-value is 0 - The value that PostQuitMessage() gave */
return messages.wParam;
}
/* This function is called by the Windows function DispatchMessage() */
void scrie() {
}
LRESULT CALLBACK WindowProcedure(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
WORD cmd = LOWORD(wParam);
switch (message) /* handle the messages */
{
case WM_CREATE: // fac butoane, label etc
button[0] = CreateWindow("BUTTON",
"Iesire",
WS_VISIBLE | WS_CHILD | WS_BORDER,
560, 220, 100, 20,
hwnd, (HMENU)1, NULL, NULL); // (HMENU) 1 reprezinta care case din switch se executa
button[1] = CreateWindow("BUTTON",
"Adauga",
WS_VISIBLE | WS_CHILD | WS_BORDER,
450, 220, 100, 20,
hwnd, (HMENU)2, NULL, NULL);
break;
case WM_COMMAND: // fac instructiuni butoane
switch (cmd)
{
case 1:
//::MessageBeep(MB_ICONERROR);
//::MessageBox(hwnd, "Ai salvat ?", "atentie", MB_OKCANCEL);
cout << "GoodBye!";
PostQuitMessage(0);
break;
case 2: // Adaug nou record
banda[i] = CreateWindow("EDIT",
"EP",
WS_BORDER | WS_CHILD | WS_VISIBLE,
20, 30 * i, 30, 25,
hwnd, NULL, NULL, NULL);
tip[i] = CreateWindow("EDIT",
"100",
WS_BORDER | WS_CHILD | WS_VISIBLE,
55, 30 * i, 100, 25,
hwnd, NULL, NULL, NULL);
lungime[i] = CreateWindow("EDIT",
"Lungime",
WS_BORDER | WS_CHILD | WS_VISIBLE,
160, 30 * i, 100, 25,
hwnd, NULL, NULL, NULL);
latime[i] = CreateWindow("EDIT",
"Latime",
WS_BORDER | WS_CHILD | WS_VISIBLE,
265, 30 * i, 100, 25,
hwnd, NULL, NULL, NULL);
data1[i] = CreateWindow("EDIT",
"Data",
WS_BORDER | WS_CHILD | WS_VISIBLE,
370, 30 * i, 100, 25,
hwnd, NULL, NULL, NULL);
button_valideaza[i] = CreateWindow("BUTTON",
"Scrie",
WS_VISIBLE | WS_CHILD | WS_BORDER,
475, 30 * i, 80, 20,
hwnd, (HMENU)(i+3), NULL, NULL);
i++;
break;
default:
if (cmd > 2 && cmd < 103)
{
int gwtstat = 0;
gwtstat = GetWindowText(banda[cmd-3], &textSaved[0], 20);
::MessageBox(hwnd, textSaved,"text", MB_OKCANCEL);
}
break;
}
break;
case WM_DESTROY:
PostQuitMessage(0); /* send a WM_QUIT to the message queue */
break;
default: /* for messages that we don't deal with */
return DefWindowProc(hwnd, message, wParam, lParam);
}
return 0;
}

Effect of MoveWindow in EnumChildWindows over listview inside the Dialog Box: Why ListView Header is not correctly scrolling

I have a listview control (lvc) and it is inside a DialogBox(dbx) and that dbx also has a vertical scroll bar.
Whenever the scrollbar is scrolled EnumChildWindows is called to enumerate all the child window of the dbx. The callback function contains a MoveWindow function that would move that lvc. lvc is scrolling fine but not its column headers, they are not moving with the list view.
If i comment out the MoveWindow function inside the callback function then nothing changes. ( Off-course lvc won't move! ) that means EnumChildWindow has got no problem, but MoveWindow inside the callback function is causing problem and i am sure about this because calling MoveWindow function from outside the callback function works correctly ( because in this example there is only one control, i.e. lvc, so i don't need to enumerate all the child window ).
here is the Code:
main.cpp
#if defined(UNICODE) && !defined(_UNICODE)
#define _UNICODE
#elif defined(_UNICODE) && !defined(UNICODE)
#define UNICODE
#endif
#include <tchar.h>
#define _WIN32_IE 0x0700
#include <windows.h>
#include <windowsx.h>
#include <commctrl.h>
#include <vector>
#include "res.h"
#define btn 0
#include <iostream>
/* Declare Windows procedure */
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
BOOL CALLBACK diaproc(HWND hwmd, UINT msg, WPARAM wp, LPARAM lp);
BOOL CALLBACK edc(HWND hwmd,LPARAM lp);
HINSTANCE gi;
int iPrevVscroll=0;
/* Make the class name into a global variable */
TCHAR szClassName[ ] = _T("CodeBlocksWindowsApp");
int WINAPI WinMain (HINSTANCE hThisInstance,
HINSTANCE hPrevInstance,
LPSTR lpszArgument,
int nCmdShow)
{
HWND hwnd; /* This is the handle for our window */
MSG messages; /* Here messages to the application are saved */
WNDCLASSEX wincl; /* Data structure for the windowclass */
/* The Window structure */
gi = wincl.hInstance = hThisInstance;
wincl.lpszClassName = szClassName;
wincl.lpfnWndProc = WindowProcedure; /* This function is called by windows */
wincl.style = CS_DBLCLKS ; /* Catch double-clicks */
wincl.cbSize = sizeof (WNDCLASSEX);
/* Use default icon and mouse-pointer */
wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
wincl.lpszMenuName = NULL; /* No menu */
wincl.cbClsExtra = 0; /* No extra bytes after the window class */
wincl.cbWndExtra = 0; /* structure or the window instance */
/* Use Windows's default colour as the background of the window */
wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;
/* Register the window class, and if it fails quit the program */
if (!RegisterClassEx (&wincl))
return 0;
/* The class is registered, let's create the program*/
hwnd = CreateWindowEx (
0, /* Extended possibilites for variation */
szClassName, /* Classname */
_T("Code::Blocks Template Windows App"), /* Title Text */
WS_OVERLAPPEDWINDOW, /* default window */
CW_USEDEFAULT, /* Windows decides the position */
CW_USEDEFAULT, /* where the window ends up on the screen */
544, /* The programs width */
375, /* and height in pixels */
HWND_DESKTOP, /* The window is a child-window to desktop */
NULL, /* No menu */
hThisInstance, /* Program Instance handler */
NULL /* No Window Creation data */
);
/* Make the window visible on the screen */
ShowWindow (hwnd, nCmdShow);
/* Run the message loop. It will run until GetMessage() returns 0 */
while (GetMessage (&messages, NULL, 0, 0))
{
/* Translate virtual-key messages into character messages */
TranslateMessage(&messages);
/* Send message to WindowProcedure */
DispatchMessage(&messages);
}
/* The program return-value is 0 - The value that PostQuitMessage() gave */
return messages.wParam;
}
/* This function is called by the Windows function DispatchMessage() */
LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message) /* handle the messages */
{
case WM_CREATE:
CreateWindow(WC_BUTTON, "CLICK", WS_CHILD | BS_DEFPUSHBUTTON | WS_VISIBLE, 10, 10, 80, 30, hwnd, (HMENU)btn, gi, NULL );
break;
case WM_COMMAND:{
if( LOWORD(wParam) == btn && HIWORD(wParam) == BN_CLICKED ) DialogBox(gi, MAKEINTRESOURCE(dia), hwnd,(DLGPROC)diaproc);
DWORD err = GetLastError();
std::cout<<err<<std::endl<<dia;
}
break;
case WM_DESTROY:
PostQuitMessage (0); /* send a WM_QUIT to the message queue */
break;
default: /* for messages that we don't deal with */
return DefWindowProc (hwnd, message, wParam, lParam);
}
return 0;
}
BOOL CALLBACK diaproc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lp)
{
static HWND lv_hwnd;
static int sci;
switch(msg)
{
case WM_INITDIALOG:
{
INITCOMMONCONTROLSEX is;
is.dwSize = sizeof(INITCOMMONCONTROLSEX);
is.dwICC = ICC_LISTVIEW_CLASSES;
InitCommonControlsEx(&is);
int col_fmt[5] = { LVCFMT_CENTER, LVCFMT_LEFT, LVCFMT_CENTER, LVCFMT_CENTER, LVCFMT_CENTER };
int col_wid[5] = { 30, 90, 50, 30, 70 };
std::vector<TCHAR*> col_nam(5);
col_nam[0] = _T("S.No"); col_nam[1] = _T("Description"); col_nam[2] = _T("HSN"); col_nam[3] = _T("QTY"); col_nam[4] = _T("Rate");
lv_hwnd = CreateWindow(
WC_LISTVIEW,
_T(""),
WS_CHILD | LVS_REPORT | LVS_EDITLABELS | WS_VISIBLE,
10, 0, 300, 200,
hwnd,
NULL,
(HINSTANCE)GetWindowLong(hwnd, GWL_HINSTANCE),
NULL
);
ListView_SetExtendedListViewStyle(lv_hwnd, LVS_EX_FLATSB | LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES | LVS_EX_LABELTIP );
LVCOLUMN lvc;
lvc.mask = LVCF_FMT | LVCF_SUBITEM | LVCF_WIDTH | LVCF_TEXT;
for(int i =0; i < 5; i++)
{
lvc.fmt = col_fmt[i];
lvc.cx = col_wid[i];
lvc.pszText = col_nam[i];
lvc.iSubItem = i;
ListView_InsertColumn(lv_hwnd, i, &lvc);
}
SetScrollRange(hwnd, SB_VERT, 0, 225, TRUE);
SetScrollPos(hwnd, SB_VERT, 0, TRUE);
} return FALSE;
case WM_VSCROLL:
{
RECT rc; GetWindowRect(lv_hwnd, &rc);
POINT pt1 = {rc.left, rc.top}; ScreenToClient(hwnd, &pt1);
POINT pt2 = {rc.right, rc.bottom}; ScreenToClient(hwnd, &pt2);
std::cout<<"rc.top : "<< rc.top<<"\nrc.bottom: "<< rc.bottom <<"\nrc.right : "<<rc.right<<"\nrc.left : "<<rc.left<<"\n\n";
std::cout<<"pt1.y : "<< pt1.y<<"\npt2.y: "<< pt2.y<<"\npt2.x : "<<pt2.x<<"\npt1.x : "<<pt1.x<<"\n\n\n";
switch(LOWORD(wParam))
{
case SB_PAGEDOWN:
case SB_LINEDOWN:
sci += 10; break;
case SB_PAGEUP:
case SB_LINEUP:
sci -= 10; break;
case SB_THUMBTRACK:
sci = HIWORD(wParam); break;
};
sci = sci < 0 ? 0 : sci > 225 ? 225 : sci;
SetScrollPos(hwnd, SB_VERT, sci, FALSE);
//MoveWindow(lv_hwnd, pt1.x, pt1.y - sci + iPrevVscroll, pt2.x - pt1.x, pt2.y - pt1.y, TRUE);
EnumChildWindows(hwnd, edc, (LPARAM)sci);
}; return TRUE;
case WM_COMMAND:
if(LOWORD(wParam) == IDCANCEL) EndDialog(hwnd, wParam); return TRUE;
default: return FALSE;
}
}
BOOL CALLBACK edc(HWND hwnd, LPARAM lp)
{
long s = (long) lp;
RECT rc; GetWindowRect(hwnd, &rc);
POINT pt1 = {rc.left, rc.top}; ScreenToClient(hwnd, &pt1);
POINT pt2 = {rc.right, rc.bottom}; ScreenToClient(hwnd, &pt2);
MoveWindow(hwnd, pt1.x, pt1.y + s - iPrevVscroll, pt2.x - pt1.x, pt2.y - pt1.y, TRUE);
}
res.h
#define lv 1
#define dia 2
res.rc
#include <windows.h>
#include <commctrl.h>
#include <richedit.h>
#include "res.h"
dia DIALOGEX 0,0,500,300
CAPTION "New Invoice"
STYLE DS_3DLOOK | DS_CENTER | DS_MODALFRAME | DS_SHELLFONT | WS_CAPTION | WS_VISIBLE | WS_POPUP | WS_SYSMENU | WS_THICKFRAME | WS_VSCROLL
FONT 8, "Ms Shell Dlg"
{
}
in main.cpp you find the MoveWindow and other related functions at the bottom.
The following Images are useful.
also that the logic of scrolling in both MoveWindow is different, again, for the purpose of illustration.
Initially i was working on a project with many controls when i encountered this problem. I analysed this separately and found out what i have written above. Although I bypassed this problem through adopting a different method to scroll down all the controls(the one which do not includes calling MoveWindow from inside EnumChildWindows ), but i am curious to know the reason and the solution of this problem.
Thank you for your time on this long post. Any suggestions or improvement would also be amazing!
From the remarks section of EnumChildWindows() reference:
If a child window has created child windows of its own,
EnumChildWindows enumerates those windows as well.
So what you are doing here is scrolling the listview control and then also scroll the header control separately. The result is that the header control moves relative to the listview control as seen in your 2nd screenshot.
Instead you should only move immediate children of the dialog box, because grand children will move automatically with their parents.
Possible solutions:
Check parent/child relationship in the EnumChildWindows() callback (e. g. by calling GetParent() on the child and compare it with your dialog handle).
Instead of calling EnumChildWindows() and MoveWindow(), call ScrollWindowEx() with SW_SCROLLCHILDREN. This is the easiest way of implementing scrolling, so I would prefer this solution.

How to Create some basic controls like listbox on winapi c++

Am doing a small project in cpp using codeblocks ide on my windows 8.1 machine. Adding the menu was fine and even I went as far as adding some common controls like button, static, edit based on some of my notes from online sources.
main.cpp
#include <stdio.h>
#include <string.h>
#include <windows.h>
#include <winuser.h>
#define BTN_BUTTON 201
#define CMB_COMBOBOX 202
#define LST_LISTBOX 203
#define TXT_TEXTBOX 204
/* Declare Windows procedure */
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
/* Make the class name into a global variable */
char szClassName[ ] = "CodeBlocksWindowsApp";
int WINAPI WinMain (HINSTANCE hThisInstance,
HINSTANCE hPrevInstance,
LPSTR lpszArgument,
int nCmdShow)
{
HWND hwnd; /* This is the handle for our window */
MSG messages; /* Here messages to the application are saved */
WNDCLASSEX wincl; /* Data structure for the windowclass */
/* The Window structure */
wincl.hInstance = hThisInstance;
wincl.lpszClassName = szClassName;
wincl.lpfnWndProc = WindowProcedure; /* This function is called by windows */
wincl.style = CS_DBLCLKS; /* Catch double-clicks */
wincl.cbSize = sizeof (WNDCLASSEX);
/* Use default icon and mouse-pointer */
wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
wincl.lpszMenuName = NULL; /* No menu */
wincl.cbClsExtra = 0; /* No extra bytes after the window class */
wincl.cbWndExtra = 0; /* structure or the window instance */
/* Use Windows's default colour as the background of the window */
wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;
/* Register the window class, and if it fails quit the program */
if (!RegisterClassEx (&wincl))
return 0;
/* The class is registered, let's create the program*/
hwnd = CreateWindowEx (
0, /* Extended possibilites for variation */
szClassName, /* Classname */
"Code::Blocks Template Windows App", /* Title Text */
WS_OVERLAPPEDWINDOW, /* default window */
CW_USEDEFAULT, /* Windows decides the position */
CW_USEDEFAULT, /* where the window ends up on the screen */
544, /* The programs width */
375, /* and height in pixels */
HWND_DESKTOP, /* The window is a child-window to desktop */
NULL, /* No menu */
hThisInstance, /* Program Instance handler */
NULL /* No Window Creation data */
);
/* Make the window visible on the screen */
ShowWindow (hwnd, nCmdShow);
/* Run the message loop. It will run until GetMessage() returns 0 */
while (GetMessage (&messages, NULL, 0, 0))
{
/* Translate virtual-key messages into character messages */
TranslateMessage(&messages);
/* Send message to WindowProcedure */
DispatchMessage(&messages);
}
/* The program return-value is 0 - The value that PostQuitMessage() gave */
return messages.wParam;
}
/* This function is called by the Windows function DispatchMessage() */
LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message) /* handle the messages */
{
case WM_CREATE:
CreateWindow ("combobox", NULL,
WS_CHILD | WS_VISIBLE | CBS_DROPDOWNLIST,
20, 10, 400, 120,
hwnd, (HMENU)CMB_COMBOBOX, GetModuleHandle(NULL), NULL);
// If you uncomment the function below that I used to create a listbox see my program does not compile
/*
CreateWindow ("listbox", NULL,
WS_CHILD | WS_VISIBLE | LBS_NOTIFY | LBS_COMBOBOX,
20, 40, 140, 120,
hwnd, (HMENU)LST_LISTBOX, GetModuleHandle(NULL), NULL);
*/
CreateWindow("edit", NULL,
WS_VISIBLE | WS_CHILD | WS_BORDER | WS_VSCROLL| ES_MULTILINE | ES_AUTOHSCROLL,
170, 40, 250, 120,
hwnd, (HMENU)TXT_TEXTBOX, GetModuleHandle(NULL), NULL);
CreateWindow("button", "Submit This",
WS_CHILD | WS_VISIBLE | BS_DEFPUSHBUTTON,
300, 170, 120, 30,
hwnd, (HMENU)BTN_BUTTON, GetModuleHandle(NULL), NULL);
break;
case WM_DESTROY:
PostQuitMessage (0); /* send a WM_QUIT to the message queue */
break;
default: /* for messages that we don't deal with */
return DefWindowProc (hwnd, message, wParam, lParam);
}
return 0;
}
Now everything on my code works perfect if I omit the code for the listbox which i dearly want to be part of my code. In the function for creating ListBox the compile comments
error: "LBS_COMBOBOX" was not declared in this scope
yet it is okay with LBS_NOTIFY. I would appreciate if someone enlightened me on an issue prventing my cpp program from running. Am still a newbie in this and I could have overlooked on something.
This is the editted version
You should not be using LBS_COMBOBOX anyway, it is a internal style used by Windows:
The combo box itself must set this style. If the style is set by anything but the combo box, the list box will regard itself incorrectly as a child of a combo box and a failure will result.
Maybe your SDK does not include a define for this style because you should never be setting it.
Here is a quick one to help yoou create a listbox based on a tutorial on a link here http://winapi.foosyerdoos.org.uk/index.php
HWND CreateListbox(const HWND hParent,const HINSTANCE hInst,DWORD dwStyle, const RECT& rc,const int id,const ustring& caption)
{
//dwStyle|=WS_CHILD|WS_VISIBLE|WS_VSCROLL|LBS_DISABLENOSCROLL|LBS_NOSEL;
dwStyle|=WS_CHILD|WS_VISIBLE|WS_VSCROLL|LBS_DISABLENOSCROLL|LBS_NOTIFY;
return CreateWindowEx(WS_EX_CLIENTEDGE, _T("listbox"),
caption.c_str(),
dwStyle,
rc.left, rc.top, rc.right, rc.bottom,
hParent,
reinterpret_cast<HMENU>(static_cast<INT_PTR>(id)),
hInst, 0);
}
check out this links for quite some good examples you are yearning for:
Windows API tutorial for the C programming language
FoosYerDoos ~ WinAPI Programming
Relisoft Software - Windows Api Tutorial
theForger's Win32 API Programming Tutorial
Win32 API Tutorials
If you go throught those tutorials you should be able to get along quite well.

c++ progress bar marquee style not working

i tried to make the progress bar in c++ and it successfully worked with setting the position but when i tried to make it marquee i always get errors
#include <windows.h>
#include <commctrl.h>
#include <tchar.h>
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
HWND hWndButton;
HWND hEdit;
HWND hProgress;
char finalName[25];
char name[10];
int WINAPI WinMain (HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR lpCmd, int nCmdShow){
HWND hwnd; /* This is the handle for our window */
MSG messages; /* Here messages to the application are saved */
WNDCLASS wincl; /* Data structure for the windowclass */
ZeroMemory(&wincl, sizeof(WNDCLASS));
/* The Window structure */
wincl.hInstance = hInst;
wincl.lpszClassName = "Window Class";
wincl.lpfnWndProc = WindowProcedure; /* This function is called by windows */
wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;
/* Register the window class, and if it fails quit the program */
if (!RegisterClass (&wincl))
return 0;
/* The class is registered, let's create the program*/
hwnd = CreateWindow (
"Window Class", /* Classname */
("Code::Blocks Template Windows App"), /* Title Text */
WS_OVERLAPPEDWINDOW, /* default window */
433, /* Windows decides the position */
134, /* where the window ends up on the screen */
500, /* The programs width */
500, /* and height in pixels */
HWND_DESKTOP, /* The window is a child-window to desktop */
NULL, /* No menu */
hInst, /* Program Instance handler */
NULL /* No Window Creation data */
);
/* Make the window visible on the screen */
ShowWindow (hwnd, nCmdShow);
/* Run the message loop. It will run until GetMessage() returns 0 */
while (GetMessage (&messages, NULL, 0, 0))
{
/* Translate virtual-key messages into character messages */
TranslateMessage(&messages);
/* Send message to WindowProcedure */
DispatchMessage(&messages);
UpdateWindow(hwnd);
}
return 1;
}
/* This function is called by the Windows function DispatchMessage() */
LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message) /* handle the messages */
{
case WM_DESTROY:
PostQuitMessage (wParam); /* send a WM_QUIT to the message queue */
exit(1);
break;
case WM_CREATE:
UpdateWindow(hwnd);
hWndButton = CreateWindow("BUTTON", "Press Me!", WS_TABSTOP|WS_VISIBLE|WS_CHILD|BS_DEFPUSHBUTTON, 0, 25, 100, 25, hwnd, (HMENU)1, NULL, NULL);
hEdit = CreateWindow("EDIT", "Write your name!", WS_CHILD|WS_VISIBLE|ES_MULTILINE|ES_AUTOVSCROLL|ES_AUTOHSCROLL, 0, 0, 200, 25, hwnd, (HMENU)2, NULL, NULL);
hProgress = CreateWindow(PROGRESS_CLASS, NULL, WS_CHILD|WS_VISIBLE|PBS_MARQUEE, 0, 100, 500, 20, hwnd, (HMENU)3, NULL, NULL);
SendMessage(hProgress, PBM_SETMARQUEE, 1, 1000);
UpdateWindow(hwnd);
break;
case WM_COMMAND:
if(wParam == 1){
GetWindowText(hEdit, _T(name), 10);
strcpy(finalName, "Hello, ");
strcat(finalName, name);
MessageBox(hwnd, (LPCSTR)finalName, "Your Name", MB_OK|MB_ICONINFORMATION);
}
break;
default: /* for messages that we don't deal with */
return DefWindowProc (hwnd, message, wParam, lParam);
}
}
saying that PBS_MARQUEE and PBM_SETMARQUEE were not defined in that scope though i included the commctrl.h header file, what is the problem??
The marquee mode was new for Windows XP, so you must define NTDDI_VERSION to be at least NTDDI_WINXP to include those constants. See Using the Windows Headers for more information.

C++ many errors when defining string

Hello i am learning C++ and i have errors while making VISUAL APP NOT CONSOLE
My problem is at the string Num1 = "0";
Please explain to me why this is happening?
The string is 0 because i want to make calculator so i need to make Num1 + "TheClicked number"
<script>
#include <windows.h>
/* Declare Windows procedure */
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
/* Make the class name into a global variable */
char szClassName[ ] = "Andrey App";
int WINAPI WinMain (HINSTANCE hThisInstance,
HINSTANCE hPrevInstance,
LPSTR lpszArgument,
int nFunsterStil)
{
HWND hwnd; /* This is the handle for our window */
MSG messages; /* Here messages to the application are saved */
WNDCLASSEX wincl; /* Data structure for the windowclass */
/* The Window structure */
wincl.hInstance = hThisInstance;
wincl.lpszClassName = szClassName;
wincl.lpfnWndProc = WindowProcedure; /* This function is called by windows */
wincl.style = CS_DBLCLKS; /* Catch double-clicks */
wincl.cbSize = sizeof (WNDCLASSEX);
/* Use default icon and mouse-pointer */
wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
wincl.lpszMenuName = NULL; /* No menu */
wincl.cbClsExtra = 0; /* No extra bytes after the window class */
wincl.cbWndExtra = 0; /* structure or the window instance */
/* Use Windows's default color as the background of the window */
wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;
/* Register the window class, and if it fails quit the program */
if (!RegisterClassEx (&wincl))
return 0;
/* The class is registered, let's create the program*/
hwnd = CreateWindowEx (
0, /* Extended possibilites for variation */
szClassName, /* Classname */
"Andrey App", /* Title Text */
WS_OVERLAPPEDWINDOW, /* default window */
CW_USEDEFAULT, /* Windows decides the position */
CW_USEDEFAULT, /* where the window ends up on the screen */
544, /* The programs width */
375, /* and height in pixels */
HWND_DESKTOP, /* The window is a child-window to desktop */
NULL, /* No menu */
hThisInstance, /* Program Instance handler */
NULL /* No Window Creation data */
);
/* Make the window visible on the screen */
ShowWindow (hwnd, nFunsterStil);
/* Run the message loop. It will run until GetMessage() returns 0 */
while (GetMessage (&messages, NULL, 0, 0))
{
/* Translate virtual-key messages into character messages */
TranslateMessage(&messages);
/* Send message to WindowProcedure */
DispatchMessage(&messages);
}
/* The program return-value is 0 - The value that PostQuitMessage() gave */
return messages.wParam;
}
/* This function is called by the Windows function DispatchMessage() */
LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message) /* handle the messages */
{
string Num1 = "0";
case WM_CREATE:
{
CreateWindow(TEXT("button"), TEXT("Beep"),
WS_VISIBLE | WS_CHILD ,
20, 50, 80, 25,
hwnd, (HMENU) 1, NULL, NULL);
CreateWindow(TEXT("button"), TEXT("2"),
WS_VISIBLE | WS_CHILD ,
120, 50, 30, 30,
hwnd, (HMENU) 2, NULL, NULL);
CreateWindow(L"STATIC", Num1,
WS_CHILD | WS_VISIBLE | SS_LEFT,
20, 20, 300, 230,
hwnd, (HMENU) 1, NULL, NULL);
break;
}
case WM_COMMAND:
{
if (LOWORD(wParam) == 1) {
Beep(40, 50);
}
if (LOWORD(wParam) == 2) {
if(Num1 == 0){
Num1 = 2;
}
else{
Num1 + "2";
}
}
break;
}
case WM_DESTROY:
PostQuitMessage (0); /* send a WM_QUIT to the message queue */
break;
default: /* for messages that we don't deal with */
return DefWindowProc (hwnd, message, wParam, lParam);
}
return 0;
}
Here's how it should look:
//At the top of your .cpp file
#include<string>
LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static std::string Num1 = "0"; /* static string created once and keeps it contents for each function call */
switch (message) /* handle the messages */
{
case WM_CREATE:
{
CreateWindow(TEXT("button"), TEXT("Beep"),
WS_VISIBLE | WS_CHILD ,
20, 50, 80, 25,
hwnd, (HMENU) 1, NULL, NULL);
CreateWindow(TEXT("button"), TEXT("2"),
WS_VISIBLE | WS_CHILD ,
120, 50, 30, 30,
hwnd, (HMENU) 2, NULL, NULL);
///CreateWindow takes a LPCSTR as second arg, which is wchar_t under the covers, so need to convert
std::wstring wstr(Num1.begin(), Num1.end());
CreateWindow(L"STATIC", wstr.c_str(), //.c_str() returns a const char*
WS_CHILD | WS_VISIBLE | SS_LEFT,
20, 20, 300, 230,
hwnd, (HMENU) 1, NULL, NULL);
break;
}
case WM_COMMAND:
{
if (LOWORD(wParam) == 1) {
Beep(40, 50);
}
if (LOWORD(wParam) == 2) {
if(Num1 == "0"){ //Num1 is a string, so you should check a string
Num1 = "2";
}
else{
Num1 += "2";
}
}
break;
}
case WM_DESTROY:
PostQuitMessage (0); /* send a WM_QUIT to the message queue */
break;
default: /* for messages that we don't deal with */
return DefWindowProc (hwnd, message, wParam, lParam);
}
return 0;
}