|
 I'm less at sub-novice level with WPF but I would imagine that the general principles for solving this problem will much the same as with System.Windows.Forms. Rather than attempting to calculate if an arbitrary new position is valid we can simply limit the mouse movement so that all achievable positions become ok.
1) On mouse button down calculate and set a constraining rectangle for the cursor
2) In mouse move relocate the label based on an offset from the cursor position
3) On losing mouse capture remove the constraining reactangle
Here's one I prepared earlier. It should compile and run:
using System;
using System.Drawing;
using System.Windows.Forms;
namespace MouseMoveLabelDemo {
public sealed class DemoForm : Form {
[STAThread]
private static void Main() {
Application.Run(new DemoForm());
}
private readonly Point home;
private Label itinerantLabel;
private Size offset;
public DemoForm() {
home = new Point(10, 10);
BackColor = Color.White;
itinerantLabel = new Label();
itinerantLabel.Text = "Move Me";
itinerantLabel.BackColor = Color.Pink;
itinerantLabel.BorderStyle = BorderStyle.FixedSingle;
itinerantLabel.Location = home;
itinerantLabel.Parent = this;
itinerantLabel.MouseDown += Label_MouseDown;
itinerantLabel.MouseMove += Label_MouseMove;
itinerantLabel.MouseCaptureChanged += Label_CaptureLost;
SizeChanged += DemoForm_SizeChanged;
}
private void DemoForm_SizeChanged(object sender, EventArgs e) {
itinerantLabel.Location = home;
}
private void Label_MouseDown(object sender, MouseEventArgs e) {
Label lbl = (Label)sender;
if (e.Button == MouseButtons.Left) {
Point cursorPosition = Cursor.Position;
Point labelPosition = PointToScreen(lbl.Location);
offset = new Size(cursorPosition.X - labelPosition.X, cursorPosition.Y - labelPosition.Y);
Rectangle cursorClippingRectangle = Rectangle.FromLTRB(
offset.Width,
offset.Height,
ClientRectangle.Width - (lbl.Width - offset.Width) + 1,
ClientRectangle.Height - (lbl.Height - offset.Height) + 1);
Cursor.Clip = RectangleToScreen(cursorClippingRectangle);
}
}
private void Label_MouseMove(object sender, MouseEventArgs e) {
Label lbl = (Label)sender;
if (lbl.Capture) {
lbl.Location = PointToClient(Cursor.Position - offset);
}
}
private void Label_CaptureLost(object sender, EventArgs e) {
Cursor.Clip = Rectangle.Empty;
}
}
}
Alan.
|
|
|
|
|
Hi All
Am facing a problem while debugging the application using NUnit. Getting one application error like below:
SetUp : System.ApplicationException : Error in the application.
at COMSVCSLib.TransactionContextClass.CreateInstance(String pszProgId)
I successfully installed Interop.COMSVCSLib.dll in GAC but unable to proceed further because of this error. Please help.
Platform:
Visual Studio 2012, Framework Version: 4.5
Application Target Platform: Any CPU, Framework: 3.5
|
|
|
|
|
I was wondering if there is a way to execute a keyboard shortcut programatically in c#?
|
|
|
|
|
Load's of 'em, depending on the environment.
For WinForms, it's pretty simple:
The first way is via a menu - each menu item (including ToolStripMenuItem) has a ShortcutKeys property which lets you activate that menu item directly.
The more flexible way is to set the form KeyPreview property to true, and override the ProcessCmdKey method:
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (!tbFilter.ContainsFocus || (ModifierKeys & Keys.Control) != 0)
{
if (keyData == Keys.Left || keyData == (Keys.Left | Keys.Control))
{
butDeselectAll.PerformClick();
return true;
}
if (keyData == Keys.Right || keyData == (Keys.Right | Keys.Control))
{
butSelectAll.PerformClick();
return true;
}
}
return base.ProcessCmdKey(ref msg, keyData);
}
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
I am trying to do this on a winform, but I don't want to detect if a key has been pressed, I just want to execute a key combination, without the user even knowing.
|
|
|
|
|
Sorry?
Are you trying to send a keystroke combination to an already running app?
If so, which one? Yours? Or a different one?
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
I am trying to send a keystroke combination to an already running app such as ms word, or any other running app.
|
|
|
|
|
I want to send a keystroke combination to an already running app such as ms word or any other app.
|
|
|
|
|
You can use SendKeys[^] to send to the active application, but if you want to send to a specific app all the time, then see here: Sending Keystrokes to another Application in C#[^]
Be aware, this may or may not work if the target app is running at a higher level than your app!
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
The problem with this solution, is that the window needs to be in focus. What if I have multiple windows I want to send the keystroke to, how do I make them come into focus one by one, so I can send the keystroke one by one?
|
|
|
|
|
Did you read the links?
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
Hi All,
Below is an interview question about using recursion to add two arrays.
I can't figure it out though because it does not seem to allow any kind of index to keep track of where you are, I have been down a few roads - trying to test for null / default values in the array but because it is a byte[] type nothing worked.
Any help would be great thanks - this is not a homework question.
public AddResult UsingARecursiveAlgorithm_ValuesAreAdded(byte[] a, byte[] b)
{
var result = AddRecursive(a, b);
return new AddResult(a, b, result);
}
E.G.
Input : { 1, 1, 1 }, { 1, 1, 1 }
Result: {2,2,2}
Input : { 1, 1, 255 }, {0, 0, 1 }
Result: {1,2,0}
Conditions:
a & b are never null, and are always of the same length.
The algorithm should be non destructive to the inputs.
modified 5-Aug-15 19:12pm.
|
|
|
|
|
Well, that's a pretty stupid spec you have there. Why would anyone want to work at a place that asks that?
And is your second result correct?
|
|
|
|
|
Maybe it is unreasonable.. I thought I might be missing something.
You are right; the second result is incorrect, just noticed it.
|
|
|
|
|
Is that the code they gave you or the code you started writing?
Seriously, that's a really stupid way of adding arrays together. It's slower than just doing it in a single call because of the call overhead, limited in the size of the arrays it can handle because of limited stack space, a bit of a memory hog as you're creating new AddResult objects to hold the pile of results that get generated.
It's probably the most inefficient way you could possibly come up with to add those two arrays together.
|
|
|
|
|
Its the code they gave me.
I understand that it is inefficient but I think the question is just to test your understanding of recursive functions.
|
|
|
|
|
Well, they found out.
On top of everything else I said, there's also no way to put any kind of level parameter in there and, therefor, no way to put a bailout condition in either. The way they started the code, there's no way it'll work.
|
|
|
|
|
I have been programming for a little over a year now. I have learned the basics to a degree (through many headache induced nights) and have noticed a trend. I tend to be more enthralled with higher level concepts and avoid taking baby steps like the plague. For example, I couldn't tell you gow to programatically build a UI or make a delegate event for saving a file, but I know the MVVM pattern and how to make a lambda expression from a delegate. How bad is this trend?
|
|
|
|
|
To me this implies you have an interest in general programming concepts but aren't interested in building applications.
/ravi
|
|
|
|
|
Ironically, I make quite a few applications while learning more and more. I started off trying to make games and switched to applications because it was easier to be inspired.
|
|
|
|
|
Interesting. In my experience, most devs acquire knowledge of design patterns and more abstract concepts after several years of just writing code and building apps. It's good that you've developed an interest in MVVM early in the game.
/ravi
|
|
|
|
|
Haha yeah, the more abstract and difficult the concept is, the more likely it is for me to obsess over learning about it.
|
|
|
|
|
Speaking as someone at the other end of a career of developing and learning, you end up with a set of tools that do the LOB job and get you paid but have a VERY narrow range of capabilities.
You will also tend to miss a lot of the more elegant solutions because you simply do not understand the concepts and cannot relate them to your problem (building a really good app).
I wish I had had a formal education in computer science rather than a self taught bag of tricks. Don't get me wrong, I'm good at what I do but it a very narrow field.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
I'm actually self taught but I have a mentor that will help me if he knows the content i'm studying (MVVM wasn't one of those, OOP concepts though, he taught me about in detail).
|
|
|
|
|
When I was learning the only support was the other side of the planet and CompuServe was the medium for communication. A mentor was the manuals, not Microsoft but Superbase, almost as bad!
Never underestimate the power of human stupidity
RAH
|
|
|
|