RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / user-309995

Павел Ериков's questions

Martin Hope
Павел Ериков
Asked: 2021-11-28 22:10:36 +0000 UTC

Lambda 函数和指向类中函数的指针

  • 0

我有一个类,它有一个指向某个函数的指针。默认情况下,该函数必须有一个实现,但例如使用 setFunc(),您可以更改指针。

这是一个示例代码

class A {
private:
    int (*func)(int value) = [](int value) { return value + 1; };

    int (*funcTest)(int value) = [](int value) {return func(value) + 1; };
public:
    void setFunc(int (*other)(int)) {
        this->func = other;
    }
    void setFuncTest(int (*other)(int)) {
        this->funcTest = other;
    }
    A() { }
    void test() {
        int t = 10;
        int s = func(t);
        cout << s << endl;
    }
};

起初,问题在于从 funcTest 调用 func,但我找到了一个解决方案并重写了这一行:int (*funcTest)(int value) = [&](int value) {return func(value) + 1; };,

但是有一个问题:не существует подходящей функции преобразования из "lambda []int (int value)->int" в "int (*)(int value)"

请告诉我如何正确实现我所需要的。如果有人分享指向以可理解的语言解释有关 lambda 函数和类似主题的源的链接,我将不胜感激。

c++
  • 1 个回答
  • 10 Views
Martin Hope
Павел Ериков
Asked: 2021-11-10 04:15:44 +0000 UTC

在大数结构中实现构造函数的最佳方法是什么?

  • 0

我写大数的结构。停止编写构造函数。

这是我如何实现它们的示例:

#include "big_uint.h"

big_uint::big_uint(){
    size = 0;
    buff_size = 4;
    bits = new uint32_t[buff_size];
}

big_uint::big_uint(uint64_t value) : big_uint() {
    bits[0] = (uint32_t)value;
    bits[1] = (uint32_t)(value >> 32);
    size += (bool)bits[1] + 1;
}
big_uint::big_uint(uint32_t value) : big_uint() {
    size = 1;
    bits[0] = value;
}
big_uint::big_uint(uint16_t value) : big_uint((uint32_t)value) {}

big_uint::big_uint(int64_t value) : big_uint((uint64_t)value) {}
big_uint::big_uint(int32_t value) : big_uint((uint32_t)value) {}
big_uint::big_uint(int16_t value) : big_uint((uint32_t)value) {}

big_uint::big_uint(const uint32_t* const bits, uint32_t size){
    this->size = size;
    this->buff_size = size + 4;
    this->bits = new uint32_t[this->buff_size];
    const uint32_t *s = bits;
    for(uint32_t* f = this->bits; s < bits + size; *f = *s, ++f, ++s);
}

big_uint::big_uint(const big_uint& other) : big_uint(other.bits, other.size) {}

big_uint::big_uint(big_uint&& other) {
    this->size = other.size;
    this->buff_size = other.buff_size;
    this->bits = other.bits;
    other.bits = nullptr;
}

问题是如何像我的或每个构造函数一样以自己的方式更好地实现它们?也就是在构造函数中uint32_t写size = 1; bits[0] = value;,和在uint16_t.

在我看来,这比在每个构造函数中编写几乎相同的代码要好。

c++
  • 1 个回答
  • 10 Views
Martin Hope
Павел Ериков
Asked: 2020-04-08 01:10:35 +0000 UTC

在 C++ 中解析 json 文件 [关闭]

  • 0
关闭。这个问题需要具体说明。目前不接受回复。

想改进这个问题? 重新构建问题,使其只关注一个问题。

2年前关闭。

改进问题

我下载了 json.hpp 文件,我需要解析这个文件。我需要先解析“标题”。然后解析这些键的“虹膜”。

这是我到目前为止所得到的。

#include <iostream>
#include <vector>
#include <string>
#include <fstream>
#include <unordered_map>
#include "json.h"

using namespace nlohmann;
using namespace std;

int main() {

    ifstream src("irises.json");

    json jsn;
    src >> jsn;

    const auto& irises = jsn["irises"];
    const auto& titles = jsn["titles"];

    return 0;
}

在程序执行过程中,我需要获取每朵花的属性值。

请通过给出示例代码告诉我如何解析这个文件。

c++
  • 1 个回答
  • 10 Views
Martin Hope
Павел Ериков
Asked: 2020-03-16 00:00:21 +0000 UTC

WinAPI 位图缩放

  • 0

有一个窗口,您可以在其中使用鼠标进行绘制。

我需要在它旁边放置相同的区域,其中将有在第一个区域中绘制的图片的缩小版本。

也就是我例如在第一个区域画出字母A,大小为300x300,然后,例如按下“压缩”按钮后,在第二个区域出现一个压缩的图片A,大小为64x64。

这是窗口代码:

#include <Windows.h>
#include <vector>

#define WINDOW_WIDTH 800
#define WINDOW_HEIGHT 800
#define WINDOW_XPOS (GetSystemMetrics(SM_CXSCREEN) >> 1) - (WINDOW_WIDTH >> 1)
#define WINDOW_YPOS (GetSystemMetrics(SM_CYSCREEN) >> 1) - (WINDOW_HEIGHT >> 1)
#define CHILD_WIDTH 300
#define CHILD_HEIGHT 300
#define PADDING_L 50
#define PADDING_B 50

HINSTANCE hInst;
RECT clientRect;

HBRUSH WINDOW_BACKGROUND = CreateSolidBrush(RGB(41, 49, 51));
HBRUSH CHILD_BACKGROUND = CreateSolidBrush(RGB(76, 81, 74));
HBRUSH BRUSH_COLOR = CreateSolidBrush(RGB(213, 213, 213));
HPEN hPen = CreatePen(PS_NULL, 0, NULL);

HDC child_hdc;
HDC hdc;

std::vector<std::vector<bool>> pixels = {};

LRESULT CALLBACK ChildProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
    static int x = 0, y = 0, x_prev = 0, y_prev = 0, N = 10;
    static BOOL fDrawEllipse;
    static HBITMAP hBMP;
    static HDC hMemDC;

    switch (msg) {
    case WM_CREATE: {
        child_hdc = GetDC(hwnd);

        hMemDC = CreateCompatibleDC(child_hdc);
        hBMP = CreateCompatibleBitmap(child_hdc, CHILD_WIDTH, CHILD_HEIGHT);
        SelectObject(hMemDC, hBMP);

        SelectObject(hMemDC, hPen);
        SelectObject(hMemDC, BRUSH_COLOR);
        FillRect(hMemDC, &clientRect, CHILD_BACKGROUND);
        return 0;
    }
    case WM_LBUTTONDOWN: {
        x = LOWORD(lParam);
        y = HIWORD(lParam);
        fDrawEllipse = true;
        Ellipse(hMemDC, x - 5, y - 5, x + 5, y + 5);
        BitBlt(child_hdc, 0, 0, CHILD_WIDTH, CHILD_HEIGHT, hMemDC, 0, 0, SRCCOPY);
        return 0;
    }
    case WM_PAINT: {
        BitBlt(child_hdc, 0, 0, CHILD_WIDTH, CHILD_HEIGHT, hMemDC, 0, 0, SRCCOPY);
        return 0;
    }
    case WM_MOUSEMOVE: {
        if (fDrawEllipse) {
            x_prev = x;
            y_prev = y;
            x = LOWORD(lParam); 
            y = HIWORD(lParam);
            for (int i = 0; i < N; ++i) {
                int px = x_prev + (x - x_prev) * i / N;
                int py = y_prev + (y - y_prev) * i / N;
                Ellipse(hMemDC, px - 5, py - 5, px + 5, py + 5);
                BitBlt(child_hdc, 0, 0, CHILD_WIDTH, CHILD_HEIGHT, hMemDC, 0, 0, SRCCOPY);
            }
        }
        return 0;
    }
    case WM_LBUTTONUP: {
        fDrawEllipse = false;
        x_prev = 0; y_prev = 0; x = 0; y = 0;
        StretchBlt(hdc, 0, 0, 64, 64, child_hdc, 0, 0, CHILD_WIDTH, CHILD_HEIGHT, SRCCOPY);
        return 0;
    }
    case WM_DESTROY: {
        ReleaseDC(hwnd, child_hdc);
        DeleteObject(WINDOW_BACKGROUND);
        DeleteObject(CHILD_BACKGROUND);
        DeleteObject(BRUSH_COLOR);
        DeleteObject(hPen);
        DeleteDC(hMemDC);
        DeleteDC(child_hdc);
        PostQuitMessage(0);
        return 0;
    }
    }
    return DefWindowProc(hwnd, msg, wParam, lParam);
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
    static HBITMAP hBMP;
    switch (msg) {
    case WM_CREATE: {

        hdc = GetDC(hwnd);

        GetClientRect(hwnd, &clientRect);

        WNDCLASSEX wc{};
        wc.cbSize = sizeof(WNDCLASSEX);
        wc.lpfnWndProc = ChildProc;
        wc.hInstance = hInst;
        wc.hbrBackground = CHILD_BACKGROUND;
        wc.lpszClassName = "ChildClass";
        wc.hCursor = LoadCursor(nullptr, IDC_CROSS);

        RegisterClassEx(&wc);

        HWND child = CreateWindowEx(0, wc.lpszClassName, (LPCTSTR)nullptr, WS_CHILD | WS_BORDER | WS_VISIBLE,
                                    clientRect.left + PADDING_L, clientRect.bottom - CHILD_HEIGHT - PADDING_B, CHILD_WIDTH, CHILD_HEIGHT, 
                                    hwnd, nullptr, hInst, nullptr);

        ShowWindow(child, SW_NORMAL);
        UpdateWindow(child);

        return 0;
    }
    case WM_DESTROY: {
        ReleaseDC(hwnd, hdc);
        PostQuitMessage(0);
        return 0;
    }
    }
    return DefWindowProc(hwnd, msg, wParam, lParam);
}

int WINAPI WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE,
                   _In_ LPSTR lpCmdLine, _In_ int nShowCmd) {
    HWND hwnd{};
    MSG msg{};
    WNDCLASSEX wc;

    hInst = hInstance;

    wc.cbSize = sizeof(WNDCLASSEX);
    wc.cbClsExtra = 0;
    wc.cbWndExtra = 0;
    wc.hbrBackground = WINDOW_BACKGROUND;
    wc.hCursor = LoadCursor(nullptr, IDC_ARROW);
    wc.hIcon = LoadIcon(nullptr, IDI_APPLICATION);
    wc.hIconSm = nullptr;
    wc.hInstance = hInstance;
    wc.lpfnWndProc = WndProc;
    wc.lpszClassName = "WindowClass";
    wc.lpszMenuName = nullptr;
    wc.style = CS_VREDRAW | CS_HREDRAW;

    if (!RegisterClassEx(&wc))
        return EXIT_FAILURE;

    hwnd = CreateWindowEx(WS_EX_WINDOWEDGE | WS_EX_TOPMOST, wc.lpszClassName, "Окно windows",
                          WS_OVERLAPPEDWINDOW, WINDOW_XPOS, WINDOW_YPOS, WINDOW_WIDTH, WINDOW_HEIGHT,
                          nullptr, nullptr, hInstance, nullptr);

    if (hwnd == INVALID_HANDLE_VALUE)
        return EXIT_FAILURE;

    ShowWindow(hwnd, nShowCmd);
    UpdateWindow(hwnd);

    BOOL bRet;
    while (bRet = GetMessage(&msg, nullptr, 0, 0)) {
        if (bRet == -1)
            return EXIT_FAILURE;
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    return msg.wParam;
}

我知道这个功能StretchBlt,但是当我试图弄清楚时,出现了很多问题。

c++
  • 1 个回答
  • 10 Views
Martin Hope
Павел Ериков
Asked: 2020-03-15 03:48:31 +0000 UTC

如何制作一定大小的绘图区域。C++

  • 0

我想创建一个位于屏幕左下角的黑色区域,我可以在其中绘制而不会超出它。如果您展示一个小示例并通过指向具有理论的站点的链接修复所有内容,我将不胜感激。

c++
  • 1 个回答
  • 10 Views
Martin Hope
Павел Ериков
Asked: 2020-01-06 06:19:31 +0000 UTC

帮助解决问题

  • 0

这是具有条件和解决方案的站点

该站点在 python3 和 c++ 中都有解决方案。请解释为什么二分搜索算法能解决这个问题。我提前为一个可能愚蠢的问题道歉,但我想了解。

具体问题:

1)为什么1 + 1e-5?它是什么,为什么它如此重要?通过删除 1e-5,我在 13 个测试中得到了 0 个。

2)l = x而且r = x这一点也不清楚。

任务:

给定一个包含圆形蛋糕半径和客人数量的数组,确定可以从蛋糕上切下的最大一块蛋糕,以便每位客人得到一块面积相同的蛋糕。一块蛋糕不可能有一块蛋糕的一部分和另一块蛋糕的一部分,而且每位客人只能享用一块蛋糕。

Python3中的解决方案

def maximumAreaServingCake(radii, numberOfGuests):
areas = [math.pi * r * r for r in radii]
def possible(x):
    k = 0
    for a in areas:
        k += a // x
        if k >= numberOfGuests:
            return True
    return False

l, r = 0, max(areas)
while l + 1e-5 <= r:
    x = (l + r) / 2
    if possible(x):
        l = x
    else:
        r = x
return round(x, 4)
алгоритм
  • 1 个回答
  • 10 Views
Martin Hope
Павел Ериков
Asked: 2020-12-10 04:33:47 +0000 UTC

C++中的回调函数

  • 1

我想确保我正确和充分地编写了所有内容。请告诉我代码中存在哪些错误,最好是关于如何正确处理的建议。

#include <iostream>
using namespace std;


class Point {
private:
    int xPos, yPos;
public:
    Point(int x, int y) : xPos(x), yPos(y) { }
    Point() : Point(0,0) { }
    int x() { return xPos; }
    int y() { return yPos; }
    void setX(int x) { this->xPos = x; }
    void setY(int y) { this->yPos = y; }
};

class Square {
private:
    void(*callBackFunc)(Point point);
    Point posLeftUp;
public:
    Square(){}
    Square(Point pos) : posLeftUp(pos) {}
    //Имитация движения квадрата по окну
    void Move(char key) {
        if (key == 'A')
            posLeftUp.setX(posLeftUp.x() - 2);
        else if (key == 'D')
            posLeftUp.setX(posLeftUp.x() + 2);
        else if (key == 'W')
            posLeftUp.setY(posLeftUp.y() + 2);
        else if (key == 'S')
            posLeftUp.setY(posLeftUp.y() - 2);
        callBackFunc(posLeftUp);
    }
    void setCallbackFunc(void(*fn)(Point point)) {
        callBackFunc = fn;
    }
};
//Имитация окна
class MainWindow {
private:
    static void getPosition(Point point);
    Square square;
public:
    MainWindow() {
        Point pos(10, 10);
        square = Square(pos);
        square.setCallbackFunc(getPosition);
        square.Move('A');
        square.Move('D');
        square.Move('W');
        square.Move('S');
    }
};

void MainWindow::getPosition(Point point){
        cout << point.x() << " : " << point.y() << endl;
}

int main() {
    MainWindow test;
    return 0;
}
c++
  • 1 个回答
  • 10 Views
Martin Hope
Павел Ериков
Asked: 2020-11-21 03:47:20 +0000 UTC

MFC 源代码

  • 0

请告诉我在哪里可以找到 MFC 源代码。更具体地说,我想看看 CFile 类的实现,但我在网上找不到。

c++
  • 1 个回答
  • 10 Views
Martin Hope
Павел Ериков
Asked: 2020-07-20 21:50:44 +0000 UTC

如何将字符串转换为以 2^32 为底的 uint 数组

  • 0

请告诉我,例如,“12413523578274952895672975235”如何制作一个数组 uint[] 数字,其中将存储这个以 2 ^ 32 为底的长数的展开系数。

我查看了 System.Numerics 库的来源,但什么都不懂。
告诉我一个将字符串解析为数组 uint[] 数字的算法,其中 digits[i] 是给定数字的扩展系数,底数为 2 ^ 32

c#
  • 1 个回答
  • 10 Views
Martin Hope
Павел Ериков
Asked: 2020-07-19 19:12:59 +0000 UTC

用于处理大量数字的库 c#

  • 2

我真的很想制作自己的库来处理大量数据。我开始做。我意识到我不知道该怎么做。

public struct BigInt
{
    private byte[] Value { get; set; }

    public BigInt(string value)
    {
        if (value.Length != 0)
            Value = new byte[value.Length];
        else
        {
            Value = null;
            return;
        }
        for (int i = value.Length - 1; i >= 0; i--)
            Value[i] = (byte)value[i];
    }

    public BigInt(byte[] value) { this.Value = value; }

    public override string ToString() => Encoding.Default.GetString(Value);

    public static implicit operator BigInt(string num) => new BigInt(num);

    public static implicit operator string(BigInt num) => Encoding.Default.GetString(num.Value);

    public static string operator +(BigInt c1, BigInt c2) => Addition(c1.Value, c2.Value);

    public static string operator +(BigInt c1, string c2) => Addition(c1.Value, Encoding.Default.GetBytes(c2));

    public static string operator *(BigInt c1, BigInt c2) => Multiplication(c1.Value, c2.Value);

    public static string operator *(BigInt c1, string c2) => Multiplication(c1.Value, Encoding.Default.GetBytes(c2));

    public static string Addition(byte[] x, byte[] y)
    {
        List<byte> result = new List<byte>();
        int c = 0, d = 0;
        List<byte> z = new List<byte>();
        if(x.Length > y.Length)
        {
            z.InsertRange(0, Encoding.Default.GetBytes(new String('0', x.Length - y.Length).ToCharArray()));
            z.AddRange(y);
            y = z.ToArray();
        }
        else
        {
            z.InsertRange(0, Encoding.Default.GetBytes(new String('0', y.Length - x.Length).ToCharArray()));
            z.AddRange(x);
            x = z.ToArray();
        }
        for(int i = x.Length-1; i>=0; i--)
        {
            byte a = x[i];
            byte b = y[i];
            c = (d + (a % 48) + (b % 48)) % 10;
            result.Add((byte)c);
            d = (d + a % 48 + b % 48) / 10;
        }
        if (d != 0)
            result.Add((byte)d);
        result.Reverse();
        return string.Join("", result);
    }

    public static string Multiplication(byte[] x, byte[] y)
    {
        List<byte>[] result = new List<byte>[y.Length];
        for (int i = 0; i < y.Length; i++)
        {
            int c = 0;
            int d = 0;
            List<byte> array = new List<byte>();
            for (int j = 0; j < x.Length; j++)
            {
                byte a = x[x.Length - 1 - j];
                byte b = y[y.Length - 1 - i];
                c = (d + (a % 48) * (b % 48)) % 10;
                array.Add((byte)c);
                d = (d + (a % 48) * (b % 48)) / 10;
            }
            if (d > 0)
                array.Add((byte)d);
            result[i] = array;
        }

        for (int i = 0; i < result.Length; i++)
            for (int j = 0; j < result[i].Count / 2; j++)
            {
                byte temp = result[i][j];
                result[i][j] = result[i][result[i].Count - 1 - j];
                result[i][result[i].Count - 1 - j] = temp;
            }

        if (result.Length > 1)
            for (int i = 1; i < result.Length; i++)
                for (int j = 0; j < result.Length - i; j++)
                    result[j + i].Add((byte)0);

        if (result.Length > 1)
        {
            for (int i = 0; i < result.Length - 1; i++)
            {
                List<byte> z = result[0];
                for (int j = 0; j < z.Count; j++)
                    z[j] += 48;
                result[0] = new List<byte>(Encoding.Default.GetBytes(Addition(z.ToArray(), result[i + 1].ToArray())));
            }
            return Encoding.Default.GetString(result[0].ToArray());
        }
        return String.Join("", result[0]);
    }

    public static string Pow(string x, int degrees)
    {
        if (degrees == 0) return "1";
        if (degrees == 1) return x;
        byte[] result = Encoding.Default.GetBytes(x);
        byte[] num = result;
        for (int i = 2; i <= degrees; i++)
            result = Encoding.Default.GetBytes(Multiplication(result, num));
        return Encoding.Default.GetString(result);
    }

    public static string Factorial(int number)
    {
        if (number == 0) return "1";
        if (number <= 2) return number.ToString();
        byte[] result = Encoding.Default.GetBytes((6).ToString());
        for (int i = 4; i <= number; i++)
            result = Encoding.Default.GetBytes(Multiplication(result, Encoding.Default.GetBytes(i.ToString())));
        return Encoding.Default.GetString(result);
    }
}

以上是我想出的所有代码。而且我确信这段代码是尽可能糟糕的。

我想知道在编写这样的库时要遵循什么。(例如,将数据存储在哪里以便它需要更少的内存或使用什么算法来提高工作速度)更多的例子是可取的。(任何语言都可以)嗯,一些文学作品,这样你以后就不必问问题了。

c#
  • 2 个回答
  • 10 Views
Martin Hope
Павел Ериков
Asked: 2020-03-09 04:32:23 +0000 UTC

新闻块布局

  • 0

在此处输入图像描述

有一个小块我想布局,但无法布局。问题是我只是不明白如何调整所有内容以使所有内容都足够且具有适应性。请帮我弥补这个块或告诉我什么和如何做。保持适应性很重要。好吧,例如,使用小屏幕时,内容位于照片下方。

加法:在纯CSS上)

添加: 在此处输入图像描述

css
  • 2 个回答
  • 10 Views
Martin Hope
Павел Ериков
Asked: 2020-03-08 15:52:25 +0000 UTC

CSS页脚布局

  • 0

在此处输入图像描述

假设我是 css 新手,我无法正确处理并继续调整上面屏幕截图中显示的页脚。

到目前为止,这是我能做到的唯一方法

在此处输入图像描述

<footer>
    <div class="silver_stroke"></div>
    <div id="footer_content" class="clearfix">
        <div id="footer_content_items">
            <div class="footer_item">
                <div class="footer_item_icon">
                    <img src="email.svg">
                </div>
                <div class="footer_item_text">
                    <p>emailpochta@gmail.com</p>
                    <span>Написать нам</span>
                </div>
            </div>
            <div class="footer_item">
                <p>2019 OOO "Анна"</p>
            </div>
            <div class="footer_item">
                <div class="footer_item_icon">
                    <img src="email.svg">
                </div>
                <div class="footer_item_text">
                    <p>emailpochta@gmail.com</p>
                    <span>Написать нам</span>
                </div>
            </div>
        </div>
    </div>
    <div class="silver_stroke"></div>
</footer>

样式.css

footer p, footer span{
    color: white;
}
.footer_item {
    float: left;
}
.clearfix:after{
    content: '';
    display: table;
    width: 100%;
    clear: both;
}

我只是没有尝试插入边距:0 auto; 或显示:弯曲;并将项目与 justify-content: 中心对齐;

都没有成功。

css
  • 2 个回答
  • 10 Views
Martin Hope
Павел Ериков
Asked: 2020-01-28 03:32:41 +0000 UTC

通过 C# 程序在资源管理器中显示多个选定文件的路径

  • 1

我希望当您在资源管理器中选择几张照片并单击“打开”时,会打开一个程序,您可以在其中处理有关这些文件的信息。

我已经尝试过实现这一点,但是选择几个文件会打开几个程序窗口。

做了一点小调整

应用程序.xaml.cs

[DllImport("user32.dll")]
public static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, ref COPYDATASTRUCT lParam);

[StructLayout(LayoutKind.Sequential)]
public struct COPYDATASTRUCT
{
    public IntPtr dwData;
    public int cbData;
    public IntPtr lpData;
}
public const uint WM_COPYDATA = 0x004A;

protected override void OnStartup(StartupEventArgs e)
{
    base.OnStartup(e);
    Process this_process = Process.GetCurrentProcess();

    Process[] other_processes =
    Process.GetProcessesByName(this_process.ProcessName).Where(pr => pr.Id != this_process.Id).ToArray();

    foreach (var pr in other_processes)
    {
         pr.WaitForInputIdle(1000);
         IntPtr hWnd = pr.MainWindowHandle;
         if (hWnd == IntPtr.Zero) continue;
         string command = e.Args[0] + " " + e.Args.Length;
         var cds = new COPYDATASTRUCT();
         cds.dwData = (IntPtr)1;
         cds.cbData = (command.Length + 1) * 2;
         cds.lpData = Marshal.StringToHGlobalUni(command);
         SendMessage(hWnd, WM_COPYDATA, IntPtr.Zero, ref cds);
         Marshal.FreeHGlobal(cds.lpData);
         Environment.Exit(0);
    }
}    

和 MainWindow.xaml.cs

private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
     if (msg == WM_COPYDATA)
     {
         COPYDATASTRUCT data = new COPYDATASTRUCT();
         data = (COPYDATASTRUCT)Marshal.PtrToStructure(lParam, data.GetType());
         MessageBox.Show("Received command: " + Marshal.PtrToStringUni(data.lpData));
     }

     return IntPtr.Zero;
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
     WindowInteropHelper h = new WindowInteropHelper(this);
     HwndSource source = HwndSource.FromHwnd(h.Handle);
     source.AddHook(new HwndSourceHook(WndProc));//регистрируем обработчик сообщений      
}

原则上一切都很好,因为。当我单击图片时(程序窗口打开时),它显示在 MessageBox.Show 它的路径中。但是现在的问题是,如果我选择几张图片并单击打开,那么与图片一样多的实例会更早打开。现在 MessageBox.Show 将只显示首先选择的图片的路径。

c#
  • 1 个回答
  • 10 Views
Martin Hope
Павел Ериков
Asked: 2020-11-02 22:11:43 +0000 UTC

C#登录的正则表达式

  • 1

有一个正则表达式

^(?=.{8,24})(?=.+[0-9])[az][a-z0-9]*[._-]{1}[a-z0-9]+$

如何添加至少有一个大写字母的支票?以及如何改进这种表达方式?

此正则表达式的条件

1) 登录长度为 8 到 24 个字符

2) 以小写字母或数字开头和结尾

3) 登录名中应该只有 1 个字符。- 或者 _

4) 此外,登录名必须包含至少 1 位数字

5) 至少 1 个大写字母

c#
  • 1 个回答
  • 10 Views
Martin Hope
Павел Ериков
Asked: 2020-10-28 06:00:15 +0000 UTC

C# WPF MVVM 中的事件

  • 0

有 2 个 ViewModel 和 AuthViewModel 和 LoginViewModel。LoginView 有一个 Frame,它的内容是一个 LoginControl,它有一个 AuthViewModel 上下文。在这个 LoginControl 中有一个按钮,当单击时,LoginCompleted 事件应该会触发,并且该事件在 App.xaml.cs 中进行处理。

AuthViewModel.cs

 public event Action LoginCompleted;
    public ICommand AuthCommand => new RelayCommand(o => AuthMethod());


    public void AuthMethod()
    {
        LoginCompleted?.Invoke();
    }

    public event PropertyChangedEventHandler PropertyChanged;
    protected void OnPropertyChanged(string name) => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));

应用程序.xaml.cs

private LoginWindow Window { get; set; }
    private MainWindow CustomWindow { get; set; }
    public MainLoginVIewModel MainViewModel { get; set; }
    public LoginViewModel LoginViewModel { get; set; }

    protected override void OnStartup(StartupEventArgs e)
    {
        base.OnStartup(e);
        LoginViewModel = new LoginViewModel();
        MainViewModel = new MainLoginVIewModel(LoginViewModel);
        LoginViewModel.OnAuthorize += LoginViewModelOnOnAuthorize;
        Window = new LoginWindow { DataContext = MainViewModel };
        Window.Show();
    }

    private void LoginViewModelOnOnAuthorize(object sender, LoginEventArgs e)
    {
        if (e.IsAuthorized)
        {
            CustomWindow = new MainWindow { DataContext = new ContentViewModel(e.User) };
            CustomWindow.Show();
            Window.Close();
        }

    }

也就是说,通过单击 LoginControl 中的按钮,App.xaml.cs 中的事件应该会触发。如果我​​做错了什么,请告诉我如何更正确地实现它。

如果你需要更多,我会扔掉代码

注册控制.xaml

<Grid>
    <TextBox Text="{Binding User}" Width="100" Height="100"></TextBox>
</Grid>

RegisterViewModel.cs

class RegisterViewModel : VM
{
    private string user;
    public string User
    {
        get => user;
        set => Set(ref user, value);
    }
}

主授权窗口的ViewModel,我称之为MainLoginViewModel

RegisterViewModel regVM;
    public MainLoginVIewModel(LoginViewModel loginVM)
    {
        TestCommand = new RelayCommand(Test);
        regVM = new RegisterViewModel();
        CurrentContent = loginVM;
    }

    private VM currentContent;
    public VM CurrentContent
    {
        get => currentContent;
        set => Set(ref currentContent, value);
    }

    public ICommand TestCommand { get; }
    private void Test()
    {
        CurrentContent = regVM;
    }
c#
  • 1 个回答
  • 10 Views
Martin Hope
Павел Ериков
Asked: 2020-10-14 03:14:11 +0000 UTC

自定义控件 c# WPF

  • 2

如何制作像 PasswordBox 这样的自定义控件,其中密码框左侧会有一个文本块(锁定图标或其他符号)a。也就是说,将密码框和文本块组合成 1 个控件。我可以从中获取密码值。有一个想法让 PasswordBox a ControlTemplate из textblocka 和 passwordbox`a 但我还需要能够更改 MainWindow.xaml 中的样式

cs:CustomPassBox Style={StaticResource DefaultStyle}>

但是当我像这样设置控件样式时, controlTemplate 消失了。

c#
  • 1 个回答
  • 10 Views

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    我看不懂措辞

    • 1 个回答
  • Marko Smith

    请求的模块“del”不提供名为“default”的导出

    • 3 个回答
  • Marko Smith

    "!+tab" 在 HTML 的 vs 代码中不起作用

    • 5 个回答
  • Marko Smith

    我正在尝试解决“猜词”的问题。Python

    • 2 个回答
  • Marko Smith

    可以使用哪些命令将当前指针移动到指定的提交而不更改工作目录中的文件?

    • 1 个回答
  • Marko Smith

    Python解析野莓

    • 1 个回答
  • Marko Smith

    问题:“警告:检查最新版本的 pip 时出错。”

    • 2 个回答
  • Marko Smith

    帮助编写一个用值填充变量的循环。解决这个问题

    • 2 个回答
  • Marko Smith

    尽管依赖数组为空,但在渲染上调用了 2 次 useEffect

    • 2 个回答
  • Marko Smith

    数据不通过 Telegram.WebApp.sendData 发送

    • 1 个回答
  • Martin Hope
    Alexandr_TT 2020年新年大赛! 2020-12-20 18:20:21 +0000 UTC
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5