|
Hi,
I am following this appbar tutorial http://www.codeproject.com/Purgatory/AppBar.asp . I can get the form to occupy a section of the desktop area but when I end the application the space isn't given back to other applications. Does anyone know what I am doing wrong and how I can stop this from happening?
My code is:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Runtime.InteropServices;
namespace WindowsApplication2
{
///
/// Summary description for Form1.
///
public class Form1 : System.Windows.Forms.Form
{
///
/// Required designer variable.
///
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
///
/// Clean up any resources being used.
///
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
///
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(88, 32);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(112, 64);
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.Closing += new System.ComponentModel.CancelEventHandler(this.OnClosing);
this.Load += new System.EventHandler(this.OnLoad);
this.ResumeLayout(false);
}
#endregion
///
/// The main entry point for the application.
///
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private System.Windows.Forms.Button button1;
#region APPBAR
[StructLayout(LayoutKind.Sequential)]
struct RECT
{
public int left;
public int top;
public int right;
public int bottom;
}
[StructLayout(LayoutKind.Sequential)]
struct APPBARDATA
{
public int cbSize;
public IntPtr hWnd;
public int uCallbackMessage;
public int uEdge;
public RECT rc;
public IntPtr lParam;
}
enum ABMsg : int
{
ABM_NEW=0,
ABM_REMOVE=1,
ABM_QUERYPOS=2,
ABM_SETPOS=3,
ABM_GETSTATE=4,
ABM_GETTASKBARPOS=5,
ABM_ACTIVATE=6,
ABM_GETAUTOHIDEBAR=7,
ABM_SETAUTOHIDEBAR=8,
ABM_WINDOWPOSCHANGED=9,
ABM_SETSTATE=10
}
enum ABNotify : int
{
ABN_STATECHANGE=0,
ABN_POSCHANGED,
ABN_FULLSCREENAPP,
ABN_WINDOWARRANGE
}
enum ABEdge : int
{
ABE_LEFT=0,
ABE_TOP,
ABE_RIGHT,
ABE_BOTTOM
}
private bool fBarRegistered = false;
[DllImport("SHELL32", CallingConvention=CallingConvention.StdCall)]
static extern uint SHAppBarMessage(int dwMessage, ref APPBARDATA pData);
[DllImport("USER32")]
static extern int GetSystemMetrics(int Index);
[DllImport("User32.dll", ExactSpelling=true, CharSet=System.Runtime.InteropServices.CharSet.Auto)]
private static extern bool MoveWindow(IntPtr hWnd, int x, int y, int cx, int cy, bool repaint);
[DllImport("User32.dll", CharSet=CharSet.Auto)]
private static extern int RegisterWindowMessage(string msg);
private int uCallBack;
private void RegisterBar()
{
APPBARDATA abd = new APPBARDATA();
abd.cbSize = Marshal.SizeOf(abd);
abd.hWnd = this.Handle;
if (!fBarRegistered)
{
uCallBack = RegisterWindowMessage("AppBarMessage");
abd.uCallbackMessage = uCallBack;
uint ret = SHAppBarMessage((int)ABMsg.ABM_NEW, ref abd);
fBarRegistered = true;
ABSetPos();
}
else
{
SHAppBarMessage((int)ABMsg.ABM_REMOVE, ref abd);
fBarRegistered = false;
}
}
private void ABSetPos()
{
APPBARDATA abd = new APPBARDATA();
abd.cbSize = Marshal.SizeOf(abd);
abd.hWnd = this.Handle;
abd.uEdge = (int)ABEdge.ABE_TOP;
if (abd.uEdge == (int)ABEdge.ABE_LEFT || abd.uEdge == (int)ABEdge.ABE_RIGHT)
{
abd.rc.top = 0;
abd.rc.bottom = SystemInformation.PrimaryMonitorSize.Height;
if (abd.uEdge == (int)ABEdge.ABE_LEFT)
{
abd.rc.left = 0;
abd.rc.right = Size.Width;
}
else
{
abd.rc.right = SystemInformation.PrimaryMonitorSize.Width;
abd.rc.left = abd.rc.right - Size.Width;
}
}
else
{
abd.rc.left = 0;
abd.rc.right = SystemInformation.PrimaryMonitorSize.Width;
if (abd.uEdge == (int)ABEdge.ABE_TOP)
{
abd.rc.top = 0;
abd.rc.bottom = Size.Height;
}
else
{
abd.rc.bottom = SystemInformation.PrimaryMonitorSize.Height;
abd.rc.top = abd.rc.bottom - Size.Height;
}
}
// Query the system for an approved size and position.
SHAppBarMessage((int)ABMsg.ABM_QUERYPOS, ref abd);
// Adjust the rectangle, depending on the edge to which the
// appbar is anchored.
switch (abd.uEdge)
{
case (int)ABEdge.ABE_LEFT:
abd.rc.right = abd.rc.left + Size.Width;
break;
case (int)ABEdge.ABE_RIGHT:
abd.rc.left= abd.rc.right - Size.Width;
break;
case (int)ABEdge.ABE_TOP:
abd.rc.bottom = abd.rc.top + Size.Height;
break;
case (int)ABEdge.ABE_BOTTOM:
abd.rc.top = abd.rc.bottom - Size.Height;
break;
}
// Pass the final bounding rectangle to the system.
SHAppBarMessage((int)ABMsg.ABM_SETPOS, ref abd);
// Move and size the appbar so that it conforms to the
// bounding rectangle passed to the system.
MoveWindow(abd.hWnd, abd.rc.left, abd.rc.top, abd.rc.right - abd.rc.left, abd.rc.bottom - abd.rc.top, true);
}
protected override void WndProc(ref System.Windows.Forms.Message m)
{
if (m.Msg == uCallBack)
{
switch(m.WParam.ToInt32())
{
case (int)ABNotify.ABN_POSCHANGED:
ABSetPos();
break;
}
}
base.WndProc(ref m);
}
protected override System.Windows.Forms.CreateParams CreateParams
{
get
{
CreateParams cp = base.CreateParams;
cp.Style &= (~0x00C00000); // WS_CAPTION
cp.Style &= (~0x00800000); // WS_BORDER
cp.ExStyle = 0x00000080 | 0x00000008; // WS_EX_TOOLWINDOW | WS_EX_TOPMOST
return cp;
}
}
#endregion
private void OnLoad(object sender, System.EventArgs e)
{
RegisterBar();
}
private void OnClosing(object sender, System.ComponentModel.CancelEventArgs e)
{
RegisterBar();
}
private void button1_Click(object sender, System.EventArgs e)
{
Application.Exit();
}
}
}
Many thanks for your time and help.
Al
|
|
|
|
|
Muller2 wrote: but when I end the application the space isn't given back to other applications.
You mean this[^]
"Oh and for everyone who tried the code and wonder how to close it, click on it to get the focus on it and hit Alt-F4 (if you hit stop you're stuck with a unaccesible area on your desktop)"
"Alot of the people on this forum are incredibly stupid, thinking that the internet is real" Score: 1.0 in the Soap Box
led mike
|
|
|
|
|
Yes that works but I want to know how to terminate the program by clicking a button without telling the user to press Alt+F4.
Is there anyother solution to closing this app?
|
|
|
|
|
Muller2 wrote: Is there anyother solution to closing this app?
I don't see that issue being addressed in the article so... not a known one, no.
"Alot of the people on this forum are incredibly stupid, thinking that the internet is real" Score: 1.0 in the Soap Box
led mike
|
|
|
|
|
Should be quite simple: If you look at the code you'll see that an event handler for Closing is being attached which is calling RegisterBar() .
So you simply can add a button that's calling this.Close(); and you should be fine.
Regards,
mav
--
Black holes are the places where God divided by 0...
|
|
|
|
|
Hello
I hope Led gave you a good enough answer. I just wanted to nudge you about the code you posted. Posting a 9,223,372,036,854,775,807 pixels long code will surely discourage anyone from reading it!! Specially if you didn't enclose it with pre tags. You should only post the code that you suspect that is causing the problem. To get to this use break points, stepwise debugging, tracing, guessing, commenting/decommenting, or any other method that suits you. Just post as little code as you can.
Regards
|
|
|
|
|
Nader Elshehabi wrote: a 9,223,372,036,854,775,807 pixels long code
I'm not saying that you are exaggerating, but at a screen resolution of 100 ppi, that would be almost a quarter of a light year...
---
b { font-weight: normal; }
|
|
|
|
|
lol
Regards
|
|
|
|
|
Hi all,
I want to play media files in a web browser
but i dont want to use the Windows media player for this.
is there any other option to do this?
Thanks & Regards,
Pramod
"Everyone is a genius at least once a year"
|
|
|
|
|
You can use Macromedia Flash for this purpose. Flash professional 8 has(i'm not sure whether the previous versions have or not) a feature named Flash 8 video encoder.
|
|
|
|
|
Is there a function that will retrieve the pixel color on a daesktop location?
Or a function that will alliow me to save the desktop as a bitmap?
--------------------------------------------------------
1 line of code equals many bugs. So don't write any!!
My mad coder blog
|
|
|
|
|
There was an article of a simple screen effect not too long ago. I lost the link to it, but here's the basic code:
int screenWidth = Screen.GetBounds(new Point(0, 0)).Width;
int screenHeight = Screen.GetBounds(new Point(0, 0)).Height;
Bitmap screenShot = new Bitmap(screenWidth, screenHeight);
Graphics gfx = Graphics.FromImage((Image)screenShot);
gfx.CopyFromScreen(0, 0, 0, 0, new Size(screenWidth, screenHeight)); After that, you should be able to use GetPixel on the screenShot variable to get pixel values from the desktop.
Logifusion[ ^]
If not entertaining, write your Congressman.
|
|
|
|
|
Just noticed that .Net 2.0 now supports CopyFromScreen() feature, which should be quite handy for GDI+ developers. Thanks for posting, Dustin.
Best,
Jun
|
|
|
|
|
Me too
|
|
|
|
|
I need to create an app that reads the color of each pixel within an image however a google search hasn't turned up much info on the subject (there's a million tutorials on how to WRITE colors to an image but now on how to READ colors from an image).
So does anyone know of a tutorial or know of some basic code to get me started on how I can take a sample image and read the color of a certain pixel?
Thanks.
-Goalie35
|
|
|
|
|
once you got a Image instance (for example Bitmap ), you can use GetPixel()
regards
modified 12-Sep-18 21:01pm.
|
|
|
|
|
What Greeeg said will work, however, it will be butt-slow. If you need fast access to the pixels of a bitmap, use the FastBitmap[^] C# library. It's faster because it uses unsafe C# code with pointers to directly access and manipulate the memory of the bitmap rather than using the slow GetPixel/SetPixel.
|
|
|
|
|
Reading the whole image data via LockBits and UnlockBits will be fast enough if you have to read (and manipulate) multiple pixels.
regards
modified 12-Sep-18 21:01pm.
|
|
|
|
|
In c++ main() is not in a class.Main() is called by OS.It is a special function.But in C# it is also in a class.For example:
class Exercise<br />
{<br />
<br />
static void Main()<br />
{<br />
Console.WriteLine();<br />
}<br />
}
Also C# designers says :C# is truely oo language.There is no code outside of classes.But Main() can not be a part of a class.Ii is sepecial func.; here main() is not member of exercise class.So why c# designers force us to write also main() in a class.Also is main() must be in a class for a language that want to be truely oo?What is the advantage/magic behind this.I think this is wrong design and confusing thing.But i really wonder what c# designers think for this subject.
|
|
|
|
|
It's the same in Java.
Since Java and C# are OO (you can't have code outside classes) this implies, that you also can't have the Main() method outside of any class.
But it's declared static , so you don't need an instance of that class, the VM will directly call Program.Main(args) without creating an instance of that class.
regards
modified 12-Sep-18 21:01pm.
|
|
|
|
|
Hello
Before all lets first admit something.. Whatever the design all code written in any language will eventually be translated to machine code, no matter what language you use even C#. In case of C# some details are different concerning JIT -Just In Time- compilation.
Anyway, OO design has many concepts including Encapsulation, ie. stuffing everything into classes instead of throwing them everywhere as global variables and funcions. In memory everything get mixed up -well, not exactly -, and when you make a memory dump in hex, you don't see bounadries between bits, and bytes!!
So, OOP is just a way of high level programming to isolate you code into smaller segments where you can debug, and understand easily. Some sort of organizing you code no more!! The end result in memory is almost the same.
Regarding your question about main method:
sawerr wrote: In c++ main() is not in a class.Main() is called by OS
Yes! In C++ main function is not in any class. Main is called the Entry point. So it's where the OS knows where to start you application. OS enters your application from here. The entry point's name and address in the code is put in the PE header, which is a small segment at the very beginning of the EXE file.
sawerr wrote: Also C# designers says :C# is truely oo language
With all respect... CRAP!! Nothing is perfect! Forgive if I don't express more about this point!
sawerr wrote: But Main() can not be a part of a class
Who said so?? You just said yourself main method must be a part of any of your programs class.. Typically it's present in the Program class of your project in Program.cs file.
sawerr wrote: here main() is not member of exercise class
It's a private static member. Yes it's a member. but it's static so that it's not specific to an object, rather it's shared between all objects of the same class. Private because we don't want it to be called outside the class or inherited to any other class
sawerr wrote: Also is main() must be in a class for a language that want to be truely oo?
Well, depends on the designers' point of view. Actually perfect OOism doesn't exist in my opinion!! It doesn't matter if ain is a part of a class or not, as long as you understand the structure your code will take in the memory. They prefer it would be encapsulated in a class. For me.. I don't care. Do you? Does it make a difference?
sawerr wrote: What is the advantage/magic behind this
Nothing!!
sawerr wrote: I think this is wrong design and confusing thing
Why?? There must be an entry point for your application. Inside a class, outside a class, doesn't really matters. In C# they try to be as OO as possible, so they put it in a class. In the typical project it's put in the only non-UI class, ie. the program class. Sounds right to me!! In here you can make a pre-Form initialization code. For example if you want only one instance to run of your program. You can check if other instances are running in the main method of Program class, before showing any forms..
Q: Can I put the main method in another class other than the Progam class.
A: Sure! Why not?
Regards
|
|
|
|
|
|
Yes, this is an "irregularity" in C# and Java. Main() is in a class but it is not an instance method.
It doesn't have to be like this though. In Eiffel all methods are instance methods and you specify a start-up class rather than a static main method. A bit analogous to the way you specify a startup page in an ASP.NET app.
Kevin
|
|
|
|
|
I'm working on CRX14 communication. I have this dll that was written in c++ language ("CRX14USBDLL.dll").
The c++ dll function is:
Get_HID_StringDescriptors(char VendorN[100], char ProductN[100], char SerialN[100])
Now in my c# code:
I dllimported this dll as:
[DllImport("CRX14USB.dll") public static extern void Get_HID_StringDescriptors(char[] VendorN, char[] ProductN, char[] SerialN);
now with the variable declarations:
char[] VendorN = new char[100];
char[] ProductN = new char[100];
char[] SerialN = new char[100];
Now to call the dll function:
Get_HID_StringDescriptors(VendorN, ProductN, SerialN);
to display the variable in message box:
messText = VendorN + ", " + ProductN.ToString() + ", " + SerialN.ToString();
MessageBox.Show(messText);
Am i doing anything wrong here? Anyone can help?
|
|
|
|
|
Hello
Blubbo wrote: Am i doing anything wrong here? Anyone can help?
You didn't specify what error do you get of this code?
Regards
|
|
|
|
|