I never said it was a good way! I agree with you that it isn't good for determining the state of other objects, but it was just a quick and dirty idea that could work if managed correctly (i.e., good synchronization and exception handling).
As I was helping someone else with a similar problem (actually, it might've been the same guy), I did find a way to determine which controller is being used without resorting to reflection. The PrintDocument class has a PrintController property. This could be used in the event handlers (or in OnEventName methods if you encapsulate the PrintDocument) to determine which PrintController - like the PreviewPrintController - is being used as the output device.
I'm trying to write an application in C# that will allow me to change to the desktop wallpaper. I would also like to place a calendar control on the Desktop using this application. I tried adding a reference to shell32.dll in Visual Studio 2003 but the IActiveDesktop interface is not listed by VS when I examine the reference.
Any suggestions as to how I may go about getting access to the IActiveDesktop from C#??
Using the IActiveDesktop interface, you'd have to declare it manually using various attributes from System.Runtime.InteropServices and go to a lot of trouble creating the instance from CLSID_ActiveDesktop (defined in shlguid.h) when only a simple function call is needed. See the link above for an example. This was the 3rd result when I googled for "C# desktop wallpaper".
Thanks for the reply! I did see that link and it does work for changing hte wallpaper. However I think I may have to go the long way as I would like to have an effect like WebShots where I can have a Calnedar control displayed on the desktop.
Would you recommend I go with C# or C++ to implement this control display? I'd really love to do it with C# to see what is involved though so If you can point me to any useful resources and how to start that would be appreciated.
First of all, read the documentation for the System.Runtime.InteropServices elements in the .NET Framework SDK documentation. It's important to understand that. There's also a couple articles about COM interop in .NET that you should read in the articles section of the .NET Framework SDK documentation.
You should also be familiar with some basic COM, since you'll be using a lot of this to use the IActiveDesktop interfaces. There are several articles here on CP that should be useful.
Honestly, though, the WebShots calendar is nothing more than a calendar drawn onto an image that is set as wallpaper. It is not an desktop item (at least, not last time I checked). It merely loads an image, paints a calendar on that, saves that image to a temporary spot and sets that as the wallpaper. Again, using the SystemParametersInfo method would be much easier. Just use the System.Drawing classes to load an image (Bitmap class) and draw on it (Graphics class), then save it and use the method. Simple.
I thought that Calendar was a control! Well it seems then that I should be able to get this done using C#. I'll have a look on those articles you mentioned. I am currently reading "Essential COM" by Dan Box to learn about COM.
Thanks again for all the info, I'll do some reasing and see what I can brew
A few days ago I invested in Visual C#.NET with a purpose of learning how to build Office add-ins. I purchased the standard, educational edition, not the professional edition which includes VB and C++.
The edition of Visual Studio I have does not have the add-in wizard that the professional edition has.
With that in mind, how can I create my add-ins?
In other words, how do I build add-ins to office without the wizard using the standard edition of Visual C#?
Thanks Michael, the link looks promising, I am looking into it. The Wizard I am talking about is called "Shared Add-in" and can be found by selecting "New Projects" from the file menu, then "Other Projects" and "Extensibility Projects".
All the documentation that I have on add-ins points to this wizard, which I don't have, hence my problem.
I wrote an application that writes to disk a certain excel document from sql database, then opens it edit.
After user finished editing the document in Excel, the application asks in a dialog if user wants to update the changes to DB. If yes, updates the file in the database.
The problem is, that after Excel window is closed and file updated to db, the Excel process is still running. It can be seen in Process Manager,
and causes incorrect behaviour of new Excel windows. After opening 50 documents, there are 50 processes. That causes serious errors in Excel, up to "OLE library error", which can be cured so far by re-installing Office.
The question is: How to kill Excel properly?
Here is the code:
private void OpenExcel(string filename)
System.Diagnostics.Process myproc = new System.Diagnostics.Process();
myproc.EnableRaisingEvents = true;
//a special exitbox is assigned to each document
ExitBox windowAfterExit = new ExitBox(filename,this,myproc);
// at a moment we can work with one document only
publicclass ExitBox : System.Windows.Forms.Form
publicvoid Excel_Exited(object sender,EventArgs e)
//Excel exited, so show this windowthis.Visible = true;
//tried this to kill Excel
//all necessary changes are submitted to Db, so delete the file
//close the form(hope it disposes all resources too :) ) this.Close();
I am looking for a way to get the equivalent functionality of WIN32's DrawText() API function when used with DT_CALCRECT and DT_WORDBREAK.
I am writing a control that will resize itself vertically on a form when its Text property is changed. I have tried using the Graphics.MeasureString() method and I cannot figure out how to get it to return the height of a string to a fixed width.
See the StringFormat class (the defaults should do, but you can create your own) and the Graphics.DrawString method, which you can use in the OnPaint method override (always override methods instead of handling events in derived classes, but make sure to call the base class's method in most cases):
protectedoverridevoid OnPaint(PaintEventArgs e)
RectangleF rectF = GetRectFBoundsForControl(); // Made-up method to// get the bounds in which text can be drawn.
e.Graphics.DrawString("Some multi-line text",
StringFormat.GenericDefault); // Or create your own
Otherwise, you can always just P/Invoke DrawText, define the DT_CALCRECT and DT_WORDBREAK constants (or enum values) based on the preproc def values, and call the method using the control's HDC, which you can get during the Paint event (override OnPaint when deriving from Control instead):
ShaneStump wrote: I want it to return the # height of the string if wrapping occurs (if the string requres 5 lines, I want the returned height to be for the 5 lines). I am probably NOT using the right method.
Hmm, the MeasureString method is overloaded, and if you know the maximum width in pixels that your control will represent (I don't know if you will know this?) then you can make a call as such which will take into account wrapping.
private SizeF MyMeasureMethod(PaintEventArgs e)
string s = "Nick";
Font f = new Font("Arial", 12);
int width = 300;
SizeF strSize = new SizeF();
strSize = e.Graphics.MeasureString(s, f, width);
How does one programmatically "select" a node in a TreeView.
IE: the user clicks a button which spawns a child process, the output of which is displayed in a TextBox. Each time the user clicks that button, a new process is kicked off and I attach a new node to the TreeView to represent said process. If the user clicks on any given node, that node's associated process output TextBox is brought to the foreground - whre you can watch the output.
Adding the node to the TreeView is straightforward, but hilighting it or "selecting" it programmatically is not so easy.
Active and Selected are two completely different things. Active just means that the node has the focus, such as what it gains when you right-click the node. Selected means that the node has been left-clicked at some point. To select a node programmatically, just use the following:
After doing this - indeed the "selected" image appears to the left of the tree node (from the ImageList index used when creating the node) but, the actual text of the node is not "highlighted. IE: the folder opens, but the folder name is not highlighted.
This is even more obvious when working with a ListView. I've selected a particular ListViewItem, but no rectangular, reverse color block around the text of the item appears onscreen.
Maybe I am doing something wrong - or missing something? Without the highlighted text, its not really obvious which element has been selected.
Out of curiousity, does the rectangular marquee (the dotted lines) around the text appear? If so, the TreeView no longer seems to have the focus. Set TreeView.HideSelection to false and see if the rectangular selection region is at least gray (or whatever the default system color for inactive selected text is).