|
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.
-----BEGIN GEEK CODE BLOCK-----
Version: 3.21
GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++
-----END GEEK CODE BLOCK-----
|
|
|
|
|
Greets all,
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#??
.:. Keno .:.
|
|
|
|
|
There's a much simpler way: http://www.c-sharpcorner.com/Code/2002/Sept/ChangeWallpaper.asp[^]
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".
-----BEGIN GEEK CODE BLOCK-----
Version: 3.21
GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++
-----END GEEK CODE BLOCK-----
|
|
|
|
|
Heath,
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.
.:. Keno .:.
|
|
|
|
|
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.
Nick Parker has a pretty good one dealing with some of the issues: http://www.codeproject.com/dotnet/nettocom.asp[^]
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.
-----BEGIN GEEK CODE BLOCK-----
Version: 3.21
GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++
-----END GEEK CODE BLOCK-----
|
|
|
|
|
Great info there Heath!
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
.:. Keno .:.
|
|
|
|
|
Hello all,
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#?
Thank you for your help...:
Peter
|
|
|
|
|
|
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.
Thanks again,
Peter
|
|
|
|
|
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?
TIA
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);
myproc.Exited+=new EventHandler(windowAfterExit.Excel_Exited);
myproc.StartInfo.WorkingDirectory = Environment.CurrentDirectory;
myproc.StartInfo.FileName = filename;
myproc.StartInfo.Verb = "open";
//open excel
myproc.Start();
// at a moment we can work with one document only
myproc.WaitForExit();
}
public class ExitBox : System.Windows.Forms.Form
{
...
public void Excel_Exited(object sender,EventArgs e)
{
this.Visible = true;
}
private void Exit()
{
myproc.Dispose();
DeleteFile(filename);
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.
Any help would be appreciated.
Happy Holidays,
Shane
|
|
|
|
|
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):
protected override void OnPaint(PaintEventArgs e)
{
RectangleF rectF = GetRectFBoundsForControl();
e.Graphics.DrawString("Some multi-line text",
this.Font,
new SolidBrush(SystemColors.ControlText),
rectF,
StringFormat.GenericDefault);
} 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):
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
IntPtr hdc = e.Graphics.GetHdc();
DrawText(hdc, "Some Text", ...);
e.Graphics.ReleaseHdc(hdc);
} You'll also need to define the RECT structure to use in managed code. See the StructLayoutAttribute for details.
-----BEGIN GEEK CODE BLOCK-----
Version: 3.21
GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++
-----END GEEK CODE BLOCK-----
|
|
|
|
|
Thanks for responding but you did NOT read what I was asking for.
I wasn't asking how to output text. I am asking how to get the SIZE of the text BEFORE it is displayed (i.e. in WIN32, DrawText(Handle,string,string length,RECT *,DT_WORDWRAP | DT_CALCRECT)).
I have NOT had any luck with the Graphic's class MeasureString() method.
Happy Holidays,
Shane
|
|
|
|
|
ShaneStump wrote:
I have NOT had any luck with the Graphic's class MeasureString() method.
Specifically what problems are you having with MeasureString() ? Have you tried to P/Invoke DrawText() ?
- Nick Parker My Blog
|
|
|
|
|
MeasureString() doesn't return what I want (or I can't get it to).
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.
Happy Holidays,
Shane
|
|
|
|
|
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);
return strSize;
}
- Nick Parker My Blog
|
|
|
|
|
Thank you very much (I didn't realize passing in just a width would return the calculated height)!
Happy Holidays,
Shane
|
|
|
|
|
I'm fairly new to C# and WinForms programming.
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.
Suggestions?
-Luther
|
|
|
|
|
Isn't there a:
treeview.setActiveNode(node) method?
if you create the node as a variable first, then add it, and set the variable to the active node, it should do it.
I had this problem with seting the active node of a tree view when it first gets control.
Cata
|
|
|
|
|
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:
treeview.SelectedNode = someNode;
-----BEGIN GEEK CODE BLOCK-----
Version: 3.21
GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++
-----END GEEK CODE BLOCK-----
|
|
|
|
|
That's what I was looking for.
|
|
|
|
|
The obvious escaped me.
Thank you both.
-LutherB
|
|
|
|
|
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.
Thanks,
-Luther
|
|
|
|
|
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).
-----BEGIN GEEK CODE BLOCK-----
Version: 3.21
GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++
-----END GEEK CODE BLOCK-----
|
|
|
|
|
Excellent - a grey "highlight" block appears around the text now - for both the visible ListView and TreeView.
Its not the normal (per my theme) "blue" highlight, but its a clear indication to the user - which is my real goal.
Thanks Again,
-LutherB
|
|
|
|