Moving a listview item from one group to another - winapi

I have created a listview in Win32 with group view enabled, I can add Items to a group, but I Have multiple groups and I want an item to pass from group A to group B. I have found the macro ListView_MoveItemToGroup, I call it like so
int i =0;
i = m_taskList.GetSelectedIndex();
bool ret = ListView_MoveItemToGroup(m_taskList.m_hWnd,i,LVI_GROUPID_EXEC);
but ret is false; What I am doing wrong, the LVI_GROUP_EXEC is added to the litview.

The LVM_MOVEITEMTOGROUP message return TRUE if successful and FALSE otherwise, so either SendMessage() is failing, or the message itself is failing or more likely is unrecognized on your version of Windows. Use GetLastError() to differentiate between a message failure and a SendMessage() failure.
If you are not seeing the item jump between groups, you will likely have to just remove the item from the ListView first and then re-add it using the desired group.

Related

Outlook VSTO Add-in: Removing a lot of recipients is too slow and not always work

I have an VSTO Outlook Add-in. In the compose windows I have a button. When this button is clicked the recipients that satisfy a condition are removed. The recipients that need to be deleted are stored in a list, that is, List<Outlook.Recipient>. I iterate this list and remove each recipient.
foreach (Outlook.Recipient recipient in this.RecipientsList)
{
this.MyMailItem?.Recipients?.Remove(recipient.Index);
}
// clear all the recipients
this.RecipientsList.Clear();
I have noted that the more recipients need to be removed the more slower is and also not always all the recipients contained in the list are removed, sometimes yet and sometimes not. Two things here:
How can I optimize the speed for removing recipients?
Why sometimes all the recipients contained in the list are not being removed? It's randomly, sometimes yes, sometimes not.
Note that this.MyMailItem is of type Outlook.MailItem and this.MyMailItem.Recipients is of type Outlook.Recipients.
Remove function requires an integer as a parameter, this is what its definition says, see here.
Keeping Outlook COM objects in the collections is not really a good idea. Instead, you can keep their indexes and etc.
In the following loop:
foreach (Outlook.Recipient recipient in this.RecipientsList)
{
this.MyMailItem?.Recipients?.Remove(recipient.Index);
}
The recipient object is ignored!
You could use the following:
foreach (Outlook.Recipient recipient in this.RecipientsList)
{
recipient.Delete();
}
The Recipient.Delete method deletes an object from the collection.
The rule of thumb is to release underlying COM objects instantly. In that case you may be sure that objects are released timely. For example, in the code each time in the loop you get the Recipients collection which increases the reference counter:
this.MyMailItem?.Recipients
I'd recommend keeping the collection and only deal with a specific items in the loop:
Outlook.Recipients recipients = this.MyMailItem?.Recipients;
// in the loop you can call the Remove method
recipients.Remove(recipient.Index);
Also you may try to get a specific recipient instance in the loop and delete it. After that you also need to release an underlying COM object:
Outlook.Recipient recipient = null;
Outlook.Recipients recipients = this.MyMailItem?.Recipients;
for(int i = 1; i<recipients.Count;i++)
{
recipient = this.RecipientsList.Item(i);
recipient.Delete();
Marshal.ReleaseComObject(recipient);
recipient = null;
}
Recipient count is changing dynamically inside for loop so not all recipients get cleared in for loop. i used below code as a workaround.
while (recipients.Count>0)
{
recipients.Remove(recipients.Count);
}

signature and functionality of selection callback of treeview in gtkmm

I have a treeview and want to get notified if the selection changes. What is the signature for the callback?
I found a code snippet like:
Gtk::TreeView *treeview = Gtk::manage(new Gtk::TreeView);
Glib::RefPtr< Gtk::TreeSelection > sel = treeview->get_selection();
sel->set_mode( Gtk::SELECTION_MULTIPLE );
sel->set_select_function(sigc::ptr_fun(&SelFun));
But I can't find anything about the SelFun!
How is the signature
How to find out which rows and columns are selected inside this function?
How to access data from the model with that object
Yes, I have actually no idea how the TreeView/Model/Path/Selection interacts. Every link to an example is highly welcome!
You seem to want multiple selection. I had the same problem too. Once you have enabled mutliple selection, getting the selected rows is a little more difficult. The method of acquiring them varies slightly.
I'll provide the most general method. First, you need to overload the signal_changed() signal after you have enabled multiple selection. Then, assign the TreeView's TreeSelection to a RefPtr for easy access.
Glib::RefPtr<Gtk::TreeSelection> TreeView_TreeSelection;
TreeView_TreeSelection = your_TreeView.get_selection();
Next, connect the TreeSelection to the signal_changed() signal.
TreeView_TreeSelection -> signal_changed().connect(sigc::mem_fun(your_TreeView,
&your_Class::on_selection_changed));
Now, make sure to make a void function header in "your_Class" named on_selction_changed() or whatever you want. Just make sure to change the name in the connection above to whatever your class' name is.
The final step is to make the function. Here is a simple example of getting a vector of all of the TreePaths of the rows selected, then converting those TreePaths into a vector of TreeModel::Row pointers.
void your_Class::on_selection_changed()
{
if((TreeView_TreeSelection -> count_selected_rows()) == 0)
{
return;
}
vector<Gtk::TreeModel::Path> selected_rows = TreeView_TreeSelection -> get_selected_rows();
vector<Gtk::TreeModel::Row*> selected_TreeRows;
vector<Gtk::TreeModel::Path>::iterator TreePath_iterator = selected_rows.begin();
Gtk::TreeRow *row;
while(TreePath_iterator != selected_rows.end()
{
selected_row_it = p_TreeModel -> get_iter(TreePath_iterator);
row = (*selected_row_it);
selected_TreeRows.push_back(row);
TreePath_iterator++;
}
}
Do you know how to iterate through a TreeModel using the STL-like contain API called children() of a TreeModel? It's most useful for iterating over all of the rows of a TreeModel or getting the size (AKA row count) of a TreeModel. Its use depends on whether you're using a ListStore, TreeStore or a custom TreeModel.

How to get a value of setting "gps location enabled" in windows phone?

While my app is working - I need to check, that GPS-location is on.
I DO NOT need any complex things with statusChanged and other - just check a value of setting.
If that setting is off - I must stop execute current operation. I got one method, which called every time, when user execute operation. And I wanna place check rule there. So - it must be fast and cheap.
Is there any human way? Without keeping GPSWatcher ON all the time, and check his last status. I must care about battery.
Thanks.
You can use this:
Geolocator geolocator = new Geolocator();
if (geolocator.LocationStatus == PositionStatus.Disabled)
{
...
}
According to the documentation
var accessStatus = await Geolocator.RequestAccessAsync();
switch (accessStatus)
{
case GeolocationAccessStatus.Allowed:
break;
case GeolocationAccessStatus.Denied:
_
break;
case GeolocationAccessStatus.Unspecified:
break;
}

emit code crashes at runtime but not in debug, why

I have some code, that build up a proxy from a type. It work perfekt.
Then I have add in the setter emit code, that it has to push a isDirty bit, when it is call. This fail, why?
If I run the code without the isDirty bit, it works.
If I run the code with the isDirty bit, it works in debug, but is start the disassembly window up in visual studio.
If I run the code with the isDirty (without-debug) the program crash (not responding) but when I hit cancel, it starts working and show all de rigth data.
PropertyBuilder property = proxy.DefineProperty(propertyInfo.Name, propertyInfo.Attributes, propertyInfo.PropertyType, null);
MethodAttributes attributes = MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.SpecialName | MethodAttributes.Virtual;
MethodBuilder setMethod = proxy.DefineMethod("set_" + propertyInfo.Name, attributes, typeof(void), new Type[] { propertyInfo.PropertyType });
ILGenerator setIL = setMethod.GetILGenerator();
setIL.Emit(OpCodes.Ldarg_0); // load this on the stack - where this is the type we are creating
setIL.Emit(OpCodes.Ldarg_1); // load first parameter on the stack
setIL.Emit(OpCodes.Call, propertyInfo.GetSetMethod());
{
//error here: when this is uncomment, it fails
// // set the isDirty bit
// setIL.Emit(OpCodes.Ldarg_0); // load this on the stack
// setIL.Emit(OpCodes.Ldc_I4_1, 1); // push a number on the stack
// setIL.Emit(OpCodes.Stfld, isDirtyField); // save the value on the stack in field
}
setIL.Emit(OpCodes.Ret);
property.SetSetMethod(setMethod);
I have a hard time, seeing why this fails? Need some help from the experts :)
// dennis
I'm not sure if this is your only issue, but you are using the wrong Emit overload when emitting your Ldc_I4_1 opcode. You should do either:
setIL.Emit(OpCodes.Ldc_I4_1)
or
setIL.Emit(OpCodes.Ldc_I4, 1)
The first option will result in a slightly smaller IL method body since it uses a specialized opcode, whereas the second one is not specific to the number being loaded.

Where can I find a good example of ReportEvent function usage?

As with most "legacy" MSDN pages, the page for ReportEvent has too little information for me to make much sense of it. I've tried searching, but can't find a good, clean, simple example of the function's usage. Could anyone suggest one?
I ended up using this:
HANDLE eventLog;
WORD type;
const char* msg;
// ... snip ...
ReportEvent(eventLog, type, 0, 1, NULL, 1, 0, &LPCTSTR(msg), NULL);
Seems to work well enough.
Well this seems to be a very old thread, landed here looking for a good example of Report Event... but figured out you have not received any replies...and would have probably already found the solution.
The reason you see "Event Id not found" is because the EventViewer is not able to load/lookup the text resource to be displayed for the event Id. Sorry if last line sounded geeky.. but this is what i understand of EventLog:
-EventLogging has two aspects
Registering with EventLog (or in other terms creating EventSource)
Logging or Writing into Event Log
Viewing or Reading from log
When you register in event log, you simply specify a eventSource (any name that identifies that log) + EventMessageFile, Category File and SupportedEventTypes. Here EventMessageFile points to a DLL/EXE that contains your message descriptions/resources.
When you log an event, you simply log it with some data like EventID, Category ID and EventData. But when you view it using any EventViewer (or Windows eventVwr.exe) the viewer reads your events, looks for a DLL/EXE associated with your eventSource(pointed by EventMessageFile), and renders the decription from the resource section of that DLL/EXE.
This DLL is nothing but a simple resource file that was compiled using MessageCompiler, and contains a "MessageTable". This is done to provide culture specific event logging
This is the reason, When you export the log into XML/TXT etc from your EventViewer, It asks you if you want to Save it "with Display informaion" or "without display information", so that you can view it on computers that do not have EventMessageFile.
JFYI the reg entry is located at:
HKLM\CurrentControlSet\System\Services\EventLog\Application
one catch: If you're wondering how .Net does it..., it simply does it by providing a default EventMessageFile called EventLogMessage.dll (found under %SYSTEMROOT%\Microsoft.Net\Framework\vXXXX\)
As I recall it is a pain to set up correctly - you need to add messages to you application using the Message Compiler - if you skip this you won't see useful messages only error codes. Take a look at Creating a Windows NT Service by Using ATL for an example
The sample Windows Service C++, is a windows service reporting to event log, you can get the code from https://code.msdn.microsoft.com/windowsapps/CppWindowsService-cacf4948
in particular, the following function (quoted from ServiceBase.cpp) does it
//
// FUNCTION: CServiceBase::WriteEventLogEntry(PWSTR, WORD)
//
// PURPOSE: Log a message to the Application event log.
//
// PARAMETERS:
// * pszMessage - string message to be logged.
// * wType - the type of event to be logged. The parameter can be one of
// the following values.
//
// EVENTLOG_SUCCESS
// EVENTLOG_AUDIT_FAILURE
// EVENTLOG_AUDIT_SUCCESS
// EVENTLOG_ERROR_TYPE
// EVENTLOG_INFORMATION_TYPE
// EVENTLOG_WARNING_TYPE
//
void CServiceBase::WriteEventLogEntry(PWSTR pszMessage, WORD wType)
{
HANDLE hEventSource = NULL;
LPCWSTR lpszStrings[2] = { NULL, NULL };
hEventSource = RegisterEventSource(NULL, m_name);
if (hEventSource)
{
lpszStrings[0] = m_name;
lpszStrings[1] = pszMessage;
ReportEvent(hEventSource, // Event log handle
wType, // Event type
0, // Event category
0, // Event identifier
NULL, // No security identifier
2, // Size of lpszStrings array
0, // No binary data
lpszStrings, // Array of strings
NULL // No binary data
);
DeregisterEventSource(hEventSource);
}
}

Resources