用C语言编写windows窗体程序,怎么创建按钮以及怎么添加按钮的触发事件啊

用C语言编写windows窗体程序,怎么创建按钮以及怎么添加按钮的触发事件啊,第1张

Windows

窗体程序是基于消息机制的,所有控件,它的本质都是一个窗体,都是使用

CreateWindows

函数来创建,不过类名,则需要指定为系统预先注册的控件类,比如,你要创建一个按钮控件,就要这样子

CreateWindows(

"BUTTON",

"按钮标题"

),CreateWindows

这个函数你应该用了不少了吧,其余的参数可以参考

MSDN,但是类名我们指定了

“BUTTON”,说明我们要创建一个按钮,每个按钮都有一个唯一的

ID,通过你的消息处理函数,可以知道哪个按钮发生了什么事件,详细讲的话,非常多,你可以参考

MSDN,或者《Windows

程序设计》这本书

打开vc 60

文件->新建

在“工程”标签中选择“MFC AppWizard(exe)”

在右边输入工程名称,并选择工程的存放路径

在Step1对话框中选择“多重文档”(默认)后点击完成,在确认对话框中点击“确认”

以上是建立一个对话框工程

点击Workspace窗口中的ResorceView标签,展开Resorces边上的+号

点击“Dialog”边上的+号,这里面就是这个工程的所有对话框资源

点击“IDD_ABOUTBOX”对话框资源,这时在右边应该出现这个对话框的样式了

“IDD_ABOUTBOX”对话框资源中有一个“确定”按钮,双击这个按钮

这时会d出一个函数名称确认对话框,对话框中的函数名称默认为 OnOK 单击确认

MFC会自动定位到这个函数的实现中,你可以在这里编写你的函数功能而这个 OnOK()函数,就是当用户点击“IDD_ABOUTBOX”这个对话框中的“确认”按钮时要运行的函数

在这个函数中输入: MessageBox("失败");

点击Build工具(或者按F7)编译这个工程,这个编译过程也就是将源代码转换为执行程序

点击Buildexecute(或者按Ctl+F5)执行这个工程的程序

这时程序应该会开始运行,这个程序是一个多文档程序

点击程序窗口菜单的“帮助”->“关于()”会d出一个对话框,点击对话框中的“确定”按钮会d出一个消息框,消息框中的文本提示为“失败”

这个步骤算详细了吧?

启动activity 可以使用InstrumentationTestCase

发送手机事件

InstrumentationsendCharacterSync(KeyEventKEYCODE_DPAD_DOWN);

InstrumentationsendCharacterSync(KeyEventKEYCODE_DPAD_CENTER);

使用android的测试功能,写一个类继承InstrumentationTestCase

然后在这个类里获得Instrumentation实例,通过它可以启动Activity,发送手机事件等

------------------------------------------------------

import androidappInstrumentation;

import androidcontentContentResolver;

public class ActivityTest extends InstrumentationTestCase {

private Instrumentation mInst = null;

private ContentResolver mContentResolver = null;

@Override

protected void setUp() throws Exception {

supersetUp();

mInst = getInstrumentation();

mContentResolver = mInstgetContext()getContentResolver();

}

public void testStartActivity() throws Exception {

//launch activity

Intent intent = new Intent(IntentACTION_MAIN);

intentaddFlags(IntentFLAG_ACTIVITY_NEW_TASK);

String activityPackagePath = "comandroid";

intentsetClassName(activityPackagePath, TargetActivitygetClass()getName());

TargetActivity mActivity = (TargetActivity) getInstrumentation()startActivitySync(intent);

mInstwaitForIdleSync();

//send keyevent to press button

mInstsendCharacterSync(KeyEventKEYCODE_DPAD_DOWN);

mInstsendCharacterSync(KeyEventKEYCODE_DPAD_CENTER);

mInstwaitForIdleSync();

}

}

[DllImport("user32dll", EntryPoint = "FindWindow", CharSet = CharSetAuto)]

static extern IntPtr FindWindow(string lpClassName,string lpWindowName);

[DllImport("user32dll", EntryPoint = "FindWindowEx", CharSet = CharSetAuto)]

extern static IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);

[STAThread]

static void Main(string[] args)

{

string path = "\\\\\\AUT3\\bin\\Debug\\AUT3exe";

Process p = ProcessStart(path);

if (p==null)

ConsoleWriteLine("Warning:process may already exist");

ConsoleWriteLine("Finding main window handle");

IntPtr mwh = FindMainWindowHandle("Form1", 100, 25);

ConsoleWriteLine("Handle to main window is " + mwh);

//有名字控件句柄

ConsoleWriteLine("Findding handle to textbox1");

IntPtr tb = FindWindowEx(mwh, IntPtrZero, null, "<enter color>");

if (tb == IntPtrZero)

throw new Exception("Unable to find textbox1");

else

ConsoleWriteLine("Handle to textbox1 is " + tb);

ConsoleWriteLine("Findding handle to button1");

IntPtr butt = FindWindowEx(mwh, IntPtrZero, null, "button1");

if (butt == IntPtrZero)

throw new Exception("Unable to find button1");

else

ConsoleWriteLine("Handle to button1 is " + butt);

//没有名字或者重名控件

ConsoleWriteLine("Findding handle to listbox1");

IntPtr lb = FindWindowByIndex(mwh,3);

if (lb == IntPtrZero)

throw new Exception("Unable to find listbox1");

else

ConsoleWriteLine("Handle to listbox1 is " + lb);

}

方法:

//通过索引查找相应控件句柄

static IntPtr FindWindowByIndex(IntPtr hwndParent,int index)

{

if (index == 0)

{

return hwndParent;

}

else

{

int ct = 0;

IntPtr result = IntPtrZero;

do

{

result = FindWindowEx(hwndParent,result,null,null);

if (result != IntPtrZero)

{

++ct;

}

} while (ct<index&&result!=IntPtrZero);

return result;

}

}

//获得待测程序主窗体句柄

private static IntPtr FindMainWindowHandle(string caption,int delay,int maxTries)

{

IntPtr mwh = IntPtrZero;

bool formFound = false;

int attempts = 0;

while (!formFound && attempts < maxTries)

{

if (mwh == IntPtrZero)

{

ConsoleWriteLine("Form not yet found");

ThreadSleep(delay);

++attempts;

mwh = FindWindow(null, caption);

}

else

{

ConsoleWriteLine("Form has been found");

formFound = true;

}

}

if (mwh == IntPtrZero)

throw new Exception("Could not find main window");

else

return mwh;

}

private/protected出来的方法,不知道楼主怎么会到用a对象对调用。

public class A

{

private void MethodA(){};

}

根据面向对象来说,

A a = new A();

aMethodA(); //这样不知道你怎么调用的,能找到方法不?对面各对象不熟悉的人伤不起啊!建议学习面向对象,将知识掌握牢固。

你的情况一般可以考虑使用委托来实现,请了解相应的知识!强烈建议使用委托!

void QWidget::closeEvent(QCloseEvent event)及其相应子类的closeEvent()

当点击关闭按钮时,会调用该函数,你要执行什么判断,就重新实现下该函数。

在你的用户控件里面添加自己的事件

public delegate void TestEvents();

public event TestEvents MyTestEvens;

在里面的button事件里面抛出事件如:

private void button1_Click(object sender, EventArgs e)

{

if (MyTestEvens != null)

{

MyTestEvens();

}

}

然后再你的主窗体里面添加事件:(test1就是用户控件MyTestEvens 是刚刚自定义的事件)

private void Form1_Load(object sender, EventArgs e)

{

test1MyTestEvens += new testTestEvents(test1_MyTestEvens);

}

void test1_MyTestEvens()

{

//这里就可以做你想做的事情了

MessageBoxShow("test ok");

}

以上就是关于用C语言编写windows窗体程序,怎么创建按钮以及怎么添加按钮的触发事件啊全部的内容,包括:用C语言编写windows窗体程序,怎么创建按钮以及怎么添加按钮的触发事件啊、vc++6.0 怎样做一个按钮事件 本人新手、android 怎么样让按钮自动执行点击事件等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zz/9608312.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-30
下一篇 2023-04-30

发表评论

登录后才能评论

评论列表(0条)

保存