标签: subclassing

安全删除窗口子类?

我试图使用全局CBT钩子在Windows系统上子类化当前关注的窗口.这与此问题中发生的事情有关,但错误是不同的.

当这个子类化生效时会发生什么,是Opera的(版本10.50)主窗口被阻止显示.Opera有一个"启动画面",您需要在主窗口中单击"开始"以显示Opera未正确关闭后显示的内容.每当弹出此窗口时,Opera的主窗口都不会显示.如果Opera正常关闭,并且未显示此启动画面,则主窗口将按原样显示.

HHOOK hHook;
HWND hWndSubclass = 0;

void SubclassWindow(HWND hWnd)
{
    Unsubclass();
    FARPROC lpfnOldWndProc = (FARPROC)SetWindowLongPtr(hWnd, GWLP_WNDPROC, (LPARAM)SubClassFunc);
    SetProp(hWnd, L"PROP_OLDWNDPROC", lpfnOldWndProc);
    hWndSubclass = hWnd;
}

void Unsubclass()
{
    if (hWndSubclass != 0 && IsWindow(hWndSubclass))
    {
        FARPROC lpfnOldWndProc = (FARPROC)GetProp(hWndSubclass, L"PROP_OLDWNDPROC");
        RemoveProp(hWndSubclass, L"PROP_OLDWNDPROC");
        SetWindowLongPtr(hWndSubclass, GWLP_WNDPROC, (LPARAM)lpfnOldWndProc);
        hWndSubclass = 0;
    }
}

static LRESULT CALLBACK SubClassFunc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    if (message == WM_MOVING)
    {
        // do something irrelevant
    }
    else if (message …
Run Code Online (Sandbox Code Playgroud)

c++ winapi subclassing

4
推荐指数
1
解决办法
1295
查看次数

如何提示使用UIView子类进行编译

我有一个UIViewController的子类,负责单个UIWebView.

由于这是一个简单的例子,我重写-(void)loadView,实例化UIWebView并为其分配控制器的view属性:

- (void)loadView 
{
    UIWebView *wv = [[[UIWebView alloc] initWithFrame:self.frame] autorelease];
    // other configuration here...  
    self.view = wv;
}
Run Code Online (Sandbox Code Playgroud)

这是好的,直到我调用UIWebView的方法.例如...

[self.view loadHTMLString:HTMLString baseURL:baseURL];
Run Code Online (Sandbox Code Playgroud)

...导致编译器警告......

warning: 'UIView' may not respond to '-loadHTMLString:baseURL:'
Run Code Online (Sandbox Code Playgroud)

...因为该view财产被宣布为UIView.

现在警告很容易通过演员来解决......

[(UIWebView *)self.view loadHTMLString:HTMLString baseURL:baseURL];
Run Code Online (Sandbox Code Playgroud)

...但我想做的是在界面中提供正确的类型提示.我尝试覆盖该view属性,MyViewController.h但这也扰乱了编译器:

warning: property 'view' type does not match super class 'UIViewController' property type
Run Code Online (Sandbox Code Playgroud)

有没有办法告诉编译器(和我的伙伴)这就是我正在做的事情,而且我知道这就是我正在做的事情,这一切都没关系?(如果不是,我想我会坚持演员.)

TIA

编辑:我尝试按照marcus.ramsden的回答重新声明视图属性:这消除了警告(以及对演员的需要)但是停止了视图出现!我不确定为什么这应该是因为控制器在被要求时仍然会返回一个UIView(子类)...

iphone uiwebview uiviewcontroller subclassing ios

4
推荐指数
1
解决办法
1067
查看次数

MVC UpdateModel和子类与基类

我正在寻找将UpdateModel方法用于在运行时检索的Sub Class,如果有人可以了解我是否正在对其进行总体哈希和/或我是否正在尝试做是可能的.

我正在使用通用操作来控制一堆部分视图的验证; 我试图摆脱每个局部视图的特定动作.

每个局部视图都有一个从基本模型派生的唯一模型:

public class ModelA : ModelBase{
     [Required]
     public string SomeStringProperty{get;set;}
...
}
public class ModelB : ModelBase{
     [Required]
     public DateTime? SomeDateProperty{get;set;}
...
}
public class ModelBase{
     public Guid InstanceId{get;set;}
}
Run Code Online (Sandbox Code Playgroud)

我正在使用Action上的FormCollection来获取提交的表单元素及其值,这包括View应该用于验证其请求的模型类型.忽略这个例子的安全含义,我知道它们,这是一个内部唯一的概念证明

    [HttpPost]
    public ActionResult ChangeCaseState(int id, FormCollection formCollection)
    {
        Guid instanceId = new Guid(formCollection["instanceId"]);
        string modelType = formCollection["modelType"];
        //Return a specific Model class based on the event/modelType
        var args = GetStateModelClass(modelType, instanceId);

        try
        {
            UpdateModel(args);
            if(Model.IsValid){
             ...
        }
        catch (Exception)
        {
            return View("~/Views/Shared/StateForms/" + modelType …
Run Code Online (Sandbox Code Playgroud)

asp.net-mvc asp.net-3.5 updatemodel subclassing

4
推荐指数
1
解决办法
1681
查看次数

是否子类化UIWebView不受欢迎?

我听说过是否允许创建UIWebView的子类.有人可以将我链接到任何澄清这种或那种方式的文档吗?

iphone cocoa-touch uiwebview subclassing

4
推荐指数
2
解决办法
3198
查看次数

正确处理Subclassed PyQT LineEdit中的keyPressEvent

所以我有一个QLineEdit,我想抓住一个Shift键盘.

这是我的代码:

class NoteText(QtGui.QLineEdit):
    def __init__(self, parent):
        super (NoteText, self).__init__(parent)

    def keyPressEvent(self, event):
        if (event.modifiers() & QtCore.Qt.ShiftModifier):
            self.shift = True
            print 'Shift!'
Run Code Online (Sandbox Code Playgroud)

你可以猜到,我可以捕捉shift键,但是你不能在LineEdit中输入文本.我试过捕捉按键,但我不确定如何处理它们以允许用户继续键入小部件.

我错过了什么?谢谢!

python pyqt subclassing

4
推荐指数
1
解决办法
3835
查看次数

c++ winapi - 子类化编辑控件 - 单击“Enter”时出现烦人的声音

我制作了一个编辑控件,当有人单击“Enter”时能够接受数据。我使用子类化来做到这一点。它几乎完美地工作了。但点击“Enter”后,系统每次都会发出“Error”声音。我尝试使用 ES_MULTILINE 和 ES_AUTOVSCROLL 来绕过它,但它有部分帮助。现在点击“Enter”后没有声音,但文本框中出现无用的“Enter”字符,无法删除。如何绕过系统声音?或者从该文本框中删除“Enter”字符(SetWindowText(handle, "") 没有帮助)。

c++ winapi subclassing

4
推荐指数
1
解决办法
1583
查看次数

在iOS 9上进行演示之前,UIPopoverPresentationController应该设置一个非零的sourceView或barButtonItem

我正在尝试使用自定义UIPopoverPresentationController类来显示弹出窗口.但它崩溃了错误(<UIPopoverPresentationController: 0x7a772950>) should have a non-nil sourceView or barButtonItem set before the presentation occurs.下面是我发生崩溃的按钮单击代码.

- (IBAction)showPopup:(UIButton *)sender {
ViewController *contentViewController = [[ViewController alloc] init];

    contentViewController.preferredContentSize = CGSizeMake(200, 200);
    contentViewController.modalPresentationStyle = UIModalPresentationPopover;
    myPopoverController *popOver = [[myPopoverController alloc]initWithPresentedViewController:contentViewController presentingViewController:self andTintColor:[UIColor lightGrayColor]];

    popOver.delegate = self;
    popOver.permittedArrowDirections = UIPopoverArrowDirectionUp;
    popOver.sourceRect = sender.frame;
    popOver.sourceView = self.view;
    [self presentViewController:contentViewController animated: YES completion: nil];
}
Run Code Online (Sandbox Code Playgroud)

下面是我的自定义UIPopoverPresentationController的样子示例

myPopoverController.h file

@interface myPopoverController : UIPopoverPresentationController

@property (readonly) UIColor *tintColor;


-(instancetype)initWithPresentedViewController:(UIViewController *)presentedViewController presentingViewController:(UIViewController *)presentingViewController andTintColor:(UIColor *)aTintColor;

@end


myPopoverController.m file …
Run Code Online (Sandbox Code Playgroud)

exception objective-c subclassing uipresentationcontroller ios9

4
推荐指数
3
解决办法
8808
查看次数

在Objective-C中如何处理相关对象的NSArray

Animal是一个BOOL名为属性的类alive.Monkey,Zebra并且Walrus是子类Animal.如果我有一个包含混合实例的NSArray调用zoo实例Monkey,Zebra并且Walrus,我想找到第一个活动Zebra实例,我可能会这样做:

Zebra *zebra; 
for (Animal *animal in zoo) {
    if ([animal isMemberOfClass:[Zebra class]] && animal.alive) {
        zebra = animal;
        break;
    }
}
Run Code Online (Sandbox Code Playgroud)

问题是编译器在设置时抱怨不兼容的指针类型zebra = animal.如果我像那样做一些铸造zebra = (Zebra *)animal似乎工作,但我不确定在Objective-C中这种铸件是否安全.

处理这种情况的更好方法是什么?

cocoa objective-c nsarray subclassing

3
推荐指数
1
解决办法
114
查看次数

如何在python中使用对象名作为变量

我怎样才能定义一个类,使对象名可以用作变量?我正在编写一个数值分析类,它只包含一个numpy数组作为即时变量和许多数学运算.为了方便和可读性,仅仅通过它的对象名访问numpy数组会很不错.例如,可以直接调用numpy数组(或来自matplotlib,pandas等许多其他包的对象),

import numpy as np

foo = np.array([[1,2],[3,4]])
print foo
Run Code Online (Sandbox Code Playgroud)

这就像我所拥有的(省略了定义方法)

import numpy as np

class MyClass():
  def __init__(self, input_array):
    self.data = np.array(input_array)

foo = MyClass([[1,2],[3,4]])
print foo
print foo.data
Run Code Online (Sandbox Code Playgroud)

这将foo打印为foo.data的指针和内容

<__main__.MyClass instance at 0x988ff0c>
[[1 2]
 [3 4]]
Run Code Online (Sandbox Code Playgroud)

我尝试self = np.array(input_array)了构造函数,但它仍然给出了一个地址.我在想像重载对象名称,但我找不到任何信息.

如果不可能,那么这些包怎么能实现呢?我试着读一些numpy和pandas的源代码.但作为python的新手,我几乎不可能在那里找到答案.

编辑

感谢CoryKramer和ŁukaszR.建议使用__str____repr__.但我意识到我真正需要的是继承numpy.ndarray甚至是pandas DataFrame,以便继承所有数学函数.我发现这两个环节非常有帮助,对ndarray对数据帧,以tweek __init__我的接口调用.

python oop subclassing

3
推荐指数
1
解决办法
497
查看次数

issubclass的抽象基类Sequence

该列表显示了你需要实现你的类方法被"视为"为序列:__getitem__,__len__,__contains__,__iter__,__reversed__,index,和count.那么为什么这个最小的实现不起作用,为什么issubclass(S, Sequence) is False呢?

from collections import *


class S(object):
    def __getitem__(self, item):
        raise IndexError

    def __len__(self):
        return 0

    def __contains__(self, item):
        return False

    def __iter__(self):
        return iter(())

    def __reversed__(self):
        return self

    def index(self, item):
        raise IndexError

    def count(self, item):
        return 0


issubclass(S, Iterable)   # True  :-)
issubclass(S, Sized)      # True  :-)
issubclass(S, Container)  # True  :-)
issubclass(S, Sequence)   # False :-(
Run Code Online (Sandbox Code Playgroud)

我需要实现一个我忽略的额外方法吗?我是否误解了抽象基类?子类化当然 …

python duck-typing subclassing abstract-base-class isinstance

3
推荐指数
1
解决办法
533
查看次数