`
lobin
  • 浏览: 417549 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Windows Win32编程 - 窗口

 
阅读更多

窗口

通常,我们理解的窗口是如下这样的窗口。我们经常所看到的这种窗口只是一个标准窗口。除了这种标准窗口,还有好几种或者说好多其他类型的窗口。

标准窗口

Pop-Up窗口

 

对话框

对话框也是一种窗口。

 

桌面窗口

也就是我们常说的桌面,Desktop或者DesktopWindow,这也是一种窗口。

 

 

桌面窗口

 

 

窗口类

Windows下的窗口有个窗口类。Windows下的窗口类包括系统类,应用程序全局类以及应用程序本地类三类。系统类是Windows提供的可以直接使用,不需要注册的窗口类。应用程序全局类以及应用程序本地类是需要调用RegisterClass函数进行注册。

 

要实现一个窗口,需要注册一个窗口类。刚开始编写Windows的窗口界面时,如果不了解Windows下的窗口注册,想要实现一个通常意义下的窗口,上面的标准窗口,这个工作还有点麻烦,因为需要自己注册一个窗口类,Windows的系统窗口类并没有这样的窗口类,到是有个对话框窗口,窗口类为#32770,可以直接使用,不过这毕竟还是跟普通的标准窗口还是有些不同。

写道
https://docs.microsoft.com/en-us/windows/win32/winmsg/about-window-classes?redirectedfrom=MSDN

注册窗口类

 

创建窗口

调用CreateWindow或者CreateWindowEx函数创建窗口。

 

CreateWindow

HWND CreateWindow( 
  LPCTSTR lpClassName, 
  LPCTSTR lpWindowName, 
  DWORD dwStyle, 
  int x, 
  int y, 
  int nWidth, 
  int nHeight, 
  HWND hWndParent, 
  HMENU hMenu, 
  HANDLE hInstance, 
  PVOID lpParam 
);

CreateWindowEx

HWND CreateWindowEx(
  DWORD dwExStyle, 
  LPCTSTR lpClassName, 
  LPCTSTR lpWindowName, 
  DWORD dwStyle, 
  int x, a
  int y, 
  int nWidth, 
  int nHeight, 
  HWND hWndParent, 
  HMENU hMenu, 
  HINSTANCE hInstance, 
  LPVOID lpParam 
);

这两个函数都返回一个窗口句柄。

 

窗口句柄

句柄在Win32 API中是一个很重要的概念。后续所有和窗口相关的操作都需要这个窗口句柄。

 

除了窗口句柄(HWND),还有HMODULE、HINSTANCE等。

 

窗口事件处理

 

窗口处理函数

函数原型

LRESULT CALLBACK WindowProc(
  HWND hwnd, 
  UINT uMsg, 
  WPARAM wParam, 
  LPARAM lParam 
);

 

父窗口

子窗口

 

窗口拥有者

 

窗口被拥有者

 

标准窗口

#include <stdio.h>
#include <windows.h>

#include "window.h"

LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
  PAINTSTRUCT ps;
  HDC hDC;

  switch (uMsg) 
  {
    case WM_CREATE:
    {
      printf("[WM_CREATE] \n");
      break;
    }
    case WM_COMMAND:
      printf("[WM_COMMAND] \n");
      break;
    case WM_PAINT:
    {
      printf("[WM_PAINT] \n");
      hDC = BeginPaint(hWnd, &ps);

      EndPaint(hWnd, &ps);
      break;
    }
    case WM_DESTROY:
      printf("[WM_DESTROY] \n");
      PostQuitMessage(0);
      printf("Goodbye!.\n");
      break;
    default:
      return DefWindowProc(hWnd, uMsg, wParam, lParam);
  }
  return 0;
}

int main(int argc, char *argv[])
{
  HMODULE hModule;

  LPCTSTR hWndCls;
  HWND hWnd;

  int nCmdShow  = SW_SHOW;

  MSG msg;

  hModule = GetModuleHandle(NULL);
  if (hModule == NULL)
  {
    DWORD error = GetLastError();
    printf("GetModuleHandle err=%d\n", error);
    return -1;
  }

  hWndCls = register_window_class(hModule, WindowProc);
  
  hWnd = CreateWindow(hWndCls, TEXT("主窗口"), 0 /* styles */, 0, 0, 500, 500, NULL, NULL, hModule, NULL);
  if (! hWnd)
  {
    DWORD error = GetLastError();
    printf("CreateWindow err=%d\n", error);
    return -1;
  }

  ShowWindow(hWnd, nCmdShow);

  UpdateWindow(hWnd);

  while (GetMessage(&msg, NULL, 0, 0)) 
  {
    TranslateMessage(&msg);
    DispatchMessage(&msg);
  }

  printf("exit.\n");
  return 0;
}

  

如果需要实现一个无标题栏、无边框的窗口,使用Pop-Up窗口是最简单的实现方式。当然,通过上面的标准窗口,也可以实现。

 

透明窗口

 

Pop-Up窗口 

无标题栏

#include <stdio.h>
#include <windows.h>

#include "window.h"

LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
  PAINTSTRUCT ps;
  HDC hDC;

  switch (uMsg) 
  {
    case WM_CREATE:
    {
      printf("[WM_CREATE] \n");
      break;
    }
    case WM_COMMAND:
      printf("[WM_COMMAND] \n");
      break;
    case WM_PAINT:
    {
      printf("[WM_PAINT] \n");
      hDC = BeginPaint(hWnd, &ps);

      EndPaint(hWnd, &ps);
      break;
    }
    case WM_DESTROY:
      printf("[WM_DESTROY] \n");
      PostQuitMessage(0);
      printf("Goodbye!.\n");
      break;
    default:
      return DefWindowProc(hWnd, uMsg, wParam, lParam);
  }
  return 0;
}

int main(int argc, char *argv[])
{
  HMODULE hModule;

  LPCTSTR hWndCls;
  HWND hWnd;
  LONG_PTR style;

  int nCmdShow  = SW_SHOW;

  MSG msg;

  hModule = GetModuleHandle(NULL);
  if (hModule == NULL)
  {
    DWORD error = GetLastError();
    printf("GetModuleHandle err=%d\n", error);
    return -1;
  }

  hWndCls = register_window_class(hModule, WindowProc);
  
  hWnd = CreateWindow(hWndCls, TEXT("主窗口"), WS_VISIBLE | WS_POPUP | WS_THICKFRAME /* styles */, 0, 0, 500, 500, NULL, NULL, hModule, NULL);
  if (! hWnd)
  {
    DWORD error = GetLastError();
    printf("CreateWindow err=%d\n", error);
    return -1;
  }
  ShowWindow(hWnd, nCmdShow);

  UpdateWindow(hWnd);

  while (GetMessage(&msg, NULL, 0, 0)) 
  {
    TranslateMessage(&msg);
    DispatchMessage(&msg);
  }

  printf("exit.\n");
  return 0;
}

 无标题栏无边框

#include <stdio.h>
#include <windows.h>

#include "window.h"

LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
  PAINTSTRUCT ps;
  HDC hDC;

  switch (uMsg) 
  {
    case WM_CREATE:
    {
      printf("[WM_CREATE] \n");
      break;
    }
    case WM_COMMAND:
      printf("[WM_COMMAND] \n");
      break;
    case WM_PAINT:
    {
      printf("[WM_PAINT] \n");
      hDC = BeginPaint(hWnd, &ps);

      EndPaint(hWnd, &ps);
      break;
    }
    case WM_DESTROY:
      printf("[WM_DESTROY] \n");
      PostQuitMessage(0);
      printf("Goodbye!.\n");
      break;
    default:
      return DefWindowProc(hWnd, uMsg, wParam, lParam);
  }
  return 0;
}

int main(int argc, char *argv[])
{
  HMODULE hModule;

  LPCTSTR hWndCls;
  HWND hWnd;
  LONG_PTR style;

  int nCmdShow  = SW_SHOW;

  MSG msg;

  hModule = GetModuleHandle(NULL);
  if (hModule == NULL)
  {
    DWORD error = GetLastError();
    printf("GetModuleHandle err=%d\n", error);
    return -1;
  }

  hWndCls = register_window_class(hModule, WindowProc);
  
  hWnd = CreateWindow(hWndCls, TEXT("主窗口"), WS_VISIBLE | WS_POPUP/* styles */, 0, 0, 500, 500, NULL, NULL, hModule, NULL);
  if (! hWnd)
  {
    DWORD error = GetLastError();
    printf("CreateWindow err=%d\n", error);
    return -1;
  }
  ShowWindow(hWnd, nCmdShow);

  UpdateWindow(hWnd);

  while (GetMessage(&msg, NULL, 0, 0)) 
  {
    TranslateMessage(&msg);
    DispatchMessage(&msg);
  }

  printf("exit.\n");
  return 0;
}

 

 

对话框

#include<windows.h>

#pragma comment(lib, "User32.lib")

int WINAPI wWinMain(HINSTANCE hInstance, 
                    HINSTANCE hPrevInstance, 
                    PWSTR pCmdLine, 
                    int nCmdShow)
{
  char *wc_name = "#32770";
  HWND hwnd;

  HACCEL hAccelTable;
  MSG msg;

  hwnd = CreateWindowEx(
    0,                                  // Optional window styles.
    TEXT(wc_name),                      // Window class
    TEXT("Dialog"),                     // Window text
    WS_OVERLAPPEDWINDOW,                // Window style

                                        // Size and position
    CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,

    NULL,                               // Parent window    
    NULL,                               // Menu
    hInstance,                          // Instance handle
    NULL                                // Additional application data
  );

  if (hwnd == NULL)
  {
    return GetLastError();
  }

  ShowWindow(hwnd, nCmdShow);
  UpdateWindow(hwnd);

  hAccelTable = LoadAccelerators(hInstance, TEXT(wc_name));
  while (GetMessage(&msg, NULL, 0, 0)) 
  {
    if (! TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) 
    {
      TranslateMessage(&msg);
      DispatchMessage(&msg);
    }
  }

  return 0;
}

对话框窗口事件处理

#include<stdio.h>
#include<windows.h>

#pragma comment(lib, "User32.lib")

LRESULT CALLBACK WindowProc(
  HWND   hwnd,
  UINT   uMsg,
  WPARAM wParam,
  LPARAM lParam)
{
  switch(uMsg) 
  {
    case WM_COMMAND:
      if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL) 
      {
        EndDialog(hwnd, LOWORD(wParam));
        return TRUE;
      }
      break;
    default: 
      return DefDlgProc(hwnd, uMsg, wParam, lParam);
  }
  return 0;
}

int WINAPI wWinMain(HINSTANCE hInstance, 
                    HINSTANCE hPrevInstance, 
                    PWSTR pCmdLine, 
                    int nCmdShow)
{
  char *wc_name = "#32770";
  HWND hwnd;

  HACCEL hAccelTable;
  MSG msg;

  AllocConsole();
  freopen("conout$", "w", stdout);

  hwnd = CreateWindowEx(
    0,                                  // Optional window styles.
    TEXT(wc_name),                      // Window class
    TEXT("Dialog"),                     // Window text
    WS_OVERLAPPEDWINDOW,                // Window style

                                        // Size and position
    CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,

    NULL,                               // Parent window    
    NULL,                               // Menu
    hInstance,                          // Instance handle
    NULL                                // Additional application data
  );

  if (hwnd == NULL)
  {
    return GetLastError();
  }

  ShowWindow(hwnd, nCmdShow);
  UpdateWindow(hwnd);

  hAccelTable = LoadAccelerators(hInstance, TEXT(wc_name));
  while (GetMessage(&msg, hwnd, 0, 0)) 
  {
    if (! TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) 
    {
      //TranslateMessage(&msg);
      //DispatchMessage(&msg);

      //DefWindowProc(msg.hwnd, msg.message, msg.wParam, msg.lParam);

      //DefDlgProc(msg.hwnd, msg.message, msg.wParam, msg.lParam);

      CallWindowProc(WindowProc, msg.hwnd, msg.message, msg.wParam, msg.lParam);
    }
  }

  return 0;
}

弹窗

#include <stdio.h>
#include <windows.h>

#include "window.h"

LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
  PAINTSTRUCT ps;
  HDC hDC;

  switch (uMsg) 
  {
    case WM_CREATE:
    {
      printf("[WM_CREATE] \n");
      break;
    }
    /*
    case WM_ERASEBKGND: 
    {
      printf("[WM_ERASEBKGND] \n");
      break;
    }
    */
    case WM_COMMAND:
      printf("[WM_COMMAND] \n");
      break;
    case WM_PAINT:
    {
      printf("[WM_PAINT] \n");
      hDC = BeginPaint(hWnd, &ps);

      EndPaint(hWnd, &ps);
      break;
    }
    case WM_DESTROY:
      printf("[WM_DESTROY] \n");
      PostQuitMessage(0);
      printf("Goodbye!.\n");
      break;
    default:
      return DefWindowProc(hWnd, uMsg, wParam, lParam);
  }
  return 0;
}

int main(int argc, char *argv[])
{
  HMODULE hModule;

  HWND hDesktop;

  HWND hWnd;
  LPCTSTR hWndCls;
  LONG_PTR style;
  HBRUSH brush;

  int nCmdShow  = SW_SHOW;

  RECT rect;

  MSG msg;

  hModule = GetModuleHandle(NULL);
  if (hModule == NULL)
  {
    DWORD error = GetLastError();
    printf("GetModuleHandle err=%d\n", error);
    return -1;
  }

  hWndCls = register_window_class(hModule, CreateSolidBrush(RGB(0, 0, 255)), WindowProc);
  
  hWnd = CreateWindowEx(WS_EX_TOOLWINDOW, hWndCls, TEXT("弹窗"), 
      WS_VISIBLE | WS_POPUP/* styles */,
      0, 0, 280, 180, NULL, 0, hModule, NULL);
  if (! hWnd)
  {
    DWORD error = GetLastError();
    printf("CreateWindow err=%d\n", error);
    return -1;
  }

  hDesktop = GetDesktopWindow();
  if (hDesktop == NULL)
  {
    DWORD error = GetLastError();
    printf("GetDesktopWindow err=%d\n", error);
    return -1;
  }
  GetClientRect(hDesktop, &rect);
  SetWindowPos(hWnd, HWND_TOP, rect.right - 280, rect.bottom - 180, rect.right, rect.bottom, 0);


  ShowWindow(hWnd, nCmdShow);
  UpdateWindow(hWnd);

  //brush = CreateSolidBrush(RGB(0, 0, 0));
  //SetClassLong(hWnd, GCL_HBRBACKGROUND, (long) brush);

  while (GetMessage(&msg, NULL, 0, 0)) 
  {
    TranslateMessage(&msg);
    DispatchMessage(&msg);
  }

  printf("exit.\n");
  return 0;
}

 

 

 

 

分享到:
评论

相关推荐

    api-ms-win-core-console-l1-1-0_Windows编程_

    【标题】"api-ms-win-core-console-l1-1-0_Windows编程_" 涉及的是Windows操作系统中的核心控制台接口API,这是Windows编程中一个关键的组件。这个标题暗示了我们将探讨的是如何利用这些API在Windows环境下进行系统...

    pywin32-221.win-amd64-32-py2.6.rar

    在标题"pywin32-221.win-amd64-32-py2.6.rar"中,"pywin32-221"表示该版本是PyWin32的221版,"win-amd64"表明这是针对64位AMD处理器优化的版本,而"py2.6"则意味着它是为Python 2.6编译的。描述中的"pywin32-221.win...

    pywin32-219.win32-py3.4.zip

    总之,PyWin32-219.win32-py3.4.zip是Python 3.4开发者在Windows平台上进行系统级编程不可或缺的工具。虽然pypi官方仓库不再支持这个版本,但这个分享确保了那些仍在使用Python 3.4的项目能够继续享受PyWin32带来的...

    windous下perl-gui编程模块 Win32-GUI-1.06-PPM-5.8

    `Win32-GUI`模块是Perl社区为Windows平台开发的一个开源库,它使得Perl程序员能够轻松地创建各种窗口、控件和事件处理程序,而无需深入学习底层的Windows编程技术。`Win32-GUI`提供了包括按钮、文本框、列表视图、...

    wxPython2.8-win32-unicode-2.8.12.1-py27

    标题"wxPython2.8-win32-unicode-2.8.12.1-py27"以及描述中的"wxPython2.8-win32-unicode-2.8.12.1-py27.exe"指向的是一个特定版本的wxPython库的安装程序,用于Windows 32位系统,支持Unicode编码,并且是针对...

    swt-4.2.2-win32-win32-x86

    标题"swt-4.2.2-win32-win32-x86"表明这是一个针对Windows 32位系统的SWT版本,版本号为4.2.2,适用于开发Eclipse 4.2版本的应用程序。 Eclipse是一款著名的开源集成开发环境(IDE),广泛用于Java应用程序的开发。...

    pywin32-221.win32-py2.7-pywin32-221.win64-py3.5.zip

    PyWin32,也被称为pywin32或pythonwin,是Python编程语言中的一个扩展模块,主要用于与Microsoft Windows操作系统进行交互。这个压缩包“pywin32-221.win32-py2.7-pywin32-221.win64-py3.5.zip”包含了针对Python ...

    win32timer-ugui3.0.zip

    Win32 Timer API允许程序设置定时器,当达到预设时间时,系统会发送一个消息给指定的窗口,这对于GUI界面的动画效果和实时性非常重要。 总的来说,移植UGUI 3.0到PC端并利用Win32 Timer API是一项涉及跨平台编程、...

    swt-3.6M3-win32-win32-x86.jar

    标题中的"swt-3.6M3-win32-win32-x86.jar"是SWT针对Windows 32位系统的特定版本。 这个jar包是SWT库的一个发行版,版本号为3.6M3(M3代表 Milestone 3,意味着这是一个在正式发布前的中间开发版本)。开发者可以将...

    win32编程高级培训--侯捷

    根据给定文件的信息,我们可以...综上所述,侯捷先生的Win32编程高级培训资料深入探讨了Win32编程的核心概念和技术,不仅适合有一定编程基础的学习者,也为希望深入了解Windows应用程序开发的开发者提供了宝贵的参考。

    Windows窗口应用程序(Win32程序) 源代码

    在Windows操作系统中,Win32程序是一种基于API(应用程序接口)的编程模型,它允许开发者创建能在Windows环境下运行的应用程序。这里的"Windows窗口应用程序",更具体地指的是使用Win32 API来构建具有图形用户界面...

    pythonWin_win32all-163.7z

    6. **win32gui**:提供图形用户界面(GUI)的支持,可以创建和控制窗口、菜单、对话框等,是编写Windows桌面应用的好帮手。 然而,值得注意的是,win32all仅支持Python2.x版本,特别是这里提到的"pythonWin_win32...

    Win32之子窗口的应用

    通过学习和实践Win32子窗口的应用,开发者可以掌握Windows编程的基本原理,为构建更复杂的桌面应用打下坚实基础。同时,理解子窗口机制也有助于理解和调试多层嵌套窗口的问题,提高软件的稳定性和用户体验。

    swt-3.7.2-win32-win32-x86-source.rar

    这个"swt-3.7.2-win32-win32-x86-source.rar"压缩包包含了SWT 3.7.2版本的源代码,专为32位Windows操作系统设计。SWT是Eclipse项目的一部分,它提供了与原生系统控件的直接交互,以实现更快、更丰富的图形界面效果,...

    商业编程-源码-Win32 编程中字体的应用.zip

    在Windows编程领域,Win32 API(应用程序接口)是一个至关重要的工具集,它允许开发者创建原生的Windows应用程序。这个压缩包"商业编程-源码-Win32 编程中字体的应用.zip"包含了关于如何在Win32编程中处理字体的应用...

    WIN32编程电子书

    《WIN32编程电子书》是一份专注于介绍Windows操作系统下WIN32 API编程技术的资料。这份电子书详细阐述了如何在Windows环境下进行系统级别的编程,涵盖了从基础概念到高级应用的广泛内容。针对初学者和有经验的开发者...

    wxPython2.8-win64-unicode-2.8.12.1-py27 wxPython2.8-win32-unicode-2.8.12.1-py27

    "win64"指的是适用于64位Windows系统的版本,而"win32"则对应32位Windows系统。这里的"2.8.12.1"是wxPython的版本号,每个版本可能包含性能优化、新功能或修复的错误。"py27"表示这些版本是为Python 2.7解释器编译的...

    win32编程学习笔记

    1. **Windows API**:这是Win32编程的核心,提供了大量的函数、结构体和常量,用于创建窗口、处理消息、绘图、输入输出、内存管理等。例如,`CreateWindowEx`用于创建窗口,`GetMessage`和`DispatchMessage`用于处理...

    pywin32-220.win32-py2.7.exe

    2. **win32con**:此模块包含了常量定义,这些常量对应于Windows API中的各种标识符,比如窗口样式、消息类型、错误代码等,使得Python代码能更直观地与Windows API交互。 3. **win32com**:这是与Windows COM...

Global site tag (gtag.js) - Google Analytics