- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
在开始阅读问题之前请注意我阅读了 Petzold 的书(windows 编程第 5 版)!
在 form.h 中,我声明了名为 CMessage 的自定义消息(它代表“自定义消息”,稍后调用创建按钮的方法),在 formbutton.cpp 中,我从其构造函数向主窗口发送消息在 form.cpp 中创建。我从我的助手类中的 getLastError 方法中发现由于窗口句柄无效而发送消息失败,这很奇怪,因为如果我在发送消息之前使用 isWindow(forms.mainWindowHandle) 我得到的消息框显示“操作成功完成”。在将整个代码拆分为多个 header 和 cpps 之前,相同的设计是可行的,但现在已损坏。
******
step by step debugging :
form myform(hInstance);
form::form(HINSTANCE hInstanceForm);
HWND form::Createform(HWND handle, HINSTANCE hInstance, int xPos , int yPos , int xSize , int ySize);
Formbutton::Formbutton(BOOL create);
form::form();
SendMessage(forms.mainWindowHandle, CMessage,0 ,0);//error
//rest of the code and main window is shown but not the button
******
表单按钮.cpp
#pragma once
#include "FormButton.h"
#include "Form.h"
#include "helper.h"
Formbutton::Formbutton(){}
Formbutton::Formbutton(BOOL create)
{
helper help;
form forms;
if (create)
{
SendMessage(forms.mainWindowHandle, CMessage,0 ,0); //same with PostMessage
help.getLastError();
}
}
HWND Formbutton::createButton(HWND button, HWND parent)
{
button = CreateWindow(TEXT("Button"), // window class name
TEXT("Button"), // window caption
WS_CHILD | WS_VISIBLE | BS_DEFPUSHBUTTON | WS_TABSTOP, // window style
500, // initial x position
500, // initial y position
300, // initial x size
500, // initial y size
parent, // parent window handle
NULL, // window menu handle
(HINSTANCE)GetWindowLongPtr(parent, -6), // program instance handle
NULL); // creation parameters
ShowWindow(button, SW_SHOW);
return button;
}
表格.h
#pragma once
#include <Windows.h>
#define CMessage (WM_USER+0x0001)
class form
{
private:
HWND Createform(HWND handle, HINSTANCE hInstance, int xPos = CW_USEDEFAULT, int yPos = CW_USEDEFAULT, int xSize = GetSystemMetrics(SM_CXFULLSCREEN), int ySize = GetSystemMetrics(SM_CYFULLSCREEN));
MSG msg;
WNDCLASS wndclass;
protected:
static LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
public:
HWND mainWindowHandle;
form::form();
form::form(HINSTANCE hInstanceForm);
form::~form();
WPARAM updateForm();
};
表单.cpp
#pragma once
#include "Form.h"
#include "FormButton.h"
LRESULT CALLBACK form::WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HDC hdc;
PAINTSTRUCT ps;
RECT rect;
Formbutton fb;
switch (message)
{
case WM_CREATE:
return 0;
case WM_PAINT:
InvalidateRect(hwnd, &rect, TRUE);
hdc = BeginPaint(hwnd, &ps);
SelectObject(hdc, GetStockObject(SYSTEM_FIXED_FONT));
SetBkMode(hdc, TRANSPARENT);
EndPaint(hwnd, &ps);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
case CMessage: //this is the custom message
fb.createButton(fb.buttonHandle, hwnd);
default:
return DefWindowProc(hwnd, message, wParam, lParam);
}
return 0;
}
HWND form::Createform(HWND handle, HINSTANCE hInstance, int xPos , int yPos , int xSize , int ySize)
{
wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = TEXT("Framework");
if (!RegisterClass(&wndclass))
{
MessageBox(NULL, TEXT("This program requires Windows NT!"),
TEXT("Framework"), MB_ICONERROR);
}
handle = CreateWindow(TEXT("Framework"), // window class name
TEXT("The Hello Program"), // window caption
WS_OVERLAPPEDWINDOW | WS_MAXIMIZEBOX | WS_MINIMIZEBOX | WS_SIZEBOX | WS_HSCROLL | WS_VSCROLL, // window style
xPos, // initial x position
yPos, // initial y position
xSize, // initial x size
ySize, // initial y size
NULL, // parent window handle
NULL, // window menu handle
hInstance, // program instance handle
NULL); // creation parameters
//ShowWindow(handle, SW_SHOWMAXIMIZED);
UpdateWindow(handle);
return handle;
}
WPARAM form::updateForm()
{
ShowWindow(mainWindowHandle, 10);
UpdateWindow(mainWindowHandle);
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
form::form()
{
}
form::form(HINSTANCE hInstanceForm)
{
mainWindowHandle = Createform(mainWindowHandle, hInstanceForm);
}
form::~form()
{
}
助手.cpp
#pragma once
#include "helper.h"
#include <strsafe.h>
void helper::getLastError()
{
wchar_t buf[256];
FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM, NULL, GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), buf, 256, NULL);
MessageBox(NULL, buf, TEXT("error"), MB_OK);
}
源代码.cpp
#pragma once
#include <Windows.h>
#include "Form.h"
#include "FormButton.h"
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
form myform(hInstance);
Formbutton btn(true);
myform.updateForm();
}
最佳答案
您正在创建 form
的两个实例:一个在您的 WinMain()
中,一个在您的 Formbutton
构造函数中。您想将按钮添加到前者,但实际上是将其添加到后者,一旦构造函数返回,后者就不复存在。
我不确定您希望如何构建您的程序,但我的建议是将正确的父表单作为 form *
参数传递给 Formbutton
构造函数。
关于c++ - SendMessage 无法将自定义消息发送到主窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38292981/
https://github.com/mattdiamond/Recorderjs/blob/master/recorder.js中的代码 我不明白 JavaScript 语法,比如 (functio
在 iOS 7 及更早版本中,如果我们想在应用程序中找到 topMostWindow,我们通常使用以下代码行 [[[UIApplication sharedApplication] windows]
我已经尝试解决这个问题很长一段时间了:我无法访问窗口的 url,因为它位于另一个域上..有一些解决方案吗? function login() { var cb = window.ope
是否可以将 FFMPEG 视频流传递到 C# 窗口?现在它在新窗口中作为新进程打开,我只是想将它传递给我自己的 SessionWindow。 此时我像这样执行ffplay: public void E
我有一个名为 x 的矩阵看起来像这样: pTime Close 1 1275087600 1.2268 2 1275264000 1.2264 3 1275264300 1.2
在编译时,发生搜索,grep搜索等,Emacs会在单独的窗口中创建一个新的缓冲区来显示结果,有没有自动跳转到那个窗口的方法?这很有用,因为我可以使用 n 和 p 而不是 M-g n 和 M-g p 移
我有一个启动 PowerShell 脚本的批处理文件。 批处理文件: START Powershell -executionpolicy RemoteSigned -noexit -file "MyS
我有一个基于菜单栏的应用程序,单击图标时会显示一个窗口。在 Mac OS X Lion 上一切正常,但由于某种原因,在 Snow Leopard 和早期版本的 Mac OS X 上会出现错误。任何时候
在 macOS 中,如何在 Xcode 和/或 Interface Builder 中创建带有“集成标题栏和工具栏”的窗口? 这是“宽标题栏”类型的窗口,已添加到 OS X 10.10 Yosemit
在浏览器 (Chrome) 中 JavaScript: var DataModler = { Data: { Something: 'value' }, Process: functi
我有 3 个 html 页面。第 1 页链接到第 2 页,第 2 页链接到第 3 页(为了简单起见)。 我希望页面 2 中的链接打开页面 3 并关闭页面 1(选项卡 1)。 据我了解,您无法使用 Ja
当点击“创建节点”按钮时,如何打开一个新的框架或窗口?我希望新框架包含一个文本字段和下拉菜单,以便用户可以选择一个选项。 Create node Search node
我有一个用户控件,用于编辑应用程序中的某些对象。 我最近遇到一个实例,我想弹出一个新的对话框(窗口)来托管此用户控件。 如何实例化新窗口并将需要设置的任何属性从窗口传递到用户控件? 感谢您的宝贵时间。
我有一个Observable,它发出许多对象,我想使用window或buffer操作对这些对象进行分组。但是,我不想指定count参数来确定窗口中应包含多少个对象,而是希望能够使用自定义条件。 例如,
我有以下代码,它打开一个新的 JavaFX 阶段(我们称之为窗口)。 openAlertBox.setOnAction(e -> { AlertBox alert = AlertBox
我要添加一个“在新窗口中打开”上下文菜单项,该菜单项将以新的UIScene打开我的应用程序文档之一。当然,我只想在实际上支持多个场景的设备上显示该菜单项。 目前,我只是在检查设备是否是使用旧设备的iP
我正在尝试创建一个 AIR 应用程序来记录应用程序的使用情况,使用 AIR 从系统获取信息的唯一简单方法是使用命令行工具和抓取 标准输出 . 我知道像 这样的工具顶部 和 ps 对于 OS X,但它们
所以我有这个简单的 turtle 螺旋制作器,我想知道是否有一种方法可以打印出由该程序创建的我的设计副本。 代码: import turtle x= float(input("Angle: ")) y
我正在编写一个 C# WPF 程序,它将文本消息发送到另一个程序的窗口。我有一个宏程序作为我的键盘驱动程序 (Logitech g15) 的一部分,它已经这样做了,尽管它不会将击键直接发送到进程,而是
我尝试使用以下代码通过 UDP 发送,但得到了奇怪的结果。 if((sendto(newSocket, sendBuf, totalLength, 0, (SOCKADDR *)&sendAd
我是一名优秀的程序员,十分优秀!