|
Hi,
I have a button that is supposed to increase the size of an image when it's held down. I want it so that when the button is held down, I get a repeated event.
How do I know in my application that a button is held down, is there an event generated that is sent to the window procedure ?
|
|
|
|
|
You'll get a WM_CHAR message sent each time (a) a key is pressed or (b) a key has been held for long enough to trigger another 'key-pressed' event.
Here's a minimal WindowProcedure
LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_DESTROY:
PostQuitMessage (0);
break;
case WM_CHAR:
printf("Key Pressed: %c\n", wParam);
break;
default:
return DefWindowProc (hwnd, message, wParam, lParam);
}
return 0;
}
|
|
|
|
|
thanks for the reply,
but when i say button I mean a button control not a keyboard key.
I'm looking for an event such as WM_COMMAND that is generated while the button is pressed until the button is released. WM_COMMAND though just is generated on click not press as well.
|
|
|
|
|
Since the keyboard auto-repeat, some of the 'low'-level stuff is already done.
If however, you want a button control to have auto-repeat, you'll have to set a timer yourself. I'd imagine that you would set(create) the timer when the particular button was pressed. Each time the timer is triggered, you'd check to see if the button's state indicated that it was pressed. If so, simply send yourself another WM_COMMAND message.
HOWEVER, this task is somewhat complicated by the fact that the WM_COMMAND is not sent by a button until either the mouse-button or the keyboard key that was used to press it is released, meaning that your first event is not fired until after the button is no longer pressed, hence no ability to auto-repeat.
I suspect that you'll need to create this button as a custom-control, handling both the drawing (reasonably easy using the DrawThemeBackground(sp?) function) and the keyboard/mouse handling.
Mouse handling should be pretty straight forward, using SetCapture and ReleaseCapture. You'll have to also work out which keyboard keys you want to be able to press the button too, setting the button's state to BS_PRESSED.
This sounds like about the kind of functionality that the spin-button control offers, albeit with a single button rather than a pair of them. To that end, to find a solution quickly, I'd probably start looking for code for a custom spin-button control, editing as needed to fulfil your needs.
|
|
|
|
|
thanks,
I've managed to get the holding button down to work. I just used a global veriable to track the state of the button via the WM_DRAWITEM notification and then in my program loop referred to this variable.
|
|
|
|
|
Bewdy!
Happy to help you scramble over that little obstacle.
|
|
|
|
|
See if you get BN_PUSHED[^] and BN_UNPUSHED[^] from the button, these are, as the documentation states, provided only for compatibility with 16-bit versions of Windows, so they might not work anymore, but if they do, you could try starting the timer with SetTimer[^] on BN_PUSHED and kill it with KillTimer[^] on BN_UNPUSHED, and do your zooming thing in the handler for WM_TIMER[^].
> The problem with computers is that they do what you tell them to do and not what you want them to do. <
> If it doesn't matter, it's antimatter.<
|
|
|
|
|
Hi all,
I am trying to read from text file using fscanf.But i am getting only last string.
Here is the code i am trying
FILE *p;
CString first,second;
p=fopen("E:\\Test.txt","r");
fscanf(p,"FirstName=%s",first);
fscanf(p,"SecondName=%s",second);
AfxMessageBox(first);
AfxMessageBox(second);
Here is the Text.txt file content:
FirstName=ee
SecondName=pp
But i am getting only first value "ee" in message box.where i am going wrong
Thanks
manju
|
|
|
|
|
Its been a long time since I used these kinds of funcs, but I would say that like fgets it will read untill it hits an end of line. Can you advance the file pointer?
==============================
Nothing to say.
|
|
|
|
|
You cannot use CString like this because no memory is internally allocated to it.
That is causing the problem here.
Here is something that could work -
FILE *p;
char first[100];
char second[100];
p=fopen("E:\\Test.txt","r");
fscanf(p,"FirstName=%s\r\n",first);
fscanf(p,"SecondName=%s",second);
AfxMessageBox(first);
AfxMessageBox(second);
However, I really think you should start using C++ file streams.
|
|
|
|
|
You must specify the end-of-line in the fscanf format string and, as the superhero already pointed out, you cannot use CString s that way, try:
FILE *p;
CString first,second;
p = fopen("E:\\Test.txt","r");
assert(p);
first.GetBuffer(MAX_PATH); second.GetBuffer(MAX_PATH); assert(first.GetAllocLength()== MAX_PATH && second.GetAllocLength()==MAX_PATH);
fscanf(p,"FirstName=%s\n",first);
fscanf(p,"SecondName=%s\n",second);
first.ReleaseBuffer();
second.ReleaseBuffer();
AfxMessageBox(first);
AfxMessageBox(second);
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
Since you are using MFC, why not use CStdioFile instead?
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Some people are making such thorough preparation for rainy days that they aren't enjoying today's sunshine." - William Feather
|
|
|
|
|
Hi Developers,
I am using a multi column list box( not list control ) and i have to handle column click because i want to sort the records accordingly, if i am clicked on any column.
Will the sorting work automatically if i click on the column or not?
Please give me some hints.
Thanks.
Amrit Agrawal
Software Developer.
|
|
|
|
|
Are you sure you mean List Box[^] rather than List View[^]? A List Box only contains a single list of items, displayed either vertically or horizontally.
|
|
|
|
|
I am sorry Richard, that is a list control. If I want to handle column click so that i can sort it contents accordingaly, how can i do it.
Thanks.
Amrit Agrawal
Software Developer
|
|
|
|
|
Your first message stated that you were using List Box (not List Control) and I asked you to confirm whether you actually mean List Box or List View. You now say that is it list control so I am still not sure. However, I will make an educated guess that you actually do mean a List View[^] control. In that case you should check the documentation (follow the foregoing link) for the LVN_COLUMNCLICK notification, and also the LVM_SORT??? messages.
|
|
|
|
|
Richard MacCutchan wrote: Your first message stated that you were using List Box (not List Control) and I asked you to confirm whether you actually mean List Box or List View. You now say that is it list control so I am still not sure. Don't you just love playing that game!
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Some people are making such thorough preparation for rainy days that they aren't enjoying today's sunshine." - William Feather
|
|
|
|
|
Maybe it's my questions that are wrong.
|
|
|
|
|
Could be your accent.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Some people are making such thorough preparation for rainy days that they aren't enjoying today's sunshine." - William Feather
|
|
|
|
|
But I don't have one.
|
|
|
|
|
As others have mentioned, ListBox controls don't allow for multiple columns of data for each item. A ListBox is scrollable in one direction or the other, while a ListView is scrollable in both directions.
Windows explorer uses a ListView - right-click inside the client area of the window and you can change the view to Details - this gives a heading control and several columns per file - size, date, name etc, etc.
In any case, no - sorting won't work automatically when you click on the column header.
You'll get a notification of LVN_COLUMNCLICK via the WM_NOTIFY message. It's then up to you to determine which column was clicked on and whether you want to sort in ascending or descending order.
Having done so, you would then call ListView_SortItems with a pointer to the appropriate sorting function, the HWND of the control and an application-defined lParam value.
There are a couple of ways to go about it - typical uses the lParam value may be to flag whether the sorting is in ascending or descending order. You could also get clever, and use negative values for sorting in one direction and positive numbers for sorting in the other direction. Abs(lParam) could be used to hold the column index that you wish to perform the sort on.
Another approach may be to have one sorting function per column, calling ListView_SortItems using the appropriate function each time.
Ensuring that you give each ListView item a lParam value when adding them, gives you an identifier that can be used to extract the text from any column, which is then used for sorting.
Perhaps you'd like to look over the following code:
#include <pre><windows.h>
#include <stdio.h>
#include <commctrl.h>
#define IDC_ListView1 10001
enum gender {male=0, female};
typedef struct person_t
{
char *firstName;
char *lastName;
int age;
gender sex;
};
person_t peopleList[] =
{
{"Micky", "Mouse", 61, male},
{"Donald", "Duck", 59, male},
{"Betty", "Boo", 34, female},
{"Olive", "Oil", 32, female},
{"Clark", "Kent", 29, male}
};
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
char szClassName[ ] = "CodeBlocksWindowsApp";
BOOL insertListViewColumn(HWND listHwnd, char *columnText, int colIndex)
{
CHAR szText[256];
LVCOLUMN lvc;
int iCol;
lvc.mask = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM;
iCol = colIndex;
lvc.iSubItem = iCol;
lvc.pszText = columnText;
lvc.cx = 70;
lvc.fmt = LVCFMT_LEFT;
if (ListView_InsertColumn(listHwnd, iCol, &lvc) == -1)
return FALSE;
return TRUE;
}
void addListViewItem(HWND hWndListView, char *pszText, int iItem, int iSubItem, int iImage=-1)
{
LVITEM lvItem;
ZeroMemory(&lvItem, sizeof(LVITEM));
lvItem.mask = LVIF_TEXT;
lvItem.iItem = iItem;
lvItem.iSubItem = iSubItem;
lvItem.pszText = pszText;
lvItem.cchTextMax = strlen(pszText);
if (iImage != -1)
{
lvItem.mask |= LVIF_IMAGE;
lvItem.iImage = iImage;
}
if (iSubItem == 0)
{
if (ListView_InsertItem(hWndListView, &lvItem) == -1)
return;
}
else
{
if (ListView_SetItem(hWndListView, &lvItem) == -1)
return;
}
}
BOOL InsertPersonIntoListView(HWND hWndListView, person_t person)
{
static int curItemIndex = 0;
char szBuffer[256];
addListViewItem(hWndListView, person.firstName, curItemIndex, 0, -1);
addListViewItem(hWndListView, person.lastName, curItemIndex, 1, -1);
sprintf(szBuffer,"%d", person.age);
addListViewItem(hWndListView, szBuffer, curItemIndex, 2, -1);
sprintf(szBuffer,"%s", person.sex==male?"male":"female");
addListViewItem(hWndListView, szBuffer, curItemIndex, 3, -1);
curItemIndex++;
}
int WINAPI WinMain (HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR lpszArgument, int nCmdShow)
{
HWND hwnd;
MSG messages;
WNDCLASSEX wincl;
wincl.hInstance = hInst;
wincl.lpszClassName = szClassName;
wincl.lpfnWndProc = WindowProcedure;
wincl.style = CS_DBLCLKS;
wincl.cbSize = sizeof (WNDCLASSEX);
wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
wincl.lpszMenuName = NULL;
wincl.cbClsExtra = 0;
wincl.cbWndExtra = 0;
wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;
if (!RegisterClassEx (&wincl))
return 0;
hwnd = CreateWindowEx(0, szClassName, "ListView Sorting demo", WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, 426, 154,
HWND_DESKTOP, NULL, hInst, NULL);
HWND btnWnd, listViewWnd;
InitCommonControls();
listViewWnd = CreateWindow(WC_LISTVIEW, "", WS_CHILD|WS_VISIBLE|LVS_REPORT, 5,5, 400,100, hwnd, (HMENU)IDC_ListView1, hInst, 0);
insertListViewColumn(listViewWnd, "First Name", 0);
insertListViewColumn(listViewWnd, "Last Name", 1);
insertListViewColumn(listViewWnd, "Age", 2);
insertListViewColumn(listViewWnd, "Sex", 3);
for (int i=0; i<sizeof(peopleList)/sizeof(peopleList[0]); i++)
InsertPersonIntoListView(listViewWnd, peopleList[i]);
ShowWindow (hwnd, nCmdShow);
while (GetMessage (&messages, NULL, 0, 0))
{
TranslateMessage(&messages);
DispatchMessage(&messages);
}
return messages.wParam;
}
LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int idControl;
char szText[256];
NM_LISTVIEW *pnmv;
switch (message)
{
case WM_DESTROY:
PostQuitMessage (0);
break;
case WM_NOTIFY:
idControl = (int)wParam;
if (idControl == IDC_ListView1)
{
pnmv = (NM_LISTVIEW*) lParam;
if (pnmv->hdr.code == LVN_COLUMNCLICK)
{
sprintf(szText, "Column %d clicked", pnmv->iSubItem);
MessageBox(hwnd, szText, "ListView WM_NOTIFY message", MB_OK);
}
}
break;
default:
return DefWindowProc (hwnd, message, wParam, lParam);
}
return 0;
}
|
|
|
|
|
How to use ShellExecute/ShellExecuteEx for windows7
I want to use it to run my exe as administrator and want to give my own window.
Is there any example available
SHELLEXECUTEINFO TempInfo = {0};
TempInfo.cbSize = sizeof(SHELLEXECUTEINFO);
TempInfo.fMask = 0;
TempInfo.hwnd = NULL;
TempInfo.lpVerb = _T("runas");
TempInfo.lpFile = "filename";
TempInfo.lpParameters = _T("runasadmin");
TempInfo.lpDirectory = "path";
TempInfo.nShow = SW_NORMAL;
BOOL bRet = ::ShellExecuteEx(&TempInfo);
|
|
|
|
|
|
You can also find some information here:
UAC[^]
Philippe Mori
|
|
|
|
|
Thanks for providing links
I am using dialog box application
when i pass window handle of APP->Main window in SHELLEXECUTEINFO program crash
i want to display my own window instead of window given by SellExecuteEx
|
|
|
|
|