In case anyone needs it, the problem is in this line: pd.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(PrintCurrentPage);
PrintCurrentPage looked like this: private void PrintCurrentPage(object sender, System.Drawing.Printing.PrintPageEventArgs e) { if (CurrentPage != null){CurrentPage = new Bitmap(@"C:\sCurrentPage.bmp");} }
I found that "When a bitmap image is loaded from a file the file remains locked open. This may be the root of the problem. To overcome the file locking open the file on a stream, read the image from the stream and explicitly close the stream." So I changed PrintCurrentPage to look like this: if (CurrentPage != null) { FileStream fs = File.Open(@"C:\sCurrentPage.bmp", FileMode.Open, FileAccess.Read); Bitmap bm = (Bitmap)Bitmap.FromStream(fs); CurrentPage = bm; e.Graphics.DrawImage(CurrentPage, 0, 0); fs.Close(); bm.Dispose(); }
And it all worked fine. Cheers, Mel :cool:
melanieab
Posts
-
Only able to print once? -
Only able to print once?I commented out just the
pd.Print();
and it worked fine. No exceptions, correct bitmap saved each time. I'm assuming the stack trace is the green arrow/highlight? If so, it points at the linePrintDocument pd = new PrintDocument();
. Any ideas? Mel :confused: -
Only able to print once?Hi, I have a form with tab pages on it, and I'd like to print each page whenever a button is pressed (all the tabpages share the same print button - I just give it a different parent when another tab is pressed). But I can only print once. When the print button is pressed the second time, I get error An unhandled exception of type 'System.Runtime.InteropServices.ExternalException' occurred in system.drawing.dll Additional information: A generic error occurred in GDI+. I'm not sure what to do here. My code is below. I also tried giving the PrintDocument a different name each time, and it still isn't happy.
private void printClick(object sender, System.EventArgs e) { Graphics currentTab = this.CreateGraphics(); Size s = this.Size; Bitmap memoryImage = new Bitmap(s.Width - 10, s.Height - 36, currentTab); Graphics memoryGraphics = Graphics.FromImage(memoryImage); IntPtr dc1 = currentTab.GetHdc(); IntPtr dc2 = memoryGraphics.GetHdc(); BitBlt(dc2, 0, 0, this.ClientRectangle.Width, this.ClientRectangle.Height, dc1, 2, 2, 13369376); CurrentPage = memoryImage; currentTab.ReleaseHdc(dc1); memoryGraphics.ReleaseHdc(dc2); CurrentPage.Save("sCurrentPage.bmp",System.Drawing.Imaging.ImageFormat.Bmp); PrintDocument pd = new PrintDocument(); PageSetupDialog pg = new PageSetupDialog(); printDialog1.Document = pd; pg.Document = pd; pg.PageSettings.Landscape = true; DialogResult result = printDialog1.ShowDialog(); if (result==DialogResult.OK) {pd.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(PrintCurrentPage); pd.Print();} pd.Dispose(); }
Thanks for any help!!! Mel -
Drawing the focus rectangle for a buttonHi, I'm trying to draw a focus rectangle on a button, since, when I load a tabpage, if the first control on that page is a button, it doesn't highlight and you can't tell that it has focus. So I stumbled across the
ControlPaint.DrawFocusRectangle
command and put this in after loading the rest of the page:int x = button2.ClientRectangle.Location.X + 3; int y = button2.ClientRectangle.Location.Y + 3; int w = button2.ClientRectangle.Width - 6; int h = button2.ClientRectangle.Height - 6; Rectangle r = new Rectangle(x,y,w,h); ControlPaint.DrawFocusRectangle(Graphics.FromHwnd(button2.Handle), r, Color.Red, Color.Red);
But I get nothing. Then I added a paint event for the button and moved the code there, although instead of sayingGraphics.FromHwnd(button2.Handle)
, I saide.Graphics
. Now it worked, but I'm not able to move away from the button using the up/down arrows. If I throw a messagebox in, after I click the OK button, I can move around without trouble. It seems to have something to do with returning control to the system. Is there anything I can say to give control back to the system? Or am I doing stuff completely wrong? Thanks so much for any thoughts!!!!!! Mel -- modified at 12:18 Monday 1st May, 2006 -
DrawReversibleFrame?Thank you very much for that explanation! That clears up one of the mysteries. What my problem has been from the start is that when I load a tabpage, if the first control is one of my buttons, it doesn't highlight and you can't tell that it has focus. What I've been doing to get around this is to focus on the control that's one tabstop before my button and
SendKeys.Send("{Tab}");
which does highlight the button. As soon as a button on the tabpage has been highlighted, all buttons will highlight when focused on until I leave the tabpage and return again. So I've deleted the focus call since the button has tabstop 0. I want the rectangle to disappear when focus is lost, so if it would just draw this first time, I'd be happy (is there a way to tell the system that it has control without having to call anything?). But nothing at all draws after deleting thebutton2.Focus()
, regardless of whether there's a MessageBox before, after, or not at all. So I tried adding the PaintEventArg for the button and moved the code into it. I also added a bool called focus since I only want the button to have the rectangle when it's in focus.private void focusPaint(object sender, System.Windows.Forms.PaintEventArgs e) { if (focus == true) { int x = toggleLength.ClientRectangle.Location.X + 3; int y = toggleLength.ClientRectangle.Location.Y + 3; int w = toggleLength.ClientRectangle.Width - 6; int h = toggleLength.ClientRectangle.Height - 6; Rectangle r = new Rectangle(x,y,w,h); ControlPaint.DrawFocusRectangle(e.Graphics, r, Color.Red, Color.Red); focus = false; } }
This results in me never being able to move to another control (using the up/down arrow keys - only with the mouse). When I returned control to the system (by adding a Messagebox above theif (focus == true)
), I could move without problem. I also noticed that if I replace thee.Graphics
with my originalGraphics.FromHwnd(toggleLength.Handle)
, it wouldn't draw with or without system control, which I don't understand. (Maybe if I get that working I wouldn't need to add a Paint event?) I'm not sure what else to try. Thanks so much for any thoughts!!!!!! Mel -- modified at 15:59 Friday 28th April, 2006 -
DrawReversibleFrame?Thank you, that makes sense. :) (I wasn't thinking and expected to see it at 5,5,15,15 on the form rather than the screen.) Another question though. I'd actually like to use
DrawFocusRectangle
on a button, and did get it working yesterday, but after I changed a couple properties of my button (like tabstop #, size, and location), it stopped working. I added aMessageBox
just before theDrawFocusRectangle
call and after I hit the ok button, the focus rectangle did draw (code below). I also added a Click event (on some label) and moved the code below (without theMessageBox
) to the event, and then it worked too.button2.Focus(); int x = button2.ClientRectangle.Location.X + 3; int y = button2.ClientRectangle.Location.Y + 3; int w = button2.ClientRectangle.Width - 6; int h = button2.ClientRectangle.Height - 6; Rectangle r = new Rectangle(x,y,w,h); MessageBox.Show(""); ControlPaint.DrawFocusRectangle(Graphics.FromHwnd(button2.Handle), r, Color.Black, Color.Black);
Anyone know why it won't work without some external help? Thanks again!!! Mel :confused: -
DrawReversibleFrame?Hi, Should this be enough to draw a dotted rectangle on my form?
private void Form1_click(object sender, System.EventArgs e) { Rectangle r = new Rectangle(5,5,15,15); ControlPaint.DrawReversibleFrame(r, Color.Red, FrameStyle.Dashed); }
If not, what am I missing? Thanks! Mel -
strange exception caused by DirectX?Thanks! That worked. Mel :)
-
strange exception caused by DirectX?Hi, I'm getting a strange error when trying to run my form with DirectX on another computer (on my own it's fine). I get: Additional information: Request for the permission of type System.Security.Permissions.SecurityPermission, mscorlib, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 failed. This problem has popped up ever since I added a panel that uses DirectX. To see if it could be something I did, I ran a simple, beginner directx file (from Tom Miller's Managed DirectX 9 book) on my computer and then copied those files to the other computer, and it again didn't work, although this time giving me the error: An unhandled exception of type 'System.Security.SecurityException' occurred in Lights.exe Additional information: Security error. I then added messageboxes to see how far the thing ran before having issues. It didn't go far. I start with the usual
Main()
entry point:Application.Run(new Tabs());
Thenprivate Tabs() { MessageBox.Show("begin"); // // Required for Windows Form Designer support // InitializeComponent(); panel1.Visible = false; tabControl1.Visible = true; ...}
Then underInitializeComponent() { MessageBox.Show("start-ic"); ... }
When running the program, the "begin" messagebox appears ("start-ic" doesn't), and a Just-In-Time Debugger pops up. I run a new instance of Visual Studio .NET, the line after InitializeComponent() gets the yellow highlighting, and the System.Security.SecurityException warning comes up (the long version from above). I've found info from people having the same exception as my first one, but they're all getting those errors when trying to connect to a database or the web which I definitely don't do. Any ideas why this could be happening? It works fine on another computer if I create a plain form. Thanks for any help/ideas!!! Mel :confused: -- modified at 16:31 Wednesday 12th April, 2006 -
DirectX and tabpagesThanks! That's exactly what I'm looking for. Cheers, Mel
-
DirectX and tabpagesHi, I have a normal windows form with a bunch of tabpages on it. But at the beginning, I want to do a little DirectX intro (which I've already created) in a class that I've been running separately. Now I'm not sure how to put them together without having to close the intro form and then open the main form. Is there any possible way to, say, create a surface on the main form that will run the directx stuff? I can't find any examples on this. Or any other thoughts? Thanks! Mel :)
-
DirectX - adding a bitmap to a drawingAccidentally fixed my problem. Just needed to call
device.Reset(params);
since I'm doing some other things before getting to this stage. Cheers, Mel -
DirectX - adding a bitmap to a drawingHi, I'm new to DirectX so sorry if this is a stupid question. I'm trying to draw stuff and add a bitmap (texture) at the same time. After I draw some rectangles I say:
this.OnCreateVertexBuffer(vb, null); device.SetTexture(0, tex); device.VertexFormat = CustomVertex.PositionColoredTextured.Format; device.SetStreamSource(0, vb, 0); device.DrawPrimitives(PrimitiveType.TriangleStrip, 0, 2);
but the rectangles vanish just afterOnCreateVertexBuffer();
though the bitmap does appear. What do I need to do so that I can have both my rectangles and the texture at the same time? Let me know if you need more of my code to see what I'm screwing up. Thanks so much! :) Mel -
can't get Texture to draw in DirectXHi, I'm just starting out with DirectX and am running into some trouble (I've been piecing parts of examples together). I have a couple rectangles that yaw, roll, and pitch as they zoom in. When they come back to face the front (so it only looks 2d), I stop the movement and add a couple triangles. Up to here it works just fine. Now I'd like to add a bitmap under the rectangles but run into trouble. I have:
private override OnPaint() {
some twirling of boxes for a few seconds that become still with this line:DrawBox(0.0f, 0.0f, 0.0f, -0.45f, 0.8f, 9.5f);
(code for DrawBox below) then I add a couple triangles...CustomVertex.TransformedColored[] vert = new CustomVertex.TransformedColored[3];
... describe the vert ...device.VertexFormat = CustomVertex.TransformedColored.Format; device.DrawUserPrimitives(PrimitiveType.TriangleList, 1, vert);
Things work fine up to here. At this point I'd like to add my bitmap (Texture tex). I change the VertexFormat and call DrawBitmapBox (this is how one example added a texture to a box):device.VertexFormat = CustomVertex.PositionTextured.Format; device.SetStreamSource(0, vb, 0);
vb is the VertexBuffer - also belowDrawBitmapBox(0.0f, 0.0f, 0.0f, 2.0f, 2.0f, 9.5f, tex);
(code also below)}
Instead of drawing the bitmap image, the original rectangles themselves kind of blink (not at any contant rate), while the triangles just sit there like everything's ok. I know I'm probably making some obvious mistake(s), but any help in getting this bitmap up would be great!!! :) Mel DrawBoxprivate void DrawBox(float yaw, float pitch, float roll, float x, float y, float z) { angle += 0.01f; device.Transform.World = Matrix.RotationYawPitchRoll(yaw, pitch, roll) * Matrix.Translation(x, y, z); Material boxMaterial = new Material(); device.Lights[0].Type = LightType.Directional; device.Lights[0].Diffuse = Color.Red; device.Lights[0].Direction = new Vector3(0,-1,-1); device.Lights[0].Commit(); device.Lights[0].Enabled = true; boxMaterial.Diffuse = Color.White; device.Material = boxMaterial; mesh.DrawSubset(0); }
DrawBitmapBoxprivate void DrawBitmapBox(float yaw, float pitch, float roll, float x, float y, float z, Texture t) { angle += 0.00f; device.Transform.World = Matrix.RotationYawPitchRoll(yaw, pitch, roll) * Matrix.Translation(x, y, z); device.SetTexture(0, t); device.
-
Cutting out a triangular chunk in DirectXHi, I'm completely new to DirectX, and have managed to create a 3D yawing, pitching, and rolling rectangle that zooms in (with the help of the graphics & game programming book by Tom Miller). I need to cut out a 3D triangle chunk (straight through, nothing fancy) from one of the long sides of the rectangle (about 1/4 size of that side and midway down). To create my yawing, pitching, rolling rectangle, I used this:
private Mesh mesh = null; public void InitializeGraphics() { ... mesh = Mesh.Box(device, 0.78f, 2.0f, 0.5f); ... } protected override void OnPaint(System.Windows.Forms.PaintEventArgs e) { ... DrawBox(angle / (float)Math.PI, angle / (float)Math.PI * 8.0f, angle / (float)Math.PI, -0.41f, 0.8f, hDepth); ... } private void DrawBox(float yaw, float pitch, float roll, float x, float y, float z) { angle += 0.01f; device.Transform.World = Matrix.RotationYawPitchRoll(yaw, pitch, roll) * Matrix.Translation(x, y, z); Material boxMaterial = new Material(); device.Lights[0].Type = LightType.Directional; device.Lights[0].Diffuse = Color.Red; device.Lights[0].Direction = new Vector3(0,-1,-1); device.Lights[0].Commit(); device.Lights[0].Enabled = true; boxMaterial.Diffuse = Color.White; device.Material = boxMaterial; mesh.DrawSubset(0); }
I basically tried following the same steps as above to put a black triangle into that chunk of rectangle.private Mesh tmesh = null;
(in InitializeGraphics)tmesh = Mesh.Polygon(device, 0.5f, 3);
(in OnPaint override)DrawTriangle(angle / (float)Math.PI, angle / (float)Math.PI * 8.0f, angle / (float)Math.PI, -0.41f, 0.8f, hDepth);
and then aprivate void DrawTriangle(float yaw, float pitch, float roll, float x, float y, float z)
with basically the same innards as theDrawBox
(replacemesh
withtmesh
, got rid ofboxMaterial
, and madeColor.White
(so I could see it)). I'm thinking that this isn't the way to go because it's not doing what I'd like it to do. It appears to create a triangle, but only in 1 plane (Mesh.Polygon doesn't ask for for depth like Mesh.Box does). I then tried creating a bunch of triangles to create layers and it didn't work so well either. What's the best way to cut this chunk out? Thanks so much! :) Mel -
problem when opening a formHi, I can't begin to understand this one. I have a form with a tabcontrol. When I first enter one of the pages (whether by tabbing or opening the form), I say to focus on a button. It does focus on that button but you wouldn't know it by looking at it (the dotted rectangle isn't there). I noticed that when the Tab key was pressed, the newly focused button will highlight, so when opening the form, I focused on the button next to the one I want focused and did a
SendKeys.Send("{Tab}");
. This works if the current tab page is the one in focus (I have another "screen" on that same tabpage where I can press an Exit button to return to the original screen), but it refuses to work when that tabpage is first opened - when tabbing into the page from another or opening the form. I'm completely out of ideas around this problem. Anyone know what I could do? :confused: Thanks so much for any help!!! Mel -
creating a truly transparent controlHi again, Ok, I created a class and put the code in it.
public class TransparentControl : Label { protected override CreateParams CreateParams { get { CreateParams cp = base.CreateParams; cp.ExStyle |= 0x00000020; //WS_EX_TRANSPARENT return cp; } } }
Back in the normal form, I made the labelprivate TransparentControl label7;
and changed the appropriate line in InitializeComponent()this.label7 = new HCAUT.TransparentControl();
. I also made the backcolor of the label Transparent. But when I run everything, the label's backcolor still isn't transparent. Can anyone see what I'm doing wrong? Thanks again, Mel -
creating a truly transparent controlHi, Easy question here. I found this online, but (I'm still new to programming) I'm not sure how to use it.
protected override CreateParams CreateParams { get { CreateParams cp = base.CreateParams; cp.ExStyle |= 0x00000020; //WS_EX_TRANSPARENT return cp; } }
Do I create a new class for this code? Either way, how and where do I say that I want a label (not all labels) to be this way. Thanks so much!!! Mel :) -
transparent label over textboxHi, I'd like to put a transparent label over a textbox. I set the label backcolor to transparent and set its parent as the textbox. But setting the textbox as the parent results in the label not coming up at all (I changed the backcolor to red so I could see if it was there). Anyone know what to do? Thanks!!! Mel
-
Forcing the dotted focus box on a button?It just hit me that the buttons I'm using are custom and descend from the checkbox class. Hopefully this will make more sense out of things, but I'd still like to know how to make it look in focus. Thanks! Mel