=============================mymain.h文件=====================================


#include <Windows.h>

#include <stdlib.h>



//测试区


//http://www.baidu.com

#define IDM_STATIC_DOMAIN1 101

#define IDM_BTN_OPEN1 102


//http://www.google.com

#define IDM_STATIC_DOMAIN2 103

#define IDM_BTN_OPEN2 104


//http://www.youtube.com

#define IDM_STATIC_DOMAIN3 105

#define IDM_BTN_OPEN3 106



//http://www.51cto.com

#define IDM_STATIC_DOMAIN4 107

#define IDM_BTN_OPEN4 108


HWND doman1;

HWND btnOpen1;


HWND doman2;

HWND btnOpen2;


HWND doman3;

HWND btnOpen3;


HWND doman4;

HWND btnOpen4;

//end 测试区



//系统默认笔刷

HBRUSH sysDefBrush;


LRESULT CALLBACK WindowProc(

_In_ HWND hwnd,

_In_ UINT uMsg,

_In_ WPARAM wParam,

_In_ LPARAM lParam

);



//窗口的x y 坐标

int mainPox, mainPoy;


void setCenterPos(_In_ HWND hwnd);



=============================mymain.cpp文件===================================

#include "mymain.h"


int CALLBACK WinMain(

_In_ HINSTANCE hInstance,

_In_ HINSTANCE hPrevInstance,

_In_ LPSTR lpCmdLine,

_In_ int nCmdShow

) {


sysDefBrush = CreateSolidBrush(RGB(30, 30, 30));


WCHAR* cls_Name = L"my window";

WNDCLASS wc = { sizeof(WNDCLASS) };

//wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);

wc.hbrBackground = sysDefBrush;


wc.lpfnWndProc = WindowProc;

wc.lpszClassName = cls_Name;

wc.hInstance = hInstance;

wc.style = CS_HREDRAW | CS_VREDRAW;

RegisterClass(&wc);


HWND hwnd = CreateWindow(

cls_Name,//类名

L"我的导航",//窗口标题文字

WS_DLGFRAME | WS_SYSMENU,//Win32中禁止窗口调整大小

//(WS_OVERLAPPEDWINDOW^WS_THICKFRAME)|WS_MINIMIZE,//窗口外观样式  將WS_OVERLAPPEDWINDOW和WS_THICKFRAME进行按位异或运算来实现 禁止改变窗口大小方法 

38,//窗口相对于父级的x坐标

20,//窗口相对于父级的y坐标

800,//窗口的宽度

600,//窗口的高度

NULL,//没有父窗口,为NULL

NULL,//没有菜单,为NULL

hInstance,//当前应用程序的实例句柄

NULL//没有附加数据,为NULL

);

if (hwnd == NULL) {

return 0;

}


//ShowWindow的第一个参数就是刚才创建的窗口的句柄,第二个参数控制窗口如何显示

ShowWindow(hwnd, SW_SHOW);


UpdateWindow(hwnd);



MSG msg;

while (GetMessage(&msg, NULL, 0, 0)) {

TranslateMessage(&msg);

DispatchMessage(&msg);

}

return 0;

}


LRESULT CALLBACK WindowProc(

_In_ HWND hwnd,

_In_ UINT uMsg,

_In_ WPARAM wParam,

_In_ LPARAM lParam

) {

switch (uMsg)

{

case WM_CREATE: {

//设置窗口位置居中

setCenterPos(hwnd);


HBRUSH brush;

brush = CreateSolidBrush(RGB(30, 30, 30));

SetClassLong(hwnd, GCL_HBRBACKGROUND, (long)brush);


doman1 = CreateWindow(TEXT("static"), TEXT("http://www.baidu.com"), WS_CHILD | WS_VISIBLE | SS_LEFT, 240, 40, 260, 20, hwnd,(HMENU)IDM_STATIC_DOMAIN1,

((LPCREATESTRUCT)lParam)->hInstance, NULL);

btnOpen1 = CreateWindow(

TEXT("button"),

TEXT("打开导航"),

WS_CHILD | WS_VISIBLE,

500, 40, 80, 20,

hwnd, (HMENU)IDM_BTN_OPEN1,

((LPCREATESTRUCT)lParam)->hInstance, NULL

);



doman2 = CreateWindow(TEXT("static"), TEXT("http://www.google.com"), WS_CHILD | WS_VISIBLE | SS_LEFT, 240, 80, 260, 20, hwnd, (HMENU)IDM_STATIC_DOMAIN2,

((LPCREATESTRUCT)lParam)->hInstance, NULL);


btnOpen2 = CreateWindow(

TEXT("button"),

TEXT("打开导航"),

WS_CHILD | WS_VISIBLE,

500, 80, 80, 20,

hwnd, (HMENU)IDM_BTN_OPEN2,

((LPCREATESTRUCT)lParam)->hInstance, NULL

);



doman3 = CreateWindow(TEXT("static"), TEXT("http://www.youtube.com"), WS_CHILD | WS_VISIBLE | SS_LEFT, 240, 120, 260, 20, hwnd, (HMENU)IDM_STATIC_DOMAIN3,

((LPCREATESTRUCT)lParam)->hInstance, NULL);


btnOpen3 = CreateWindow(

TEXT("button"),

TEXT("打开导航"),

WS_CHILD | WS_VISIBLE,

500, 120, 80, 20,

hwnd, (HMENU)IDM_BTN_OPEN3,

((LPCREATESTRUCT)lParam)->hInstance, NULL

);


doman4 = CreateWindow(TEXT("static"), TEXT("http://www.51cto.com"), WS_CHILD | WS_VISIBLE | SS_LEFT, 240, 160, 260, 20, hwnd, (HMENU)IDM_STATIC_DOMAIN4,

((LPCREATESTRUCT)lParam)->hInstance, NULL);


btnOpen4 = CreateWindow(

TEXT("button"),

TEXT("打开导航"),

WS_CHILD | WS_VISIBLE,

500, 160, 80, 20,

hwnd, (HMENU)IDM_BTN_OPEN4,

((LPCREATESTRUCT)lParam)->hInstance, NULL

);



}

return 0;

case WM_DESTROY: {

PostQuitMessage(0);

return 0;

}

case WM_COMMAND: {

switch (LOWORD(wParam))

{

case IDM_BTN_OPEN1:

ShellExecute(NULL,TEXT("open"),TEXT("http://www.baidu.com"),NULL,NULL,SW_SHOWNORMAL);

break;

case IDM_BTN_OPEN2:

ShellExecute(NULL, TEXT("open"), TEXT("http://www.google.com"), NULL, NULL, SW_SHOWNORMAL);

break;


case IDM_BTN_OPEN3:

ShellExecute(NULL, TEXT("open"), TEXT("http://www.youtube.com"), NULL, NULL, SW_SHOWNORMAL);

break;


case IDM_BTN_OPEN4:

ShellExecute(NULL, TEXT("open"), TEXT("http://www.51cto.com"), NULL, NULL, SW_SHOWNORMAL);

break;


default:

break;

}

}

return 0;

case WM_PAINT: {

}

break;


// Windows提供了一个消息,叫做WM_CTLCOLORSTATIC。这个消息看上去很长,我们拆分看,即WM_、CTL、COLOR、STATIC,分别表示窗口消息、CTL(Control控件)、颜色、静态的。合起来一看,就是静态控件颜色消息。这个消息给我们提供了修改


static静态控件的文字颜色、文字背景颜色以及static静态控件的表面颜色。

case WM_CTLCOLORSTATIC: {

if ((HWND)lParam == doman1) {

//SetBkColor((HDC)wParam,RGB(30,30,30));

SetBkMode((HDC)wParam, TRANSPARENT);

SetTextColor((HDC)wParam, RGB(250,250,250));

}

else if ((HWND)lParam == doman2)

{

SetBkMode((HDC)wParam, TRANSPARENT);

SetTextColor((HDC)wParam, RGB(250, 250, 250));

}

else if ((HWND)lParam == doman3)

{

SetBkMode((HDC)wParam, TRANSPARENT);

SetTextColor((HDC)wParam, RGB(250, 250, 250));

}

else if ((HWND)lParam == doman4)

{

SetBkMode((HDC)wParam, TRANSPARENT);

SetTextColor((HDC)wParam, RGB(250, 250, 250));

}

}

return (INT_PTR)sysDefBrush;

default:

break;

}

return DefWindowProc(hwnd, uMsg, wParam, lParam);

}



//设置窗口位置居中

void setCenterPos(_In_ HWND hwnd) {

int srcWidth, srcHeight;

RECT rect;


srcWidth = GetSystemMetrics(SM_CXSCREEN);

srcHeight = GetSystemMetrics(SM_CYSCREEN);


GetWindowRect(hwnd, &rect);

mainPox = (srcWidth - rect.right) / 2;

mainPoy = (srcHeight - rect.bottom) / 2;


SetWindowPos(hwnd, HWND_TOP, mainPox, mainPoy, rect.right, rect.bottom, SWP_SHOWWINDOW);

}