Windows api编写窗口应用程序在子窗口中遇到的问题。
父窗口的客户区显示的是文本,那么我想在菜单中选中某个选项,跳转到子窗口中,子窗口中应该要有焦点,实现文字输入,和剪贴复制粘贴功能,怎么做?我的方法是使用下面的函数Move...
父窗口的客户区显示的是文本,那么我想在菜单中选中某个选项,跳转到子窗口中,子窗口中应该要有焦点,实现文字输入,和剪贴复制粘贴功能,怎么做?我的方法是使用下面的函数MoveWindows (), SetFocus (), 告诉我怎样从父窗口中跳转到子窗口中就可以了,有懂的话,我可以把代码发给你看下。
再定义一个回调函数? 展开
再定义一个回调函数? 展开
展开全部
#include <windows.h>
#include <windowsx.h>
#include <commctrl.h>
#include <tchar.h>
#pragma comment(lib, "user32.lib")
#pragma comment(lib, "gdi32.lib")
#pragma comment(lib, "comctl32.lib")
TCHAR g_szMainText[128] = _T("显示一些东西");
LRESULT CALLBACK MainProc(HWND hwnd, UINT uMsg, WPARAM wp, LPARAM lp)
{
switch(uMsg) {
case WM_CLOSE:
DestroyWindow(hwnd);
return 0;
case WM_NCDESTROY:
PostQuitMessage(0);
return 0;
case WM_PAINT: {
PAINTSTRUCT ps;
HDC dc = BeginPaint(hwnd, &ps);
TextOut(dc, 0, 0, g_szMainText, lstrlen(g_szMainText));
EndPaint(hwnd, &ps);
return 0;
}
case WM_CREATE: {
CreateWindow(_T("BUTTON"), _T("变更文字"), WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON,
300, 200, 80, 50, hwnd, (HMENU) 10001, (HINSTANCE)GetWindowLong(hwnd, GWL_HINSTANCE), 0);
return 0;
}
case WM_COMMAND: {
if (LOWORD(wp) == 10001 && HIWORD(wp) == BN_CLICKED) {
EnableWindow(hwnd, FALSE);
HWND h = CreateWindow(_T("SubWindow"), _T("子窗口"), WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, 400, 300, NULL, NULL, (HINSTANCE)GetWindowLong(hwnd, GWL_HINSTANCE), 0);
ShowWindow(h, SW_SHOWNORMAL);
UpdateWindow(h);
MSG msg;
while(GetMessage(&msg, 0, 0, 0) > 0) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
InvalidateRect(hwnd, NULL, TRUE);
EnableWindow(hwnd, TRUE);
SetFocus(hwnd);
}
return 0;
}
default:
return DefWindowProc(hwnd, uMsg, wp, lp);
}
}
LRESULT CALLBACK SubProc(HWND hwnd, UINT uMsg, WPARAM wp, LPARAM lp)
{
static HWND hEdit;
switch(uMsg) {
case WM_CLOSE:
DestroyWindow(hwnd);
return 0;
case WM_NCDESTROY:
PostQuitMessage(0);
return 0;
case WM_CREATE:
hEdit = CreateWindow(_T("EDIT"), g_szMainText, WS_TABSTOP | WS_VISIBLE | WS_CHILD | WS_BORDER | ES_AUTOHSCROLL,
10, 10, 250, 30, hwnd, NULL, (HINSTANCE)GetWindowLong(hwnd, GWL_HINSTANCE), 0);
CreateWindow(_T("BUTTON"), _T("确定"), WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON,
300, 200, 80, 50, hwnd, (HMENU) 10001, (HINSTANCE)GetWindowLong(hwnd, GWL_HINSTANCE), 0);
SetFocus(hEdit);
return 0;
case WM_COMMAND: {
if (LOWORD(wp) == 10001 && HIWORD(wp) == BN_CLICKED) {
Edit_GetText(hEdit, g_szMainText, 128);
PostMessage(hwnd, WM_CLOSE, 0, 0);
}
return 0;
}
default:
return DefWindowProc(hwnd, uMsg, wp, lp);
}
}
int APIENTRY _tWinMain(HINSTANCE hInst, HINSTANCE hPrev, LPTSTR szCmd, INT nShow)
{
InitCommonControls();
WNDCLASS wc;
ZeroMemory(&wc, sizeof(wc));
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = MainProc;
wc.hInstance = hInst;
wc.lpszClassName = _T("MainWindow");
wc.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
RegisterClass(&wc);
wc.lpszClassName = _T("SubWindow");
wc.lpfnWndProc = SubProc;
RegisterClass(&wc);
HWND hMain = CreateWindow(_T("MainWindow"), _T("主窗口"), WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, 400, 300, NULL, NULL, hInst, 0);
ShowWindow(hMain, SW_SHOWNORMAL);
UpdateWindow(hMain);
MSG msg;
while(GetMessage(&msg, 0, 0, 0) > 0) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
我写了个例子,你看看?
p.s. 我记得你以前好像不是学windows api写图形界面的样子,记错了吗orz
追问
我之前 学C++,我在百度贴吧也看过你? 你写的例子跟我的想法有一些差别,我加你企鹅详细说下问题,
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询