我希望能够访问鼠标的坐标,无论光标是否在我的应用程序的窗口上.
当我使用Mouse.Capture(IInputElement)或UIElement.CaptureMouse()时,两者都无法捕获鼠标并返回false.
可能是我的问题?
我窗口的cs文件如下:
using System.Windows;
using System.Windows.Input;
namespace ScreenLooker
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
bool bSuccess = Mouse.Capture(this);
bSuccess = this.CaptureMouse();
}
protected override void OnMouseMove(MouseEventArgs e)
{
tbCoordX.Text = e.GetPosition(this).X.ToString();
tbCoordY.Text = e.GetPosition(this).Y.ToString();
//System.Drawing.Point oPoint = System.Windows.Forms.Cursor.Position;
//tbCoordX.Text = oPoint.X.ToString();
//tbCoordY.Text = oPoint.Y.ToString();
base.OnMouseMove(e);
}
}
}
Run Code Online (Sandbox Code Playgroud) 我在.net Regexes的平衡匹配上找到了以下资源:
根据我在这些中读到的内容,以下示例应该有效:
这个正则表达式应该在一个角括号组中的任何地方找到一个"a",无论多深.它应该匹配" <a>
"," <<a>>
"," <a<>>
"," <<>a>
"," <<><a>>
"等.
(?<=
^
(
(
<(?<Depth>)
|
>(?<-Depth>)
)
[^<>]*?
)+?
)
(?(Depth)a|(?!))
Run Code Online (Sandbox Code Playgroud)
匹配字符串"<<> a>"中的"a"
虽然它适用于字符串" <a<>>
"和" <<a>>
",但我无法使其与">"之后的"a"相匹配.
根据我读过的解释,前两个"<"应该增加深度两次,然后第一个">"应该减少一次.此时,(?(深度)a |(?!))应执行"是"选项,但正则表达式从未在此处进行.
考虑以下正则表达式,它没有进行这样的检查,仍然无法匹配有问题的字符串:
(?<=
^
(
(
<(?<Depth>)
|
>(?<-Depth>)
)
[^<>]*?
)+?
)
a
Run Code Online (Sandbox Code Playgroud)
我错过了什么,或者正则表达式引擎是否正常工作?
我有学生对象的载体,我想用排序#include <algorithm>
和sort(list.begin(), list.end());
为了做到这一点,我理解我需要重载"<"运算符,但在尝试(并失败)使用在线建议的几种方法后,我的想法已经用完了.
这是我最近的尝试:
在Student.h ...
...
using namespace std;
class Student
{
friend bool operator <(const Student& first, const Student& second);
public:
...
private:
...
};
Run Code Online (Sandbox Code Playgroud)
在Student.cpp中......
...
#include "Student.h"
using namespace std;
...
bool operator <(const Student& first, const Student& second)
{
return first.Name() < second.Name();
}
Run Code Online (Sandbox Code Playgroud)
其中"Name()"是一个返回字符串的常量函数.
程序编译并运行,但我的操作符函数在排序期间从未被调用,当我尝试比较两个Student对象时,例如s1 < s2
我收到了"错误:未找到重载的运算符"
我怎样才能正确地重载此运算符,以便我的排序可以按照我的意图运行?
在我的代码中,我有一个Student对象的向量.
vector<Student> m_students;
Run Code Online (Sandbox Code Playgroud)
我想要:
请考虑以下代码:
// Check to see if the Student already exists.
Student* targetStudent = NULL;
for each (Student student in m_students)
{
if (student.Name() == strName)
{
targetStudent = &student;
break;
}
}
// If the Student didn't exist, add it.
if (targetStudent == NULL)
{
targetStudent = new Student(strName);
m_students.push_back(*targetStudent);
}
// Add the course info to the Student.
targetStudent->Add(strQuarter, strCourse, strCredits, strGrade);
Run Code Online (Sandbox Code Playgroud)
当我调用m_students.push_back(*targetStudent);
它时,似乎向量"m_students"最终得到了"targetStudent"当时指向的Student对象的副本.
后续尝试添加到targetStudent不会更改向量中包含的对象.
我怎样才能从指向对象的指针开始,将该对象添加到向量中,然后访问向量中的对象?
这是我所拥有的(不工作)的简化版本:
prog.h:
...
const string c_strExample1 = "ex1";
const string c_strExample2 = "ex2";
const string c_astrExamples[] = {c_strExample1, c_strExample2};
...
Run Code Online (Sandbox Code Playgroud)
prog.cpp:
...
int main()
{
int nLength = c_astrExamples.length();
for (int i = 0; i < nLength; i++)
cout << c_astrExamples[i] << "\n";
return 0;
}
...
Run Code Online (Sandbox Code Playgroud)
当我尝试构建时,我收到以下错误:错误C2228:'.length'的左边必须有class/struct/union
仅当我尝试使用c_astrExamples的成员函数时才会发生此错误.如果我用数字2替换"c_astrExamples.length()",一切似乎都能正常工作.
我能够使用c_strExample1和c_strExample2的成员函数,所以我认为这种行为源于我使用字符串与字符串数组之间的一些区别.
我在prog.h中的初始化是错误的吗?在prog.cpp中我需要一些特别的东西吗?